[Mlir-commits] [mlir] 43ff4a6 - [mlir] Add ConstantLike trait to LLVM::ConstantOp
Alex Zinenko
llvmlistbot at llvm.org
Fri Jan 7 00:56:12 PST 2022
Author: Alex Zinenko
Date: 2022-01-07T09:56:03+01:00
New Revision: 43ff4a6d5562099d9a34ef6bb567912ef5ce923e
URL: https://github.com/llvm/llvm-project/commit/43ff4a6d5562099d9a34ef6bb567912ef5ce923e
DIFF: https://github.com/llvm/llvm-project/commit/43ff4a6d5562099d9a34ef6bb567912ef5ce923e.diff
LOG: [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
Added:
Modified:
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
mlir/test/Dialect/LLVMIR/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index dd4def5449f3..3d6f7f30ec77 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -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
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 995d2eaa89a3..56b14a77d2c6 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -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
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/LLVMIR/canonicalize.mlir b/mlir/test/Dialect/LLVMIR/canonicalize.mlir
index 3b52bc1efb94..7185920efe54 100644
--- a/mlir/test/Dialect/LLVMIR/canonicalize.mlir
+++ b/mlir/test/Dialect/LLVMIR/canonicalize.mlir
@@ -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
+}
More information about the Mlir-commits
mailing list