Skip to content

Commit f424639

Browse files
Merge pull request #80990 from nate-chandler/rdar147586673
[OSSACanOwned] Don't dead-end extend if consumed.
2 parents 8e19f3f + 40bcc74 commit f424639

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class CanonicalizeOSSALifetime final {
261261
SILValue currentDef;
262262

263263
/// Instructions beyond which liveness is not extended by destroy uses.
264-
ArrayRef<SILInstruction *> currentLexicalLifetimeEnds;
264+
ArrayRef<SILInstruction *> explicitLifetimeEnds;
265265

266266
/// Original points in the CFG where the current value's lifetime is consumed
267267
/// or destroyed. Each block either contains a consuming instruction (e.g.
@@ -368,7 +368,7 @@ class CanonicalizeOSSALifetime final {
368368
clear();
369369

370370
currentDef = def;
371-
currentLexicalLifetimeEnds = lexicalLifetimeEnds;
371+
explicitLifetimeEnds = lexicalLifetimeEnds;
372372

373373
liveness->initializeDiscoveredBlocks(&discoveredBlocks);
374374
liveness->initializeDef(getCurrentDef());
@@ -465,6 +465,16 @@ class CanonicalizeOSSALifetime final {
465465
UserRange getUsers() const { return liveness->getAllUsers(); }
466466

467467
private:
468+
bool endingLifetimeAtExplicitEnds() const {
469+
return explicitLifetimeEnds.size() > 0;
470+
}
471+
472+
bool respectsDeadEnds() const {
473+
// TODO: OSSALifetimeCompletion: Once lifetimes are always complete, delete
474+
// this method.
475+
return !endingLifetimeAtExplicitEnds();
476+
}
477+
468478
bool respectsDeinitBarriers() const {
469479
if (!currentDef->isLexical())
470480
return false;

lib/SILOptimizer/Utils/CanonicalizeOSSALifetime.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ void CanonicalizeOSSALifetime::extendLivenessToDeadEnds() {
387387

388388
void CanonicalizeOSSALifetime::extendLivenessToDeinitBarriers() {
389389
SmallVector<SILInstruction *, 8> ends;
390-
if (currentLexicalLifetimeEnds.size() > 0) {
390+
if (endingLifetimeAtExplicitEnds()) {
391391
visitExtendedUnconsumedBoundary(
392-
currentLexicalLifetimeEnds,
392+
explicitLifetimeEnds,
393393
[&ends](auto *instruction, auto lifetimeEnding) {
394394
instruction->visitSubsequentInstructions([&](auto *next) {
395395
ends.push_back(next);
@@ -1406,10 +1406,12 @@ bool CanonicalizeOSSALifetime::computeLiveness() {
14061406
clear();
14071407
return false;
14081408
}
1409-
if (respectsDeinitBarriers()) {
1410-
extendLexicalLivenessToDeadEnds();
1409+
if (respectsDeadEnds()) {
1410+
if (respectsDeinitBarriers()) {
1411+
extendLexicalLivenessToDeadEnds();
1412+
}
1413+
extendLivenessToDeadEnds();
14111414
}
1412-
extendLivenessToDeadEnds();
14131415
if (respectsDeinitBarriers()) {
14141416
extendLivenessToDeinitBarriers();
14151417
}

test/Interpreter/consume_operator_kills_copyable_values.swift.gyb

+15
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ func testNested2_${introducer}(_ c1: Bool, _ c2: Bool) {
176176
end([c1, c2])
177177
}
178178

179+
// CHECK-LABEL: begin testDeadEnd_{{let|var}}(_:)(true)
180+
// CHECK: hi
181+
// CHECK: adios
182+
// CHECK: barrier
183+
// CHECK-LABEL: end testDeadEnd_{{let|var}}(_:)(true)
184+
func testDeadEnd_${introducer}(_ condition: Bool) {
185+
begin([condition])
186+
${introducer} x = X(0)
187+
_ = consume x
188+
guard condition else { fatalError() }
189+
barrier()
190+
end([condition])
191+
}
192+
179193
func main_${introducer}() {
180194
simple_${introducer}()
181195
testLeft_${introducer}(true)
@@ -188,6 +202,7 @@ func main_${introducer}() {
188202
testNested2_${introducer}(false, true)
189203
testNested2_${introducer}(true, false)
190204
testNested2_${introducer}(true, true)
205+
testDeadEnd_${introducer}(true)
191206
}
192207

193208
% end

0 commit comments

Comments
 (0)