Skip to content

Commit 4642e30

Browse files
committed
[OSSACanOwned] Dead-end extend base lifetime.
Regardless of consumes of copies, if the original value is not consumed on a dead-end path, it must remain life to that dead-end. rdar://145154549
1 parent 653925b commit 4642e30

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ class CanonicalizeOSSALifetime final {
237237
/// copies.
238238
const MaximizeLifetime_t maximizeLifetime;
239239

240+
SILFunction *function;
241+
240242
// If present, will be used to ensure that the lifetime is not shortened to
241243
// end inside an access scope which it previously enclosed. (Note that ending
242244
// before such an access scope is fine regardless.)
@@ -354,7 +356,7 @@ class CanonicalizeOSSALifetime final {
354356
DeadEndBlocksAnalysis *deadEndBlocksAnalysis, DominanceInfo *domTree,
355357
BasicCalleeAnalysis *calleeAnalysis, InstructionDeleter &deleter)
356358
: pruneDebugMode(pruneDebugMode), maximizeLifetime(maximizeLifetime),
357-
accessBlockAnalysis(accessBlockAnalysis),
359+
function(function), accessBlockAnalysis(accessBlockAnalysis),
358360
deadEndBlocksAnalysis(deadEndBlocksAnalysis), domTree(domTree),
359361
calleeAnalysis(calleeAnalysis), deleter(deleter) {}
360362

lib/SILOptimizer/Utils/CanonicalizeOSSALifetime.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
277277
void CanonicalizeOSSALifetime::extendLivenessToDeadEnds() {
278278
// TODO: OSSALifetimeCompletion: Once lifetimes are always complete, delete
279279
// this method.
280+
281+
// (1) Extend liveness to the availability boundary of currentDef.
282+
SmallVector<SILBasicBlock *, 32> directDiscoverdBlocks;
283+
SSAPrunedLiveness directLiveness(function, &directDiscoverdBlocks);
284+
directLiveness.initializeDef(getCurrentDef());
285+
directLiveness.computeSimple();
286+
OSSALifetimeCompletion::visitAvailabilityBoundary(
287+
getCurrentDef(), directLiveness, [&](auto *unreachable, auto end) {
288+
if (end == OSSALifetimeCompletion::LifetimeEnd::Boundary) {
289+
recordUnreachableLifetimeEnd(unreachable);
290+
}
291+
unreachable->visitPriorInstructions([&](auto *inst) {
292+
liveness->extendToNonUse(inst);
293+
return true;
294+
});
295+
});
296+
297+
// (2) Extend liveness to the availability boundary of the copy-extended
298+
// availability boundary of currentDef.
280299
SmallVector<SILBasicBlock *, 32> discoveredBlocks(this->discoveredBlocks);
281300
SSAPrunedLiveness completeLiveness(*liveness, &discoveredBlocks);
282301

test/SILOptimizer/canonicalize_ossa_lifetime_unit.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import Swift
44

55
class C {}
6+
7+
enum Never {}
8+
sil @run : $@convention(thin) () -> Never
69
sil @getOwned : $@convention(thin) () -> @owned C
710
sil @barrier : $@convention(thin) () -> ()
811
sil [ossa] @getC : $@convention(thin) () -> @owned C
@@ -918,3 +921,18 @@ entry(%a: @owned $C):
918921
%retval = tuple ()
919922
return %retval
920923
}
924+
925+
// CHECK-LABEL: begin running test {{.*}} on extend_lifetime_to_deadend_despite_copy_consume
926+
// CHECK-LABEL: sil [ossa] @extend_lifetime_to_deadend_despite_copy_consume : {{.*}} {
927+
// CHECK : copy_value
928+
// CHECK-LABEL: } // end sil function 'extend_lifetime_to_deadend_despite_copy_consume'
929+
// CHECK-LABEL: end running test {{.*}} on extend_lifetime_to_deadend_despite_copy_consume
930+
sil [ossa] @extend_lifetime_to_deadend_despite_copy_consume : $@convention(thin) (@owned C) -> () {
931+
entry(%c : @owned $C):
932+
specify_test "canonicalize_ossa_lifetime true false true @argument"
933+
%cc = copy_value %c
934+
%takeC = function_ref @takeC : $@convention(thin) (@owned C) -> ()
935+
apply %takeC(%cc) : $@convention(thin) (@owned C) -> ()
936+
%run = function_ref @run : $@convention(thin) () -> Never
937+
unreachable
938+
}

0 commit comments

Comments
 (0)