@@ -410,21 +410,29 @@ static void rewriteApplyInst(const CallSiteDescriptor &CSDesc,
410
410
411
411
Builder.setInsertionPoint (AI.getInstruction ());
412
412
FullApplySite NewAI;
413
- if (auto *TAI = dyn_cast<TryApplyInst>(AI)) {
413
+ switch (AI.getKind ()) {
414
+ case FullApplySiteKind::TryApplyInst: {
415
+ auto *TAI = cast<TryApplyInst>(AI);
414
416
NewAI = Builder.createTryApply (AI.getLoc (), FRI,
415
417
SubstitutionMap (), NewArgs,
416
418
TAI->getNormalBB (), TAI->getErrorBB ());
417
419
// If we passed in the original closure as @owned, then insert a release
418
420
// right after NewAI. This is to balance the +1 from being an @owned
419
421
// argument to AI.
420
- if (CSDesc.isClosureConsumed () && CSDesc.closureHasRefSemanticContext ()) {
421
- Builder.setInsertionPoint (TAI->getNormalBB ()->begin ());
422
- Builder.createReleaseValue (Closure->getLoc (), Closure, Builder.getDefaultAtomicity ());
423
- Builder.setInsertionPoint (TAI->getErrorBB ()->begin ());
424
- Builder.createReleaseValue (Closure->getLoc (), Closure, Builder.getDefaultAtomicity ());
425
- Builder.setInsertionPoint (AI.getInstruction ());
422
+ if (!CSDesc.isClosureConsumed () || !CSDesc.closureHasRefSemanticContext ()) {
423
+ break ;
426
424
}
427
- } else {
425
+
426
+ Builder.setInsertionPoint (TAI->getNormalBB ()->begin ());
427
+ Builder.createReleaseValue (Closure->getLoc (), Closure,
428
+ Builder.getDefaultAtomicity ());
429
+ Builder.setInsertionPoint (TAI->getErrorBB ()->begin ());
430
+ Builder.createReleaseValue (Closure->getLoc (), Closure,
431
+ Builder.getDefaultAtomicity ());
432
+ Builder.setInsertionPoint (AI.getInstruction ());
433
+ break ;
434
+ }
435
+ case FullApplySiteKind::ApplyInst: {
428
436
auto oldApply = cast<ApplyInst>(AI);
429
437
auto newApply = Builder.createApply (oldApply->getLoc (), FRI,
430
438
SubstitutionMap (),
@@ -438,6 +446,10 @@ static void rewriteApplyInst(const CallSiteDescriptor &CSDesc,
438
446
439
447
// Replace all uses of the old apply with the new apply.
440
448
oldApply->replaceAllUsesWith (newApply);
449
+ break ;
450
+ }
451
+ case FullApplySiteKind::BeginApplyInst:
452
+ llvm_unreachable (" Unhandled case" );
441
453
}
442
454
443
455
// Erase the old apply.
@@ -937,6 +949,16 @@ static bool isClosureAppliedIn(SILFunction *Callee, unsigned closureArgIdx,
937
949
return false ;
938
950
}
939
951
952
+ static bool canSpecializeFullApplySite (FullApplySiteKind kind) {
953
+ switch (kind) {
954
+ case FullApplySiteKind::TryApplyInst:
955
+ case FullApplySiteKind::ApplyInst:
956
+ return true ;
957
+ case FullApplySiteKind::BeginApplyInst:
958
+ return false ;
959
+ }
960
+ }
961
+
940
962
bool SILClosureSpecializerTransform::gatherCallSites (
941
963
SILFunction *Caller,
942
964
llvm::SmallVectorImpl<std::unique_ptr<ClosureInfo>> &ClosureCandidates,
@@ -986,7 +1008,7 @@ bool SILClosureSpecializerTransform::gatherCallSites(
986
1008
Uses.append (CFI->getUses ().begin (), CFI->getUses ().end ());
987
1009
continue ;
988
1010
}
989
- if (auto *Cvt= dyn_cast<ConvertEscapeToNoEscapeInst>(Use->getUser ())) {
1011
+ if (auto *Cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(Use->getUser ())) {
990
1012
Uses.append (Cvt->getUses ().begin (), Cvt->getUses ().end ());
991
1013
continue ;
992
1014
}
@@ -1010,11 +1032,12 @@ bool SILClosureSpecializerTransform::gatherCallSites(
1010
1032
continue ;
1011
1033
}
1012
1034
1013
- // If this use is not an apply inst or an apply inst with
1014
- // substitutions, there is nothing interesting for us to do, so
1015
- // continue...
1035
+ // If this use is not a full apply site that we can process or an apply
1036
+ // inst with substitutions, there is nothing interesting for us to do,
1037
+ // so continue...
1016
1038
auto AI = FullApplySite::isa (Use->getUser ());
1017
- if (!AI || AI.hasSubstitutions ())
1039
+ if (!AI || AI.hasSubstitutions () ||
1040
+ !canSpecializeFullApplySite (AI.getKind ()))
1018
1041
continue ;
1019
1042
1020
1043
// Check if we have already associated this apply inst with a closure to
0 commit comments