https://github.com/andykaylor created 
https://github.com/llvm/llvm-project/pull/190834

This implements EH cleanup handling that performs regular partial array 
destruction for array constructor loops with a destructed type. Because CIR 
represents array construction using an abstract operation, we do not go through 
the normal EH stack mechanism. Instead, we add a partial_dtor region to the 
cir.array.ctor operation indicating how a single element should be destructed. 
When the cir.array.ctor operation is expanded during LoweringPrepare, we create 
a cleanup scope with a partial array dtor loop in an EH cleanup region. This 
gets further expanded during CFG flattening to produce a control flow 
equivalent to that generated by classic codegen.

Assisted-by: Cursor / claude-4.6-opus-high

>From aa9c3f2d26d513575648ff317f5508ab5f5b305d Mon Sep 17 00:00:00 2001
From: Andy Kaylor <[email protected]>
Date: Wed, 1 Apr 2026 15:07:42 -0700
Subject: [PATCH] [CIR] Implement partial array destroy handling

This implements EH cleanup handling that performs regular partial array
destruction for array constructor loops with a destructed type. Because
CIR represents array construction using an abstract operation, we do
not go through the normal EH stack mechanism. Instead, we add a
partial_dtor region to the cir.array.ctor operation indicating how a
single element should be destructed. When the cir.array.ctor operation
is expanded during LoweringPrepare, we create a cleanup scope with a
partial array dtor loop in an EH cleanup region. This gets further
expanded during CFG flattening to produce a control flow equivalent to
that generated by classic codegen.

Assisted-by: Cursor / claude-4.6-opus-high
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |  65 +++++--
 clang/lib/CIR/CodeGen/CIRGenClass.cpp         |  31 ++--
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  89 +++++++++-
 .../Dialect/Transforms/LoweringPrepare.cpp    | 118 +++++++++----
 clang/test/CIR/CodeGen/array-ctor.cpp         |  13 ++
 .../CIR/CodeGen/partial-array-cleanup.cpp     | 163 ++++++++++++++++++
 6 files changed, 420 insertions(+), 59 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/partial-array-cleanup.cpp

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index d2fd2df7ab7d1..bbe1ff71da9f3 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -4578,8 +4578,17 @@ def CIR_ArrayCtor : CIR_Op<"array.ctor"> {
   let summary = "Initialize array elements with C++ constructors";
   let description = [{
     Initialize each array element using the same C++ constructor. This
-    operation has one region, with one single block. The block has an
-    incoming argument for the current array element to initialize.
+    operation has a `body` region and an optional `partial_dtor` region.
+    Both regions have a single block whose argument is a pointer to the
+    current array element.
+
+    The `body` region contains the constructor call for one element.
+
+    The `partial_dtor` region, when non-empty, contains the destructor call
+    for one element. During lowering, it is used to build a cleanup that
+    destroys already-constructed elements if a constructor throws. When the
+    element type has a trivial destructor or exceptions are disabled, the
+    `partial_dtor` region is left empty.
 
     When `num_elements` is absent, `addr` must be a pointer to a fixed-size
     CIR array type and the element count is derived from that array type.
@@ -4591,17 +4600,30 @@ def CIR_ArrayCtor : CIR_Op<"array.ctor"> {
     Examples:
 
     ```mlir
+    // Fixed size without partial destructor:
     cir.array.ctor(%0 : !cir.ptr<!cir.array<!rec_S x 42>>) {
       ^bb0(%arg0: !cir.ptr<!rec_S>):
         cir.call @some_ctor(%arg0) : (!cir.ptr<!rec_S>) -> ()
         cir.yield
     }
 
+    // Variable size without partial destructor:
     cir.array.ctor(%ptr, %n : !cir.ptr<!rec_S>, !u64i) {
       ^bb0(%arg0: !cir.ptr<!rec_S>):
         cir.call @some_ctor(%arg0) : (!cir.ptr<!rec_S>) -> ()
         cir.yield
     }
+
+    // Fixed size with partial destructor:
+    cir.array.ctor(%0 : !cir.ptr<!cir.array<!rec_S x 42>>) {
+      ^bb0(%arg0: !cir.ptr<!rec_S>):
+        cir.call @some_ctor(%arg0) : (!cir.ptr<!rec_S>) -> ()
+        cir.yield
+    } partial_dtor {
+      ^bb0(%arg0: !cir.ptr<!rec_S>):
+        cir.call @some_dtor(%arg0) : (!cir.ptr<!rec_S>) -> ()
+        cir.yield
+    }
     ```
   }];
 
@@ -4610,32 +4632,41 @@ def CIR_ArrayCtor : CIR_Op<"array.ctor"> {
     Optional<CIR_AnyIntType>:$num_elements
   );
 
-  let regions = (region SizedRegion<1>:$body);
-  let assemblyFormat = [{
-    $addr (`,` $num_elements^)? `:` qualified(type($addr))
-    (`,` type($num_elements)^)? $body attr-dict
-  }];
+  let regions = (region SizedRegion<1>:$body, AnyRegion:$partial_dtor);
+  let hasCustomAssemblyFormat = 1;
 
   let builders = [
     // Static form: addr is ptr<array<T x N>>, no num_elements.
     OpBuilder<(ins "mlir::Value":$addr,
-      "llvm::function_ref<void(mlir::OpBuilder &, 
mlir::Location)>":$regionBuilder), [{
-        assert(regionBuilder && "builder callback expected");
+      "llvm::function_ref<void(mlir::OpBuilder &, 
mlir::Location)>":$bodyBuilder,
+      "llvm::function_ref<void(mlir::OpBuilder &, 
mlir::Location)>":$partialDtorBuilder), [{
+        assert(bodyBuilder && "builder callback expected");
         mlir::OpBuilder::InsertionGuard guard($_builder);
-        mlir::Region *r = $_state.addRegion();
         $_state.addOperands(ValueRange{addr});
-        $_builder.createBlock(r);
-        regionBuilder($_builder, $_state.location);
+        mlir::Region *body = $_state.addRegion();
+        $_builder.createBlock(body);
+        bodyBuilder($_builder, $_state.location);
+        mlir::Region *partialDtor = $_state.addRegion();
+        if (partialDtorBuilder) {
+          $_builder.createBlock(partialDtor);
+          partialDtorBuilder($_builder, $_state.location);
+        }
     }]>,
     // Dynamic form: addr is ptr<T>, num_elements is the runtime count.
     OpBuilder<(ins "mlir::Value":$addr, "mlir::Value":$num_elements,
-      "llvm::function_ref<void(mlir::OpBuilder &, 
mlir::Location)>":$regionBuilder), [{
-        assert(regionBuilder && "builder callback expected");
+      "llvm::function_ref<void(mlir::OpBuilder &, 
mlir::Location)>":$bodyBuilder,
+      "llvm::function_ref<void(mlir::OpBuilder &, 
mlir::Location)>":$partialDtorBuilder), [{
+        assert(bodyBuilder && "builder callback expected");
         mlir::OpBuilder::InsertionGuard guard($_builder);
-        mlir::Region *r = $_state.addRegion();
         $_state.addOperands({addr, num_elements});
-        $_builder.createBlock(r);
-        regionBuilder($_builder, $_state.location);
+        mlir::Region *body = $_state.addRegion();
+        $_builder.createBlock(body);
+        bodyBuilder($_builder, $_state.location);
+        mlir::Region *partialDtor = $_state.addRegion();
+        if (partialDtorBuilder) {
+          $_builder.createBlock(partialDtor);
+          partialDtorBuilder($_builder, $_state.location);
+        }
     }]>
   ];
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenClass.cpp 
b/clang/lib/CIR/CodeGen/CIRGenClass.cpp
index f8d65fa9a6d33..02e074200204d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenClass.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenClass.cpp
@@ -783,20 +783,15 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
   {
     RunCleanupsScope scope(*this);
 
-    // Evaluate the constructor and its arguments in a regular
-    // partial-destroy cleanup.
-    if (getLangOpts().Exceptions &&
-        !ctor->getParent()->hasTrivialDestructor()) {
-      cgm.errorNYI(e->getSourceRange(), "partial array cleanups");
-    }
+    bool needsPartialArrayCleanup =
+        getLangOpts().Exceptions &&
+        !ctor->getParent()->hasTrivialDestructor();
 
     auto emitCtorBody = [&](mlir::OpBuilder &b, mlir::Location l) {
       mlir::BlockArgument arg =
           b.getInsertionBlock()->addArgument(ptrToElmType, l);
       Address curAddr = Address(arg, elementType, eltAlignment);
       assert(!cir::MissingFeatures::sanitizers());
-      // Match CGClass::EmitCXXAggrConstructorCall: zero-initialize each 
element
-      // in the array-ctor loop before invoking the constructor for that slot.
       if (zeroInitialize)
         emitNullInitialization(l, curAddr, type);
       auto currAVS = AggValueSlot::forAddr(
@@ -809,16 +804,30 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
       cir::YieldOp::create(b, l);
     };
 
-    // Emit the per-element initialization.
+    llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>
+        emitPartialDtorBody = nullptr;
+    auto partialDtorBuilder = [&](mlir::OpBuilder &b, mlir::Location l) {
+      mlir::BlockArgument arg =
+          b.getInsertionBlock()->addArgument(ptrToElmType, l);
+      Address curAddr = Address(arg, elementType, eltAlignment);
+      emitCXXDestructorCall(ctor->getParent()->getDestructor(), Dtor_Complete,
+                            /*forVirtualBase=*/false,
+                            /*delegating=*/false, curAddr, type);
+      cir::YieldOp::create(b, l);
+    };
+    if (needsPartialArrayCleanup)
+      emitPartialDtorBody = partialDtorBuilder;
+
     if (useDynamicArrayCtor) {
       cir::ArrayCtor::create(builder, loc, dynamicElPtr, numElements,
-                             emitCtorBody);
+                             emitCtorBody, emitPartialDtorBody);
     } else {
       cir::ArrayType arrayTy =
           cir::ArrayType::get(elementType, constElementCount);
       mlir::Value arrayOp =
           builder.createPtrBitcast(arrayBase.getPointer(), arrayTy);
-      cir::ArrayCtor::create(builder, loc, arrayOp, emitCtorBody);
+      cir::ArrayCtor::create(builder, loc, arrayOp, emitCtorBody,
+                             emitPartialDtorBody);
     }
   }
 }
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index ab1c0b453ac18..3e020e52ebece 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -341,7 +341,94 @@ template <typename Op> static LogicalResult 
verifyArrayCtorDtor(Op op) {
   return success();
 }
 
-LogicalResult cir::ArrayCtor::verify() { return verifyArrayCtorDtor(*this); }
+ParseResult cir::ArrayCtor::parse(OpAsmParser &parser, OperationState &result) 
{
+  OpAsmParser::UnresolvedOperand addrOperand;
+  OpAsmParser::UnresolvedOperand numElementsOperand;
+  bool hasNumElements = false;
+
+  if (parser.parseOperand(addrOperand))
+    return failure();
+
+  if (succeeded(parser.parseOptionalComma())) {
+    if (parser.parseOperand(numElementsOperand))
+      return failure();
+    hasNumElements = true;
+  }
+
+  if (parser.parseColon())
+    return failure();
+
+  mlir::Type addrType;
+  if (parser.parseType(addrType))
+    return failure();
+  if (parser.resolveOperand(addrOperand, addrType, result.operands))
+    return failure();
+
+  if (hasNumElements) {
+    if (parser.parseComma())
+      return failure();
+    mlir::Type numElementsType;
+    if (parser.parseType(numElementsType))
+      return failure();
+    if (parser.resolveOperand(numElementsOperand, numElementsType,
+                              result.operands))
+      return failure();
+  }
+
+  Region *body = result.addRegion();
+  if (parser.parseRegion(*body))
+    return failure();
+
+  Region *partialDtor = result.addRegion();
+  if (!parser.parseOptionalKeyword("partial_dtor")) {
+    if (parser.parseRegion(*partialDtor))
+      return failure();
+  }
+
+  if (parser.parseOptionalAttrDict(result.attributes))
+    return failure();
+
+  return success();
+}
+
+void cir::ArrayCtor::print(OpAsmPrinter &p) {
+  p << " " << getAddr();
+  if (getNumElements())
+    p << ", " << getNumElements();
+
+  p << " : " << getAddr().getType();
+  if (getNumElements())
+    p << ", " << getNumElements().getType();
+
+  p << " ";
+  p.printRegion(getBody());
+
+  if (!getPartialDtor().empty()) {
+    p << " partial_dtor ";
+    p.printRegion(getPartialDtor());
+  }
+
+  p.printOptionalAttrDict(getOperation()->getAttrs());
+}
+
+LogicalResult cir::ArrayCtor::verify() {
+  if (failed(verifyArrayCtorDtor(*this)))
+    return failure();
+
+  mlir::Region &partialDtor = getPartialDtor();
+  if (!partialDtor.empty()) {
+    mlir::Block &dtorBlock = partialDtor.front();
+    if (dtorBlock.getNumArguments() != 1)
+      return emitOpError(
+          "partial_dtor must have exactly one block argument");
+
+    auto bodyArgTy = getBody().front().getArgument(0).getType();
+    if (dtorBlock.getArgument(0).getType() != bodyArgTy)
+      return emitOpError("partial_dtor block argument type must match "
+                         "the body block argument type");
+  }
+  return success();
+}
 LogicalResult cir::ArrayDtor::verify() { return verifyArrayCtorDtor(*this); }
 
 
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp 
b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index 91f082725711f..f5ab406789882 100644
--- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -1369,13 +1369,16 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() {
   cir::ReturnOp::create(builder, f.getLoc());
 }
 
+/// Lower a cir.array.ctor or cir.array.dtor into a do-while loop that
+/// iterates over every element.  For cir.array.ctor ops whose partial_dtor
+/// region is non-empty, the ctor loop is wrapped in a cir.cleanup.scope whose
+/// EH cleanup performs a reverse destruction loop using the partial dtor body.
 static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
                                        clang::ASTContext *astCtx,
                                        mlir::Operation *op, mlir::Type eltTy,
                                        mlir::Value addr,
                                        mlir::Value numElements,
                                        uint64_t arrayLen, bool isCtor) {
-  // Generate loop to call into ctor/dtor for every element.
   mlir::Location loc = op->getLoc();
   bool isDynamic = numElements != nullptr;
 
@@ -1432,7 +1435,7 @@ static void 
lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
 
   // Clone the region body (ctor/dtor call and any setup ops like per-element
   // zero-init) into the loop, remapping the block argument to the current
-  // element pointer.
+  // element pointer.  
   auto cloneRegionBodyInto = [&](mlir::Block *srcBlock,
                                  mlir::Value replacement) {
     mlir::IRMapping map;
@@ -1443,34 +1446,89 @@ static void 
lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder,
     }
   };
 
-  builder.createDoWhile(
-      loc,
-      /*condBuilder=*/
-      [&](mlir::OpBuilder &b, mlir::Location loc) {
-        auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr);
-        auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne,
-                                      currentElement, stop);
-        builder.createCondition(cmp);
-      },
-      /*bodyBuilder=*/
-      [&](mlir::OpBuilder &b, mlir::Location loc) {
-        auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr);
-        if (isCtor) {
-          cloneRegionBodyInto(bodyBlock, currentElement);
-          mlir::Value stride = builder.getUnsignedInt(loc, 1, sizeTypeSize);
-          auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy,
-                                                      currentElement, stride);
-          builder.createStore(loc, nextElement, tmpAddr);
-        } else {
-          mlir::Value stride = builder.getSignedInt(loc, -1, sizeTypeSize);
-          auto prevElement = cir::PtrStrideOp::create(builder, loc, eltTy,
-                                                      currentElement, stride);
-          builder.createStore(loc, prevElement, tmpAddr);
-          cloneRegionBodyInto(bodyBlock, prevElement);
-        }
-
-        builder.createYield(loc);
-      });
+  mlir::Block *partialDtorBlock = nullptr;
+  if (auto arrayCtor = mlir::dyn_cast<cir::ArrayCtor>(op)) {
+    mlir::Region &partialDtor = arrayCtor.getPartialDtor();
+    if (!partialDtor.empty())
+      partialDtorBlock = &partialDtor.front();
+  }
+
+  auto emitCtorDtorLoop = [&]() {
+    builder.createDoWhile(
+        loc,
+        /*condBuilder=*/
+        [&](mlir::OpBuilder &b, mlir::Location loc) {
+          auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr);
+          auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne,
+                                        currentElement, stop);
+          builder.createCondition(cmp);
+        },
+        /*bodyBuilder=*/
+        [&](mlir::OpBuilder &b, mlir::Location loc) {
+          auto currentElement = cir::LoadOp::create(b, loc, eltTy, tmpAddr);
+          if (isCtor) {
+            cloneRegionBodyInto(bodyBlock, currentElement);
+            mlir::Value stride = builder.getUnsignedInt(loc, 1, sizeTypeSize);
+            auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy,
+                                                        currentElement, 
stride);
+            builder.createStore(loc, nextElement, tmpAddr);
+          } else {
+            mlir::Value stride = builder.getSignedInt(loc, -1, sizeTypeSize);
+            auto prevElement = cir::PtrStrideOp::create(builder, loc, eltTy,
+                                                        currentElement, 
stride);
+            builder.createStore(loc, prevElement, tmpAddr);
+            cloneRegionBodyInto(bodyBlock, prevElement);
+          }
+
+          builder.createYield(loc);
+        });
+  };
+
+  if (partialDtorBlock) {
+    cir::CleanupScopeOp::create(
+        builder, loc, cir::CleanupKind::EH,
+        /*bodyBuilder=*/
+        [&](mlir::OpBuilder &b, mlir::Location loc) {
+          emitCtorDtorLoop();
+          builder.createYield(loc);
+        },
+        /*cleanupBuilder=*/
+        [&](mlir::OpBuilder &b, mlir::Location loc) {
+          auto cur = cir::LoadOp::create(b, loc, eltTy, tmpAddr);
+          auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne,
+                                        cur, begin);
+          cir::IfOp::create(
+              builder, loc, cmp, /*withElseRegion=*/false,
+              [&](mlir::OpBuilder &b, mlir::Location loc) {
+                builder.createDoWhile(
+                    loc,
+                    /*condBuilder=*/
+                    [&](mlir::OpBuilder &b, mlir::Location loc) {
+                      auto el =
+                          cir::LoadOp::create(b, loc, eltTy, tmpAddr);
+                      auto neq = cir::CmpOp::create(
+                          builder, loc, cir::CmpOpKind::ne, el, begin);
+                      builder.createCondition(neq);
+                    },
+                    /*bodyBuilder=*/
+                    [&](mlir::OpBuilder &b, mlir::Location loc) {
+                      auto el =
+                          cir::LoadOp::create(b, loc, eltTy, tmpAddr);
+                      mlir::Value negOne =
+                          builder.getSignedInt(loc, -1, sizeTypeSize);
+                      auto prev = cir::PtrStrideOp::create(
+                          builder, loc, eltTy, el, negOne);
+                      builder.createStore(loc, prev, tmpAddr);
+                      cloneRegionBodyInto(partialDtorBlock, prev);
+                      builder.createYield(loc);
+                    });
+                cir::YieldOp::create(builder, loc);
+              });
+          builder.createYield(loc);
+        });
+  } else {
+    emitCtorDtorLoop();
+  }
 
   if (ifOp)
     cir::YieldOp::create(builder, loc);
diff --git a/clang/test/CIR/CodeGen/array-ctor.cpp 
b/clang/test/CIR/CodeGen/array-ctor.cpp
index 2233529a192c8..f58a46d4afc49 100644
--- a/clang/test/CIR/CodeGen/array-ctor.cpp
+++ b/clang/test/CIR/CodeGen/array-ctor.cpp
@@ -173,3 +173,16 @@ void multi_dimensional() {
 // OGCG:       br i1 %[[DONE]], label %[[EXIT:.*]], label %[[LOOP]]
 // OGCG:     [[EXIT]]:
 // OGCG:       ret void
+
+struct HasDtor {
+    HasDtor();
+    ~HasDtor();
+};
+
+void test_partial_array_cleanup() {
+    HasDtor s[4];
+}
+
+// CIR-BEFORE-LPP:     cir.func {{.*}} @_Z26test_partial_array_cleanupv()
+
+// CIR:     cir.func {{.*}} @_Z26test_partial_array_cleanupv()
diff --git a/clang/test/CIR/CodeGen/partial-array-cleanup.cpp 
b/clang/test/CIR/CodeGen/partial-array-cleanup.cpp
new file mode 100644
index 0000000000000..f41f80ab601e0
--- /dev/null
+++ b/clang/test/CIR/CodeGen/partial-array-cleanup.cpp
@@ -0,0 +1,163 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fclangir -fexceptions -fcxx-exceptions -emit-cir -mmlir 
--mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir  2> %t-before-lp.cir
+// RUN: FileCheck --input-file=%t-before-lp.cir %s -check-prefix=CIR-BEFORE-LPP
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fexceptions -fcxx-exceptions -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fexceptions -fcxx-exceptions -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct S {
+    S();
+    ~S();
+};
+
+void test_partial_array_cleanup() {
+    S s[4];
+}
+
+// CIR-BEFORE-LPP:     cir.func {{.*}} @_Z26test_partial_array_cleanupv()
+// CIR-BEFORE-LPP:       %[[ARRAY:.*]] = cir.alloca !cir.array<!rec_S x 4>, 
!cir.ptr<!cir.array<!rec_S x 4>>, ["s", init]
+// CIR-BEFORE-LPP:       cir.array.ctor %[[ARRAY]] : 
!cir.ptr<!cir.array<!rec_S x 4>> {
+// CIR-BEFORE-LPP:       ^bb0(%[[CTOR_ARG:.*]]: !cir.ptr<!rec_S>):
+// CIR-BEFORE-LPP:         cir.call @_ZN1SC1Ev(%[[CTOR_ARG]]) : 
(!cir.ptr<!rec_S>{{.*}}) -> ()
+// CIR-BEFORE-LPP:         cir.yield
+// CIR-BEFORE-LPP:       } partial_dtor {
+// CIR-BEFORE-LPP:       ^bb0(%[[DTOR_ARG:.*]]: !cir.ptr<!rec_S>):
+// CIR-BEFORE-LPP:         cir.call @_ZN1SD1Ev(%[[DTOR_ARG]]){{.*}} : 
(!cir.ptr<!rec_S>{{.*}}) -> ()
+// CIR-BEFORE-LPP:         cir.yield
+// CIR-BEFORE-LPP:       }
+
+// CIR:     cir.func {{.*}} @_Z26test_partial_array_cleanupv()
+// CIR:       %[[ARRAY:.*]] = cir.alloca !cir.array<!rec_S x 4>, 
!cir.ptr<!cir.array<!rec_S x 4>>, ["s", init]
+// CIR:       %[[CONST4:.*]] = cir.const #cir.int<4> : !u64i
+// CIR:       %[[BEGIN:.*]] = cir.cast array_to_ptrdecay %[[ARRAY]] : 
!cir.ptr<!cir.array<!rec_S x 4>> -> !cir.ptr<!rec_S>
+// CIR:       %[[END:.*]] = cir.ptr_stride %[[BEGIN]], %[[CONST4]] : 
(!cir.ptr<!rec_S>, !u64i) -> !cir.ptr<!rec_S>
+// CIR:       %[[ITER:.*]] = cir.alloca !cir.ptr<!rec_S>, 
!cir.ptr<!cir.ptr<!rec_S>>, ["__array_idx"]
+// CIR:       cir.store %[[BEGIN]], %[[ITER]] : !cir.ptr<!rec_S>, 
!cir.ptr<!cir.ptr<!rec_S>>
+// CIR:       cir.cleanup.scope {
+// CIR:         cir.do {
+// CIR:           %[[CUR:.*]] = cir.load %[[ITER]] : 
!cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
+// CIR:           cir.call @_ZN1SC1Ev(%[[CUR]]) : (!cir.ptr<!rec_S>{{.*}}) -> 
()
+// CIR:           %[[CONST1:.*]] = cir.const #cir.int<1> : !u64i
+// CIR:           %[[NEXT:.*]] = cir.ptr_stride %[[CUR]], %[[CONST1]] : 
(!cir.ptr<!rec_S>, !u64i) -> !cir.ptr<!rec_S>
+// CIR:           cir.store %[[NEXT]], %[[ITER]] : !cir.ptr<!rec_S>, 
!cir.ptr<!cir.ptr<!rec_S>>
+// CIR:           cir.yield
+// CIR:         } while {
+// CIR:           %[[CUR2:.*]] = cir.load %[[ITER]] : 
!cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
+// CIR:           %[[CMP:.*]] = cir.cmp ne %[[CUR2]], %[[END]] : 
!cir.ptr<!rec_S>
+// CIR:           cir.condition(%[[CMP]])
+// CIR:         }
+// CIR:         cir.yield
+// CIR:       } cleanup eh {
+// CIR:         %[[CUR3:.*]] = cir.load %[[ITER]] : 
!cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
+// CIR:         %[[NE:.*]] = cir.cmp ne %[[CUR3]], %[[BEGIN]] : 
!cir.ptr<!rec_S>
+// CIR:         cir.if %[[NE]] {
+// CIR:           cir.do {
+// CIR:             %[[EL:.*]] = cir.load %[[ITER]] : 
!cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
+// CIR:             %[[NEG1:.*]] = cir.const #cir.int<-1> : !s64i
+// CIR:             %[[PREV:.*]] = cir.ptr_stride %[[EL]], %[[NEG1]] : 
(!cir.ptr<!rec_S>, !s64i) -> !cir.ptr<!rec_S>
+// CIR:             cir.store %[[PREV]], %[[ITER]] : !cir.ptr<!rec_S>, 
!cir.ptr<!cir.ptr<!rec_S>>
+// CIR:             cir.call @_ZN1SD1Ev(%[[PREV]]){{.*}} : 
(!cir.ptr<!rec_S>{{.*}}) -> ()
+// CIR:             cir.yield
+// CIR:           } while {
+// CIR:             %[[EL2:.*]] = cir.load %[[ITER]] : 
!cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
+// CIR:             %[[NE2:.*]] = cir.cmp ne %[[EL2]], %[[BEGIN]] : 
!cir.ptr<!rec_S>
+// CIR:             cir.condition(%[[NE2]])
+// CIR:           }
+// CIR:         }
+// CIR:         cir.yield
+// CIR:       }
+
+// LLVM:     define dso_local void @_Z26test_partial_array_cleanupv()
+// LLVM:       %[[ARRAY:.*]] = alloca [4 x %struct.S]
+// LLVM:       %[[BEGIN:.*]] = getelementptr %struct.S, ptr %[[ARRAY]], i32 0
+// LLVM:       %[[END:.*]] = getelementptr %struct.S, ptr %[[BEGIN]], i64 4
+// LLVM:       %[[ITER:.*]] = alloca ptr
+// LLVM:       store ptr %[[BEGIN]], ptr %[[ITER]]
+//
+//            --- ctor loop condition ---
+// LLVM:     [[CLEANUP_SCOPE:.*]]:
+// LLVM:       br label %[[CTOR_LOOP_COND:.*]]
+//
+// LLVM:     [[CTOR_LOOP_COND:]]:
+// LLVM:       %[[COND_CUR:.*]] = load ptr, ptr %[[ITER]]
+// LLVM:       %[[CTOR_DONE:.*]] = icmp ne ptr %[[COND_CUR]], %[[END]]
+// LLVM:       br i1 %[[CTOR_DONE]], label %[[CTOR_BODY:.*]], label 
%[[CTOR_EXIT:.*]]
+//
+//            --- ctor loop body ---
+// LLVM:     [[CTOR_BODY]]:
+// LLVM:       %[[CUR:.*]] = load ptr, ptr %[[ITER]]
+// LLVM:       invoke void @_ZN1SC1Ev(ptr{{.*}} %[[CUR]])
+// LLVM:         to label %[[CTOR_CONT:.*]] unwind label %[[LPAD:.*]]
+//
+// LLVM:     [[CTOR_CONT]]:
+// LLVM:       %[[NEXT:.*]] = getelementptr %struct.S, ptr %[[CUR]], i64 1
+// LLVM:       store ptr %[[NEXT]], ptr %[[ITER]]
+// LLVM:       br label %[[CTOR_LOOP_COND]]
+//
+//            --- landing pad + cleanup guard ---
+// LLVM:     [[LPAD]]:
+// LLVM:       landingpad { ptr, i32 }
+// LLVM:         cleanup
+// LLVM:       %[[PAD_CUR:.*]] = load ptr, ptr %[[ITER]]
+// LLVM:       %[[GUARD:.*]] = icmp ne ptr %[[PAD_CUR]], %[[BEGIN]]
+// LLVM:       br i1 %[[GUARD]], label %[[DTOR_ENTRY:.*]], label 
%[[EH_RESUME:.*]]
+//
+//            --- partial dtor do-while entry ---
+// LLVM:     [[DTOR_ENTRY]]:
+// LLVM:       br label %[[DTOR_BODY:.*]]
+//
+//            --- partial dtor loop condition (back-edge) ---
+// LLVM:     [[DTOR_LOOP_COND:.*]]:
+// LLVM:       %[[DTOR_CUR:.*]] = load ptr, ptr %[[ITER]]
+// LLVM:       %[[DTOR_CONT:.*]] = icmp ne ptr %[[DTOR_CUR]], %[[BEGIN]]
+// LLVM:       br i1 %[[DTOR_CONT]], label %[[DTOR_BODY]], label 
%[[DTOR_DONE:.*]]
+//
+//            --- partial dtor loop body ---
+// LLVM:     [[DTOR_BODY]]:
+// LLVM:       %[[DCUR:.*]] = load ptr, ptr %[[ITER]]
+// LLVM:       %[[PREV:.*]] = getelementptr %struct.S, ptr %[[DCUR]], i64 -1
+// LLVM:       store ptr %[[PREV]], ptr %[[ITER]]
+// LLVM:       call void @_ZN1SD1Ev(ptr{{.*}} %[[PREV]])
+// LLVM:       br label %[[DTOR_LOOP_COND]]
+//
+// LLVM:     [[DTOR_DONE]]:
+// LLVM:       br label %[[EH_RESUME]]
+//
+// LLVM:     [[EH_RESUME]]:
+// LLVM:       resume { ptr, i32 }
+
+// OGCG:     define dso_local void @_Z26test_partial_array_cleanupv()
+// OGCG:     [[ENTRY:.*]]:
+// OGCG:       %[[ARRAY:.*]] = alloca [4 x %struct.S]
+// OGCG:       %[[BEGIN:.*]] = getelementptr inbounds [4 x %struct.S], ptr 
%[[ARRAY]], i32 0, i32 0
+// OGCG:       %[[END:.*]] = getelementptr inbounds %struct.S, ptr %[[BEGIN]], 
i64 4
+//
+//            --- ctor loop ---
+// OGCG:     [[CTOR_BODY:.*]]:
+// OGCG:       %[[CUR:.*]] = phi ptr [ %[[BEGIN]], %[[ENTRY]] ], [ 
%[[NEXT:.*]], %[[CONT:.*]] ]
+// OGCG:       invoke void @_ZN1SC1Ev(ptr{{.*}})
+// OGCG:         to label %[[CONT]] unwind label %[[LPAD:.*]]
+//
+// OGCG:     [[CONT]]:
+// OGCG:       %[[NEXT]] = getelementptr inbounds %struct.S, ptr %[[CUR]], i64 
1
+// OGCG:       %[[DONE:.*]] = icmp eq ptr %[[NEXT]], %[[END]]
+// OGCG:       br i1 %[[DONE]], label %[[CTOR_EXIT:.*]], label 
%[[CTOR_BODY:.*]]
+//
+//            --- landing pad + cleanup guard ---
+// OGCG:     [[LPAD]]:
+// OGCG:       landingpad { ptr, i32 }
+// OGCG:         cleanup
+// OGCG:       %[[ISEMPTY:.*]] = icmp eq ptr %[[BEGIN]], %[[CUR]]
+// OGCG:       br i1 %[[ISEMPTY]], label %[[EH_RESUME:.*]], label 
%[[DTOR_LOOP:.*]]
+//
+//            --- partial dtor loop ---
+// OGCG:     [[DTOR_LOOP]]:
+// OGCG:       %[[PAST:.*]] = phi ptr [ %[[CUR]], %[[LPAD]] ], [ %[[PREV:.*]], 
%[[DTOR_LOOP]] ]
+// OGCG:       %[[PREV]] = getelementptr inbounds %struct.S, ptr %[[PAST]], 
i64 -1
+// OGCG:       call void @_ZN1SD1Ev(ptr{{.*}} %[[PREV]])
+// OGCG:       %[[DDONE:.*]] = icmp eq ptr %[[PREV]], %[[BEGIN]]
+// OGCG:       br i1 %[[DDONE]], label %[[EH_RESUME]], label %[[DTOR_LOOP]]
+//
+// OGCG:     [[EH_RESUME]]:
+// OGCG:       resume { ptr, i32 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to