Skip to content

Commit d8c49df

Browse files
committed
[SILCombine] Rewrite try_apply(convert_function).
A partial revert of 428ab47.
1 parent 5abc22e commit d8c49df

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
149149
if (SubstCalleeTy->hasArchetype() || ConvertCalleeTy->hasArchetype())
150150
return nullptr;
151151

152+
// If we converted from a non-throwing to a throwing function which is
153+
// try_apply'd, rewriting would require changing the CFG. Bail for now.
154+
if (!ConvertCalleeTy->hasErrorResult() && isa<TryApplyInst>(AI)) {
155+
assert(SubstCalleeTy->hasErrorResult());
156+
return nullptr;
157+
}
158+
152159
// Ok, we can now perform our transformation. Grab AI's operands and the
153160
// relevant types from the ConvertFunction function type and AI.
154161
Builder.setCurrentDebugScope(AI.getDebugScope());
@@ -240,7 +247,8 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
240247

241248
Builder.createBranch(AI.getLoc(), TAI->getNormalBB(), branchArgs);
242249
}
243-
250+
251+
Builder.setInsertionPoint(AI.getInstruction());
244252
return Builder.createTryApply(AI.getLoc(), funcOper, SubstitutionMap(), Args,
245253
normalBB, TAI->getErrorBB(),
246254
TAI->getApplyOptions());
@@ -1623,6 +1631,10 @@ SILInstruction *SILCombiner::visitTryApplyInst(TryApplyInst *AI) {
16231631
if (isa<PartialApplyInst>(AI->getCallee()))
16241632
return nullptr;
16251633

1634+
if (auto *CFI = dyn_cast<ConvertFunctionInst>(AI->getCallee())) {
1635+
return optimizeApplyOfConvertFunctionInst(AI, CFI);
1636+
}
1637+
16261638
// Optimize readonly functions with no meaningful users.
16271639
SILFunction *Fn = AI->getReferencedFunctionOrNull();
16281640
if (Fn && Fn->getEffectsKind() < EffectsKind::ReleaseNone) {

test/SILOptimizer/sil_combine.sil

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,9 @@ bb0(%0 : $*@callee_owned (@in ()) -> @out AnotherClass, %1 : $*AnotherClass):
14201420

14211421
sil @convertible_result_with_error : $@convention(thin) () -> (@owned AnotherClass, @error Error)
14221422

1423-
// TODO
1423+
// CHECK-LABEL: sil @peephole_convert_function_result_change_with_error : {{.*}} {
1424+
// CHECK-NOT: convert_function
1425+
// CHECK-LABEL: } // end sil function 'peephole_convert_function_result_change_with_error'
14241426
sil @peephole_convert_function_result_change_with_error : $@convention(thin) () -> () {
14251427
entry:
14261428
%f = function_ref @convertible_result_with_error : $@convention(thin) () -> (@owned AnotherClass, @error Error)

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,9 @@ bb0(%0 : $*@callee_owned (@in ()) -> @out AnotherClass, %1 : $*AnotherClass):
17451745

17461746
sil @convertible_result_with_error : $@convention(thin) () -> (@owned AnotherClass, @error Error)
17471747

1748-
// TODO
1748+
// CHECK-LABEL: sil [ossa] @peephole_convert_function_result_change_with_error : {{.*}} {
1749+
// CHECK-NOT: convert_function
1750+
// CHECK-LABEL: } // end sil function 'peephole_convert_function_result_change_with_error'
17491751
sil [ossa] @peephole_convert_function_result_change_with_error : $@convention(thin) () -> () {
17501752
entry:
17511753
%f = function_ref @convertible_result_with_error : $@convention(thin) () -> (@owned AnotherClass, @error Error)

0 commit comments

Comments
 (0)