File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
lib/SILOptimizer/Mandatory Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff 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.
96108static 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++;
Original file line number Diff line number Diff line change 33import Builtin
44import Swift
55
6+ sil @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
7+
68sil private @test1 : $() -> () {
79bb0:
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+ }
You can’t perform that action at this time.
0 commit comments