@@ -2503,23 +2503,26 @@ void VPlanTransforms::handleUncountableEarlyExit(
2503
2503
// Update the exit phis in the early exit block.
2504
2504
VPBuilder MiddleBuilder (NewMiddle);
2505
2505
VPBuilder EarlyExitB (VectorEarlyExitVPBB);
2506
- for (VPRecipeBase &R : * VPEarlyExitBlock->phis ()) {
2506
+ for (VPRecipeBase &R : VPEarlyExitBlock->phis ()) {
2507
2507
auto *ExitIRI = cast<VPIRPhi>(&R);
2508
+ // By default, assume early exit operand is first, e.g., when the two exit
2509
+ // blocks are distinct - VPEarlyExitBlock has a single predecessor.
2508
2510
unsigned EarlyExitIdx = 0 ;
2509
2511
if (OrigLoop->getUniqueExitBlock ()) {
2510
- // After the transform, the first incoming value is coming from the
2511
- // orignial loop latch, while the second operand is from the early exit.
2512
- // Sawp the phi operands, if the first predecessor in the original IR is
2513
- // not the loop latch.
2512
+ // The incoming values currently correspond to the original IR
2513
+ // predecessors. After the transform, the first incoming value is coming
2514
+ // from the original loop latch, while the second operand is from the
2515
+ // early exit. Swap the phi operands, if the first predecessor in the
2516
+ // original IR is not the loop latch.
2514
2517
if (*pred_begin (VPEarlyExitBlock->getIRBasicBlock ()) !=
2515
2518
OrigLoop->getLoopLatch ())
2516
2519
ExitIRI->swapOperands ();
2517
2520
2518
2521
EarlyExitIdx = 1 ;
2519
2522
// If there's a unique exit block, VPEarlyExitBlock has 2 predecessors
2520
- // (MiddleVPBB and NewMiddle). Add the incoming value from MiddleVPBB
2521
- // which is coming from the original latch.
2522
- ExitIRI->extractLastLaneOfOperand (MiddleBuilder);
2523
+ // (MiddleVPBB and NewMiddle). Extract the last lane of the incoming value
2524
+ // from MiddleVPBB which is coming from the original latch.
2525
+ ExitIRI->extractLastLaneOfFirstOperand (MiddleBuilder);
2523
2526
}
2524
2527
2525
2528
VPValue *IncomingFromEarlyExit = ExitIRI->getOperand (EarlyExitIdx);
@@ -2530,15 +2533,14 @@ void VPlanTransforms::handleUncountableEarlyExit(
2530
2533
// and vector VFs.
2531
2534
if (!IncomingFromEarlyExit->isLiveIn () &&
2532
2535
LoopVectorizationPlanner::getDecisionAndClampRange (IsVector, Range)) {
2533
- // Add the incoming value from the early exit.
2536
+ // Update the incoming value from the early exit.
2534
2537
VPValue *FirstActiveLane = EarlyExitB.createNaryOp (
2535
2538
VPInstruction::FirstActiveLane, {EarlyExitTakenCond}, nullptr ,
2536
2539
" first.active.lane" );
2537
- ExitIRI->setOperand (
2538
- EarlyExitIdx,
2539
- EarlyExitB.createNaryOp (Instruction::ExtractElement,
2540
- {IncomingFromEarlyExit, FirstActiveLane},
2541
- nullptr , " early.exit.value" ));
2540
+ IncomingFromEarlyExit = EarlyExitB.createNaryOp (
2541
+ Instruction::ExtractElement, {IncomingFromEarlyExit, FirstActiveLane},
2542
+ nullptr , " early.exit.value" );
2543
+ ExitIRI->setOperand (EarlyExitIdx, IncomingFromEarlyExit);
2542
2544
}
2543
2545
}
2544
2546
MiddleBuilder.createNaryOp (VPInstruction::BranchOnCond, {IsEarlyExitTaken});
0 commit comments