@@ -149,6 +149,13 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
149
149
if (SubstCalleeTy->hasArchetype () || ConvertCalleeTy->hasArchetype ())
150
150
return nullptr ;
151
151
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
+
152
159
// Ok, we can now perform our transformation. Grab AI's operands and the
153
160
// relevant types from the ConvertFunction function type and AI.
154
161
Builder.setCurrentDebugScope (AI.getDebugScope ());
@@ -240,7 +247,8 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
240
247
241
248
Builder.createBranch (AI.getLoc (), TAI->getNormalBB (), branchArgs);
242
249
}
243
-
250
+
251
+ Builder.setInsertionPoint (AI.getInstruction ());
244
252
return Builder.createTryApply (AI.getLoc (), funcOper, SubstitutionMap (), Args,
245
253
normalBB, TAI->getErrorBB (),
246
254
TAI->getApplyOptions ());
@@ -1623,6 +1631,10 @@ SILInstruction *SILCombiner::visitTryApplyInst(TryApplyInst *AI) {
1623
1631
if (isa<PartialApplyInst>(AI->getCallee ()))
1624
1632
return nullptr ;
1625
1633
1634
+ if (auto *CFI = dyn_cast<ConvertFunctionInst>(AI->getCallee ())) {
1635
+ return optimizeApplyOfConvertFunctionInst (AI, CFI);
1636
+ }
1637
+
1626
1638
// Optimize readonly functions with no meaningful users.
1627
1639
SILFunction *Fn = AI->getReferencedFunctionOrNull ();
1628
1640
if (Fn && Fn->getEffectsKind () < EffectsKind::ReleaseNone) {
0 commit comments