Skip to content

[CopyProp] Don't extend lifetime over end_access. #60792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/SILOptimizer/Utils/CanonicalOSSALifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ class CanonicalizeOSSALifetime {
/// ending". end_borrows do not end a liverange that may include owned copies.
PrunedLiveness liveness;

/// The destroys of the value. These are not uses, but need to be recorded so
/// that we know when the last use in a consuming block is (without having to
/// repeatedly do use-def walks from destroys).
SmallPtrSet<SILInstruction *, 8> destroys;

/// remnantLiveOutBlocks are part of the original extended lifetime that are
/// not in canonical pruned liveness. There is a path from a PrunedLiveness
/// boundary to an original destroy that passes through a remnant block.
Expand Down
49 changes: 41 additions & 8 deletions lib/SILOptimizer/Utils/CanonicalOSSALifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
liveness.updateForUse(user, /*lifetimeEnding*/ true);
break;
case OperandOwnership::DestroyingConsume:
// destroy_value does not force pruned liveness (but store etc. does).
if (!isa<DestroyValueInst>(user)) {
if (isa<DestroyValueInst>(user)) {
destroys.insert(user);
} else {
// destroy_value does not force pruned liveness (but store etc. does).
liveness.updateForUse(user, /*lifetimeEnding*/ true);
}
recordConsumingUse(use);
Expand Down Expand Up @@ -335,18 +337,33 @@ void CanonicalizeOSSALifetime::extendLivenessThroughOverlappingAccess() {
bool changed = true;
while (changed) {
changed = false;
blockWorklist.initializeRange(consumingBlocks);
while (auto *bb = blockWorklist.pop()) {
// The blocks in which we may have to extend liveness over access scopes.
//
// It must be populated first so that we can test membership during the loop
// (see findLastConsume).
BasicBlockSetVector blocksToVisit(currentDef->getFunction());
for (auto *block : consumingBlocks) {
blocksToVisit.insert(block);
}
for (auto iterator = blocksToVisit.begin(); iterator != blocksToVisit.end();
++iterator) {
auto *bb = *iterator;
// If the block isn't dead, then we won't need to extend liveness within
// any of its predecessors (though we may within it).
if (liveness.getBlockLiveness(bb) != PrunedLiveBlocks::Dead)
continue;
// Continue searching upward to find the pruned liveness boundary.
for (auto *predBB : bb->getPredecessorBlocks()) {
blocksToVisit.insert(predBB);
}
}
for (auto *bb : blocksToVisit) {
auto blockLiveness = liveness.getBlockLiveness(bb);
// Ignore blocks within pruned liveness.
if (blockLiveness == PrunedLiveBlocks::LiveOut) {
continue;
}
if (blockLiveness == PrunedLiveBlocks::Dead) {
// Continue searching upward to find the pruned liveness boundary.
for (auto *predBB : bb->getPredecessorBlocks()) {
blockWorklist.insert(predBB);
}
// Otherwise, ignore dead blocks with no nonlocal end_access.
if (!accessBlocks->containsNonLocalEndAccess(bb)) {
continue;
Expand All @@ -356,7 +373,23 @@ void CanonicalizeOSSALifetime::extendLivenessThroughOverlappingAccess() {
// Find the latest partially overlapping access scope, if one exists:
// use %def // pruned liveness ends here
// end_access

// Whether to look for the last consume in the block.
//
// We need to avoid extending liveness over end_accesses that occur after
// original liveness ended.
bool findLastConsume =
consumingBlocks.contains(bb) &&
llvm::none_of(bb->getSuccessorBlocks(), [&](auto *successor) {
return blocksToVisit.contains(successor) &&
liveness.getBlockLiveness(successor) ==
PrunedLiveBlocks::Dead;
});
for (auto &inst : llvm::reverse(*bb)) {
if (findLastConsume) {
findLastConsume = !destroys.contains(&inst);
continue;
}
// Stop at the latest use. An earlier end_access does not overlap.
if (blockHasUse && liveness.isInterestingUser(&inst)) {
break;
Expand Down
60 changes: 49 additions & 11 deletions test/SILOptimizer/copy_propagation.sil
Original file line number Diff line number Diff line change
Expand Up @@ -616,24 +616,21 @@ bb2:

// Original Overlapping (unnecessarilly extends pruned liveness):
//
// TODO: this copy could be avoided but is probably an unusual case,
// and sinking the destroy outside the access scope might help to
// optimize the access itself.
//
// %def
// begin_access // access scope unrelated to def
// use %def // pruned liveness ends here
// destroy %def
// end_access
//
// CHECK-LABEL: sil [ossa] @testOriginalOverlapInLiveBlock : $@convention(thin) () -> () {
// CHECK: [[DEF:%.*]] = apply %{{.*}}() : $@convention(thin) () -> @owned AnyObject
// CHECK: begin_access
// CHECK: [[COPY:%.*]] = copy_value [[DEF]] : $AnyObject
// CHECK: store [[COPY]] to [init] %{{.*}} : $*AnyObject
// CHECK: end_access
// CHECK: destroy_value [[DEF]] : $AnyObject
// CHECK: br bb1
// CHECK: [[DEF:%.*]] = apply %{{.*}}() : $@convention(thin) () -> @owned AnyObject
// CHECK: begin_access
// CHECK-OPT: store [[DEF]] to [init] %{{.*}} : $*AnyObject
// CHECK-ONONE: [[COPY:%[^,]+]] = copy_value [[DEF]]
// CHECK-ONONE: store [[COPY]] to [init] %{{.*}} : $*AnyObject
// CHECK-ONONE: destroy_value [[DEF]]
// CHECK: end_access
// CHECK: br bb1
// CHECK-LABEL: } // end sil function 'testOriginalOverlapInLiveBlock'
sil [ossa] @testOriginalOverlapInLiveBlock : $@convention(thin) () -> () {
bb0:
Expand Down Expand Up @@ -901,3 +898,44 @@ exit:
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil [ossa] @dont_extend_beyond_nonoverlapping_end_access_after_store_in_consuming_block : {{.*}} {
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] : @owned $C
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
// CHECK: begin_access
// CHECK-NOT: copy_value
// CHECK: store [[COPY]] to [init] {{%[^,]+}}
// CHECK-LABEL: } // end sil function 'dont_extend_beyond_nonoverlapping_end_access_after_store_in_consuming_block'
sil [ossa] @dont_extend_beyond_nonoverlapping_end_access_after_store_in_consuming_block : $@convention(thin) (@owned C) -> () {
bb0(%instance : @owned $C):
%addr = alloc_stack [lexical] $C, var
%borrow = begin_borrow %instance : $C
%copy = copy_value %borrow : $C
end_borrow %borrow : $C
destroy_value %instance : $C
%access = begin_access [static] [modify] %addr : $*C
store %copy to [init] %addr : $*C
end_access %access : $*C
destroy_addr %addr : $*C
dealloc_stack %addr : $*C
%retval = tuple()
return %retval : $()
}

// CHECK-LABEL: sil [ossa] @dont_extend_beyond_nonoverlapping_endaccess_after_destroyvalue_in_consuming_block : $@convention(thin) () -> () {
// CHECK: begin_access
// CHECK: destroy_value
// CHECK: end_access
// CHECK-NOT: destroy_value
// CHECK-LABEL: } // end sil function 'dont_extend_beyond_nonoverlapping_endaccess_after_destroyvalue_in_consuming_block'
sil [ossa] @dont_extend_beyond_nonoverlapping_endaccess_after_destroyvalue_in_consuming_block : $@convention(thin) () -> () {
%instance = apply undef() : $@convention(thin) () -> @owned C
%addr = alloc_stack [lexical] $C, var
%access = begin_access [static] [modify] %addr : $*C
apply undef(%instance) : $@convention(thin) (@guaranteed C) -> ()
destroy_value %instance : $C
end_access %access : $*C
dealloc_stack %addr : $*C
%retval = tuple()
return %retval : $()
}