[llvm] e7bf2ea - [LV] Move code to place induction increment to VPlan post-processing.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 28 08:22:38 PDT 2022
Author: Florian Hahn
Date: 2022-03-28T16:20:02+01:00
New Revision: e7bf2ea93472bfdab8c5a31b81f94727f1def07b
URL: https://github.com/llvm/llvm-project/commit/e7bf2ea93472bfdab8c5a31b81f94727f1def07b
DIFF: https://github.com/llvm/llvm-project/commit/e7bf2ea93472bfdab8c5a31b81f94727f1def07b.diff
LOG: [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
Added:
Modified:
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
llvm/lib/Transforms/Vectorize/VPlan.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 68897588a4b07..8a4d8f2c39f61 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -9534,16 +9534,14 @@ void VPWidenIntOrFpInductionRecipe::execute(VPTransformState &State) {
LastInduction->setDebugLoc(EntryVal->getDebugLoc());
}
- // Move the last step to the end of the latch block. This ensures consistent
- // placement of all induction updates.
- auto *LoopVectorLatch =
- State.LI->getLoopFor(State.CFG.PrevBB)->getLoopLatch();
- auto *Br = cast<BranchInst>(LoopVectorLatch->getTerminator());
- LastInduction->moveBefore(Br);
LastInduction->setName("vec.ind.next");
-
VecInd->addIncoming(SteppedStart, State.CFG.VectorPreHeader);
- VecInd->addIncoming(LastInduction, LoopVectorLatch);
+ // Add induction update using an incorrect block temporarily. The phi node
+ // will be fixed after VPlan execution. Note that at this point the latch
+ // block cannot be used, as it does not exist yet.
+ // TODO: Model increment value in VPlan, by turning the recipe into a
+ // multi-def and a subclass of VPHeaderPHIRecipe.
+ VecInd->addIncoming(LastInduction, State.CFG.VectorPreHeader);
}
void VPWidenPointerInductionRecipe::execute(VPTransformState &State) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 6edade8888cae..6e7c12a9cda2f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -982,10 +982,22 @@ void VPlan::execute(VPTransformState *State) {
for (VPRecipeBase &R : Header->phis()) {
// Skip phi-like recipes that generate their backedege values themselves.
// TODO: Model their backedge values explicitly.
- if (isa<VPWidenIntOrFpInductionRecipe>(&R) || isa<VPWidenPHIRecipe>(&R) ||
- isa<VPWidenPointerInductionRecipe>(&R))
+ if (isa<VPWidenPHIRecipe>(&R) || isa<VPWidenPointerInductionRecipe>(&R))
continue;
+ // Set the correct incoming block for backedge values and move induction to
+ // latch.
+ if (auto *IndR = dyn_cast<VPWidenIntOrFpInductionRecipe>(&R)) {
+ auto *Phi = cast<PHINode>(State->get(IndR, 0));
+ Phi->setIncomingBlock(1, VectorLatchBB);
+
+ // Move the last step to the end of the latch block. This ensures
+ // consistent placement of all induction updates.
+ Instruction *Inc = cast<Instruction>(Phi->getIncomingValue(1));
+ Inc->moveBefore(VectorLatchBB->getTerminator()->getPrevNode());
+ continue;
+ }
+
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
// For canonical IV, first-order recurrences and in-order reduction phis,
// only a single part is generated, which provides the last part from the
More information about the llvm-commits
mailing list