[clang] [CIR] Emit lifetime markers for temporaries (PR #236102)
Jiahao Guo via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 01:23:32 PST 2026
https://github.com/E00N777 updated https://github.com/llvm/llvm-project/pull/236102
>From c868f066a5e7c3439d15a50b8dfce52073a9e342 Mon Sep 17 00:00:00 2001
From: E00N777 <E0N_gjh at 163.com>
Date: Tue, 27 Oct 2026 20:29:07 +0800
Subject: [PATCH 1/2] [CIR] Emit lifetime markers for temporaries
---
clang/lib/CIR/CodeGen/CIRGenCleanup.cpp | 51 ++++-
clang/lib/CIR/CodeGen/CIRGenCleanup.h | 10 +
clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 67 ++++---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 20 +-
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 2 +-
clang/lib/CIR/CodeGen/CIRGenFunction.h | 75 +++++--
clang/lib/CIR/CodeGen/CIRGenStmt.cpp | 22 +-
clang/test/CIR/CodeGen/lifetime-marker.cpp | 223 +++++++++++++++++++--
9 files changed, 387 insertions(+), 85 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
index 63e9dc24acbcf..71a613372b75f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
@@ -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())
+ .emit(*this, EHScopeStack::Cleanup::Flags());
+ return;
+ }
+ emitDestroy(entry.addr, entry.type, entry.destroyer);
+}
+
namespace {
/// Return true if the expression tree contains a construct that causes cleanups
/// to be deferred via pushFullExprCleanup.
@@ -322,12 +368,11 @@ static void closeConditionalCleanupScope(
cir::IfOp::create(builder, scope.getLoc(), flag,
/*withElseRegion=*/false,
[&](mlir::OpBuilder &b, mlir::Location loc) {
- cgf.emitDestroy(entry.addr, entry.type,
- entry.destroyer);
+ cgf.emitPendingCleanup(entry);
builder.createYield(loc);
});
} else {
- cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
+ cgf.emitPendingCleanup(entry);
}
}
builder.createYield(scope.getLoc());
diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.h b/clang/lib/CIR/CodeGen/CIRGenCleanup.h
index 0f761b69c02d1..1b4b2f202553c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCleanup.h
+++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.h
@@ -263,5 +263,15 @@ const EHPersonality &getEHPersonality(CIRGenModule &cgm,
/// emitted.
const EHPersonality &getEHPersonality(CIRGenFunction &cgf);
+struct CallLifetimeEnd final : EHScopeStack::Cleanup {
+ // The raw alloca pointer (in the alloca address space). Mirrors classic
+ // CodeGen's CallLifetimeEnd, which stores the llvm::Value pointer rather
+ // than an Address.
+ mlir::Value addr;
+ CallLifetimeEnd(mlir::Value addr) : addr(addr) {}
+ bool isRedundantBeforeReturn() override { return true; }
+ void emit(CIRGenFunction &cgf, Flags flags) override;
+};
+
} // namespace clang::CIRGen
#endif // CLANG_LIB_CIR_CODEGEN_CIRGENCLEANUP_H
diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
index f74d7b643debb..f68324c4e4bec 100644
--- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
@@ -32,18 +32,6 @@
using namespace clang;
using namespace clang::CIRGen;
-struct CallLifetimeEnd final : EHScopeStack::Cleanup {
- // The raw alloca pointer (in the alloca address space). Mirrors classic
- // CodeGen's CallLifetimeEnd, which stores the llvm::Value pointer rather
- // than an Address.
- mlir::Value addr;
- CallLifetimeEnd(mlir::Value addr) : addr(addr) {}
- bool isRedundantBeforeReturn() override { return true; }
- void emit(CIRGenFunction &cgf, Flags flags) override {
- cgf.emitLifetimeEndOp(addr.getLoc(), addr);
- }
-};
-
CIRGenFunction::AutoVarEmission
CIRGenFunction::emitAutoVarAlloca(const VarDecl &d,
mlir::OpBuilder::InsertPoint ip) {
@@ -390,32 +378,37 @@ void CIRGenFunction::emitAutoVarDecl(const VarDecl &d) {
}
namespace {
-/// Finds the temporaries with a destructor that the initializer of a variable
-/// with automatic storage duration lifetime-extends. Their cleanups are pushed
-/// onto the EH stack when the initializer's full-expression ends.
+/// Finds the temporaries that the initializer of a variable with automatic
+/// storage duration lifetime-extends. Their cleanups are pushed onto the EH
+/// stack when the initializer's full-expression ends.
class LifetimeExtendedTemporaryFinder
: public RecursiveASTVisitor<LifetimeExtendedTemporaryFinder> {
const VarDecl &var;
llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 2> visited;
SmallVector<QualType, 2> types;
+ bool any = false;
public:
explicit LifetimeExtendedTemporaryFinder(const VarDecl &var) : var(var) {}
- /// The types of the temporaries that were found.
+ /// The types of the found temporaries that have a destructor.
ArrayRef<QualType> found() const { return types; }
+ /// Whether any temporary was found, including trivially destructible ones.
+ bool foundAny() const { return any; }
+
// Look into the semantic form of initializer lists, which holds the backing
// array of a std::initializer_list, and into default member initializers.
// The array filler of an initializer list is not visited.
bool shouldVisitImplicitCode() const { return true; }
- /// Records the type of each temporary requiring destruction whose lifetime
- /// is extended by \c var.
+ /// Records each temporary whose lifetime is extended by \c var, and the
+ /// type of those requiring destruction.
bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *m) {
if (m->getExtendingDecl() != &var ||
m->getStorageDuration() != SD_Automatic || !visited.insert(m).second)
return true;
+ any = true;
// Like pushTemporaryCleanup, look at the type of the whole temporary, not
// at the subobject that the reference binds to.
SmallVector<const Expr *, 2> commaLHSs;
@@ -439,19 +432,19 @@ static bool destructorMayThrow(QualType type) {
!fpt->isNothrow();
}
-SmallVector<QualType, 2>
-CIRGenFunction::getLifetimeExtendedTemporariesWithDestructor(const VarDecl &d) {
+CIRGenFunction::ExtendedTemporaries
+CIRGenFunction::getLifetimeExtendedTemporaries(const VarDecl &d) {
const Expr *init = d.getInit();
if (!init)
return {};
LifetimeExtendedTemporaryFinder finder(d);
finder.TraverseStmt(const_cast<Expr *>(init));
- return SmallVector<QualType, 2>(finder.found());
+ return {SmallVector<QualType, 2>(finder.found()), finder.foundAny()};
}
void CIRGenFunction::emitLoopConditionVariable(
const VarDecl &d, CapturedCleanups &condCleanup,
- ArrayRef<QualType> extendedTemporaries) {
+ const ExtendedTemporaries &temps) {
// A condition variable always has automatic storage duration, so this
// mirrors the auto-var path of emitVarDecl/emitAutoVarDecl. Capture the
// lifetime-end cleanup pushed while emitting the alloca, but emit the
@@ -488,17 +481,20 @@ void CIRGenFunction::emitLoopConditionVariable(
// The temporaries that the initializer lifetime-extends are destroyed with
// the variable as well ([class.temporary]p6). Their cleanups are pushed when
// the initializer's full-expression ends, so capture them too and guard them
- // with the same flag.
+ // with the same flag. Their lifetime-end cleanups are captured unguarded, as
+ // their lifetimes start before the initializer runs.
bool needsCleanup = d.needsDestruction(getContext()) != QualType::DK_none;
- bool extendsTemporaries = !extendedTemporaries.empty();
+ bool extendsTemporaries = !temps.withDestructor.empty();
+ bool capturesExtendedCleanups =
+ extendsTemporaries || extendedTemporariesNeedLifetimeEnd(temps);
// Destroy the variable first, then the temporaries in reverse order of
// construction. A throw would skip the remaining destructors because CFG
// flattening does not yet support EH paths in loop cleanup regions.
if (getLangOpts().Exceptions && extendsTemporaries &&
((needsCleanup && destructorMayThrow(d.getType())) ||
- (extendedTemporaries.size() > 1 &&
- llvm::any_of(extendedTemporaries, destructorMayThrow))))
+ (temps.withDestructor.size() > 1 &&
+ llvm::any_of(temps.withDestructor, destructorMayThrow))))
cgm.errorNYI(d.getSourceRange(),
"loop condition variable cleanup with a potentially-throwing "
"destructor before another destructor");
@@ -517,15 +513,16 @@ void CIRGenFunction::emitLoopConditionVariable(
{
LifetimeExtensionCapture capture{&d, &condCleanup, activeFlag};
llvm::SaveAndRestore<const LifetimeExtensionCapture *> captureExtended(
- lifetimeExtensionCapture, extendsTemporaries ? &capture : nullptr);
+ lifetimeExtensionCapture,
+ capturesExtendedCleanups ? &capture : nullptr);
emitAutoVarInit(emission);
}
- // Only lifetime-extended cleanups remain after initialization. If the AST
- // walk found no extended temporaries, these cleanups are uncaptured and wrap
+ // Only lifetime-extended cleanups remain after initialization. If none were
+ // expected to be captured, these cleanups are uncaptured and wrap
// cir.condition in cir.cleanup.scope (e.g. for initializer-list array
// fillers).
- if (!extendsTemporaries && ehStack.stable_begin() != initDepth) {
+ if (!capturesExtendedCleanups && ehStack.stable_begin() != initDepth) {
cgm.errorNYI(d.getSourceRange(),
"loop condition variable whose initializer leaves uncaptured "
"cleanups");
@@ -1361,8 +1358,14 @@ void CIRGenFunction::pushPendingCleanupToEHStack(
std::optional<CapturedCleanups::CaptureScope> capture;
if (entry.capturedBy)
capture.emplace(*entry.capturedBy);
- ehStack.pushCleanup<DestroyObject>(entry.kind, entry.addr, entry.type,
- entry.destroyer);
+ if (entry.kind & LifetimeMarker) {
+ assert(entry.addr.getDefiningOp<cir::AllocaOp>() &&
+ "lifetime cleanup requires the raw alloca address");
+ ehStack.pushCleanup<CallLifetimeEnd>(entry.kind, entry.addr.getPointer());
+ } else {
+ ehStack.pushCleanup<DestroyObject>(entry.kind, entry.addr, entry.type,
+ entry.destroyer);
+ }
if (entry.activeFlag.isValid()) {
EHCleanupScope &scope = cast<EHCleanupScope>(*ehStack.begin());
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 2637266cf3fee..12a118ec2bcdd 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "Address.h"
+#include "CIRGenCleanup.h"
#include "CIRGenConstantEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
@@ -2121,7 +2122,22 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
emitAnyExprToMem(e, object, Qualifiers(), /*isInitializer=*/true);
}
} else {
- assert(!cir::MissingFeatures::emitLifetimeMarkers());
+ mlir::Location loc = getLoc(m->getSourceRange());
+ if (cir::AllocaOp alloca = object.getUnderlyingAllocaOp()) {
+ if (emitLifetimeStartOp(loc, alloca.getResult())) {
+ Address lifetimeAddr(alloca.getResult(), object.getAlignment());
+ switch (m->getStorageDuration()) {
+ case SD_FullExpression:
+ pushFullExprLifetimeEnd(lifetimeAddr);
+ break;
+ case SD_Automatic:
+ pushLifetimeEndAfterFullExpr(lifetimeAddr, m->getExtendingDecl());
+ break;
+ default:
+ llvm_unreachable("alloca-backed temporary has unexpected duration");
+ }
+ }
+ }
emitAnyExprToMem(e, object, Qualifiers(), /*isInitializer=*/true);
}
pushTemporaryCleanup(*this, m, e, object);
@@ -3262,7 +3278,7 @@ CIRGenFunction::emitConditionalBlocks(const AbstractConditionalOperator *e,
resultLV = branchGenFunc(*this, expr);
resultPtr = resultLV ? resultLV->getPointer() : mlir::Value();
eval.endEvaluation();
- branchCleanups.forceCleanup({&resultPtr});
+ branchCleanups.forceCleanup({&resultPtr}, /*deferLifetimeExtended=*/true);
}
// A branch that produced no result is a throw-expression; its region is
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 8e6ded0527cc9..3c3ad1d8840ad 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -3424,7 +3424,7 @@ mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
eval.beginEvaluation();
branch = Visit(expr);
eval.endEvaluation();
- branchCleanups.forceCleanup({&branch});
+ branchCleanups.forceCleanup({&branch}, /*deferLifetimeExtended=*/true);
}
if (branch)
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 409a94b9acff8..c215528b34941 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -309,7 +309,7 @@ void CIRGenFunction::LexicalScope::cleanup() {
// blocks or branches are needed.
if (performCleanup) {
assert(!cir::MissingFeatures::generateDebugInfo());
- forceCleanup();
+ forceCleanup({}, /*deferLifetimeExtended=*/isTernary());
}
mlir::Block *curBlock = builder.getBlock();
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index 396c10ec9001a..d13c645ef9ff7 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -130,19 +130,18 @@ class CIRGenFunction : public CIRGenTypeCache {
class CapturedCleanups;
- /// A cleanup whose destructor call is not emitted where the cleanup is
- /// registered. Used by the lifetime-extended cleanup stack, whose entries
- /// are later promoted onto the EH scope stack, and by the deferred
+ /// A cleanup that is not emitted where it is registered. Used by the
+ /// lifetime-extended cleanup stack, whose entries are later promoted onto
+ /// the EH scope stack, and by the deferred
/// conditional cleanup stack, whose entries are emitted directly into a
/// conditional scope's cleanup region.
///
/// A valid \c activeFlag means the cleanup is conditional. The flag tracks
- /// at run time whether the object has been constructed, and guards the
- /// destructor call.
+ /// at run time whether the branch activated the cleanup.
///
- /// Currently only DestroyObject cleanups use this. When other cleanup types
- /// are needed (e.g., CallLifetimeEnd), this struct can be extended with a
- /// std::variant of cleanup data types.
+ /// LifetimeMarker entries represent CallLifetimeEnd: addr is the raw alloca
+ /// address, and type and destroyer are unused. Other entries represent
+ /// DestroyObject and carry its type and destroyer.
struct PendingCleanupEntry {
CleanupKind kind;
Address addr;
@@ -1313,6 +1312,18 @@ class CIRGenFunction : public CIRGenTypeCache {
/// Defined in CIRGenDecl.cpp where the concrete cleanup types are visible.
void pushPendingCleanupToEHStack(const PendingCleanupEntry &entry);
+ /// Emit a deferred cleanup at the current insertion point.
+ void emitPendingCleanup(const PendingCleanupEntry &entry);
+
+ /// Register a storage lifetime end at the end of the current full-expression,
+ /// deferring it with an active flag when inside a conditional branch.
+ void pushFullExprLifetimeEnd(Address addr);
+
+ /// Register a storage lifetime end for the enclosing scope, after the current
+ /// full-expression has finished.
+ void pushLifetimeEndAfterFullExpr(Address addr,
+ const ValueDecl *extendingDecl);
+
/// Push a cleanup to be run at the end of the current full-expression. Safe
/// against the possibility that we're currently inside a
/// conditionally-evaluated expression.
@@ -1400,7 +1411,10 @@ class CIRGenFunction : public CIRGenTypeCache {
/// Force the emission of cleanups now, instead of waiting
/// until this object is destroyed.
- void forceCleanup(ArrayRef<mlir::Value *> valuesToReload = {}) {
+ /// Conditional arms leave lifetime-extended entries pending until the
+ /// enclosing full expression ends.
+ void forceCleanup(ArrayRef<mlir::Value *> valuesToReload = {},
+ bool deferLifetimeExtended = false) {
assert(performCleanup && "Already forced cleanup");
cgf.didCallStackSave = oldDidCallStackSave;
@@ -1446,9 +1460,15 @@ class CIRGenFunction : public CIRGenTypeCache {
// by either forceDeactivate's or popCleanupBlocks's pops (both only
// pop cir.cleanup.scope ops); they already dominate the post-scope
// insertion point on their own.
- if (didSpillAny) {
- cgf.popCleanupBlocks(cleanupStackDepth, oldLifetimeExtendedSize());
+ ArrayRef<mlir::Value *> valuesToPop =
+ didSpillAny ? ArrayRef<mlir::Value *>() : valuesToReload;
+ if (deferLifetimeExtended)
+ cgf.popCleanupBlocks(cleanupStackDepth, valuesToPop);
+ else
+ cgf.popCleanupBlocks(cleanupStackDepth, oldLifetimeExtendedSize(),
+ valuesToPop);
+ if (didSpillAny) {
// Reload the spilled values now that all cleanup popping (and
// promotion of any lifetime-extended cleanups onto the EH stack) is
// done.
@@ -1457,9 +1477,6 @@ class CIRGenFunction : public CIRGenTypeCache {
continue;
*valPtr = cgf.builder.createLoad(valPtr->getLoc(), addr);
}
- } else {
- cgf.popCleanupBlocks(cleanupStackDepth, oldLifetimeExtendedSize(),
- valuesToReload);
}
performCleanup = false;
@@ -1968,19 +1985,33 @@ class CIRGenFunction : public CIRGenTypeCache {
void emitAutoVarCleanups(const AutoVarEmission &emission);
+ /// The temporaries that a variable's initializer lifetime-extends.
+ struct ExtendedTemporaries {
+ /// The types of those with a destructor.
+ SmallVector<QualType, 2> withDestructor;
+ /// Whether any temporary is extended, including trivially destructible
+ /// ones.
+ bool any = false;
+ };
+
/// Emit a loop's condition-variable declaration. This needs special handling
/// so that we can manage per-iteration cleanups for the loop condition.
- /// \p extendedTemporaries are the types of the temporaries with a destructor
- /// that its initializer lifetime-extends (see
- /// getLifetimeExtendedTemporariesWithDestructor).
+ /// \p temps are the temporaries that its initializer lifetime-extends (see
+ /// getLifetimeExtendedTemporaries).
void emitLoopConditionVariable(const clang::VarDecl &d,
CapturedCleanups &condCleanup,
- llvm::ArrayRef<QualType> extendedTemporaries);
+ const ExtendedTemporaries &temps);
+
+ /// Whether the extended temporaries \p t get lifetime-end cleanups. This
+ /// must match the check in emitLifetimeStartOp.
+ bool extendedTemporariesNeedLifetimeEnd(const ExtendedTemporaries &t) const {
+ return shouldEmitLifetimeMarkers && t.any;
+ }
- /// The types of the temporaries with a destructor that the initializer of
- /// the variable \p d, which has automatic storage duration, lifetime-extends.
- static llvm::SmallVector<QualType, 2>
- getLifetimeExtendedTemporariesWithDestructor(const clang::VarDecl &d);
+ /// The temporaries that the initializer of the variable \p d, which has
+ /// automatic storage duration, lifetime-extends.
+ static ExtendedTemporaries
+ getLifetimeExtendedTemporaries(const clang::VarDecl &d);
/// Emit the initializer for an allocated variable. If this call is not
/// associated with the call to emitAutoVarAlloca (as the address of the
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
index 021148a473977..5189220f43460 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmt.cpp
@@ -1169,15 +1169,15 @@ mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &s,
// per-iteration cleanup region. This scope is constructed after the
// init-statement so the init-statement's cleanups are not captured.
const VarDecl *condVar = s.getConditionVariable();
- SmallVector<QualType, 2> condVarExtendedTemporaries;
+ ExtendedTemporaries condVarTemps;
if (condVar)
- condVarExtendedTemporaries =
- getLifetimeExtendedTemporariesWithDestructor(*condVar);
+ condVarTemps = getLifetimeExtendedTemporaries(*condVar);
bool needsCondCleanup =
condVar &&
(condVar->needsDestruction(getContext()) != QualType::DK_none ||
shouldEmitLifetimeMarkersForAutoVar() ||
- !condVarExtendedTemporaries.empty());
+ !condVarTemps.withDestructor.empty() ||
+ extendedTemporariesNeedLifetimeEnd(condVarTemps));
CapturedCleanups loopCondScope(*this, needsCondCleanup);
auto condBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
@@ -1187,8 +1187,7 @@ mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &s,
if (s.getCond()) {
// If the for statement declares a condition variable, emit that here.
if (condVar)
- emitLoopConditionVariable(*condVar, loopCondScope,
- condVarExtendedTemporaries);
+ emitLoopConditionVariable(*condVar, loopCondScope, condVarTemps);
// C99 6.8.5p2/p4: The first substatement is executed if the
// expression compares unequal to 0. The condition must be a
// scalar type.
@@ -1316,15 +1315,15 @@ CIRGenFunction::emitWhileStmt(const WhileStmt &s,
// its initializer lifetime-extends, and emit them into the loop's
// per-iteration cleanup region.
const VarDecl *condVar = s.getConditionVariable();
- SmallVector<QualType, 2> condVarExtendedTemporaries;
+ ExtendedTemporaries condVarTemps;
if (condVar)
- condVarExtendedTemporaries =
- getLifetimeExtendedTemporariesWithDestructor(*condVar);
+ condVarTemps = getLifetimeExtendedTemporaries(*condVar);
bool needsCondCleanup =
condVar &&
(condVar->needsDestruction(getContext()) != QualType::DK_none ||
shouldEmitLifetimeMarkersForAutoVar() ||
- !condVarExtendedTemporaries.empty());
+ !condVarTemps.withDestructor.empty() ||
+ extendedTemporariesNeedLifetimeEnd(condVarTemps));
CapturedCleanups loopCondScope(*this, needsCondCleanup);
auto condBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
@@ -1332,8 +1331,7 @@ CIRGenFunction::emitWhileStmt(const WhileStmt &s,
assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
// If the while statement declares a condition variable, emit that here.
if (condVar)
- emitLoopConditionVariable(*condVar, loopCondScope,
- condVarExtendedTemporaries);
+ emitLoopConditionVariable(*condVar, loopCondScope, condVarTemps);
// C99 6.8.5p2/p4: The first substatement is executed if the
// expression compares unequal to 0. The condition must be a
// scalar type.
diff --git a/clang/test/CIR/CodeGen/lifetime-marker.cpp b/clang/test/CIR/CodeGen/lifetime-marker.cpp
index 57855c2bdab7f..80d374b3c1398 100644
--- a/clang/test/CIR/CodeGen/lifetime-marker.cpp
+++ b/clang/test/CIR/CodeGen/lifetime-marker.cpp
@@ -278,9 +278,7 @@ void while_record_condvar() {
LoopCond makeLoopCond();
-// Destroy the extended temporary before ending the reference's lifetime.
-// CIR does not emit lifetime markers for the temporary yet and are
-// therefore not checked here.
+// Destroy the extended temporary, then end its lifetime and the reference's.
void while_extended_condvar() {
while (const LoopCond &c = makeLoopCond()) {}
}
@@ -290,26 +288,32 @@ void while_extended_condvar() {
// EXT-CIR: %[[C:.*]] = cir.alloca "c" {{.*}} : !cir.ptr<!cir.ptr<!rec_LoopCond>>
// EXT-CIR: cir.while {
// EXT-CIR-NEXT: cir.lifetime.start %[[C]] : !cir.ptr<!cir.ptr<!rec_LoopCond>>
-// EXT-CIR: cir.call @_Z12makeLoopCondv(%[[TMP]])
+// EXT-CIR: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_LoopCond>
+// EXT-CIR-NEXT: cir.call @_Z12makeLoopCondv(%[[TMP]])
// EXT-CIR: } do {
// EXT-CIR-NOEH: } cleanup normal {
// EXT-CIR-EH: } cleanup all {
// EXT-CIR: cir.if %{{.*}} {
// EXT-CIR-NEXT: cir.call @_ZN8LoopCondD1Ev(%[[TMP]]){{.*}}
// EXT-CIR-NEXT: }
+// EXT-CIR-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_LoopCond>
// EXT-CIR-NEXT: cir.lifetime.end %[[C]] : !cir.ptr<!cir.ptr<!rec_LoopCond>>
// EXT-CIR-NEXT: cir.yield
// EXT-LLVM-LABEL: define{{.*}} void @_Z22while_extended_condvarv
// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[C:.*]])
-// EXT-LLVM-NOEH: call void @_Z12makeLoopCondv(ptr {{.*}} %[[TMP:[^)]*]])
-// EXT-LLVM-EH: invoke void @_Z12makeLoopCondv(ptr {{.*}} %[[TMP:[^)]*]])
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// EXT-LLVM-NOEH: call void @_Z12makeLoopCondv(ptr {{.*}} %[[TMP]])
+// EXT-LLVM-EH: invoke void @_Z12makeLoopCondv(ptr {{.*}} %[[TMP]])
// EXT-LLVM: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[TMP]])
-// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[C]])
+// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.end.p0(ptr %[[C]])
// EXT-LLVM-EH: landingpad { ptr, i32 }
// EXT-LLVM-EH-NEXT: cleanup
// EXT-LLVM-EH: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[TMP]])
-// EXT-LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[C]])
+// EXT-LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// EXT-LLVM-EH-NEXT: call void @llvm.lifetime.end.p0(ptr %[[C]])
+// EXT-LLVM-EH: resume
struct LoopCondHolder {
const LoopCond &c;
@@ -317,7 +321,7 @@ struct LoopCondHolder {
operator bool() const;
};
-// Destroy the record, then its extended temporary, then end its lifetime.
+// Destroy the record, then its extended temporary, then end their lifetimes.
void while_extended_record_condvar() {
while (LoopCondHolder h{makeLoopCond()}) {}
}
@@ -327,6 +331,8 @@ void while_extended_record_condvar() {
// EXT-CIR: %[[H:.*]] = cir.alloca "h" {{.*}} : !cir.ptr<!rec_LoopCondHolder>
// EXT-CIR: cir.while {
// EXT-CIR-NEXT: cir.lifetime.start %[[H]] : !cir.ptr<!rec_LoopCondHolder>
+// EXT-CIR: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_LoopCond>
+// EXT-CIR-NEXT: cir.call @_Z12makeLoopCondv(%[[TMP]])
// EXT-CIR-NOEH: } cleanup normal {
// EXT-CIR-EH: } cleanup all {
// EXT-CIR: cir.if %{{.*}} {
@@ -335,19 +341,212 @@ void while_extended_record_condvar() {
// EXT-CIR: cir.if %{{.*}} {
// EXT-CIR-NEXT: cir.call @_ZN8LoopCondD1Ev(%[[TMP]]){{.*}}
// EXT-CIR-NEXT: }
+// EXT-CIR-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_LoopCond>
// EXT-CIR-NEXT: cir.lifetime.end %[[H]] : !cir.ptr<!rec_LoopCondHolder>
// EXT-CIR-NEXT: cir.yield
// EXT-LLVM-LABEL: define{{.*}} void @_Z29while_extended_record_condvarv
// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[H:.*]])
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
// EXT-LLVM: call void @_ZN14LoopCondHolderD1Ev(ptr {{.*}} %[[H]])
-// EXT-LLVM: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[TMP:[^)]*]])
-// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[H]])
+// EXT-LLVM: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[TMP]])
+// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.end.p0(ptr %[[H]])
// EXT-LLVM-EH: landingpad { ptr, i32 }
// EXT-LLVM-EH-NEXT: cleanup
// EXT-LLVM-EH: call void @_ZN14LoopCondHolderD1Ev(ptr {{.*}} %[[H]])
// EXT-LLVM-EH: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[TMP]])
-// EXT-LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[H]])
+// EXT-LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// EXT-LLVM-EH-NEXT: call void @llvm.lifetime.end.p0(ptr %[[H]])
+// EXT-LLVM-EH: resume
+
+struct TrivialCond {
+ int x;
+ operator bool() const;
+};
+
+TrivialCond makeTrivialCond();
+
+// A trivially destructible extended temporary only needs its lifetime ended
+// in the loop cleanup region, so no active flag is created. A cleanup holding
+// only lifetime markers needs no landing pad.
+void while_extended_trivial_condvar() {
+ while (const TrivialCond &c = makeTrivialCond()) {}
+}
+
+// EXT-CIR-LABEL: cir.func{{.*}} @_Z30while_extended_trivial_condvarv
+// EXT-CIR-NOT: cond.cleanup.isactive
+// EXT-CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR: %[[C:.*]] = cir.alloca "c" {{.*}} : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// EXT-CIR: cir.while {
+// EXT-CIR-NEXT: cir.lifetime.start %[[C]] : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// EXT-CIR-NEXT: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR-NEXT: cir.call @_Z15makeTrivialCondv()
+// EXT-CIR: } do {
+// EXT-CIR-NOEH: } cleanup normal {
+// EXT-CIR-EH: } cleanup all {
+// EXT-CIR-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR-NEXT: cir.lifetime.end %[[C]] : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// EXT-CIR-NEXT: cir.yield
+
+// EXT-LLVM-LABEL: define{{.*}} void @_Z30while_extended_trivial_condvarv()
+// EXT-LLVM-NOT: personality
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[C:.*]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// EXT-LLVM-NEXT: call i32 @_Z15makeTrivialCondv()
+// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.end.p0(ptr %[[C]])
+
+void for_extended_trivial_condvar() {
+ for (; const TrivialCond &c = makeTrivialCond();) {}
+}
+
+// EXT-CIR-LABEL: cir.func{{.*}} @_Z28for_extended_trivial_condvarv
+// EXT-CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR: %[[C:.*]] = cir.alloca "c" {{.*}} : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// EXT-CIR: cir.for : cond {
+// EXT-CIR-NEXT: cir.lifetime.start %[[C]] : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// EXT-CIR-NEXT: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR: } step {
+// EXT-CIR-NOEH: } cleanup normal {
+// EXT-CIR-EH: } cleanup all {
+// EXT-CIR-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR-NEXT: cir.lifetime.end %[[C]] : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// EXT-CIR-NEXT: cir.yield
+
+// EXT-LLVM-LABEL: define{{.*}} void @_Z28for_extended_trivial_condvarv()
+// EXT-LLVM-NOT: personality
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[C:.*]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.end.p0(ptr %[[C]])
+
+struct MixedHolder {
+ const LoopCond &c;
+ const TrivialCond &t;
+ operator bool() const;
+};
+
+// Extended temporaries end in reverse construction order. Only the destructor
+// is guarded by the active flag. If the second initializer throws, the first
+// temporary is destroyed by its full-expression EH cleanup, and the flag keeps
+// the loop cleanup from destroying it again.
+void while_extended_mixed_condvar() {
+ while (MixedHolder h{makeLoopCond(), makeTrivialCond()}) {}
+}
+
+// EXT-CIR-LABEL: cir.func{{.*}} @_Z28while_extended_mixed_condvarv
+// EXT-CIR: %[[CTMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_LoopCond>
+// EXT-CIR: %[[TTMP:.*]] = cir.alloca "ref.tmp1" {{.*}} : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR: %[[H:.*]] = cir.alloca "h" {{.*}} : !cir.ptr<!rec_MixedHolder>
+// EXT-CIR: cir.while {
+// EXT-CIR-NEXT: cir.lifetime.start %[[H]] : !cir.ptr<!rec_MixedHolder>
+// EXT-CIR: cir.lifetime.start %[[CTMP]] : !cir.ptr<!rec_LoopCond>
+// EXT-CIR-NEXT: cir.call @_Z12makeLoopCondv(%[[CTMP]])
+// EXT-CIR: cir.lifetime.start %[[TTMP]] : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR-NEXT: cir.call @_Z15makeTrivialCondv()
+// EXT-CIR-EH: } cleanup eh {
+// EXT-CIR-EH-NEXT: cir.call @_ZN8LoopCondD1Ev(%[[CTMP]]){{.*}}
+// EXT-CIR: } do {
+// EXT-CIR-NOEH: } cleanup normal {
+// EXT-CIR-EH: } cleanup all {
+// EXT-CIR-NEXT: cir.lifetime.end %[[TTMP]] : !cir.ptr<!rec_TrivialCond>
+// EXT-CIR: cir.if %{{.*}} {
+// EXT-CIR-NEXT: cir.call @_ZN8LoopCondD1Ev(%[[CTMP]]){{.*}}
+// EXT-CIR-NEXT: }
+// EXT-CIR-NEXT: cir.lifetime.end %[[CTMP]] : !cir.ptr<!rec_LoopCond>
+// EXT-CIR-NEXT: cir.lifetime.end %[[H]] : !cir.ptr<!rec_MixedHolder>
+// EXT-CIR-NEXT: cir.yield
+
+// EXT-LLVM-LABEL: define{{.*}} void @_Z28while_extended_mixed_condvarv
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[H:.*]])
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[CTMP:.*]])
+// EXT-LLVM: call void @llvm.lifetime.start.p0(ptr %[[TTMP:.*]])
+// EXT-LLVM-NOEH: call i32 @_Z15makeTrivialCondv()
+// EXT-LLVM-EH: invoke i32 @_Z15makeTrivialCondv()
+// EXT-LLVM-EH: landingpad { ptr, i32 }
+// EXT-LLVM-EH-NEXT: cleanup
+// EXT-LLVM-EH: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[CTMP]])
+// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[TTMP]])
+// EXT-LLVM: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[CTMP]])
+// EXT-LLVM: call void @llvm.lifetime.end.p0(ptr %[[CTMP]])
+// EXT-LLVM-NEXT: call void @llvm.lifetime.end.p0(ptr %[[H]])
+// EXT-LLVM-EH: landingpad { ptr, i32 }
+// EXT-LLVM-EH-NEXT: cleanup
+// EXT-LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TTMP]])
+// EXT-LLVM-EH: call void @_ZN8LoopCondD1Ev(ptr {{.*}} %[[CTMP]])
+// EXT-LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[CTMP]])
+// EXT-LLVM-EH-NEXT: call void @llvm.lifetime.end.p0(ptr %[[H]])
+// EXT-LLVM-EH: resume
+
+int consume(const TrivialCond &);
+
+// A full-expression temporary's lifetime ends with the full-expression.
+void full_expr_temporary() {
+ use(consume(makeTrivialCond()));
+}
+
+// CIR-LABEL: cir.func{{.*}} @_Z19full_expr_temporaryv
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_TrivialCond>
+// CIR: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// CIR-NEXT: cir.cleanup.scope {
+// CIR: cir.call @_Z7consumeRK11TrivialCond(%[[TMP]])
+// CIR: cir.call @_Z3usei
+// CIR: } cleanup normal {
+// CIR-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+
+// CIR-EH-LABEL: cir.func{{.*}} @_Z19full_expr_temporaryv
+// CIR-EH: %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_TrivialCond>
+// CIR-EH: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// CIR-EH-NEXT: cir.cleanup.scope {
+// CIR-EH: } cleanup all {
+// CIR-EH-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+
+// LLVM-LABEL: define{{.*}} void @_Z19full_expr_temporaryv
+// LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// LLVM: call noundef i32 @_Z7consumeRK11TrivialCond(ptr {{.*}} %[[TMP]])
+// LLVM: call void @_Z3usei
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+
+// LLVM-EH-LABEL: define{{.*}} void @_Z19full_expr_temporaryv()
+// LLVM-EH-NOT: personality
+// LLVM-EH: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// LLVM-EH: call void @_Z3usei
+// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+
+// A temporary extended by a local reference lives until the end of the
+// enclosing scope, and its lifetime ends before the reference's.
+void extended_temporary() {
+ const TrivialCond &r = makeTrivialCond();
+ use(r.x);
+}
+
+// CIR-LABEL: cir.func{{.*}} @_Z18extended_temporaryv
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_TrivialCond>
+// CIR: %[[R:.*]] = cir.alloca "r" {{.*}} : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// CIR: cir.lifetime.start %[[R]] : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+// CIR: cir.lifetime.start %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// CIR-NEXT: cir.call @_Z15makeTrivialCondv()
+// CIR: cir.call @_Z3usei
+// CIR: } cleanup normal {
+// CIR-NEXT: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_TrivialCond>
+// CIR: } cleanup normal {
+// CIR-NEXT: cir.lifetime.end %[[R]] : !cir.ptr<!cir.ptr<!rec_TrivialCond>>
+
+// LLVM-LABEL: define{{.*}} void @_Z18extended_temporaryv
+// LLVM: call void @llvm.lifetime.start.p0(ptr %[[R:.*]])
+// LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// LLVM: call void @_Z3usei
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[R]])
+
+// LLVM-EH-LABEL: define{{.*}} void @_Z18extended_temporaryv()
+// LLVM-EH-NOT: personality
+// LLVM-EH: call void @llvm.lifetime.start.p0(ptr %[[R:.*]])
+// LLVM-EH: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// LLVM-EH: call void @_Z3usei
+// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[R]])
#ifdef __EXCEPTIONS
>From dd94444ccc7ba4caada93442be8353290070c9be Mon Sep 17 00:00:00 2001
From: E00N777 <E0N_gjh at 163.com>
Date: Fri, 13 Nov 2026 10:56:01 +0800
Subject: [PATCH 2/2] [CIR] Address review feedback on temporary lifetime
markers
- Hoist lifetime.start of trivially destructible full-expression
temporaries out of conditionals, so lifetime.end needs no active flag.
- Restore forceCleanup; callers use deferLifetimeExtendedCleanups().
- Call emitLifetimeEndOp directly in emitPendingCleanup.
- Add tests for conditional temporaries.
---
clang/lib/CIR/CodeGen/CIRGenCleanup.cpp | 4 +-
clang/lib/CIR/CodeGen/CIRGenCleanup.h | 4 +-
clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 5 +-
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 52 +++-
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 3 +-
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 6 +-
clang/lib/CIR/CodeGen/CIRGenFunction.h | 18 +-
clang/test/CIR/CodeGen/lifetime-marker.cpp | 297 +++++++++++++++++++++
8 files changed, 359 insertions(+), 30 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
index 71a613372b75f..7498550a88476 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp
@@ -64,8 +64,8 @@ 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())
- .emit(*this, EHScopeStack::Cleanup::Flags());
+ mlir::Value addr = entry.addr.getPointer();
+ emitLifetimeEndOp(addr.getLoc(), addr);
return;
}
emitDestroy(entry.addr, entry.type, entry.destroyer);
diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.h b/clang/lib/CIR/CodeGen/CIRGenCleanup.h
index 1b4b2f202553c..788467e488e2b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCleanup.h
+++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.h
@@ -264,9 +264,7 @@ const EHPersonality &getEHPersonality(CIRGenModule &cgm,
const EHPersonality &getEHPersonality(CIRGenFunction &cgf);
struct CallLifetimeEnd final : EHScopeStack::Cleanup {
- // The raw alloca pointer (in the alloca address space). Mirrors classic
- // CodeGen's CallLifetimeEnd, which stores the llvm::Value pointer rather
- // than an Address.
+ // The raw alloca pointer (in the alloca address space).
mlir::Value addr;
CallLifetimeEnd(mlir::Value addr) : addr(addr) {}
bool isRedundantBeforeReturn() override { return true; }
diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
index f68324c4e4bec..2dde4de1195f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
@@ -481,8 +481,9 @@ void CIRGenFunction::emitLoopConditionVariable(
// The temporaries that the initializer lifetime-extends are destroyed with
// the variable as well ([class.temporary]p6). Their cleanups are pushed when
// the initializer's full-expression ends, so capture them too and guard them
- // with the same flag. Their lifetime-end cleanups are captured unguarded, as
- // their lifetimes start before the initializer runs.
+ // with the same flag. Their lifetime-end cleanups are captured without the
+ // flag: ending the lifetime of storage whose lifetime never started is
+ // harmless, unlike destroying an object that was never constructed.
bool needsCleanup = d.needsDestruction(getContext()) != QualType::DK_none;
bool extendsTemporaries = !temps.withDestructor.empty();
bool capturesExtendedCleanups =
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 12a118ec2bcdd..9af6bfaf662f4 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2122,17 +2122,50 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
emitAnyExprToMem(e, object, Qualifiers(), /*isInitializer=*/true);
}
} else {
- mlir::Location loc = getLoc(m->getSourceRange());
- if (cir::AllocaOp alloca = object.getUnderlyingAllocaOp()) {
- if (emitLifetimeStartOp(loc, alloca.getResult())) {
- Address lifetimeAddr(alloca.getResult(), object.getAlignment());
+ if (shouldEmitLifetimeMarkers) {
+ if (cir::AllocaOp alloca = object.getUnderlyingAllocaOp()) {
+ mlir::Location loc = getLoc(m->getSourceRange());
+ Address addr(alloca.getResult(), object.getAlignment());
switch (m->getStorageDuration()) {
- case SD_FullExpression:
- pushFullExprLifetimeEnd(lifetimeAddr);
- break;
case SD_Automatic:
- pushLifetimeEndAfterFullExpr(lifetimeAddr, m->getExtendingDecl());
+ if (emitLifetimeStartOp(loc, alloca.getResult()))
+ pushLifetimeEndAfterFullExpr(addr, m->getExtendingDecl());
break;
+
+ case SD_FullExpression: {
+ // Avoid creating a conditional cleanup just to hold a lifetime end.
+ // Instead, start the lifetime of a conditional temporary before the
+ // conditional so that its end is unconditional too. A temporary with
+ // a destructor needs a conditional cleanup anyway, and the sanitizers
+ // that need precise lifetimes must not see the temporary live on a
+ // path that never creates it.
+ const SanitizerSet &sanitizers = getLangOpts().Sanitize;
+ if (isInConditionalBranch() && !e->getType().isDestructedType() &&
+ !(sanitizers.has(SanitizerKind::HWAddress) ||
+ sanitizers.has(SanitizerKind::Memory) ||
+ sanitizers.has(SanitizerKind::MemtagStack) ||
+ cgm.getCodeGenOpts().SanitizeAddressUseAfterScope)) {
+ {
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.restoreInsertionPoint(
+ outermostConditional->getInsertPoint());
+ emitLifetimeStartOp(loc, alloca.getResult());
+ outermostConditional->advanceInsertPoint(
+ &*std::prev(builder.getInsertionPoint()));
+ }
+ // Without an active flag, the end runs whether or not the branch
+ // did.
+ deferredConditionalCleanupStack.push_back(
+ {NormalEHLifetimeMarker, addr, QualType(), nullptr,
+ Address::invalid()});
+ break;
+ }
+
+ if (emitLifetimeStartOp(loc, alloca.getResult()))
+ pushFullExprLifetimeEnd(addr);
+ break;
+ }
+
default:
llvm_unreachable("alloca-backed temporary has unexpected duration");
}
@@ -3278,7 +3311,8 @@ CIRGenFunction::emitConditionalBlocks(const AbstractConditionalOperator *e,
resultLV = branchGenFunc(*this, expr);
resultPtr = resultLV ? resultLV->getPointer() : mlir::Value();
eval.endEvaluation();
- branchCleanups.forceCleanup({&resultPtr}, /*deferLifetimeExtended=*/true);
+ branchCleanups.deferLifetimeExtendedCleanups();
+ branchCleanups.forceCleanup({&resultPtr});
}
// A branch that produced no result is a throw-expression; its region is
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 3c3ad1d8840ad..2de0f6fda044c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -3424,7 +3424,8 @@ mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
eval.beginEvaluation();
branch = Visit(expr);
eval.endEvaluation();
- branchCleanups.forceCleanup({&branch}, /*deferLifetimeExtended=*/true);
+ branchCleanups.deferLifetimeExtendedCleanups();
+ branchCleanups.forceCleanup({&branch});
}
if (branch)
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index c215528b34941..ea15b72a63842 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -309,7 +309,11 @@ void CIRGenFunction::LexicalScope::cleanup() {
// blocks or branches are needed.
if (performCleanup) {
assert(!cir::MissingFeatures::generateDebugInfo());
- forceCleanup({}, /*deferLifetimeExtended=*/isTernary());
+ // A ternary arm leaves lifetime-extended cleanups to the enclosing
+ // full-expression.
+ if (isTernary())
+ deferLifetimeExtendedCleanups();
+ forceCleanup();
}
mlir::Block *curBlock = builder.getBlock();
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index d13c645ef9ff7..8add0b1bc42c3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -1411,10 +1411,7 @@ class CIRGenFunction : public CIRGenTypeCache {
/// Force the emission of cleanups now, instead of waiting
/// until this object is destroyed.
- /// Conditional arms leave lifetime-extended entries pending until the
- /// enclosing full expression ends.
- void forceCleanup(ArrayRef<mlir::Value *> valuesToReload = {},
- bool deferLifetimeExtended = false) {
+ void forceCleanup(ArrayRef<mlir::Value *> valuesToReload = {}) {
assert(performCleanup && "Already forced cleanup");
cgf.didCallStackSave = oldDidCallStackSave;
@@ -1460,15 +1457,9 @@ class CIRGenFunction : public CIRGenTypeCache {
// by either forceDeactivate's or popCleanupBlocks's pops (both only
// pop cir.cleanup.scope ops); they already dominate the post-scope
// insertion point on their own.
- ArrayRef<mlir::Value *> valuesToPop =
- didSpillAny ? ArrayRef<mlir::Value *>() : valuesToReload;
- if (deferLifetimeExtended)
- cgf.popCleanupBlocks(cleanupStackDepth, valuesToPop);
- else
- cgf.popCleanupBlocks(cleanupStackDepth, oldLifetimeExtendedSize(),
- valuesToPop);
-
if (didSpillAny) {
+ cgf.popCleanupBlocks(cleanupStackDepth, oldLifetimeExtendedSize());
+
// Reload the spilled values now that all cleanup popping (and
// promotion of any lifetime-extended cleanups onto the EH stack) is
// done.
@@ -1477,6 +1468,9 @@ class CIRGenFunction : public CIRGenTypeCache {
continue;
*valPtr = cgf.builder.createLoad(valPtr->getLoc(), addr);
}
+ } else {
+ cgf.popCleanupBlocks(cleanupStackDepth, oldLifetimeExtendedSize(),
+ valuesToReload);
}
performCleanup = false;
diff --git a/clang/test/CIR/CodeGen/lifetime-marker.cpp b/clang/test/CIR/CodeGen/lifetime-marker.cpp
index 80d374b3c1398..23205209f0094 100644
--- a/clang/test/CIR/CodeGen/lifetime-marker.cpp
+++ b/clang/test/CIR/CodeGen/lifetime-marker.cpp
@@ -8,6 +8,10 @@
// RUN: FileCheck --input-file=%t-eh.cir %s --check-prefixes=CIR-EH,EXT-CIR,EXT-CIR-EH
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -fcxx-exceptions -fexceptions -fclangir -emit-llvm -disable-llvm-passes %s -o %t-eh.ll
// RUN: FileCheck --input-file=%t-eh.ll %s --check-prefixes=LLVM-EH,EXT-LLVM,EXT-LLVM-EH
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -fsanitize-address-use-after-scope -fclangir -emit-cir %s -o %t-precise.cir
+// RUN: FileCheck --input-file=%t-precise.cir %s --check-prefix=PRECISE
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -disable-lifetime-markers -fclangir -emit-cir %s -o %t-disabled.cir
+// RUN: FileCheck --input-file=%t-disabled.cir %s --implicit-check-not "cir.lifetime"
void use(int);
@@ -548,6 +552,299 @@ void extended_temporary() {
// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[R]])
+extern "C" void consume(const int &);
+extern "C" void after();
+
+struct Guard {
+ Guard(int);
+ ~Guard();
+ int value;
+};
+
+extern "C" void use_guard(const Guard &);
+
+// Trivial destruction must not suppress the lifetime end. End the storage
+// lifetime at the full-expression boundary, before the next statement.
+extern "C" void scalar() {
+ consume(42);
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @scalar()
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!s32i>
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR: cir.store{{.*}}, %[[TMP]]
+// CIR: cir.call @consume(%[[TMP]])
+// CIR: cir.lifetime.end %[[TMP]]
+// CIR: cir.call @after()
+// LLVM-LABEL: define{{.*}} void @scalar()
+// LLVM: %[[TMP:.*]] = alloca i32
+// LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP]])
+// LLVM: call void @consume(ptr{{.*}} %[[TMP]])
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM: call void @after()
+// A lifetime-only cleanup does not introduce an unwind edge on its own.
+// LLVM-EH-LABEL: define{{.*}} void @scalar()
+// LLVM-EH-NOT: invoke
+// LLVM-EH: call void @consume(
+// LLVM-EH: call void @llvm.lifetime.end.p0(
+// LLVM-EH: call void @after()
+// LLVM-EH-NOT: landingpad
+// LLVM-EH: ret void
+
+// Register the end before the destructor so the destructor runs first.
+extern "C" void nontrivial() {
+ use_guard(Guard(1));
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @nontrivial()
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!rec_Guard>
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR: cir.call @_ZN5GuardC1Ei(%[[TMP]]
+// CIR: cir.call @use_guard(%[[TMP]])
+// CIR: cir.call @_ZN5GuardD1Ev(%[[TMP]])
+// CIR: cir.lifetime.end %[[TMP]]
+// CIR: cir.call @after()
+// LLVM-LABEL: define{{.*}} void @nontrivial()
+// LLVM: %[[TMP:.*]] = alloca %struct.Guard
+// LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP]])
+// LLVM: call void @use_guard(ptr{{.*}} %[[TMP]])
+// LLVM: call void @_ZN5GuardD1Ev(ptr{{.*}} %[[TMP]])
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM: call void @after()
+
+// The pending end is promoted after initialization and belongs to the
+// enclosing scope, rather than the initialization full-expression.
+extern "C" void extended() {
+ const Guard &ref = Guard(2);
+ use_guard(ref);
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @extended()
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!rec_Guard>
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR: cir.call @use_guard(
+// CIR: cir.call @after()
+// CIR: cir.call @_ZN5GuardD1Ev(%[[TMP]])
+// CIR: cir.lifetime.end %[[TMP]]
+// LLVM-LABEL: define{{.*}} void @extended()
+// LLVM: %[[TMP:.*]] = alloca %struct.Guard
+// LLVM: call void @use_guard(
+// LLVM: call void @after()
+// LLVM: call void @_ZN5GuardD1Ev(ptr{{.*}} %[[TMP]])
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+
+// The conditional pending entry contains only an address and a marker kind.
+// Its empty type and null destroyer must never reach emitDestroy. Hoisting
+// the start lets the full-expression cleanup end its lifetime without a flag.
+extern "C" void conditional_scalar(bool enabled) {
+ (void)(enabled && (consume(43), true));
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @conditional_scalar(
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!s32i>
+// CIR-NOT: cleanup.cond
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR-NEXT: {{.*}}cir.ternary
+// CIR: cir.call @consume(%[[TMP]])
+// CIR: } cleanup normal {
+// CIR-NEXT: cir.lifetime.end %[[TMP]]
+// CIR: cir.call @after()
+// LLVM-LABEL: define{{.*}} void @conditional_scalar(
+// LLVM: call void @llvm.lifetime.start.p0(ptr %[[TMP:.*]])
+// LLVM: br i1
+// LLVM: call void @consume(ptr{{.*}} %[[TMP]])
+// LLVM: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM: call void @after()
+
+// Precise lifetime boundaries must retain conditional starts and guarded ends.
+// PRECISE-LABEL: cir.func{{.*}} @conditional_scalar(
+// PRECISE: %[[TMP:.*]] = cir.alloca "ref.tmp0"
+// PRECISE: %[[ACTIVE:.*]] = cir.alloca "cleanup.cond"
+// PRECISE: cir.ternary
+// PRECISE: cir.lifetime.start %[[TMP]]
+// PRECISE: cir.call @consume(%[[TMP]])
+// PRECISE: } cleanup normal {
+// PRECISE: %[[FLAG:.*]] = cir.load{{.*}} %[[ACTIVE]]
+// PRECISE: cir.if %[[FLAG]] {
+// PRECISE-NEXT: cir.lifetime.end %[[TMP]]
+
+// Both pending actions use the same stack. Guard the destructor and end
+// separately, in reverse registration order.
+extern "C" void conditional_guard(bool enabled) {
+ (void)(enabled && (use_guard(Guard(3)), true));
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @conditional_guard(
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!rec_Guard>
+// CIR: %[[LIFE:.*]] = cir.alloca "cleanup.cond"{{.*}} : !cir.ptr<!cir.bool>
+// CIR: %[[DTOR:.*]] = cir.alloca "cleanup.cond"{{.*}} : !cir.ptr<!cir.bool>
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR: cir.call @_ZN5GuardC1Ei(%[[TMP]]
+// CIR: cir.call @use_guard(%[[TMP]])
+// CIR: } cleanup normal {
+// CIR: %[[CHECK_DTOR:.*]] = cir.load{{.*}} %[[DTOR]]
+// CIR: cir.if %[[CHECK_DTOR]] {
+// CIR: cir.call @_ZN5GuardD1Ev(%[[TMP]])
+// CIR: %[[CHECK_LIFE:.*]] = cir.load{{.*}} %[[LIFE]]
+// CIR: cir.if %[[CHECK_LIFE]] {
+// CIR: cir.lifetime.end %[[TMP]]
+// CIR: cir.call @after()
+
+// The deferred destructor and lifetime end also run in that order during
+// unwinding, with their own activation flags.
+// LLVM-EH-LABEL: define{{.*}} void @conditional_guard(
+// LLVM-EH: %[[TMP:.*]] = alloca %struct.Guard
+// LLVM-EH: %[[LIFE:.*]] = alloca i8
+// LLVM-EH: %[[DTOR:.*]] = alloca i8
+// LLVM-EH: call void @llvm.lifetime.start.p0(ptr %[[TMP]])
+// LLVM-EH: store i8 1, ptr %[[LIFE]]
+// LLVM-EH: invoke void @_ZN5GuardC1Ei(
+// LLVM-EH: store i8 1, ptr %[[DTOR]]
+// LLVM-EH: invoke void @use_guard(
+// LLVM-EH: load i8, ptr %[[DTOR]]
+// LLVM-EH: call void @_ZN5GuardD1Ev(ptr{{.*}} %[[TMP]])
+// LLVM-EH: load i8, ptr %[[LIFE]]
+// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM-EH: landingpad
+// LLVM-EH: load i8, ptr %[[DTOR]]
+// LLVM-EH: call void @_ZN5GuardD1Ev(ptr{{.*}} %[[TMP]])
+// LLVM-EH: load i8, ptr %[[LIFE]]
+// LLVM-EH: call void @llvm.lifetime.end.p0(ptr %[[TMP]])
+// LLVM-EH: resume
+// LLVM-EH: call void @after()
+
+// A conditionally initialized scalar reference extends the chosen temporary's
+// lifetime. The end entries are promoted with their active flags intact.
+extern "C" void conditional_extended(bool enabled) {
+ const int &ref = enabled ? static_cast<const int &>(44)
+ : static_cast<const int &>(45);
+ consume(ref);
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @conditional_extended(
+// CIR-DAG: %[[LEFT:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!s32i>
+// CIR-DAG: %[[RIGHT:.*]] = cir.alloca "ref.tmp1"{{.*}} : !cir.ptr<!s32i>
+// CIR: cir.lifetime.start %[[LEFT]]
+// CIR-NOT: cir.lifetime.end
+// CIR: cir.lifetime.start %[[RIGHT]]
+// CIR-NOT: cir.lifetime.end
+// CIR: cir.call @consume(
+// CIR-NOT: cir.lifetime.end
+// CIR: cir.call @after()
+// CIR: cir.if %{{.*}} {
+// CIR: cir.lifetime.end %[[RIGHT]]
+// CIR: cir.if %{{.*}} {
+// CIR: cir.lifetime.end %[[LEFT]]
+
+struct Holder {
+ int value;
+};
+
+// End the complete object's storage lifetime, rather than its member address.
+extern "C" void subobject() {
+ consume(Holder{46}.value);
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @subobject()
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!rec_Holder>
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR: cir.call @consume(
+// CIR: cir.lifetime.end %[[TMP]] : !cir.ptr<!rec_Holder>
+// CIR: cir.call @after()
+
+using IntArray = int[2];
+
+// A pending array lifetime end applies once to the entire alloca.
+extern "C" void extended_array() {
+ const IntArray &ref = IntArray{47, 48};
+ consume(ref[0]);
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @extended_array()
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!cir.array<!s32i x 2>>
+// CIR: cir.lifetime.start %[[TMP]]
+// CIR: cir.call @consume(
+// CIR: cir.call @after()
+// CIR: cir.lifetime.end %[[TMP]]
+
+// Mixing unconditional and conditional temporaries keeps both destructor and
+// lifetime-end order across the conditional scope.
+extern "C" void mixed(bool enabled) {
+ (use_guard(Guard(4)), (void)(enabled && (use_guard(Guard(5)), true)),
+ use_guard(Guard(6)));
+ after();
+}
+
+// CIR-LABEL: cir.func{{.*}} @mixed(
+// CIR-DAG: %[[FIRST:.*]] = cir.alloca "ref.tmp0"{{.*}} : !cir.ptr<!rec_Guard>
+// CIR-DAG: %[[SECOND:.*]] = cir.alloca "ref.tmp1"{{.*}} : !cir.ptr<!rec_Guard>
+// CIR-DAG: %[[THIRD:.*]] = cir.alloca "ref.tmp2"{{.*}} : !cir.ptr<!rec_Guard>
+// CIR: cir.call @use_guard(%[[THIRD]])
+// CIR: cir.call @_ZN5GuardD1Ev(%[[THIRD]])
+// CIR: cir.lifetime.end %[[THIRD]]
+// CIR: cir.call @_ZN5GuardD1Ev(%[[SECOND]])
+// CIR: cir.lifetime.end %[[SECOND]]
+// CIR: cir.call @_ZN5GuardD1Ev(%[[FIRST]])
+// CIR: cir.lifetime.end %[[FIRST]]
+// CIR: cir.call @after()
+
+// Hoisting nested branches must place both starts before the outer conditional.
+// The starts execute on every loop iteration, and both ends precede after().
+extern "C" void nested_loop(bool first, bool second) {
+ while (first) {
+ (void)(first && (second ? (consume(49), true) : (consume(50), false)));
+ after();
+ }
+}
+
+// CIR-LABEL: cir.func{{.*}} @nested_loop(
+// CIR-NOT: cleanup.cond
+// CIR: cir.while
+// CIR: } do {
+// CIR: cir.lifetime.start %[[LEFT:.*]] :
+// CIR-NEXT: cir.lifetime.start %[[RIGHT:.*]] :
+// CIR-NEXT: {{.*}}cir.ternary
+// CIR: cir.call @consume(%[[LEFT]])
+// CIR: cir.call @consume(%[[RIGHT]])
+// CIR: } cleanup normal {
+// CIR-NEXT: cir.lifetime.end %[[RIGHT]]
+// CIR-NEXT: cir.lifetime.end %[[LEFT]]
+// CIR: cir.call @after()
+
+// Lifetime-extended entries from both arms are captured by the loop cleanup,
+// keeping their branch flags rather than ending at the initializer boundary.
+extern "C" void conditional_extended_loop(bool enabled) {
+ while (const int &ref = enabled ? static_cast<const int &>(51)
+ : static_cast<const int &>(0)) {
+ consume(ref);
+ }
+}
+
+// CIR-LABEL: cir.func{{.*}} @conditional_extended_loop(
+// CIR-DAG: %[[LEFT:.*]] = cir.alloca "ref.tmp0"
+// CIR-DAG: %[[RIGHT:.*]] = cir.alloca "ref.tmp1"
+// CIR: cir.while {
+// CIR: cir.lifetime.start %[[LEFT]]
+// CIR-NOT: cir.lifetime.end
+// CIR: cir.lifetime.start %[[RIGHT]]
+// CIR-NOT: cir.lifetime.end
+// CIR: cir.condition
+// CIR: } do {
+// CIR: cir.call @consume(
+// CIR: } cleanup normal {
+// CIR: cir.if %{{.*}} {
+// CIR: cir.lifetime.end %[[RIGHT]]
+// CIR: cir.if %{{.*}} {
+// CIR: cir.lifetime.end %[[LEFT]]
+
#ifdef __EXCEPTIONS
struct Ex {};
More information about the cfe-commits
mailing list