@@ -7423,8 +7423,9 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
7423
7423
}
7424
7424
7425
7425
// Check if \p RedResult is a ComputeReductionResult instruction, and if it is
7426
- // create a merge phi node for it and add it to \p ReductionResumeValues.
7427
- static void createAndCollectMergePhiForReduction (
7426
+ // add it to \p ReductionResumeValues and add incoming values from the main
7427
+ // vector loop.
7428
+ static void updateAndCollectMergePhiForReductionForEpilogueVectorization (
7428
7429
VPInstruction *RedResult,
7429
7430
DenseMap<const RecurrenceDescriptor *, Value *> &ReductionResumeValues,
7430
7431
VPTransformState &State, Loop *OrigLoop, BasicBlock *LoopMiddleBlock,
@@ -7433,14 +7434,24 @@ static void createAndCollectMergePhiForReduction(
7433
7434
RedResult->getOpcode () != VPInstruction::ComputeReductionResult)
7434
7435
return ;
7435
7436
7437
+ using namespace VPlanPatternMatch ;
7438
+ VPValue *ResumePhiVPV =
7439
+ cast<VPInstruction>(*find_if (RedResult->users (), [](VPUser *U) {
7440
+ return match (U, m_VPInstruction<VPInstruction::ResumePhi>(m_VPValue (),
7441
+ m_VPValue ()));
7442
+ }));
7443
+ auto *BCBlockPhi = cast<PHINode>(State.get (ResumePhiVPV, true ));
7436
7444
auto *PhiR = cast<VPReductionPHIRecipe>(RedResult->getOperand (0 ));
7437
7445
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor ();
7446
+ if (!VectorizingEpilogue) {
7447
+ ReductionResumeValues[&RdxDesc] = BCBlockPhi;
7448
+ return ;
7449
+ }
7438
7450
7439
- Value *FinalValue = State.get (RedResult, VPLane (VPLane::getFirstLane ()));
7440
7451
auto *ResumePhi =
7441
7452
dyn_cast<PHINode>(PhiR->getStartValue ()->getUnderlyingValue ());
7442
- if (VectorizingEpilogue && RecurrenceDescriptor::isAnyOfRecurrenceKind (
7443
- RdxDesc.getRecurrenceKind ())) {
7453
+ if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
7454
+ RdxDesc.getRecurrenceKind ())) {
7444
7455
auto *Cmp = cast<ICmpInst>(PhiR->getStartValue ()->getUnderlyingValue ());
7445
7456
assert (Cmp->getPredicate () == CmpInst::ICMP_NE);
7446
7457
assert (Cmp->getOperand (1 ) == RdxDesc.getRecurrenceStartValue ());
@@ -7450,42 +7461,15 @@ static void createAndCollectMergePhiForReduction(
7450
7461
" when vectorizing the epilogue loop, we need a resume phi from main "
7451
7462
" vector loop" );
7452
7463
7453
- // TODO: bc.merge.rdx should not be created here, instead it should be
7454
- // modeled in VPlan.
7455
7464
BasicBlock *LoopScalarPreHeader = OrigLoop->getLoopPreheader ();
7456
- // Create a phi node that merges control-flow from the backedge-taken check
7457
- // block and the middle block.
7458
- auto *BCBlockPhi =
7459
- PHINode::Create (FinalValue->getType (), 2 , " bc.merge.rdx" ,
7460
- LoopScalarPreHeader->getTerminator ()->getIterator ());
7461
-
7462
7465
// If we are fixing reductions in the epilogue loop then we should already
7463
7466
// have created a bc.merge.rdx Phi after the main vector body. Ensure that
7464
7467
// we carry over the incoming values correctly.
7465
7468
for (auto *Incoming : predecessors (LoopScalarPreHeader)) {
7466
- if (Incoming == LoopMiddleBlock)
7467
- BCBlockPhi->addIncoming (FinalValue, Incoming);
7468
- else if (ResumePhi && is_contained (ResumePhi->blocks (), Incoming))
7469
- BCBlockPhi->addIncoming (ResumePhi->getIncomingValueForBlock (Incoming),
7470
- Incoming);
7471
- else
7472
- BCBlockPhi->addIncoming (RdxDesc.getRecurrenceStartValue (), Incoming);
7469
+ if (ResumePhi && is_contained (ResumePhi->blocks (), Incoming))
7470
+ BCBlockPhi->setIncomingValueForBlock (
7471
+ Incoming, ResumePhi->getIncomingValueForBlock (Incoming));
7473
7472
}
7474
-
7475
- auto *OrigPhi = cast<PHINode>(PhiR->getUnderlyingValue ());
7476
- // TODO: This fixup should instead be modeled in VPlan.
7477
- // Fix the scalar loop reduction variable with the incoming reduction sum
7478
- // from the vector body and from the backedge value.
7479
- int IncomingEdgeBlockIdx =
7480
- OrigPhi->getBasicBlockIndex (OrigLoop->getLoopLatch ());
7481
- assert (IncomingEdgeBlockIdx >= 0 && " Invalid block index" );
7482
- // Pick the other block.
7483
- int SelfEdgeBlockIdx = (IncomingEdgeBlockIdx ? 0 : 1 );
7484
- OrigPhi->setIncomingValue (SelfEdgeBlockIdx, BCBlockPhi);
7485
- Instruction *LoopExitInst = RdxDesc.getLoopExitInstr ();
7486
- OrigPhi->setIncomingValue (IncomingEdgeBlockIdx, LoopExitInst);
7487
-
7488
- ReductionResumeValues[&RdxDesc] = BCBlockPhi;
7489
7473
}
7490
7474
7491
7475
std::pair<DenseMap<const SCEV *, Value *>,
@@ -7579,11 +7563,12 @@ LoopVectorizationPlanner::executePlan(
7579
7563
DenseMap<const RecurrenceDescriptor *, Value *> ReductionResumeValues;
7580
7564
auto *ExitVPBB =
7581
7565
cast<VPBasicBlock>(BestVPlan.getVectorLoopRegion ()->getSingleSuccessor ());
7582
- for (VPRecipeBase &R : *ExitVPBB) {
7583
- createAndCollectMergePhiForReduction (
7584
- dyn_cast<VPInstruction>(&R), ReductionResumeValues, State, OrigLoop,
7585
- State.CFG .VPBB2IRBB [ExitVPBB], ExpandedSCEVs);
7586
- }
7566
+ if (IsEpilogueVectorization)
7567
+ for (VPRecipeBase &R : *ExitVPBB) {
7568
+ updateAndCollectMergePhiForReductionForEpilogueVectorization (
7569
+ dyn_cast<VPInstruction>(&R), ReductionResumeValues, State, OrigLoop,
7570
+ State.CFG .VPBB2IRBB [ExitVPBB], ExpandedSCEVs);
7571
+ }
7587
7572
7588
7573
// 2.6. Maintain Loop Hints
7589
7574
// Keep all loop hints from the original loop on the vector loop (we'll
@@ -9369,6 +9354,22 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9369
9354
m_VPValue ()));
9370
9355
});
9371
9356
9357
+ VPBasicBlock *ScalarPHVPBB = nullptr ;
9358
+ if (MiddleVPBB->getNumSuccessors () == 2 ) {
9359
+ // Order is strict: first is the exit block, second is the scalar
9360
+ // preheader.
9361
+ ScalarPHVPBB = cast<VPBasicBlock>(MiddleVPBB->getSuccessors ()[1 ]);
9362
+ } else {
9363
+ ScalarPHVPBB = cast<VPBasicBlock>(MiddleVPBB->getSingleSuccessor ());
9364
+ }
9365
+
9366
+ VPBuilder ScalarPHBuilder (ScalarPHVPBB);
9367
+ auto *ResumePhiRecipe = ScalarPHBuilder.createNaryOp (
9368
+ VPInstruction::ResumePhi, {FinalReductionResult, PhiR->getStartValue ()},
9369
+ {}, " bc.merge.rdx" );
9370
+ auto *RedPhi = cast<PHINode>(PhiR->getUnderlyingInstr ());
9371
+ Plan->addLiveOut (RedPhi, ResumePhiRecipe);
9372
+
9372
9373
// Adjust AnyOf reductions; replace the reduction phi for the selected value
9373
9374
// with a boolean reduction phi node to check if the condition is true in
9374
9375
// any iteration. The final value is selected by the final
0 commit comments