-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR][LLVM] Fold extract of constant #127927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-mlir-llvm @llvm/pr-subscribers-mlir Author: William Moses (wsmoses) ChangesFull diff: https://github.com/llvm/llvm-project/pull/127927.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index bfcba40555a7c..23fbe38467b6a 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -1932,6 +1932,19 @@ OpFoldResult LLVM::ExtractValueOp::fold(FoldAdaptor adaptor) {
getContainerMutable().set(extractValueOp.getContainer());
return getResult();
}
+
+ {
+ DenseElementsAttr constval;
+ matchPattern(getContainer().getOperand(), m_Constant(&constval));
+ if (constval && constval.getElementType() == getType()) {
+ if (constval.isSplat()) {
+ return constval.getSplatValue();
+ } else if (getPosition().size() == 1) {
+ return constval.getValues()[getPosition()[0]];
+ }
+ }
+ }
+
auto insertValueOp = getContainer().getDefiningOp<InsertValueOp>();
OpFoldResult result = {};
while (insertValueOp) {
diff --git a/mlir/test/Dialect/LLVMIR/canonicalize.mlir b/mlir/test/Dialect/LLVMIR/canonicalize.mlir
index c509cd82227c2..055cb38c65337 100644
--- a/mlir/test/Dialect/LLVMIR/canonicalize.mlir
+++ b/mlir/test/Dialect/LLVMIR/canonicalize.mlir
@@ -94,6 +94,26 @@ llvm.func @fold_extract_extractvalue(%arr: !llvm.struct<(i64, array<1 x ptr<1>>)
llvm.return %b : !llvm.ptr<1>
}
+// -----
+// CHECK-LABEL: fold_extract_const
+// CHECK-NOT: extractvalue
+// CHECK: llvm.mlir.constant(-5.0)
+llvm.func @fold_extract_const() -> f64 {
+ %a = llvm.mlir.constant(dense<[-8.900000e+01, 5.000000e-01]> : tensor<2xf64>) : !llvm.array<2 x f64>
+ %b = llvm.extractvalue %a[1] : !llvm.array<2 x f64>
+ llvm.return %b : f64
+}
+
+// -----
+// CHECK-LABEL: fold_extract_splat
+// CHECK-NOT: extractvalue
+// CHECK: llvm.mlir.constant(-8.9)
+llvm.func @fold_extract_const() -> f64 {
+ %a = llvm.mlir.constant(dense<[-8.900000e+01> : tensor<2xf64>) : !llvm.array<2 x f64>
+ %b = llvm.extractvalue %a[1] : !llvm.array<2 x f64>
+ llvm.return %b : f64
+}
+
// -----
// CHECK-LABEL: fold_bitcast
|
aa08e66
to
1daf6c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM % nits.
Co-authored-by: Christian Ulmann <[email protected]>
No description provided.