Skip to content

Commit 3e35df0

Browse files
committed
Simplification: run begin_borrow simplification in SILCombine
1 parent 12626e3 commit 3e35df0

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginAndLoadBorrow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SIL
1414

15-
extension BeginBorrowInst : OnoneSimplifyable {
15+
extension BeginBorrowInst : OnoneSimplifyable, SILCombineSimplifyable {
1616
func simplify(_ context: SimplifyContext) {
1717
if borrowedValue.ownership == .owned,
1818
// We need to keep lexical lifetimes in place.

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ private func registerSwiftPasses() {
101101
registerPass(autodiffClosureSpecialization, { autodiffClosureSpecialization.run($0) })
102102

103103
// Instruction passes
104+
registerForSILCombine(BeginBorrowInst.self, { run(BeginBorrowInst.self, $0) })
104105
registerForSILCombine(BeginCOWMutationInst.self, { run(BeginCOWMutationInst.self, $0) })
105106
registerForSILCombine(GlobalValueInst.self, { run(GlobalValueInst.self, $0) })
106107
registerForSILCombine(StrongRetainInst.self, { run(StrongRetainInst.self, $0) })

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ PASS(PruneVTables, "prune-vtables",
518518
"Mark class methods that do not require vtable dispatch")
519519
PASS_RANGE(AllPasses, AliasInfoDumper, PruneVTables)
520520

521+
SWIFT_SILCOMBINE_PASS(BeginBorrowInst)
521522
SWIFT_SILCOMBINE_PASS(BeginCOWMutationInst)
522523
SWIFT_SILCOMBINE_PASS(ClassifyBridgeObjectInst)
523524
SWIFT_SILCOMBINE_PASS(GlobalValueInst)

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ sil @use_anyobject_guaranteed : $@convention(thin) (@guaranteed AnyObject) -> ()
9090
sil @use_generic_obj_guaranteed : $@convention(thin) <τ_0_0> (@guaranteed τ_0_0) -> ()
9191
sil [ossa] @unknown : $@convention(thin) () -> ()
9292
sil [ossa] @get_myint : $@convention(thin) () -> (MyInt)
93+
sil [ossa] @use_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
94+
sil [ossa] @useC : $@convention(thin) (@guaranteed C) -> ()
9395

9496
enum KlassNativeObjEither {
9597
case lhs(Klass)
@@ -5414,3 +5416,40 @@ bb1(%10 : @owned $Optional<@callee_guaranteed () -> ()>, %11 : @guaranteed $Opti
54145416
%12 = tuple ()
54155417
return %12 : $()
54165418
}
5419+
5420+
// CHECK-LABEL: sil [ossa] @remove_borrow_of_thin_function :
5421+
// CHECK: [[F:%.*]] = function_ref @unknown
5422+
// CHECK: [[T:%.*]] = thin_to_thick_function [[F]]
5423+
// CHECK-NOT: begin_borrow
5424+
// CHECK: apply %{{[0-9]+}}([[T]])
5425+
// CHECK: apply %0()
5426+
// CHECK-LABEL: } // end sil function 'remove_borrow_of_thin_function'
5427+
sil [ossa] @remove_borrow_of_thin_function : $@convention(thin) () -> () {
5428+
bb0:
5429+
%0 = function_ref @unknown : $@convention(thin) () -> ()
5430+
%1 = thin_to_thick_function %0 to $@callee_guaranteed () -> ()
5431+
%2 = begin_borrow %1
5432+
%3 = function_ref @use_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
5433+
%4 = apply %3(%2) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
5434+
%5 = apply %2(): $@callee_guaranteed () -> ()
5435+
end_borrow %2
5436+
%r = tuple ()
5437+
return %r
5438+
}
5439+
5440+
// CHECK-LABEL: sil [ossa] @remove_borrow_of_owned :
5441+
// CHECK-NOT: begin_borrow
5442+
// CHECK: apply %1(%0)
5443+
// CHECK-NEXT: destroy_value %0
5444+
// CHECK: } // end sil function 'remove_borrow_of_owned'
5445+
sil [ossa] @remove_borrow_of_owned : $@convention(thin) (@owned C) -> () {
5446+
bb0(%0 : @owned $C):
5447+
%1 = begin_borrow %0 : $C
5448+
%2 = function_ref @useC : $@convention(thin) (@guaranteed C) -> ()
5449+
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> ()
5450+
end_borrow %1 : $C
5451+
destroy_value %0: $C
5452+
%6 = tuple ()
5453+
return %6 : $()
5454+
}
5455+

0 commit comments

Comments
 (0)