Skip to content

Commit 12626e3

Browse files
committed
Simplification: remove begin_borrow if the borrowed value is a thin_to_thick_function
`thin_to_thick_function` has "none" ownership and is compatible with guaranteed values. Therefore the `begin_borrow` is not needed.
1 parent 6b38f2a commit 12626e3

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginAndLoadBorrow.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extension BeginBorrowInst : OnoneSimplifyable {
2121
!findPointerEscapingUse(of: borrowedValue)
2222
{
2323
tryReplaceBorrowWithOwnedOperand(beginBorrow: self, context)
24+
} else {
25+
removeBorrowOfThinFunction(beginBorrow: self, context)
2426
}
2527
}
2628
}
@@ -77,6 +79,19 @@ private func tryReplaceBorrowWithOwnedOperand(beginBorrow: BeginBorrowInst, _ co
7779
}
7880
}
7981

82+
private func removeBorrowOfThinFunction(beginBorrow: BeginBorrowInst, _ context: SimplifyContext) {
83+
guard let thin2thickFn = beginBorrow.borrowedValue as? ThinToThickFunctionInst,
84+
// For simplicity don't go into the trouble of removing reborrow phi arguments.
85+
beginBorrow.uses.filterUsers(ofType: BranchInst.self).isEmpty else
86+
{
87+
return
88+
}
89+
// `thin_to_thick_function` has "none" ownership and is compatible with guaranteed values.
90+
// Therefore the `begin_borrow` is not needed.
91+
beginBorrow.uses.ignoreUsers(ofType: EndBorrowInst.self).replaceAll(with: thin2thickFn, context)
92+
context.erase(instructionIncludingAllUsers: beginBorrow)
93+
}
94+
8095
/// Replace
8196
/// ```
8297
/// %1 = begin_borrow %0

test/SILOptimizer/simplify_begin_borrow.sil

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ sil @takeC : $@convention(thin) (@guaranteed C) -> ()
3131
sil @takeOwnedC : $@convention(thin) (@owned C) -> ()
3232
sil @takeS1 : $@convention(thin) (@guaranteed S1) -> ()
3333
sil @takeS2 : $@convention(thin) (@guaranteed S2) -> ()
34+
sil @simple_func : $@convention(thin) () -> ()
35+
sil @use_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
3436

3537
// CHECK-LABEL: sil [ossa] @replace_simple :
3638
// CHECK-NOT: begin_borrow
@@ -438,3 +440,43 @@ bb3:
438440
%retval = tuple ()
439441
return %retval : $()
440442
}
443+
444+
// CHECK-LABEL: sil [ossa] @remove_borrow_of_thin_function :
445+
// CHECK: %1 = thin_to_thick_function
446+
// CHECK-NOT: begin_borrow
447+
// CHECK: %3 = apply %2(%1)
448+
// CHECK: %4 = apply %1()
449+
// CHECK-LABEL: } // end sil function 'remove_borrow_of_thin_function'
450+
sil [ossa] @remove_borrow_of_thin_function : $@convention(thin) () -> () {
451+
bb0:
452+
%0 = function_ref @simple_func : $@convention(thin) () -> ()
453+
%1 = thin_to_thick_function %0 to $@callee_guaranteed () -> ()
454+
%2 = begin_borrow %1
455+
%3 = function_ref @use_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
456+
%4 = apply %3(%2) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
457+
%5 = apply %2(): $@callee_guaranteed () -> ()
458+
end_borrow %2
459+
%r = tuple ()
460+
return %r
461+
}
462+
463+
// CHECK-LABEL: sil [ossa] @dont_remove_borrow_of_thin_function :
464+
// CHECK: thin_to_thick_function
465+
// CHECK-NEXT: begin_borrow
466+
// CHECK-LABEL: } // end sil function 'dont_remove_borrow_of_thin_function'
467+
sil [ossa] @dont_remove_borrow_of_thin_function : $@convention(thin) () -> () {
468+
bb0:
469+
%0 = function_ref @simple_func : $@convention(thin) () -> ()
470+
%1 = thin_to_thick_function %0 to $@callee_guaranteed () -> ()
471+
%2 = begin_borrow %1
472+
%3 = function_ref @use_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
473+
%4 = apply %3(%2) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
474+
%5 = apply %2(): $@callee_guaranteed () -> ()
475+
br bb1(%2)
476+
bb1(%7 : @reborrow $@callee_guaranteed () -> ()):
477+
%8 = borrowed %7 from ()
478+
end_borrow %8
479+
%r = tuple ()
480+
return %r
481+
}
482+

0 commit comments

Comments
 (0)