Skip to content

Commit 8b9f2a7

Browse files
committed
SimplifyApply: optimize thick calls where the callee is a thin_to_thick_function
``` %2 = thin_to_thick_function %1 %3 = apply %2(...) : @callee_guaranteed -> %2 = thin_to_thick_function %1 %3 = apply %1(...): @convention(thin) ```
1 parent a94d22e commit 8b9f2a7

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyApply.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import SIL
1414

1515
extension ApplyInst : OnoneSimplifyable {
1616
func simplify(_ context: SimplifyContext) {
17+
if tryTransformThickToThinCallee(of: self, context) {
18+
return
19+
}
1720
_ = context.tryDevirtualize(apply: self, isMandatory: false)
1821
}
1922
}
@@ -29,3 +32,29 @@ extension BeginApplyInst : OnoneSimplifyable {
2932
_ = context.tryDevirtualize(apply: self, isMandatory: false)
3033
}
3134
}
35+
36+
/// Optimizes a thick function call if the callee is a `thin_to_thick_function` instruction:
37+
///
38+
/// %2 = thin_to_thick_function %1
39+
/// %3 = apply %2(...) : @callee_guaranteed
40+
/// ->
41+
/// %2 = thin_to_thick_function %1
42+
/// %3 = apply %1(...): @convention(thin)
43+
///
44+
private func tryTransformThickToThinCallee(of apply: ApplyInst, _ context: SimplifyContext) -> Bool {
45+
if let tttf = apply.callee as? ThinToThickFunctionInst,
46+
!apply.callee.type.isCalleeConsumedFunction
47+
{
48+
let builder = Builder(before: apply, context)
49+
let newApply = builder.createApply(function: tttf.operand.value,
50+
apply.substitutionMap,
51+
arguments: Array(apply.arguments),
52+
isNonThrowing: apply.isNonThrowing,
53+
isNonAsync: apply.isNonAsync,
54+
specializationInfo: apply.specializationInfo)
55+
apply.uses.replaceAll(with: newApply, context)
56+
context.erase(instruction: apply)
57+
return true
58+
}
59+
return false
60+
}

test/IRGen/big_types_corner_cases.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -I %S/Inputs/abi %s -emit-ir | %FileCheck %s
1+
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=simplification -I %S/Inputs/abi %s -emit-ir | %FileCheck %s
22

33
// REQUIRES: CPU=x86_64
44
// REQUIRES: OS=macosx

test/SILGen/reabstract.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// RUN: %target-swift-emit-silgen -module-name reabstract -Xllvm -sil-full-demangle %s | %FileCheck %s
3-
// RUN: %target-swift-emit-sil -module-name reabstract -Xllvm -sil-full-demangle %s | %FileCheck %s --check-prefix=MANDATORY
3+
// RUN: %target-swift-emit-sil -module-name reabstract -Xllvm -sil-disable-pass=simplification -Xllvm -sil-full-demangle %s | %FileCheck %s --check-prefix=MANDATORY
44

55
func closureTakingOptional(_ fn: (Int?) -> ()) {}
66
closureTakingOptional({ (_: Any) -> () in })

test/SILGen/rethrows.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// RUN: %target-swift-emit-sil -module-name rethrows -verify %s | %FileCheck %s
2+
// RUN: %target-swift-emit-sil -module-name rethrows -Xllvm -sil-disable-pass=simplification -verify %s | %FileCheck %s
33

44
@discardableResult
55
func rethrower(_ fn: () throws -> Int) rethrows -> Int {

test/SILOptimizer/simplify_apply.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ class Bar {
1313
func foo() -> Int
1414
}
1515

16+
sil @cl : $@convention(thin) () -> Int
17+
18+
// CHECK-LABEL: sil [ossa] @thick_to_thin :
19+
// CHECK: [[F:%.*]] = function_ref @cl
20+
// CHECK: apply [[F]]() : $@convention(thin
21+
// CHECK: } // end sil function 'thick_to_thin'
22+
sil [ossa] @thick_to_thin : $@convention(thin) () -> Int {
23+
bb0:
24+
%0 = function_ref @cl : $@convention(thin) () -> Int
25+
%1 = thin_to_thick_function %0 : $@convention(thin) () -> Int to $@callee_guaranteed () -> Int
26+
%2 = apply %1() : $@callee_guaranteed () -> Int
27+
return %2 : $Int
28+
}
29+
1630
// CHECK-LABEL: sil @devirt_class_method :
1731
// CHECK: [[F:%.*]] = function_ref @bar_foo
1832
// CHECK: apply [[F]]

0 commit comments

Comments
 (0)