@@ -538,6 +538,7 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
538
538
SILBasicBlock *currentBB = num > 1 ? normalTargetGenerator (nextNormalBB)
539
539
: forEachCall->getParentBlock ();
540
540
SILBuilderWithScope unrollBuilder (currentBB, forEachCall);
541
+ SILBuilderWithScope normalBuilder (&nextNormalBB->front (), forEachCall);
541
542
SILValue borrowedElem;
542
543
SILValue addr;
543
544
@@ -552,9 +553,8 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
552
553
borrowedElem = unrollBuilder.createBeginBorrow (forEachLoc, elementCopy);
553
554
addr =
554
555
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);
558
558
}
559
559
560
560
SILBasicBlock *errorTarget =
@@ -566,16 +566,18 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
566
566
unrollBuilder.createTryApply (forEachLoc, forEachBodyClosure,
567
567
SubstitutionMap (), addr, nextNormalBB,
568
568
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
+ }
569
575
nextNormalBB = currentBB;
570
576
}
571
-
572
577
// Dealloc the stack in the normalBB and also in errorBB. Note that every
573
578
// try_apply created during the unrolling must pass through these blocks.
574
- SILBuilderWithScope (&normalBB->front ())
575
- .createDeallocStack (forEachLoc, allocStack);
576
579
SILBuilderWithScope (&errorBB->front ())
577
580
.createDeallocStack (forEachLoc, allocStack);
578
-
579
581
// Remove the forEach call as it has now been unrolled.
580
582
removeForEachCall (forEachCall, deleter);
581
583
}
0 commit comments