Skip to content

Commit 3d75abc

Browse files
committed
Add an option to force boundary completion of lexical values
1 parent 8f99736 commit 3d75abc

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

include/swift/SIL/OSSALifetimeCompletion.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ class OSSALifetimeCompletion {
5353
/// Insert a lifetime-ending instruction on every path to complete the OSSA
5454
/// lifetime of \p value. Lifetime completion is only relevant for owned
5555
/// values or borrow introducers.
56-
///
56+
/// For lexical values lifetime is completed at unreachable instructions.
57+
/// For non-lexical values lifetime is completed at the lifetime boundary.
58+
/// When \p forceBoundaryCompletion is true, the client is able to guarantee
59+
/// that lifetime completion of lexical values at the lifetime boundary is
60+
/// sufficient.
61+
/// Currently \p forceBoundaryCompletion is used by mem2reg and temprvalueopt
62+
/// to complete lexical enum values on trivial paths.
5763
/// Returns true if any new instructions were created to complete the
5864
/// lifetime.
5965
///
6066
/// TODO: We also need to complete scoped addresses (e.g. store_borrow)!
61-
LifetimeCompletion completeOSSALifetime(SILValue value) {
67+
LifetimeCompletion
68+
completeOSSALifetime(SILValue value, bool forceBoundaryCompletion = false) {
6269
if (value->getOwnershipKind() == OwnershipKind::None)
6370
return LifetimeCompletion::NoLifetime;
6471

@@ -73,13 +80,13 @@ class OSSALifetimeCompletion {
7380
if (!completedValues.insert(value))
7481
return LifetimeCompletion::AlreadyComplete;
7582

76-
return analyzeAndUpdateLifetime(value)
77-
? LifetimeCompletion::WasCompleted
78-
: LifetimeCompletion::AlreadyComplete;
83+
return analyzeAndUpdateLifetime(value, forceBoundaryCompletion)
84+
? LifetimeCompletion::WasCompleted
85+
: LifetimeCompletion::AlreadyComplete;
7986
}
8087

8188
protected:
82-
bool analyzeAndUpdateLifetime(SILValue value);
89+
bool analyzeAndUpdateLifetime(SILValue value, bool forceBoundaryCompletion);
8390
};
8491

8592
//===----------------------------------------------------------------------===//

lib/SIL/Utils/OSSALifetimeCompletion.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ static bool endLifetimeAtUnreachableBlocks(SILValue value,
142142
/// This is only meant to cleanup lifetimes that lead to dead-end blocks. After
143143
/// recursively completing all nested scopes, it then simply ends the lifetime
144144
/// at the Unreachable instruction.
145-
bool OSSALifetimeCompletion::analyzeAndUpdateLifetime(SILValue value) {
145+
bool OSSALifetimeCompletion::analyzeAndUpdateLifetime(
146+
SILValue value, bool forceBoundaryCompletion) {
146147
// Called for inner borrows, inner adjacent reborrows, inner reborrows, and
147148
// scoped addresses.
148149
auto handleInnerScope = [this](SILValue innerBorrowedValue) {
@@ -152,7 +153,7 @@ bool OSSALifetimeCompletion::analyzeAndUpdateLifetime(SILValue value) {
152153
liveness.compute(domInfo, handleInnerScope);
153154

154155
bool changed = false;
155-
if (value->isLexical()) {
156+
if (value->isLexical() && !forceBoundaryCompletion) {
156157
changed |= endLifetimeAtUnreachableBlocks(value, liveness.getLiveness());
157158
} else {
158159
changed |= endLifetimeAtBoundary(value, liveness.getLiveness());

0 commit comments

Comments
 (0)