[mlir] Add ConstantLike trait to LLVM::ConstantOp

This make LLVM dialect constants to work with `m_constant` matches. Implement
the folding hook for this operation as required by the trait. This in turn
allows LLVM::ConstantOp to properly participate in constant-folding.

Depends On D116757

Reviewed By: wsmoses

Differential Revision: https://reviews.llvm.org/D116758
This commit is contained in:
Alex Zinenko 2022-01-06 23:29:52 +01:00
parent cafaa35036
commit 43ff4a6d55
3 changed files with 22 additions and 1 deletions

View File

@ -1364,7 +1364,7 @@ def LLVM_UndefOp : LLVM_Op<"mlir.undef", [NoSideEffect]>,
}
def LLVM_ConstantOp
: LLVM_Op<"mlir.constant", [NoSideEffect]>,
: LLVM_Op<"mlir.constant", [NoSideEffect, ConstantLike]>,
LLVM_Builder<[{$res = getLLVMConstant($_resultType, $value, $_location,
moduleTranslation);}]>
{
@ -1403,6 +1403,7 @@ def LLVM_ConstantOp
let builders = [LLVM_OneResultOpBuilder];
let assemblyFormat = "`(` $value `)` attr-dict `:` type($res)";
let verifier = [{ return ::verify(*this); }];
let hasFolder = 1;
}
// Operations that correspond to LLVM intrinsics. With MLIR operation set being

View File

@ -2086,6 +2086,9 @@ static LogicalResult verify(LLVM::ConstantOp op) {
return success();
}
// Constant op constant-folds to its value.
OpFoldResult LLVM::ConstantOp::fold(ArrayRef<Attribute>) { return getValue(); }
//===----------------------------------------------------------------------===//
// Utility functions for parsing atomic ops
//===----------------------------------------------------------------------===//

View File

@ -40,6 +40,7 @@ llvm.func @no_fold_extractvalue(%arr: !llvm.array<4xf32>) -> f32 {
}
// -----
// CHECK-LABEL: fold_bitcast
// CHECK-SAME: %[[a0:arg[0-9]+]]
// CHECK-NEXT: llvm.return %[[a0]]
@ -87,3 +88,19 @@ llvm.func @fold_gep(%x : !llvm.ptr<i8>) -> !llvm.ptr<i8> {
llvm.return %c : !llvm.ptr<i8>
}
// -----
// Check that LLVM constants participate in cross-dialect constant folding. The
// resulting constant is created in the arith dialect because the last folded
// operation belongs to it.
// CHECK-LABEL: llvm_constant
func @llvm_constant() -> i32 {
// CHECK-NOT: llvm.mlir.constant
%0 = llvm.mlir.constant(40 : i32) : i32
%1 = llvm.mlir.constant(42 : i32) : i32
// CHECK: %[[RES:.*]] = arith.constant 82 : i32
// CHECK-NOT: arith.addi
%2 = arith.addi %0, %1 : i32
// CHECK: return %[[RES]]
return %2 : i32
}