[Mlir-commits] [mlir] [MLIR][LLVMIR] Fixing how ZeroInitializers are converted to undef instead of constant zero type when importing from LLVMIR to llvm.mlir (PR #171107)

Tobias Gysi llvmlistbot at llvm.org
Mon Dec 8 07:49:24 PST 2025


================
@@ -1765,31 +1765,33 @@ FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
     return lookupValue(inst);
   }
 
+  // Convert zero-initialized aggregates to ZeroOp.
+  if (auto *aggregateZero = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
+    Type type = convertType(aggregateZero->getType());
+    return ZeroOp::create(builder, loc, type).getResult();
+  }
+
   // Convert aggregate constants.
-  if (isa<llvm::ConstantAggregate>(constant) ||
-      isa<llvm::ConstantAggregateZero>(constant)) {
+  if (isa<llvm::ConstantAggregate>(constant)) {
     // Lookup the aggregate elements that have been converted before.
     SmallVector<Value> elementValues;
     if (auto *constAgg = dyn_cast<llvm::ConstantAggregate>(constant)) {
       elementValues.reserve(constAgg->getNumOperands());
       for (llvm::Value *operand : constAgg->operands())
         elementValues.push_back(lookupValue(operand));
     }
-    if (auto *constAgg = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
-      unsigned numElements = constAgg->getElementCount().getFixedValue();
-      elementValues.reserve(numElements);
-      for (unsigned i = 0, e = numElements; i != e; ++i)
-        elementValues.push_back(lookupValue(constAgg->getElementValue(i)));
-    }
     assert(llvm::count(elementValues, nullptr) == 0 &&
            "expected all elements have been converted before");
 
-    // Generate an UndefOp as root value and insert the aggregate elements.
+    // Generate a root value and insert the aggregate elements.
+    // For ConstantAggregateZero, use ZeroOp to preserve zero-initialization
+    // semantics. Otherwise use UndefOp as the root.Type rootType = convertType(constant->getType());
----------------
gysit wrote:

```suggestion
    // Generate an UndefOp as root value and insert the aggregate elements.
```
nit: I would revert the comment change since we are not working on ConstantAggregateZero here.

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


More information about the Mlir-commits mailing list