Skip to content

Commit ac19de3

Browse files
committed
!fixup address comments, thanks!
1 parent 5192da5 commit ac19de3

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "llvm/Transforms/Vectorize/LoopVectorize.h"
5757
#include "LoopVectorizationPlanner.h"
5858
#include "VPRecipeBuilder.h"
59+
#include "VPlanPatternMatch.h"
5960
#include "VPlan.h"
6061
#include "VPlanAnalysis.h"
6162
#include "VPlanHCFGBuilder.h"
@@ -606,8 +607,8 @@ class InnerLoopVectorizer {
606607
BasicBlock *MiddleBlock, BasicBlock *VectorHeader,
607608
VPlan &Plan, VPTransformState &State);
608609

609-
/// Create the exit value of first order recurrences in the middle block and
610-
/// update their users.
610+
/// Create the phi node for the resume value of first order recurrences in the scalar preheader and
611+
/// update the users in the scalar loop.
611612
void fixFixedOrderRecurrence(VPLiveOut *LO, VPTransformState &State);
612613

613614
/// Iteratively sink the scalarized operands of a predicated instruction into
@@ -3392,8 +3393,8 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State,
33923393
// At this point every instruction in the original loop is widened to a
33933394
// vector form. Note that fixing reduction phis, as well as extracting the
33943395
// exit and resume values for fixed-order recurrences are already modeled in
3395-
// VPlan. All that remains to do here is creating a phi in the scalar
3396-
// pre-header for each fixed-rder recurrence resume value.
3396+
// VPlan. All that remains to do here is to create a phi in the scalar
3397+
// pre-header for each fixed-order recurrence resume value.
33973398
// TODO: Also model creating phis in the scalar pre-header in VPlan.
33983399
for (const auto &[_, LO] : to_vector(Plan.getLiveOuts())) {
33993400
if (!Legal->isFixedOrderRecurrence(LO->getPhi()))
@@ -3473,21 +3474,24 @@ void InnerLoopVectorizer::fixFixedOrderRecurrence(VPLiveOut *LO,
34733474
VPTransformState &State) {
34743475
// Extract the last vector element in the middle block. This will be the
34753476
// initial value for the recurrence when jumping to the scalar loop.
3476-
Value *ExtractForScalar = State.get(LO->getOperand(0), UF - 1, true);
3477+
VPValue *VPExtract = LO->getOperand(0);
3478+
using namespace llvm::VPlanPatternMatch;
3479+
assert(match(VPExtract, m_VPInstruction<VPInstruction::ExtractFromEnd>(m_VPValue(), m_VPValue())) && "FOR LiveOut expects to use an extract from end.");
3480+
Value *ResumeScalarFOR = State.get(VPExtract, UF - 1, true);
34773481

34783482
// Fix the initial value of the original recurrence in the scalar loop.
34793483
Builder.SetInsertPoint(LoopScalarPreHeader, LoopScalarPreHeader->begin());
3480-
PHINode *Phi = LO->getPhi();
3481-
auto *Start = Builder.CreatePHI(Phi->getType(), 2, "scalar.recur.init");
3482-
auto *ScalarInit =
3483-
LO->getPhi()->getIncomingValueForBlock(LoopScalarPreHeader);
3484+
PHINode *ScalarHeaderPhi = LO->getPhi();
3485+
auto *NewScalarHeaderPhi = Builder.CreatePHI(ScalarHeaderPhi->getType(), 2, "scalar.recur.init");
3486+
auto *InitScalarFOR =
3487+
ScalarHeaderPhi->getIncomingValueForBlock(LoopScalarPreHeader);
34843488
for (auto *BB : predecessors(LoopScalarPreHeader)) {
3485-
auto *Incoming = BB == LoopMiddleBlock ? ExtractForScalar : ScalarInit;
3486-
Start->addIncoming(Incoming, BB);
3489+
auto *Incoming = BB == LoopMiddleBlock ? ResumeScalarFOR : InitScalarFOR;
3490+
NewScalarHeaderPhi ->addIncoming(Incoming, BB);
34873491
}
34883492

3489-
Phi->setIncomingValueForBlock(LoopScalarPreHeader, Start);
3490-
Phi->setName("scalar.recur");
3493+
ScalarHeaderPhi->setIncomingValueForBlock(LoopScalarPreHeader, NewScalarHeaderPhi );
3494+
ScalarHeaderPhi->setName("scalar.recur");
34913495
}
34923496

34933497
void InnerLoopVectorizer::sinkScalarOperands(Instruction *PredInst) {

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3166,7 +3166,7 @@ class VPlan {
31663166
/// definitions are VPValues that hold a pointer to their underlying IR.
31673167
SmallVector<VPValue *, 16> VPLiveInsToFree;
31683168

3169-
/// Values used outside the plan.
3169+
/// Values used outside the plan. It contains live-outs that need fixing. Any live-out that is fixed outside VPlan needs to be removed. The remaining live-outs are fixed via VPLiveOut::fixPhi.
31703170
MapVector<PHINode *, VPLiveOut *> LiveOuts;
31713171

31723172
/// Mapping from SCEVs to the VPValues representing their expansions.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -886,30 +886,44 @@ bool VPlanTransforms::adjustFixedOrderRecurrences(VPlan &Plan,
886886
// br cond, vector.body, middle.block
887887
//
888888
// middle.block:
889-
// x = v2(3)
890-
// br scalar.ph
889+
// s_penultimate = v2(2) = v3(3)
890+
// s_resume = v2(3)
891+
// br cond, scalar.ph, exit.block
891892
//
892893
// scalar.ph:
893-
// s_init = phi [x, middle.block], [a[-1], otherwise]
894+
// s_init' = phi [s_resume, middle.block], [s_init, otherwise]
894895
// br scalar.body
895896
//
897+
// scalar.body:
898+
// i = phi [0, scalar.ph], [i+1, scalar.body]
899+
// s1 = phi [s_init', scalar.ph], [s2, scalar.body]
900+
// s2 = a[i]
901+
// b[i] = s2 - s1
902+
// br cond, scalar.body, exit.block
903+
//
904+
// exit.block:
905+
// lo = lcssa.phi [s1, scalar.body], [s.penultimate, middle.block]
906+
//
896907
// After execution completes the vector loop, we extract the next value of
897908
// the recurrence (x) to use as the initial value in the scalar loop. This
898-
// is modeled by ExtractRecurrenceResume.
909+
// is modeled by ExtractFromEnd.
899910
Type *IntTy = Plan.getCanonicalIV()->getScalarType();
900-
auto *Result = cast<VPInstruction>(MiddleBuilder.createNaryOp(
911+
912+
// Extract the penultimate value of the recurrence and update VPLiveOut users of the recurrence splice.
913+
auto *Penultimate = cast<VPInstruction>(MiddleBuilder.createNaryOp(
901914
VPInstruction::ExtractFromEnd,
902915
{FOR->getBackedgeValue(),
903916
Plan.getOrAddLiveIn(ConstantInt::get(IntTy, 2))},
904917
{}, "vector.recur.extract.for.phi"));
905918
RecurSplice->replaceUsesWithIf(
906-
Result, [](VPUser &U, unsigned) { return isa<VPLiveOut>(&U); });
919+
Penultimate, [](VPUser &U, unsigned) { return isa<VPLiveOut>(&U); });
920+
921+
// Extract the resume value and create a new VPLiveOut for it.
907922
auto *Resume = MiddleBuilder.createNaryOp(
908923
VPInstruction::ExtractFromEnd,
909924
{FOR->getBackedgeValue(),
910925
Plan.getOrAddLiveIn(ConstantInt::get(IntTy, 1))},
911926
{}, "vector.recur.extract");
912-
// Introduce VPUsers modeling the exit values.
913927
Plan.addLiveOut(cast<PHINode>(FOR->getUnderlyingInstr()), Resume);
914928
}
915929
return true;

0 commit comments

Comments
 (0)