[clang] [CIR] Add support for array cleanups (PR #150499)

Morris Hafner via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 25 08:51:37 PDT 2025


================
@@ -213,15 +215,23 @@ static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
         op->walk([&](cir::CallOp c) { ctorCall = c; });
         assert(ctorCall && "expected ctor call");
 
-        auto one = builder.create<cir::ConstantOp>(
-            loc, ptrDiffTy, cir::IntAttr::get(ptrDiffTy, 1));
-
-        ctorCall->moveAfter(one);
-        ctorCall->setOperand(0, currentElement);
-
-        // Advance pointer and store them to temporary variable
-        auto nextElement =
-            builder.create<cir::PtrStrideOp>(loc, eltTy, currentElement, one);
+        // Array elements get constructed in order but destructed in reverse.
+        cir::PtrStrideOp nextElement;
+        if (isCtor) {
+          mlir::Value stride = builder.getUnsignedInt(loc, 1, sizeTypeSize);
+          ctorCall->moveBefore(stride.getDefiningOp());
+          ctorCall->setOperand(0, currentElement);
+          nextElement = builder.create<cir::PtrStrideOp>(
+              loc, eltTy, currentElement, stride);
+        } else {
+          mlir::Value stride = builder.getSignedInt(loc, -1, sizeTypeSize);
+          nextElement = builder.create<cir::PtrStrideOp>(
+              loc, eltTy, currentElement, stride);
+          ctorCall->moveAfter(nextElement);
----------------
mmha wrote:

I *think* your loop is correct. But I also think if you initialize stop with `end - 1` you don't have to use `moveAfter` here and can share more code between the ctor and dtor case (the zero case is handled elsewhere so that would be safe to do). But that's more of a comment and less of a request to change your patch. I don't care either way :)

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


More information about the cfe-commits mailing list