Skip to content

Commit 8155707

Browse files
committed
MandatoryInlining: Add a peephole for inlining thorough a thin to noescape conversion
%0 = function_ref @return_one : $@convention(thin) () -> Builtin.Int32 %1 = convert_function %0 : $@convention(thin) () -> Builtin.Int32 to $@convention(thin) @NoEscape () -> Builtin.Int32 %2 = thin_to_thick_function %1 : $@convention(thin) @NoEscape () -> Builtin.Int32 to $@NoEscape () -> Builtin.Int32 %3 = apply %2() : $@NoEscape () -> Builtin.Int32 rdar://37945226
1 parent 5f2f440 commit 8155707

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,27 @@ static SILFunction *getCalleeFunction(
319319
// would be a good optimization to handle and would be as simple as inserting
320320
// a cast.
321321
auto skipFuncConvert = [](SILValue CalleeValue) {
322+
// We can also allow a thin @escape to noescape conversion as such:
323+
// %1 = function_ref @thin_closure_impl : $@convention(thin) () -> ()
324+
// %2 = convert_function %1 :
325+
// $@convention(thin) () -> () to $@convention(thin) @noescape () -> ()
326+
// %3 = thin_to_thick_function %2 :
327+
// $@convention(thin) @noescape () -> () to
328+
// $@noescape @callee_guaranteed () -> ()
329+
// %4 = apply %3() : $@noescape @callee_guaranteed () -> ()
330+
if (auto *ThinToNoescapeCast = dyn_cast<ConvertFunctionInst>(CalleeValue)) {
331+
auto FromCalleeTy =
332+
ThinToNoescapeCast->getOperand()->getType().castTo<SILFunctionType>();
333+
if (FromCalleeTy->getExtInfo().hasContext())
334+
return CalleeValue;
335+
auto ToCalleeTy = ThinToNoescapeCast->getType().castTo<SILFunctionType>();
336+
auto EscapingCalleeTy = ToCalleeTy->getWithExtInfo(
337+
ToCalleeTy->getExtInfo().withNoEscape(false));
338+
if (FromCalleeTy != EscapingCalleeTy)
339+
return CalleeValue;
340+
return ThinToNoescapeCast->getOperand();
341+
}
342+
322343
auto *CFI = dyn_cast<ConvertEscapeToNoEscapeInst>(CalleeValue);
323344
if (!CFI)
324345
return CalleeValue;

test/SILOptimizer/mandatory_inlining.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,3 +1184,23 @@ bb0(%0: $C):
11841184
apply %closure() : $@callee_guaranteed () -> ()
11851185
return %closure : $@callee_guaranteed () -> ()
11861186
}
1187+
1188+
1189+
sil [transparent] @return_one : $@convention(thin) () -> Builtin.Int32 {
1190+
bb0:
1191+
%0 = integer_literal $Builtin.Int32, 1
1192+
return %0 : $Builtin.Int32
1193+
}
1194+
1195+
// CHECK-LABEL: sil @test_thin_convert_function_inline
1196+
// CHECK-NEXT: bb0
1197+
// CHECK-NEXT: [[RES:%.*]] = integer_literal $Builtin.Int32, 1
1198+
// CHECK-NEXT return [[RES]]
1199+
sil @test_thin_convert_function_inline : $@convention(thin) () -> Builtin.Int32 {
1200+
bb0:
1201+
%0 = function_ref @return_one : $@convention(thin) () -> Builtin.Int32
1202+
%1 = convert_function %0 : $@convention(thin) () -> Builtin.Int32 to $@convention(thin) @noescape () -> Builtin.Int32
1203+
%2 = thin_to_thick_function %1 : $@convention(thin) @noescape () -> Builtin.Int32 to $@noescape () -> Builtin.Int32
1204+
%3 = apply %2() : $@noescape () -> Builtin.Int32
1205+
return %3 : $Builtin.Int32
1206+
}

0 commit comments

Comments
 (0)