Skip to content

Commit 6d81a14

Browse files
wsmosesivanradanov
andauthored
[MLIR][LLVM] Fold extract of extract (#125980)
Co-authored-by: Ivan R. Ivanov <[email protected]>
1 parent 137c378 commit 6d81a14

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,13 @@ static Type getInsertExtractValueElementType(Type llvmType,
18981898
}
18991899

19001900
OpFoldResult LLVM::ExtractValueOp::fold(FoldAdaptor adaptor) {
1901+
if (auto extractValueOp = getContainer().getDefiningOp<ExtractValueOp>()) {
1902+
SmallVector<int64_t, 4> newPos(extractValueOp.getPosition());
1903+
newPos.append(getPosition().begin(), getPosition().end());
1904+
setPosition(newPos);
1905+
getContainerMutable().set(extractValueOp.getContainer());
1906+
return getResult();
1907+
}
19011908
auto insertValueOp = getContainer().getDefiningOp<InsertValueOp>();
19021909
OpFoldResult result = {};
19031910
while (insertValueOp) {

mlir/test/Dialect/LLVMIR/canonicalize.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ llvm.func @fold_unrelated_extractvalue(%arr: !llvm.array<4 x f32>) -> f32 {
8484
llvm.return %3 : f32
8585
}
8686

87+
// -----
88+
// CHECK-LABEL: fold_extract_extractvalue
89+
llvm.func @fold_extract_extractvalue(%arr: !llvm.struct<(i64, array<1 x ptr<1>>)>) -> !llvm.ptr<1> {
90+
// CHECK: llvm.extractvalue %{{.*}}[1, 0]
91+
// CHECK-NOT: extractvalue
92+
%a = llvm.extractvalue %arr[1] : !llvm.struct<(i64, array<1 x ptr<1>>)>
93+
%b = llvm.extractvalue %a[0] : !llvm.array<1 x ptr<1>>
94+
llvm.return %b : !llvm.ptr<1>
95+
}
96+
8797
// -----
8898

8999
// CHECK-LABEL: fold_bitcast

0 commit comments

Comments
 (0)