Skip to content

Commit d6399e2

Browse files
committed
Don't generate end_borrow for GuaranteedForwardingPhi in DCE
1 parent 79496ab commit d6399e2

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,8 @@ void DCE::endLifetimeOfLiveValue(SILValue value, SILInstruction *insertPt) {
563563
builder.emitDestroyOperation(RegularLocation::getAutoGeneratedLocation(),
564564
value);
565565
}
566-
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
566+
BorrowedValue borrow(value);
567+
if (borrow && borrow.isLocalScope()) {
567568
builder.emitEndBorrowOperation(RegularLocation::getAutoGeneratedLocation(),
568569
value);
569570
}

test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,32 @@ bb3(%phi2 : @guaranteed $Klass, %phi3 : @guaranteed $Wrapper1):
10551055
return %9999 : $()
10561056
}
10571057

1058+
// CHECK-LABEL: sil [ossa] @test_forwarded_phi3 :
1059+
// CHECK: end_borrow
1060+
// CHECK: br bb3
1061+
// CHECK-LABEL: } // end sil function 'test_forwarded_phi3'
1062+
sil [ossa] @test_forwarded_phi3 : $@convention(thin) (@owned Wrapper1) -> () {
1063+
bb0(%0 : @owned $Wrapper1):
1064+
%outer = begin_borrow %0 : $Wrapper1
1065+
br bb1
1066+
1067+
bb1:
1068+
br bb2(%outer : $Wrapper1)
1069+
1070+
bb2(%phi1 : @guaranteed $Wrapper1):
1071+
%ex1 = struct_extract %phi1 : $Wrapper1, #Wrapper1.val1 // user: %4
1072+
%ex2 = struct_extract %ex1 : $Wrapper2, #Wrapper2.val2
1073+
%f = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1074+
apply %f(%ex2) : $@convention(thin) (@guaranteed Klass) -> ()
1075+
br bb3(%ex2 : $Klass, %phi1 : $Wrapper1)
1076+
1077+
bb3(%phi2 : @guaranteed $Klass, %phi3 : @guaranteed $Wrapper1):
1078+
end_borrow %phi3 : $Wrapper1
1079+
destroy_value %0 : $Wrapper1
1080+
%9999 = tuple()
1081+
return %9999 : $()
1082+
}
1083+
10581084
// CHECK-LABEL: sil [ossa] @test_loadborrow_dep :
10591085
// CHECK: br bb3
10601086
// CHECK: end_borrow

0 commit comments

Comments
 (0)