Skip to content

Commit d490c22

Browse files
committed
SimplifyCFG: Skip convert_escape_to_noescape in try_apply -> apply optimization
1 parent 025a8b9 commit d490c22

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,10 @@ static SILValue getActualCallee(SILValue Callee) {
20042004
Callee = CFI->getConverted();
20052005
continue;
20062006
}
2007+
if (auto *Cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(Callee)) {
2008+
Callee = Cvt->getConverted();
2009+
continue;
2010+
}
20072011
if (auto *TTI = dyn_cast<ThinToThickFunctionInst>(Callee)) {
20082012
Callee = TTI->getConverted();
20092013
continue;
@@ -2019,7 +2023,14 @@ static SILValue getActualCallee(SILValue Callee) {
20192023
static bool isTryApplyOfConvertFunction(TryApplyInst *TAI,
20202024
SILValue &Callee,
20212025
SILType &CalleeType) {
2022-
auto *CFI = dyn_cast<ConvertFunctionInst>(TAI->getCallee());
2026+
auto CalleeOperand = TAI->getCallee();
2027+
2028+
// Look through a @noescape conversion.
2029+
auto *Cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(CalleeOperand);
2030+
if (Cvt)
2031+
CalleeOperand = Cvt->getConverted();
2032+
2033+
auto *CFI = dyn_cast<ConvertFunctionInst>(CalleeOperand);
20232034
if (!CFI)
20242035
return false;
20252036

test/SILOptimizer/simplify_cfg.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,3 +3017,30 @@ bb9(%6 : $Builtin.Int32):
30173017
return %6 : $Builtin.Int32
30183018
}
30193019

3020+
sil @adder : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32
3021+
3022+
// CHECK-LABEL: sil @test_noescape
3023+
// CHECK: [[FN:%.*]] = function_ref @adder
3024+
// CHECK: [[PA:%.*]] = partial_apply [callee_guaranteed] [[FN]](%0)
3025+
// CHECK-NOT: try_apply
3026+
// CHECK: apply [[PA]](%1)
3027+
// CHECK: return
3028+
sil @test_noescape : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32 {
3029+
bb0(%0 : $Builtin.Int32, %1 : $Builtin.Int32):
3030+
%f = function_ref @adder : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32
3031+
%pa = partial_apply [callee_guaranteed] %f(%0) : $@convention(thin) (Builtin.Int32, Builtin.Int32) -> Builtin.Int32
3032+
%conv = convert_function %pa : $@callee_guaranteed (Builtin.Int32) -> (Builtin.Int32) to $@callee_guaranteed (Builtin.Int32) -> (Builtin.Int32, @error Error)
3033+
%ne = convert_escape_to_noescape %conv : $@callee_guaranteed (Builtin.Int32) -> (Builtin.Int32, @error Error) to $@noescape @callee_guaranteed (Builtin.Int32) -> (Builtin.Int32, @error Error)
3034+
try_apply %ne(%1) : $@noescape @callee_guaranteed (Builtin.Int32) -> (Builtin.Int32, @error Error), normal bb1, error bb2
3035+
3036+
bb1(%r : $Builtin.Int32):
3037+
br bb3(%r : $Builtin.Int32)
3038+
3039+
bb2(%e : $Error):
3040+
%r1 = integer_literal $Builtin.Int32, 0
3041+
br bb3(%r1 : $Builtin.Int32)
3042+
3043+
bb3(%res : $Builtin.Int32):
3044+
release_value %pa : $@callee_guaranteed (Builtin.Int32) -> (Builtin.Int32)
3045+
return %res : $Builtin.Int32
3046+
}

0 commit comments

Comments
 (0)