@@ -1688,8 +1688,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
16881688 }
16891689
16901690 static bool isLegalSILTokenProducer (SILValue value) {
1691- if (auto *baResult = isaResultOf<BeginApplyInst>(value))
1692- return baResult->isBeginApplyToken ();
1691+ if (auto *baResult = isaResultOf<BeginApplyInst>(value)) {
1692+ auto *bai = cast<BeginApplyInst>(baResult->getParent ());
1693+ return value == bai->getTokenResult () ||
1694+ value == bai->getCalleeAllocationResult ();
1695+ }
16931696
16941697 if (isa<OpenPackElementInst>(value))
16951698 return true ;
@@ -3772,12 +3775,23 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
37723775 }
37733776
37743777 void checkDeallocStackInst (DeallocStackInst *DI) {
3778+ auto isTokenFromCalleeAllocatedBeginApply = [](SILValue value) -> bool {
3779+ auto *inst = value->getDefiningInstruction ();
3780+ if (!inst)
3781+ return false ;
3782+ auto *bai = dyn_cast<BeginApplyInst>(inst);
3783+ if (!bai)
3784+ return false ;
3785+ return value == bai->getCalleeAllocationResult ();
3786+ };
37753787 require (isa<SILUndef>(DI->getOperand ()) ||
37763788 isa<AllocStackInst>(DI->getOperand ()) ||
37773789 isa<AllocVectorInst>(DI->getOperand ()) ||
37783790 (isa<PartialApplyInst>(DI->getOperand ()) &&
3779- cast<PartialApplyInst>(DI->getOperand ())->isOnStack ()),
3780- " Operand of dealloc_stack must be an alloc_stack, alloc_vector or partial_apply "
3791+ cast<PartialApplyInst>(DI->getOperand ())->isOnStack ()) ||
3792+ (isTokenFromCalleeAllocatedBeginApply (DI->getOperand ())),
3793+ " Operand of dealloc_stack must be an alloc_stack, alloc_vector or "
3794+ " partial_apply "
37813795 " [stack]" );
37823796 }
37833797 void checkDeallocPackInst (DeallocPackInst *DI) {
@@ -6768,7 +6782,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
67686782 };
67696783
67706784 struct BBState {
6771- std::vector<SingleValueInstruction* > Stack;
6785+ std::vector<SILValue > Stack;
67726786
67736787 // / Contents: BeginAccessInst*, BeginApplyInst*.
67746788 std::set<SILInstruction*> ActiveOps;
@@ -6818,9 +6832,15 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
68186832 }
68196833
68206834 if (i.isAllocatingStack ()) {
6821- state.Stack .push_back (cast<SingleValueInstruction>(&i));
6822-
6823- } else if (i.isDeallocatingStack ()) {
6835+ if (auto *BAI = dyn_cast<BeginApplyInst>(&i)) {
6836+ state.Stack .push_back (BAI->getCalleeAllocationResult ());
6837+ } else {
6838+ state.Stack .push_back (cast<SingleValueInstruction>(&i));
6839+ }
6840+ // Not "else if": begin_apply both allocates stack and begins an
6841+ // operation.
6842+ }
6843+ if (i.isDeallocatingStack ()) {
68246844 SILValue op = i.getOperand (0 );
68256845 while (auto *mvi = dyn_cast<MoveValueInst>(op)) {
68266846 op = mvi->getOperand ();
0 commit comments