Skip to content

[MLIR][LLVM] Fold extract of extract #125980

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

Merged
merged 2 commits into from
Feb 6, 2025
Merged

[MLIR][LLVM] Fold extract of extract #125980

merged 2 commits into from
Feb 6, 2025

Conversation

wsmoses
Copy link
Member

@wsmoses wsmoses commented Feb 6, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Feb 6, 2025

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: William Moses (wsmoses)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/125980.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp (+7)
  • (modified) mlir/test/Dialect/LLVMIR/canonicalize.mlir (+10)
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index a6e996f3fb810db..6d51042efcebdfb 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -1898,6 +1898,13 @@ static Type getInsertExtractValueElementType(Type llvmType,
 }
 
 OpFoldResult LLVM::ExtractValueOp::fold(FoldAdaptor adaptor) {
+  if (auto extractValueOp = getContainer().getDefiningOp<ExtractValueOp>()) {
+    SmallVector<int64_t, 1> newPos(extractValueOp.getPosition());
+    newPos.append(getPosition().begin(), getPosition().end());
+    setPosition(newPos);
+    getContainerMutable().set(extractValueOp.getContainer());
+    return getResult();
+  }
   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 15f960167cb5f32..c509cd82227c295 100644
--- a/mlir/test/Dialect/LLVMIR/canonicalize.mlir
+++ b/mlir/test/Dialect/LLVMIR/canonicalize.mlir
@@ -84,6 +84,16 @@ llvm.func @fold_unrelated_extractvalue(%arr: !llvm.array<4 x f32>) -> f32 {
   llvm.return %3 : f32
 }
 
+// -----
+// CHECK-LABEL: fold_extract_extractvalue
+llvm.func @fold_extract_extractvalue(%arr: !llvm.struct<(i64, array<1 x ptr<1>>)>) -> !llvm.ptr<1> {
+  // CHECK: llvm.extractvalue %{{.*}}[1, 0] 
+  // CHECK-NOT: extractvalue
+  %a = llvm.extractvalue %arr[1] : !llvm.struct<(i64, array<1 x ptr<1>>)> 
+  %b = llvm.extractvalue %a[0] : !llvm.array<1 x ptr<1>> 
+  llvm.return %b : !llvm.ptr<1>
+}
+
 // -----
 
 // CHECK-LABEL: fold_bitcast

@wsmoses wsmoses merged commit 6d81a14 into main Feb 6, 2025
6 of 7 checks passed
@wsmoses wsmoses deleted the users/wm/extractextract branch February 6, 2025 07:05
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants