[clang] [CIR] Emit lifetime markers for temporaries (PR #236102)

Jiahao Guo via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 04:52:27 PST 2026


================
@@ -25,6 +25,52 @@
 using namespace clang;
 using namespace clang::CIRGen;
 
+void CallLifetimeEnd::emit(CIRGenFunction &cgf, Flags flags) {
+  cgf.emitLifetimeEndOp(addr.getLoc(), addr);
+}
+
+void CIRGenFunction::pushFullExprLifetimeEnd(Address addr) {
+  assert(addr.getDefiningOp<cir::AllocaOp>() &&
+         "lifetime cleanup requires the raw alloca address");
+  if (!isInConditionalBranch()) {
+    ehStack.pushCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker,
+                                         addr.getPointer());
+    return;
+  }
+
+  Address activeFlag = createCleanupActiveFlag();
+  deferredConditionalCleanupStack.push_back(
+      {NormalEHLifetimeMarker, addr, QualType(), nullptr, activeFlag});
+}
+
+void CIRGenFunction::pushLifetimeEndAfterFullExpr(
+    Address addr, const ValueDecl *extendingDecl) {
+  assert(addr.getDefiningOp<cir::AllocaOp>() &&
+         "lifetime cleanup requires the raw alloca address");
+  Address activeFlag =
+      isInConditionalBranch() ? createCleanupActiveFlag() : Address::invalid();
+  PendingCleanupEntry entry{NormalEHLifetimeMarker, addr, QualType(), nullptr,
+                            activeFlag};
+  // A loop condition variable's temporary ends its lifetime in the loop's
+  // cleanup region. Unlike its destructor, this is not guarded by the
+  // variable's active flag.
+  if (lifetimeExtensionCapture &&
+      lifetimeExtensionCapture->extendingDecl == extendingDecl)
+    entry.capturedBy = lifetimeExtensionCapture->cleanups;
+  lifetimeExtendedCleanupStack.push_back(entry);
+}
+
+void CIRGenFunction::emitPendingCleanup(const PendingCleanupEntry &entry) {
+  if (entry.kind & LifetimeMarker) {
+    assert(entry.addr.getDefiningOp<cir::AllocaOp>() &&
+           "lifetime cleanup requires the raw alloca address");
+    CallLifetimeEnd(entry.addr.getPointer())
----------------
E00N777 wrote:

There really isn't any obvious advantage to using it indirectly here, so I should just call it directly instead.

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


More information about the cfe-commits mailing list