Skip to content

Commit 14065bf

Browse files
committed
Teach ForEachLoopUnroll to deal with error basic blocks without arguments
1 parent 3bc6497 commit 14065bf

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/SILOptimizer/LoopTransforms/ForEachLoopUnroll.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,11 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
483483
// targets must be taking a phi argument.
484484
SILBasicBlock *normalBB = forEachCall->getNormalBB();
485485
SILBasicBlock *errorBB = forEachCall->getErrorBB();
486-
assert(errorBB->getSILPhiArguments().size() == 1 &&
487-
normalBB->getSILPhiArguments().size() == 1);
486+
assert(normalBB->getSILPhiArguments().size() == 1);
488487
SILPhiArgument *normalArgument = normalBB->getSILPhiArguments()[0];
489-
SILPhiArgument *errorArgument = errorBB->getSILPhiArguments()[0];
488+
SILPhiArgument *errorArgument = nullptr;
489+
if (errorBB->getSILPhiArguments().size() == 1)
490+
errorArgument = errorBB->getSILPhiArguments()[0];
490491

491492
// A generator for creating a basic block for use as the target of the
492493
// "normal" branch of a try_apply.
@@ -503,8 +504,12 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
503504
auto errorTargetGenerator = [&](SILBasicBlock *insertionBlock,
504505
SILValue borrowedElem, SILValue storeBorrow) {
505506
SILBasicBlock *newErrorBB = fun->createBasicBlockBefore(insertionBlock);
506-
SILValue argument = newErrorBB->createPhiArgument(
507+
SILValue argument;
508+
if (errorArgument) {
509+
argument = newErrorBB->createPhiArgument(
507510
errorArgument->getType(), errorArgument->getOwnershipKind());
511+
}
512+
508513
// Make the errorBB jump to the error target of the original forEach.
509514
SILBuilderWithScope builder(newErrorBB, forEachCall);
510515
if (storeBorrow) {
@@ -513,7 +518,11 @@ static void unrollForEach(ArrayInfo &arrayInfo, TryApplyInst *forEachCall,
513518
if (borrowedElem) {
514519
builder.createEndBorrow(forEachLoc, borrowedElem);
515520
}
516-
builder.createBranch(forEachLoc, errorBB, argument);
521+
522+
if (argument)
523+
builder.createBranch(forEachLoc, errorBB, argument);
524+
else
525+
builder.createBranch(forEachLoc, errorBB);
517526
return newErrorBB;
518527
};
519528

0 commit comments

Comments
 (0)