Skip to content

Commit db84a47

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 07a3189 commit db84a47

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h

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

241+
SILFunction *function;
242+
241243
// If present, will be used to ensure that the lifetime is not shortened to
242244
// end inside an access scope which it previously enclosed. (Note that ending
243245
// before such an access scope is fine regardless.)
@@ -398,7 +400,7 @@ class CanonicalizeOSSALifetime final {
398400
DeadEndBlocksAnalysis *deadEndBlocksAnalysis, DominanceInfo *domTree,
399401
BasicCalleeAnalysis *calleeAnalysis, InstructionDeleter &deleter)
400402
: pruneDebugMode(pruneDebugMode), maximizeLifetime(maximizeLifetime),
401-
accessBlockAnalysis(accessBlockAnalysis),
403+
function(function), accessBlockAnalysis(accessBlockAnalysis),
402404
deadEndBlocksAnalysis(deadEndBlocksAnalysis), domTree(domTree),
403405
calleeAnalysis(calleeAnalysis), deleter(deleter) {}
404406

lib/SILOptimizer/Utils/CanonicalizeOSSALifetime.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
161161
for (Operand *use : value->getUses()) {
162162
LLVM_DEBUG(llvm::dbgs() << " Use:\n";
163163
use->getUser()->print(llvm::dbgs()));
164-
164+
165165
auto *user = use->getUser();
166166
// Recurse through copies.
167167
if (auto *copy = dyn_cast<CopyValueInst>(user)) {
@@ -276,6 +276,25 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
276276
void CanonicalizeOSSALifetime::extendLivenessToDeadEnds() {
277277
// TODO: OSSALifetimeCompletion: Once lifetimes are always complete, delete
278278
// this method.
279+
280+
// (1) Extend liveness to the availability boundary of currentDef.
281+
SmallVector<SILBasicBlock *, 32> directDiscoverdBlocks;
282+
SSAPrunedLiveness directLiveness(function, &directDiscoverdBlocks);
283+
directLiveness.initializeDef(getCurrentDef());
284+
directLiveness.computeSimple();
285+
OSSALifetimeCompletion::visitAvailabilityBoundary(
286+
getCurrentDef(), directLiveness, [&](auto *unreachable, auto end) {
287+
if (end == OSSALifetimeCompletion::LifetimeEnd::Boundary) {
288+
recordUnreachableLifetimeEnd(unreachable);
289+
}
290+
unreachable->visitPriorInstructions([&](auto *inst) {
291+
liveness->extendToNonUse(inst);
292+
return true;
293+
});
294+
});
295+
296+
// (2) Extend liveness to the availability boundary of the copy-extended
297+
// availability boundary of currentDef.
279298
SmallVector<SILBasicBlock *, 32> discoveredBlocks(this->discoveredBlocks);
280299
SSAPrunedLiveness completeLiveness(*liveness, &discoveredBlocks);
281300

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
@@ -884,3 +887,18 @@ die:
884887
apply undef(%reload) : $@convention(thin) (@guaranteed C) -> ()
885888
unreachable
886889
}
890+
891+
// CHECK-LABEL: begin running test {{.*}} on extend_lifetime_to_deadend_despite_copy_consume
892+
// CHECK-LABEL: sil [ossa] @extend_lifetime_to_deadend_despite_copy_consume : {{.*}} {
893+
// CHECK : copy_value
894+
// CHECK-LABEL: } // end sil function 'extend_lifetime_to_deadend_despite_copy_consume'
895+
// CHECK-LABEL: end running test {{.*}} on extend_lifetime_to_deadend_despite_copy_consume
896+
sil [ossa] @extend_lifetime_to_deadend_despite_copy_consume : $@convention(thin) (@owned C) -> () {
897+
entry(%c : @owned $C):
898+
specify_test "canonicalize_ossa_lifetime true false true @argument"
899+
%cc = copy_value %c
900+
%takeC = function_ref @takeC : $@convention(thin) (@owned C) -> ()
901+
apply %takeC(%cc) : $@convention(thin) (@owned C) -> ()
902+
%run = function_ref @run : $@convention(thin) () -> Never
903+
unreachable
904+
}

0 commit comments

Comments
 (0)