[Mlir-commits] [mlir] [MLIR] Extend the extractvalue fold method (PR #172297)

Vadim Curcă llvmlistbot at llvm.org
Mon Dec 15 06:33:32 PST 2025


================
@@ -1975,9 +1981,32 @@ OpFoldResult LLVM::ExtractValueOp::fold(FoldAdaptor adaptor) {
       getContainerMutable().assign(insertValueOp.getContainer());
       result = getResult();
     }
-    insertValueOp = insertValueOp.getContainer().getDefiningOp<InsertValueOp>();
+    container = insertValueOp.getContainer().getDefiningOp();
+  }
+  if (!container)
+    return result;
+
+  Attribute containerAttr;
+  if (!matchPattern(container, m_Constant(&containerAttr)))
+    return nullptr;
+  for (int64_t pos : extractPos) {
+    Attribute attrElement = extractElementAt(containerAttr, pos);
+
+    // It is possible to fail to extract an element from the container and still
+    // fold the operation to a constant. For example:
+    // ```
+    // %container = llvm.mlir.zero : !llvm.struct<(i8, i32)>
+    // %result = llvm.extractvalue %container[0] : !llvm.struct<(i8, i32)>
+    // ```
+    // In this case, `containerAttr` is an `LLVM::ZeroAttr` that does not
+    // contain any nested elements, yet the operation can be folded to a zero
+    // constant.
----------------
VadimCurca wrote:

I'll reply to the other comment in this thread as well:
> I don't remember if we support any kind of nested struct constants, but might be nice to check if this is actually foldable as well (assuming they exit).

Unfortunately no, `LLVM::ConstantOp` doesn't support nested structs - [LLVMDialect.cpp#L3353](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp#L3353).

https://github.com/llvm/llvm-project/pull/172297


More information about the Mlir-commits mailing list