Skip to content

Commit e7bf2ea

Browse files
committed
[LV] Move code to place induction increment to VPlan post-processing.
This patch moves the code to set the correct incoming block for the backedge value to VPlan::execute. When generating the phi node, the backedge value is temporarily added using the pre-header as incoming block. The invalid phi node will be fixed up during VPlan::execute after main VPlan code generation. At the same time, the backedge value is also moved to the latch. This change removes the requirement to create the latch block up-front for VPWidenIntOrFpInductionRecipe::execute, which in turn will enable modeling the pre-header in VPlan. As an alternative, the increment could be modeled as separate recipe, but that would require more work and a bit of redundant code, as we need to create the step-vector during VPWidenIntOrFpInductionRecipe::execute anyways, to create the values for different parts. Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D121617
1 parent a786522 commit e7bf2ea

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9534,16 +9534,14 @@ void VPWidenIntOrFpInductionRecipe::execute(VPTransformState &State) {
95349534
LastInduction->setDebugLoc(EntryVal->getDebugLoc());
95359535
}
95369536

9537-
// Move the last step to the end of the latch block. This ensures consistent
9538-
// placement of all induction updates.
9539-
auto *LoopVectorLatch =
9540-
State.LI->getLoopFor(State.CFG.PrevBB)->getLoopLatch();
9541-
auto *Br = cast<BranchInst>(LoopVectorLatch->getTerminator());
9542-
LastInduction->moveBefore(Br);
95439537
LastInduction->setName("vec.ind.next");
9544-
95459538
VecInd->addIncoming(SteppedStart, State.CFG.VectorPreHeader);
9546-
VecInd->addIncoming(LastInduction, LoopVectorLatch);
9539+
// Add induction update using an incorrect block temporarily. The phi node
9540+
// will be fixed after VPlan execution. Note that at this point the latch
9541+
// block cannot be used, as it does not exist yet.
9542+
// TODO: Model increment value in VPlan, by turning the recipe into a
9543+
// multi-def and a subclass of VPHeaderPHIRecipe.
9544+
VecInd->addIncoming(LastInduction, State.CFG.VectorPreHeader);
95479545
}
95489546

95499547
void VPWidenPointerInductionRecipe::execute(VPTransformState &State) {

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,10 +982,22 @@ void VPlan::execute(VPTransformState *State) {
982982
for (VPRecipeBase &R : Header->phis()) {
983983
// Skip phi-like recipes that generate their backedege values themselves.
984984
// TODO: Model their backedge values explicitly.
985-
if (isa<VPWidenIntOrFpInductionRecipe>(&R) || isa<VPWidenPHIRecipe>(&R) ||
986-
isa<VPWidenPointerInductionRecipe>(&R))
985+
if (isa<VPWidenPHIRecipe>(&R) || isa<VPWidenPointerInductionRecipe>(&R))
987986
continue;
988987

988+
// Set the correct incoming block for backedge values and move induction to
989+
// latch.
990+
if (auto *IndR = dyn_cast<VPWidenIntOrFpInductionRecipe>(&R)) {
991+
auto *Phi = cast<PHINode>(State->get(IndR, 0));
992+
Phi->setIncomingBlock(1, VectorLatchBB);
993+
994+
// Move the last step to the end of the latch block. This ensures
995+
// consistent placement of all induction updates.
996+
Instruction *Inc = cast<Instruction>(Phi->getIncomingValue(1));
997+
Inc->moveBefore(VectorLatchBB->getTerminator()->getPrevNode());
998+
continue;
999+
}
1000+
9891001
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
9901002
// For canonical IV, first-order recurrences and in-order reduction phis,
9911003
// only a single part is generated, which provides the last part from the

0 commit comments

Comments
 (0)