Skip to content

DCE: Don't generate end_borrow for GuaranteedForwardingPhi #61670

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 2 commits into from
Oct 21, 2022
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
3 changes: 3 additions & 0 deletions include/swift/SIL/OwnershipUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ class BorrowedValueKind {
})) {
return Kind::Invalid;
}
if (isGuaranteedForwardingPhi(value)) {
return Kind::Invalid;
}
return Kind::Phi;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ void DCE::endLifetimeOfLiveValue(SILValue value, SILInstruction *insertPt) {
builder.emitDestroyOperation(RegularLocation::getAutoGeneratedLocation(),
value);
}
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
BorrowedValue borrow(value);
if (borrow && borrow.isLocalScope()) {
builder.emitEndBorrowOperation(RegularLocation::getAutoGeneratedLocation(),
value);
}
Expand Down
26 changes: 26 additions & 0 deletions test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,32 @@ bb3(%phi2 : @guaranteed $Klass, %phi3 : @guaranteed $Wrapper1):
return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @test_forwarded_phi3 :
// CHECK: end_borrow
// CHECK: br bb3
// CHECK-LABEL: } // end sil function 'test_forwarded_phi3'
sil [ossa] @test_forwarded_phi3 : $@convention(thin) (@owned Wrapper1) -> () {
bb0(%0 : @owned $Wrapper1):
%outer = begin_borrow %0 : $Wrapper1
br bb1

bb1:
br bb2(%outer : $Wrapper1)

bb2(%phi1 : @guaranteed $Wrapper1):
%ex1 = struct_extract %phi1 : $Wrapper1, #Wrapper1.val1 // user: %4
%ex2 = struct_extract %ex1 : $Wrapper2, #Wrapper2.val2
%f = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
apply %f(%ex2) : $@convention(thin) (@guaranteed Klass) -> ()
br bb3(%ex2 : $Klass, %phi1 : $Wrapper1)

bb3(%phi2 : @guaranteed $Klass, %phi3 : @guaranteed $Wrapper1):
end_borrow %phi3 : $Wrapper1
destroy_value %0 : $Wrapper1
%9999 = tuple()
return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @test_loadborrow_dep :
// CHECK: br bb3
// CHECK: end_borrow
Expand Down