[clang] [clang-tools-extra] [codegen] Emit missing cleanups when an expression contains a branch (PR #80698)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 13 12:00:07 PST 2024


================
@@ -592,10 +590,14 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
       // observed to be unnecessary.
       if (endOfInit.isValid()) Builder.CreateStore(element, endOfInit);
     }
-
-    LValue elementLV = CGF.MakeAddrLValue(
-        Address(element, llvmElementType, elementAlign), elementType);
+    Address address = Address(element, llvmElementType, elementAlign);
+    LValue elementLV = CGF.MakeAddrLValue(address, elementType);
     EmitInitializationToLValue(Args[i], elementLV);
+    // Schedule to emit element cleanup if we see a branch in the array
+    // initialisation expression.
+    if (CGF.needsBranchCleanup(dtorKind))
+      CGF.pushDestroy(BranchInExprCleanup, address, elementType,
----------------
efriedma-quic wrote:

The complexity here is basically that we'd have to reimplement mem2reg to compute the correct number of cleanups along every control-flow path.  Instead, we store to an alloca, and just let the mem2reg optimization clean it up.

On a similar note, because mem2reg cleans up the stores, their overhead is minimal.

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


More information about the cfe-commits mailing list