Skip to content

Add support for end_lifetime in DCE #38315

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
merged 1 commit into from
Jul 10, 2021
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
10 changes: 6 additions & 4 deletions lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ namespace {
// FIXME: Reconcile the similarities between this and
// isInstructionTriviallyDead.
static bool seemsUseful(SILInstruction *I) {
// Even though begin_access/destroy_value/copy_value have side-effects, they
// can be DCE'ed if they do not have useful dependencies/reverse dependencies
// Even though begin_access/destroy_value/copy_value/end_lifetime have
// side-effects, they can be DCE'ed if they do not have useful
// dependencies/reverse dependencies
if (isa<BeginAccessInst>(I) || isa<CopyValueInst>(I) ||
isa<DestroyValueInst>(I))
isa<DestroyValueInst>(I) || isa<EndLifetimeInst>(I))
return false;

// A load [copy] is okay to be DCE'ed if there are no useful dependencies
Expand Down Expand Up @@ -268,7 +269,8 @@ void DCE::markLive() {
break;
}
case SILInstructionKind::DestroyValueInst:
case SILInstructionKind::EndBorrowInst: {
case SILInstructionKind::EndBorrowInst:
case SILInstructionKind::EndLifetimeInst: {
// The instruction is live only if it's operand value is also live
addReverseDependency(I.getOperand(0), &I);
break;
Expand Down
41 changes: 41 additions & 0 deletions test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,44 @@ bb3:
return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @dce_deadendlifetime1 :
// CHECK-NOT: end_lifetime
// CHECK-LABEL: } // end sil function 'dce_deadendlifetime1'
sil [ossa] @dce_deadendlifetime1 : $@convention(thin) () -> () {
bb0:
cond_br undef, bb1, bb2

bb1:
br bb3(undef : $Klass)

bb2:
br bb3(undef : $Klass)

bb3(%2 : @owned $Klass):
end_lifetime %2 : $Klass
%res = tuple ()
return %res : $()
}

// CHECK-LABEL: sil [ossa] @dce_deadendlifetime2 :
// CHECK-NOT: end_lifetime
// CHECK-LABEL: } // end sil function 'dce_deadendlifetime2'
sil [ossa] @dce_deadendlifetime2 : $@convention(thin) () -> () {
bb0:
cond_br undef, bb1, bb2

bb1:
br bb3(undef : $Klass)

bb2:
br bb3(undef : $Klass)

bb3(%2 : @owned $Klass):
br bb4(%2 : $Klass)

bb4(%3 : @owned $Klass):
end_lifetime %3 : $Klass
%res = tuple ()
return %res : $()
}