Skip to content

Commit 4ea9dc8

Browse files
authored
Merge pull request #22774 from gottesmm/pr-d8bb6062232a40d5a4a85999a7ef0763d867a7b2
2 parents 4a1b98c + 0274aa7 commit 4ea9dc8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/SILOptimizer/Mandatory/DiagnoseUnreachable.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ class UnreachableUserCodeReportingState {
9191
llvm::DenseMap<const SILBasicBlock*, UnreachableInfo> MetaMap;
9292
};
9393

94+
static void deleteEndBorrows(SILValue v) {
95+
SmallVector<SILInstruction *, 4> endBorrowList;
96+
for (auto *use : v->getUses()) {
97+
if (auto *ebi = dyn_cast<EndBorrowInst>(use->getUser())) {
98+
endBorrowList.push_back(ebi);
99+
}
100+
}
101+
while (!endBorrowList.empty()) {
102+
endBorrowList.pop_back_val()->eraseFromParent();
103+
}
104+
}
105+
94106
/// Propagate/remove basic block input values when all predecessors
95107
/// supply the same arguments.
96108
static void propagateBasicBlockArgs(SILBasicBlock &BB) {
@@ -173,6 +185,13 @@ static void propagateBasicBlockArgs(SILBasicBlock &BB) {
173185
// this to CCP and trigger another round of copy propagation.
174186
SILArgument *Arg = *AI;
175187

188+
// If this argument is guaranteed and Args[Idx] is a SILFunctionArgument,
189+
// delete the end_borrow.
190+
if (Arg->getOwnershipKind() == ValueOwnershipKind::Guaranteed &&
191+
isa<SILFunctionArgument>(Args[Idx])) {
192+
deleteEndBorrows(Arg);
193+
}
194+
176195
// We were able to fold, so all users should use the new folded value.
177196
Arg->replaceAllUsesWith(Args[Idx]);
178197
NumBasicBlockArgsPropagated++;

test/SILOptimizer/diagnose_unreachable.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import Builtin
44
import Swift
55

6+
sil @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
7+
68
sil private @test1 : $() -> () {
79
bb0:
810
%5 = integer_literal $Builtin.Int1, 1
@@ -502,3 +504,21 @@ bb3:
502504
%9999 = tuple()
503505
return %9999 : $()
504506
}
507+
508+
// CHECK-LABEL: sil [ossa] @eliminate_end_borrow_when_propagating_bb_args : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
509+
// CHECK-NOT: end_borrow
510+
// CHECK: [[FUNC_REF:%.*]] = function_ref @guaranteed_nativeobject_user
511+
// CHECK-NEXT: apply [[FUNC_REF]](%0) :
512+
// CHECK-NOT: end_borrow
513+
// CHECK: } // end sil function 'eliminate_end_borrow_when_propagating_bb_args'
514+
sil [ossa] @eliminate_end_borrow_when_propagating_bb_args : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
515+
bb0(%0 : @guaranteed $Builtin.NativeObject):
516+
br bb1(%0 : $Builtin.NativeObject)
517+
518+
bb1(%1 : @guaranteed $Builtin.NativeObject):
519+
%2 = function_ref @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
520+
apply %2(%1) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
521+
end_borrow %1 : $Builtin.NativeObject
522+
%9999 = tuple()
523+
return %9999 : $()
524+
}

0 commit comments

Comments
 (0)