[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
Mon Feb 19 17:23:03 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:

Are you sure you generated the IR correctly?  Note that clang marks everything optnone at -O0 by default.

That said, I guess -O0 codegen is a potential issue; I don't think we run any passes that would clean up the dead stores in that case.  I'd only be concerned about the case where we don't actually emit the cleanup; trying to minimize the codesize of the cleanup code isn't important at -O0.

To address the case of no cleanups, maybe we could generate the stores lazily?  Basically, save where the stores should be, but don't generate them until we know the cleanup is actually used.  Or equivalently, emit the stores, then erase them if the cleanup isn't emitted.

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


More information about the cfe-commits mailing list