[clang] [CIR] Upstream support for array new with empty initializer list (PR #178806)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 30 10:39:36 PST 2026
================
@@ -568,13 +569,111 @@ void CIRGenFunction::emitNewArrayInitializer(
if (!e->hasInitializer())
return;
+ Address curPtr = beginPtr;
+
unsigned initListElements = 0;
const Expr *init = e->getInitializer();
+ QualType::DestructionKind dtorKind = elementType.isDestructedType();
+ assert(!cir::MissingFeatures::cleanupDeactivationScope());
+
+ // Attempt to perform zero-initialization using memset.
+ auto tryMemsetInitialization = [&]() -> bool {
+ mlir::Location loc = numElements.getLoc();
+
+ // FIXME: If the type is a pointer-to-data-member under the Itanium ABI,
+ // we can initialize with a memset to -1.
+ if (!cgm.getTypes().isZeroInitializable(elementType))
+ return false;
+
+ // Optimization: since zero initialization will just set the memory
+ // to all zeroes, generate a single memset to do it in one shot.
+
+ // Subtract out the size of any elements we've already initialized.
+ auto remainingSize = allocSizeWithoutCookie;
+ if (initListElements) {
+ // We know this can't overflow; we check this when doing the allocation.
+ unsigned initializedSize =
+ getContext().getTypeSizeInChars(elementType).getQuantity() *
+ initListElements;
+ cir::ConstantOp initSizeOp =
+ builder.getConstInt(loc, remainingSize.getType(), initializedSize);
+ remainingSize = builder.createSub(loc, remainingSize, initSizeOp);
+ }
+
+ // Create the memset.
+ mlir::Value castOp =
+ builder.createPtrBitcast(curPtr.getPointer(), cgm.voidTy);
+ builder.createMemSet(loc, castOp, builder.getConstInt(loc, cgm.sInt32Ty, 0),
----------------
andykaylor wrote:
The cast above was probably an artifact of code evolution. In the incubator this code was passing an i8 (as it does in classic codegen), but I saw the extra bitcast in the output and thought, "Oh, I'll just change that to an i32 so the cast gets folded away." I "fixed" the wrong place I guess.
https://github.com/llvm/llvm-project/pull/178806
More information about the cfe-commits
mailing list