llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Morris Hafner (mmha)

<details>
<summary>Changes</summary>

Destroy elements already constructed when array initialization exits early, for 
example through a return in a statement expression. Previously, partial array 
cleanup only handled exception unwinding.

Track the end-of-initialization `alloca` in `EHCleanupScope` and erase it and 
its users when the cleanup is discarded without being emitted.

Assisted-By: Claude Fable 5.2

---

Patch is 21.29 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/240934.diff


8 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenCleanup.cpp (+2) 
- (modified) clang/lib/CIR/CodeGen/CIRGenCleanup.h (+24-1) 
- (modified) clang/lib/CIR/CodeGen/CIRGenDecl.cpp (+7-5) 
- (modified) clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp (+10-4) 
- (added) clang/test/CIR/CodeGen/array-init-early-exit.cpp (+104) 
- (modified) clang/test/CIR/CodeGen/cleanup-conditional-eh.cpp (+38) 
- (modified) clang/test/CIR/CodeGen/cleanup-conditional.cpp (+73-3) 
- (modified) clang/test/CIR/CodeGen/static-local-guard-abort.cpp (+20-9) 


``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp 
b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
index 63e9dc24acbcf..78610aca72555 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
@@ -757,6 +757,7 @@ void CIRGenFunction::popCleanupBlock(bool forDeactivation) {
   if (!cleanupScope) {
     assert(!scope.isNormalCleanup() && !scope.isEHCleanup() &&
            "missing cir.cleanup.scope for active cleanup");
+    scope.eraseAuxAlloca();
     ehStack.popCleanup();
     return;
   }
@@ -833,6 +834,7 @@ void CIRGenFunction::popCleanupBlock(bool forDeactivation) {
       builder.setInsertionPointToEnd(&cleanupBlock);
       cir::YieldOp::create(builder, builder.getUnknownLoc());
     }
+    scope.eraseAuxAlloca();
     ehStack.popCleanup();
     return;
   }
diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.h 
b/clang/lib/CIR/CodeGen/CIRGenCleanup.h
index 0f761b69c02d1..e40d97d29923a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCleanup.h
+++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.h
@@ -20,6 +20,7 @@
 #include "mlir/IR/Value.h"
 #include "clang/AST/StmtCXX.h"
 #include "clang/CodeGenUtils/EHPersonality.h"
+#include "llvm/ADT/SetVector.h"
 
 namespace clang::CIRGen {
 
@@ -116,6 +117,11 @@ class alignas(EHScopeStack::ScopeStackAlignment) 
EHCleanupScope
   /// Cleanup scope op that represent the current scope in CIR
   cir::CleanupScopeOp cleanupScopeOp;
 
+  /// An alloca that only the cleanup reads, such as the end of the
+  /// initialized part of an array. If the cleanup is popped without being
+  /// emitted, the alloca and the operations that use it are erased.
+  mlir::Operation *auxAlloca = nullptr;
+
 public:
   /// Gets the size required for a lazy cleanup scope with the given
   /// cleanup-data requirements.
@@ -195,7 +201,24 @@ class alignas(EHScopeStack::ScopeStackAlignment) 
EHCleanupScope
     return (scope->getKind() == Cleanup);
   }
 
-  void markEmitted() {}
+  void setAuxAlloca(mlir::Operation *alloca) { auxAlloca = alloca; }
+
+  void eraseAuxAlloca() {
+    if (!auxAlloca)
+      return;
+    llvm::SetVector<mlir::Operation *> ops;
+    ops.insert(auxAlloca);
+    for (size_t i = 0; i < ops.size(); ++i)
+      for (mlir::Operation *user : ops[i]->getUsers())
+        ops.insert(user);
+    for (mlir::Operation *op : llvm::reverse(ops)) {
+      op->dropAllUses();
+      op->erase();
+    }
+    auxAlloca = nullptr;
+  }
+
+  void markEmitted() { auxAlloca = nullptr; }
 };
 
 /// A non-stable pointer into the scope stack.
diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp 
b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
index b3bca0782e84c..b0352b11fdaaa 100644
--- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
@@ -1326,9 +1326,11 @@ struct IrregularPartialArrayDestroy final : 
EHScopeStack::Cleanup {
 };
 } // namespace
 
-/// Push an EH cleanup to destroy already-constructed elements of the given
-/// array. The cleanup is deactivated when the enclosing
-/// CleanupDeactivationScope exits.
+/// Push a NormalAndEHCleanup to destroy already-constructed elements of the
+/// given array. The cleanup is deactivated when the enclosing
+/// CleanupDeactivationScope exits. It is a normal cleanup as well so that an
+/// exit from the middle of the initialization, such as a return in a statement
+/// expression, destroys the elements constructed so far.
 ///
 /// \param elementType - the immediate element type of the array;
 ///   possibly still an array type
@@ -1338,8 +1340,8 @@ void 
CIRGenFunction::pushIrregularPartialArrayCleanup(mlir::Value arrayBegin,
                                                       CharUnits elementAlign,
                                                       Destroyer *destroyer) {
   pushCleanupAndDeferDeactivation<IrregularPartialArrayDestroy>(
-      EHCleanup, arrayBegin, arrayEndPointer, elementType, elementAlign,
-      destroyer);
+      NormalAndEHCleanup, arrayBegin, arrayEndPointer, elementType,
+      elementAlign, destroyer);
 }
 
 /// pushEHDestroyIfNeeded - Push the standard destructor for the given type as
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
index ac186a5b58a38..a8aad498d01c0 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
@@ -11,6 +11,7 @@
 
//===----------------------------------------------------------------------===//
 
 #include "CIRGenBuilder.h"
+#include "CIRGenCleanup.h"
 #include "CIRGenConstantEmitter.h"
 #include "CIRGenFunction.h"
 #include "CIRGenValue.h"
@@ -765,20 +766,25 @@ void AggExprEmitter::emitArrayInit(Address destPtr, 
cir::ArrayType arrayTy,
   const CharUnits elementAlign =
       destPtr.getAlignment().alignmentOfArrayElement(elementSize);
 
-  // Destroy already-constructed elements if a later initializer throws.
-  // The cleanup is deactivated when this initialization finishes.
+  // Destroy already-constructed elements if a later initializer throws or
+  // leaves the initialization early. The cleanup is deactivated when this
+  // initialization finishes.
   QualType::DestructionKind dtorKind = elementType.isDestructedType();
   Address endOfInit = Address::invalid();
   CIRGenFunction::CleanupDeactivationScope deactivateCleanups(cgf);
 
-  if (dtorKind && cgf.getLangOpts().Exceptions) {
+  if (dtorKind) {
+    Address endOfInitAlloca = Address::invalid();
     endOfInit = cgf.createTempAlloca(cirElementPtrType, cgf.getPointerAlign(),
-                                     loc, "arrayinit.endOfInit");
+                                     loc, "arrayinit.endOfInit",
+                                     /*arraySize=*/nullptr, &endOfInitAlloca);
     builder.createStore(loc, begin, endOfInit);
 
     cgf.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
                                          elementAlign,
                                          cgf.getDestroyer(dtorKind));
+    cast<EHCleanupScope>(*cgf.ehStack.find(cgf.ehStack.stable_begin()))
+        .setAuxAlloca(endOfInitAlloca.getPointer().getDefiningOp());
   }
 
   // The 'current element to initialize'.  The invariants on this
diff --git a/clang/test/CIR/CodeGen/array-init-early-exit.cpp 
b/clang/test/CIR/CodeGen/array-init-early-exit.cpp
new file mode 100644
index 0000000000000..1f7324a945286
--- /dev/null
+++ b/clang/test/CIR/CodeGen/array-init-early-exit.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir 
-emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefixes=CIR,CIR-NOEH
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu 
-fcxx-exceptions -fexceptions -fclangir -emit-cir %s -o %t-eh.cir
+// RUN: FileCheck --input-file=%t-eh.cir %s -check-prefixes=CIR,CIR-EH
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir 
-emit-llvm -disable-llvm-passes %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm 
-disable-llvm-passes %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+// The elements of an array that an initialization has constructed so far are
+// destroyed when the initialization is left early, here by a return in a
+// statement expression, and not only when an initializer throws. With or
+// without exceptions, the partial array destroy is a normal cleanup guarded by
+// a flag that the end of the initialization clears.
+
+struct Obj {
+  Obj(char);
+  ~Obj();
+};
+
+void early_return(bool c) {
+  Obj arr[2] = {Obj{'a'}, ({
+                  if (c)
+                    return;
+                  Obj{'b'};
+                })};
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z12early_returnb(
+// CIR:         %[[ARR:.*]] = cir.alloca "arr"
+// CIR:         %[[END:.*]] = cir.alloca "arrayinit.endOfInit"
+// CIR:         %[[ACTIVE:.*]] = cir.alloca "cleanup.isactive"
+// CIR:         %[[BEGIN:.*]] = cir.cast array_to_ptrdecay %[[ARR]]
+// CIR-NEXT:    cir.store {{.*}} %[[BEGIN]], %[[END]]
+// CIR-NEXT:    %[[TRUE:.*]] = cir.const #true
+// CIR-NEXT:    cir.store %[[TRUE]], %[[ACTIVE]]
+// CIR-NEXT:    cir.cleanup.scope {
+// CIR:           cir.call @_ZN3ObjC1Ec(%[[BEGIN]],
+// CIR:           %[[SECOND:.*]] = cir.ptr_stride %[[BEGIN]]
+// CIR-NEXT:      cir.store {{.*}} %[[SECOND]], %[[END]]
+// CIR:           cir.if
+// CIR-NEXT:        cir.return
+// CIR:           cir.call @_ZN3ObjC1Ec(%[[SECOND]],
+// CIR:           %[[FALSE:.*]] = cir.const #false
+// CIR-NEXT:      cir.store %[[FALSE]], %[[ACTIVE]]
+// CIR-NEXT:      cir.yield
+// CIR-NOEH-NEXT: } cleanup normal {
+// CIR-EH-NEXT:   } cleanup all {
+// CIR-NEXT:      %[[IS_ACTIVE:.*]] = cir.load {{.*}} %[[ACTIVE]]
+// CIR-NEXT:      cir.if %[[IS_ACTIVE]] {
+// CIR-NEXT:        %[[CUR_END:.*]] = cir.load {{.*}} %[[END]]
+// CIR-NEXT:        %[[NOT_EMPTY:.*]] = cir.cmp ne %[[CUR_END]], %[[BEGIN]]
+// CIR-NEXT:        cir.if %[[NOT_EMPTY]] {
+// CIR:               cir.do {
+// CIR:                 cir.call @_ZN3ObjD1Ev(
+// CIR:         cir.return
+
+// The return leaves through the cleanup, which destroys the first element.
+//
+// LLVM-LABEL: define dso_local void @_Z12early_returnb(
+// LLVM:         store i8 1, ptr %[[ACTIVE:[0-9]+]]
+// LLVM:         call void @_ZN3ObjC1Ec(ptr {{.*}}, i8 noundef signext 97)
+// LLVM:         br i1 %{{.*}}, label %[[RETURN:[0-9]+]], label %{{.*}}
+// LLVM:       [[RETURN]]:
+// LLVM-NEXT:    store i32 1, ptr %[[SLOT:[0-9]+]]
+// LLVM-NEXT:    br label %[[CLEANUP:[0-9]+]]
+// LLVM:         call void @_ZN3ObjC1Ec(ptr {{.*}}, i8 noundef signext 98)
+// LLVM:         store i8 0, ptr %[[ACTIVE]]
+// LLVM-NEXT:    store i32 0, ptr %[[SLOT]]
+// LLVM-NEXT:    br label %[[CLEANUP]]
+// LLVM:       [[CLEANUP]]:
+// LLVM-NEXT:    %[[FLAG:.*]] = load i8, ptr %[[ACTIVE]]
+// LLVM-NEXT:    %[[IS_ACTIVE:.*]] = trunc i8 %[[FLAG]] to i1
+// LLVM-NEXT:    br i1 %[[IS_ACTIVE]], label %{{.*}}, label %{{.*}}
+// LLVM:         call void @_ZN3ObjD1Ev(
+// LLVM:         ret void
+
+// OGCG-LABEL: define dso_local void @_Z12early_returnb(
+// OGCG:       if.then:
+// OGCG-NEXT:    load ptr, ptr %arrayinit.endOfInit
+// OGCG:       arraydestroy.body:
+// OGCG:         call void @_ZN3ObjD1Ev(
+
+// Without an early exit, nothing reaches the cleanup on the normal path. It
+// stays an EH cleanup with exceptions, and without them it is dropped together
+// with the end of the initialized part that it would read.
+
+void no_exit() { Obj arr[2] = {Obj{'a'}, Obj{'b'}}; }
+
+// CIR-LABEL: cir.func {{.*}} @_Z7no_exitv(
+// CIR-NOEH-NOT: arrayinit.endOfInit
+// CIR-NOEH-NOT: cleanup.isactive
+// CIR-EH:        %[[END:.*]] = cir.alloca "arrayinit.endOfInit"
+// CIR-EH:        cir.cleanup.scope {
+// CIR-EH:          cir.call @_ZN3ObjC1Ec(
+// CIR-EH:          cir.call @_ZN3ObjC1Ec(
+// CIR-EH:        } cleanup eh {
+// CIR-EH-NEXT:     cir.load {{.*}} %[[END]]
+// CIR:           cir.call @_ZN3ObjD1Ev(
+// CIR:         cir.return
+
+// LLVM-LABEL: define dso_local void @_Z7no_exitv(
+// LLVM-NOT:     store i8 1
+// LLVM:         ret void
diff --git a/clang/test/CIR/CodeGen/cleanup-conditional-eh.cpp 
b/clang/test/CIR/CodeGen/cleanup-conditional-eh.cpp
index f66dc5872d6d4..67c6e310828c2 100644
--- a/clang/test/CIR/CodeGen/cleanup-conditional-eh.cpp
+++ b/clang/test/CIR/CodeGen/cleanup-conditional-eh.cpp
@@ -851,6 +851,44 @@ void logical_and(bool b, bool c) {
 // CIR-NEXT:      cir.if %[[IS_ACTIVE]] {
 // CIR-NEXT:        cir.call @_ZN3ObjD1Ev(%[[A]])
 
+// The partial array destroy of an array initialization is deactivated at the
+// end of the initialization as well.
+
+void array_ternary(bool b, bool c) {
+  Obj arr[2] = {Obj{'a'}, b ? ({
+                            if (c)
+                              return;
+                            Obj{'b'};
+                          })
+                            : Obj{'c'}};
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z13array_ternarybb(
+// CIR:         %[[ARR:.*]] = cir.alloca "arr"
+// CIR:         %[[END:.*]] = cir.alloca "arrayinit.endOfInit"
+// CIR:         %[[ACTIVE:.*]] = cir.alloca "cleanup.isactive"
+// CIR:         %[[BEGIN:.*]] = cir.cast array_to_ptrdecay %[[ARR]]
+// CIR:         cir.store %{{.*}}, %[[ACTIVE]]
+// CIR-NEXT:    cir.cleanup.scope {
+// CIR:           cir.call @_ZN3ObjC1Ec(%[[BEGIN]],
+// CIR:           %[[SECOND:.*]] = cir.ptr_stride %[[BEGIN]]
+// CIR-NEXT:      cir.store {{.*}} %[[SECOND]], %[[END]]
+// CIR:           cir.if
+// CIR:             cir.if
+// CIR-NEXT:          cir.return
+// CIR:           } else {
+// CIR:             cir.call @_ZN3ObjC1Ec(%[[SECOND]],
+// CIR-NEXT:      } loc(
+// CIR-NEXT:      %[[FALSE:.*]] = cir.const #false
+// CIR-NEXT:      cir.store %[[FALSE]], %[[ACTIVE]]
+// CIR-NEXT:      cir.yield
+// CIR-NEXT:    } cleanup all {
+// CIR-NEXT:      %[[IS_ACTIVE:.*]] = cir.load {{.*}} %[[ACTIVE]]
+// CIR-NEXT:      cir.if %[[IS_ACTIVE]] {
+// CIR-NEXT:        cir.load {{.*}} %[[END]]
+// CIR:             cir.do {
+// CIR:               cir.call @_ZN3ObjD1Ev(
+
 // The cleanup of an initialized member or init-capture, or the operator delete
 // or partial-array cleanup of a new-expression, is deactivated once the object
 // is initialized.
diff --git a/clang/test/CIR/CodeGen/cleanup-conditional.cpp 
b/clang/test/CIR/CodeGen/cleanup-conditional.cpp
index 65bd49a2fdbb7..c49328c4281f7 100644
--- a/clang/test/CIR/CodeGen/cleanup-conditional.cpp
+++ b/clang/test/CIR/CodeGen/cleanup-conditional.cpp
@@ -1479,6 +1479,67 @@ void logical_and(bool b, bool c) {
 // OGCG:       if.then:
 // OGCG-NEXT:    call void @_ZN3ObjD1Ev(
 
+// The partial array destroy of an array initialization is deactivated at the
+// end of the initialization as well.
+
+void array_ternary(bool b, bool c) {
+  Obj arr[2] = {Obj{'a'}, b ? ({
+                            if (c)
+                              return;
+                            Obj{'b'};
+                          })
+                            : Obj{'c'}};
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z13array_ternarybb(
+// CIR:         %[[ARR:.*]] = cir.alloca "arr"
+// CIR:         %[[END:.*]] = cir.alloca "arrayinit.endOfInit"
+// CIR:         %[[ACTIVE:.*]] = cir.alloca "cleanup.isactive"
+// CIR:         %[[BEGIN:.*]] = cir.cast array_to_ptrdecay %[[ARR]]
+// CIR:         cir.store %{{.*}}, %[[ACTIVE]]
+// CIR-NEXT:    cir.cleanup.scope {
+// CIR:           cir.call @_ZN3ObjC1Ec(%[[BEGIN]],
+// CIR:           %[[SECOND:.*]] = cir.ptr_stride %[[BEGIN]]
+// CIR-NEXT:      cir.store {{.*}} %[[SECOND]], %[[END]]
+// CIR:           cir.if
+// CIR:             cir.if
+// CIR-NEXT:          cir.return
+// CIR:           } else {
+// CIR:             cir.call @_ZN3ObjC1Ec(%[[SECOND]],
+// CIR-NEXT:      } loc(
+// CIR-NEXT:      %[[FALSE:.*]] = cir.const #false
+// CIR-NEXT:      cir.store %[[FALSE]], %[[ACTIVE]]
+// CIR-NEXT:      cir.yield
+// CIR-NEXT:    } cleanup normal {
+// CIR-NEXT:      %[[IS_ACTIVE:.*]] = cir.load {{.*}} %[[ACTIVE]]
+// CIR-NEXT:      cir.if %[[IS_ACTIVE]] {
+// CIR-NEXT:        cir.load {{.*}} %[[END]]
+// CIR:             cir.do {
+// CIR:               cir.call @_ZN3ObjD1Ev(
+
+// LLVMCIR-LABEL: define dso_local void @_Z13array_ternarybb(
+// LLVMCIR:         store i8 1, ptr %[[ACTIVE:[0-9]+]]
+// LLVMCIR:         call void @_ZN3ObjC1Ec(ptr {{.*}}, i8 noundef signext 97)
+// LLVMCIR:         store i32 1, ptr %[[SLOT:[0-9]+]]
+// LLVMCIR-NEXT:    br label %[[CLEANUP:[0-9]+]]
+// LLVMCIR:         call void @_ZN3ObjC1Ec(ptr {{.*}}, i8 noundef signext 99)
+// LLVMCIR-NEXT:    br label %[[END:[0-9]+]]
+// LLVMCIR:       [[END]]:
+// LLVMCIR-NEXT:    store i8 0, ptr %[[ACTIVE]]
+// LLVMCIR-NEXT:    store i32 0, ptr %[[SLOT]]
+// LLVMCIR-NEXT:    br label %[[CLEANUP]]
+// LLVMCIR:       [[CLEANUP]]:
+// LLVMCIR-NEXT:    %[[FLAG:.*]] = load i8, ptr %[[ACTIVE]]
+// LLVMCIR-NEXT:    %[[IS_ACTIVE:.*]] = trunc i8 %[[FLAG]] to i1
+// LLVMCIR-NEXT:    br i1 %[[IS_ACTIVE]], label %{{.*}}, label %{{.*}}
+// LLVMCIR:         call void @_ZN3ObjD1Ev(
+
+// OGCG-LABEL: define dso_local void @_Z13array_ternarybb(
+// OGCG:       if.then:
+// OGCG-NEXT:    load ptr, ptr %arrayinit.endOfInit
+// OGCG:       arraydestroy.body:
+// OGCG:         call void @_ZN3ObjD1Ev(
+
 // The cleanup of an initialized member or init-capture, or the operator delete
 // or partial-array cleanup of a new-expression, is deactivated once the object
 // is initialized.
@@ -1554,14 +1615,16 @@ void array_new(bool b) {
 }
 
 // The member of the first element is deactivated with a flag, which keeps it
-// on the EH stack above the partial-array cleanup. Without exceptions, that
-// cleanup is EH-only and therefore neither a normal nor an EH cleanup, so its
-// deactivation needs no flag.
+// on the EH stack above the partial-array cleanup, so that cleanup is
+// deactivated with a flag as well.
 // CIR-LABEL: cir.func {{.*}} @_Z9array_newb(
 // CIR:         %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_Obj>
 // CIR:         %[[COND:.*]] = cir.alloca "cleanup.cond" {{.*}} : 
!cir.ptr<!cir.bool>
 // CIR:         %[[ACTIVE:.*]] = cir.alloca "cleanup.isactive" {{.*}} : 
!cir.ptr<!cir.bool>
+// CIR-NEXT:    %[[ARRAY_ACTIVE:.*]] = cir.alloca "cleanup.isactive" {{.*}} : 
!cir.ptr<!cir.bool>
 // CIR:         %[[ARRAY:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!void> -> 
!cir.ptr<!rec_Agg>
+// CIR-NEXT:    %[[ARRAY_TRUE:.*]] = cir.const #true
+// CIR-NEXT:    cir.store %[[ARRAY_TRUE]], %[[ARRAY_ACTIVE]] : !cir.bool, 
!cir.ptr<!cir.bool>
 // CIR:         %[[A:.*]] = cir.get_member %[[ARRAY]][0] {name = "o"}
 // CIR:         cir.call @_ZN3ObjC1Ec(%[[A]], %{{.*}})
 // CIR-NEXT:    %[[TRUE:.*]] = cir.const #true
@@ -1574,6 +1637,8 @@ void array_new(bool b) {
 // CIR-NEXT:        cir.store %[[FALSE]], %[[ACTIVE]] : !cir.bool, 
!cir.ptr<!cir.bool>
 // CIR:             %[[C:.*]] = cir.const #cir.int<99> : !s8i
 // CIR-NEXT:        cir.call @_ZN3ObjC1Ec(%{{.*}}, %[[C]])
+// CIR:             %[[ARRAY_FALSE:.*]] = cir.const #false
+// CIR-NEXT:        cir.store %[[ARRAY_FALSE]], %[[ARRAY_ACTIVE]] : !cir.bool, 
!cir.ptr<!cir.bool>
 // CIR:             %[[G:.*]] = cir.call @_Z1gv()
 // CIR-NEXT:        cir.call @_Z9use_arrayP3Aggi(%[[ARRAY]], %[[G]])
 // CIR-NEXT:        cir.yield
@@ -1589,6 +1654,11 @@ void array_new(bool b) {
 // CIR-NEXT:      %[[IS_ACTIVE:.*]] = cir.load {{.*}} %[[ACTIVE]] : 
!cir.ptr<!cir.bool>, !cir.bool
 // CIR-NEXT:      cir.if %[[IS_ACTIVE]] {
 // CIR-NEXT:        cir.call @_ZN3ObjD1Ev(%[[A]]){{.*}}
+// CIR:         } cleanup normal {
+// CIR-NEXT:      %[[IS_ARRAY_ACTIVE:.*]] = cir.load {{.*}} %[[ARRAY_ACTIVE]] 
: !cir.ptr<!cir.bool>, !cir.bool
+// CIR-NEXT:      cir.if %[[IS_ARRAY_ACTIVE]] {
+// CIR:             cir.do {
+// CIR:               cir.call @_ZN3AggD1Ev(
 
 void use_int(int, int);
 
diff --git a/clang/test/CIR/CodeGen/static-local-guard-abort.cpp 
b/clang/test/CIR/CodeGen/static-local-guard-abort.cpp
index 3b18fc8d187d5..3d4a61cb64a05 100644
--- a/clang/test/CIR/CodeGen/static-local-guard-abort.cpp
+++ b/clang/test/CIR/CodeGen/static-local-guard-abort.cpp
@@ -69,25 +69,36 @@ void g() { static Elem arr[] = {Temp(1), Temp(2), Temp(3)}; 
}
 
 // An array element constructor can throw part-way through, which stacks three
 // kinds of cleanup inside the guard. Outermost is the guard-abort scope; 
within
-// it the partial-array destroy, EH-only and bounded by the arrayinit.endOfInit
-// watermark so it destroys exactly the elements already constructed; and 
within
-// that a `cleanup all` per argument temporary, which is destroyed on the 
normal
-// path too. The __cxa_atexit that registers the whole array stays inside the
-// guard-abort scope, and only __cxa_guard_release is outside it.
+// it the partial-array destroy, bounded by the arrayinit.endOfInit watermark 
so
+// it destroys exactly the elements already constructed; and within that a
+// `cleanup all` per argument temporary, which is destroyed on the normal path
+// too. The temporaries outlive the array initialization, so the partial-array
+// destroy is deactivated below them, with a flag that the end of the array
+// initialization clears. The __cxa_atexit that registers the whole array stays
+// inside the guard-abort scope, and only __cxa_guard_release is outside it.
 
 // CIR-LABEL: cir.func {{.*}}@_Z1gv
 // CIR:         %[[G:.*]] = cir.get_global @_ZGVZ1gvE3arr
 // CIR:         cir.call...
[truncated]

``````````

</details>


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

Reply via email to