[clang] [CIR][CodeGen] Support DesignatedInitUpdateExpr in constant emission (PR #194238)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Fri May 1 12:37:58 PDT 2026


================
@@ -347,7 +347,80 @@ std::optional<size_t> ConstantAggregateBuilder::splitAt(CharUnits pos) {
 /// Hint indicates the location at which we'd like to split, but may be
 /// ignored.
 bool ConstantAggregateBuilder::split(size_t index, CharUnits hint) {
-  cgm.errorNYI("split constant at index");
+  naturalLayout = false;
+  mlir::TypedAttr c = elements[index].element;
+  CharUnits offset = elements[index].offset;
+
+  if (auto constRecord = mlir::dyn_cast<cir::ConstRecordAttr>(c)) {
+    // Expand the record into its contained member elements.
+    auto recordTy = mlir::cast<cir::RecordType>(constRecord.getType());
+    mlir::ArrayAttr members = constRecord.getMembers();
+    unsigned numMembers = members.size();
+
+    SmallVector<Element> newElems;
+    newElems.reserve(numMembers);
+    for (unsigned i = 0; i < numMembers; ++i) {
+      auto memberAttr = mlir::cast<mlir::TypedAttr>(members[i]);
+      CharUnits memberOffset =
+          offset + CharUnits::fromQuantity(
+                       recordTy.getElementOffset(dataLayout.layout, i));
+      newElems.emplace_back(memberAttr, memberOffset);
+    }
+    replace(elements, index, index + 1, newElems);
+    return true;
+  }
+
+  if (auto constArray = mlir::dyn_cast<cir::ConstArrayAttr>(c)) {
+    // Expand the array into its contained elements.
+    auto arrayTy = mlir::cast<cir::ArrayType>(constArray.getType());
+    CharUnits elemSize = getSize(arrayTy.getElementType());
+
+    if (auto arrayAttr =
+            mlir::dyn_cast<mlir::ArrayAttr>(constArray.getElts())) {
+      // Array with explicit elements (plus possible trailing zeros).
+      SmallVector<Element> newElems;
+      unsigned numExplicit = arrayAttr.size();
+      unsigned trailingZeros = constArray.getTrailingZerosNum();
+      newElems.reserve(numExplicit + trailingZeros);
+
+      for (unsigned i = 0; i < numExplicit; ++i) {
+        auto eltAttr = mlir::cast<mlir::TypedAttr>(arrayAttr[i]);
+        newElems.emplace_back(eltAttr, offset + i * elemSize);
+      }
+      // Expand trailing zeros as individual zero elements.
----------------
andykaylor wrote:

Why are you doing this? Trailing zeros can make an initializer quite large.

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


More information about the cfe-commits mailing list