Skip to content

Commit eb7716b

Browse files
authored
Merge pull request #19444 from gottesmm/pr-9bb82b4578138006a4705894a87e8a908766feca
[closure-spec] Do not try to process begin_apply. It is not supported now.
2 parents 78cd814 + e919de9 commit eb7716b

File tree

3 files changed

+80
-13
lines changed

3 files changed

+80
-13
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7554,6 +7554,10 @@ class ApplySite {
75547554
}
75557555
}
75567556

7557+
ApplySiteKind getKind() const {
7558+
return ApplySiteKind(Inst->getKind());
7559+
}
7560+
75577561
explicit operator bool() const {
75587562
return Inst != nullptr;
75597563
}
@@ -7897,6 +7901,10 @@ class FullApplySite : public ApplySite {
78977901
}
78987902
}
78997903

7904+
FullApplySiteKind getKind() const {
7905+
return FullApplySiteKind(getInstruction()->getKind());
7906+
}
7907+
79007908
bool hasIndirectSILResults() const {
79017909
return getSubstCalleeConv().hasIndirectSILResults();
79027910
}

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,21 +410,29 @@ static void rewriteApplyInst(const CallSiteDescriptor &CSDesc,
410410

411411
Builder.setInsertionPoint(AI.getInstruction());
412412
FullApplySite NewAI;
413-
if (auto *TAI = dyn_cast<TryApplyInst>(AI)) {
413+
switch (AI.getKind()) {
414+
case FullApplySiteKind::TryApplyInst: {
415+
auto *TAI = cast<TryApplyInst>(AI);
414416
NewAI = Builder.createTryApply(AI.getLoc(), FRI,
415417
SubstitutionMap(), NewArgs,
416418
TAI->getNormalBB(), TAI->getErrorBB());
417419
// If we passed in the original closure as @owned, then insert a release
418420
// right after NewAI. This is to balance the +1 from being an @owned
419421
// 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;
426424
}
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: {
428436
auto oldApply = cast<ApplyInst>(AI);
429437
auto newApply = Builder.createApply(oldApply->getLoc(), FRI,
430438
SubstitutionMap(),
@@ -438,6 +446,10 @@ static void rewriteApplyInst(const CallSiteDescriptor &CSDesc,
438446

439447
// Replace all uses of the old apply with the new apply.
440448
oldApply->replaceAllUsesWith(newApply);
449+
break;
450+
}
451+
case FullApplySiteKind::BeginApplyInst:
452+
llvm_unreachable("Unhandled case");
441453
}
442454

443455
// Erase the old apply.
@@ -937,6 +949,16 @@ static bool isClosureAppliedIn(SILFunction *Callee, unsigned closureArgIdx,
937949
return false;
938950
}
939951

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+
940962
bool SILClosureSpecializerTransform::gatherCallSites(
941963
SILFunction *Caller,
942964
llvm::SmallVectorImpl<std::unique_ptr<ClosureInfo>> &ClosureCandidates,
@@ -986,7 +1008,7 @@ bool SILClosureSpecializerTransform::gatherCallSites(
9861008
Uses.append(CFI->getUses().begin(), CFI->getUses().end());
9871009
continue;
9881010
}
989-
if (auto *Cvt= dyn_cast<ConvertEscapeToNoEscapeInst>(Use->getUser())) {
1011+
if (auto *Cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(Use->getUser())) {
9901012
Uses.append(Cvt->getUses().begin(), Cvt->getUses().end());
9911013
continue;
9921014
}
@@ -1010,11 +1032,12 @@ bool SILClosureSpecializerTransform::gatherCallSites(
10101032
continue;
10111033
}
10121034

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...
10161038
auto AI = FullApplySite::isa(Use->getUser());
1017-
if (!AI || AI.hasSubstitutions())
1039+
if (!AI || AI.hasSubstitutions() ||
1040+
!canSpecializeFullApplySite(AI.getKind()))
10181041
continue;
10191042

10201043
// Check if we have already associated this apply inst with a closure to

test/SILOptimizer/closure_specialize.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,39 @@ bb0(%0 : $Int):
732732
%empty = tuple ()
733733
return %empty : $()
734734
}
735+
736+
//////////////////////
737+
// Begin Apply Test //
738+
//////////////////////
739+
740+
sil @coroutine_user : $@yield_once @convention(thin) (@noescape @callee_guaranteed () -> Int) -> @yields Int {
741+
bb0(%0 : $@noescape @callee_guaranteed () -> Int):
742+
%1 = apply %0() : $@noescape @callee_guaranteed () -> Int
743+
unreachable
744+
}
745+
746+
// CHECK-LABEL: sil @test_coroutine_user : $@convention(thin) (Int) -> Int {
747+
// CHECK: [[COROUTINE_USER:%.*]] = function_ref @coroutine_user
748+
// CHECK: begin_apply [[COROUTINE_USER]](
749+
// CHECK: } // end sil function 'test_coroutine_user'
750+
sil @test_coroutine_user : $@convention(thin) (Int) -> Int {
751+
bb0(%0 : $Int):
752+
%1 = function_ref @testClosureConvertHelper2 : $@convention(thin) (Int) -> Int
753+
%2 = partial_apply [callee_guaranteed] %1(%0) : $@convention(thin) (Int) -> Int
754+
%3 = convert_escape_to_noescape %2 : $@callee_guaranteed () -> Int to $@noescape @callee_guaranteed () -> Int
755+
%4 = function_ref @coroutine_user : $@yield_once @convention(thin) (@noescape @callee_guaranteed () -> Int) -> @yields Int
756+
(%value, %token) = begin_apply %4(%3) : $@yield_once @convention(thin) (@noescape @callee_guaranteed () -> Int) -> @yields Int
757+
cond_br undef, bb1, bb2
758+
759+
bb1:
760+
end_apply %token
761+
br bb3
762+
763+
bb2:
764+
abort_apply %token
765+
br bb3
766+
767+
bb3:
768+
release_value %2 : $@callee_guaranteed () -> Int
769+
return %value : $Int
770+
}

0 commit comments

Comments
 (0)