@@ -538,6 +538,7 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
538538 SILBasicBlock *currentBB = num > 1 ? normalTargetGenerator (nextNormalBB)
539539 : forEachCall->getParentBlock ();
540540 SILBuilderWithScope unrollBuilder (currentBB, forEachCall);
541+ SILBuilderWithScope normalBuilder (&nextNormalBB->front (), forEachCall);
541542 SILValue borrowedElem;
542543 SILValue addr;
543544
@@ -552,9 +553,8 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
552553 borrowedElem = unrollBuilder.createBeginBorrow (forEachLoc, elementCopy);
553554 addr =
554555 unrollBuilder.createStoreBorrow (forEachLoc, borrowedElem, allocStack);
555- SILBuilderWithScope builder (&nextNormalBB->front (), forEachCall);
556- builder.createEndBorrow (forEachLoc, addr);
557- builder.createEndBorrow (forEachLoc, borrowedElem);
556+ normalBuilder.createEndBorrow (forEachLoc, addr);
557+ normalBuilder.createEndBorrow (forEachLoc, borrowedElem);
558558 }
559559
560560 SILBasicBlock *errorTarget =
@@ -566,16 +566,18 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
566566 unrollBuilder.createTryApply (forEachLoc, forEachBodyClosure,
567567 SubstitutionMap (), addr, nextNormalBB,
568568 errorTarget);
569+
570+ if (nextNormalBB == normalBB) {
571+ // Dealloc the stack in the normalBB and also in errorBB. Note that every
572+ // try_apply created during the unrolling must pass through these blocks.
573+ normalBuilder.createDeallocStack (forEachLoc, allocStack);
574+ }
569575 nextNormalBB = currentBB;
570576 }
571-
572577 // Dealloc the stack in the normalBB and also in errorBB. Note that every
573578 // try_apply created during the unrolling must pass through these blocks.
574- SILBuilderWithScope (&normalBB->front ())
575- .createDeallocStack (forEachLoc, allocStack);
576579 SILBuilderWithScope (&errorBB->front ())
577580 .createDeallocStack (forEachLoc, allocStack);
578-
579581 // Remove the forEach call as it has now been unrolled.
580582 removeForEachCall (forEachCall, deleter);
581583}
0 commit comments