Skip to content

Commit 15d11a4

Browse files
committed
[VPlan] Track IsOrdered in VPReductionRecipe, remove use of ILV (NFCI).
Instead of using ILV.useOrderedReductions during ::execute, instead store the information at recipe construction. Another step towards making recipe'::execute independent of legacy ILV.
1 parent 3f16ff4 commit 15d11a4

File tree

4 files changed

+61
-67
lines changed

4 files changed

+61
-67
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,6 @@ class InnerLoopVectorizer {
573573
/// Fix the non-induction PHIs in \p Plan.
574574
void fixNonInductionPHIs(VPlan &Plan, VPTransformState &State);
575575

576-
/// Returns true if the reordering of FP operations is not allowed, but we are
577-
/// able to vectorize with strict in-order reductions for the given RdxDesc.
578-
bool useOrderedReductions(const RecurrenceDescriptor &RdxDesc);
579-
580576
/// Create a new phi node for the induction variable \p OrigPhi to resume
581577
/// iteration count in the scalar epilogue, from where the vectorized loop
582578
/// left off. \p Step is the SCEV-expanded induction step to use. In cases
@@ -3714,11 +3710,6 @@ void InnerLoopVectorizer::fixNonInductionPHIs(VPlan &Plan,
37143710
}
37153711
}
37163712

3717-
bool InnerLoopVectorizer::useOrderedReductions(
3718-
const RecurrenceDescriptor &RdxDesc) {
3719-
return Cost->useOrderedReductions(RdxDesc);
3720-
}
3721-
37223713
void LoopVectorizationCostModel::collectLoopScalars(ElementCount VF) {
37233714
// We should not collect Scalars more than once per VF. Right now, this
37243715
// function is called from collectUniformsAndScalars(), which already does
@@ -9056,8 +9047,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90569047
if (CM.blockNeedsPredicationForAnyReason(BB))
90579048
CondOp = RecipeBuilder.getBlockInMask(BB);
90589049

9059-
VPReductionRecipe *RedRecipe = new VPReductionRecipe(
9060-
RdxDesc, CurrentLinkI, PreviousLink, VecOp, CondOp);
9050+
VPReductionRecipe *RedRecipe =
9051+
new VPReductionRecipe(RdxDesc, CurrentLinkI, PreviousLink, VecOp,
9052+
CondOp, CM.useOrderedReductions(RdxDesc));
90619053
// Append the recipe to the end of the VPBasicBlock because we need to
90629054
// ensure that it comes after all of it's inputs, including CondOp.
90639055
// Note that this transformation may leave over dead recipes (including
@@ -9307,57 +9299,6 @@ void VPInterleaveRecipe::execute(VPTransformState &State) {
93079299
NeedsMaskForGaps);
93089300
}
93099301

9310-
void VPReductionRecipe::execute(VPTransformState &State) {
9311-
assert(!State.Instance && "Reduction being replicated.");
9312-
Value *PrevInChain = State.get(getChainOp(), 0, /*IsScalar*/ true);
9313-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9314-
bool IsOrdered = State.ILV->useOrderedReductions(RdxDesc);
9315-
// Propagate the fast-math flags carried by the underlying instruction.
9316-
IRBuilderBase::FastMathFlagGuard FMFGuard(State.Builder);
9317-
State.Builder.setFastMathFlags(RdxDesc.getFastMathFlags());
9318-
for (unsigned Part = 0; Part < State.UF; ++Part) {
9319-
Value *NewVecOp = State.get(getVecOp(), Part);
9320-
if (VPValue *Cond = getCondOp()) {
9321-
Value *NewCond = State.get(Cond, Part, State.VF.isScalar());
9322-
VectorType *VecTy = dyn_cast<VectorType>(NewVecOp->getType());
9323-
Type *ElementTy = VecTy ? VecTy->getElementType() : NewVecOp->getType();
9324-
Value *Iden = RdxDesc.getRecurrenceIdentity(Kind, ElementTy,
9325-
RdxDesc.getFastMathFlags());
9326-
if (State.VF.isVector()) {
9327-
Iden =
9328-
State.Builder.CreateVectorSplat(VecTy->getElementCount(), Iden);
9329-
}
9330-
9331-
Value *Select = State.Builder.CreateSelect(NewCond, NewVecOp, Iden);
9332-
NewVecOp = Select;
9333-
}
9334-
Value *NewRed;
9335-
Value *NextInChain;
9336-
if (IsOrdered) {
9337-
if (State.VF.isVector())
9338-
NewRed = createOrderedReduction(State.Builder, RdxDesc, NewVecOp,
9339-
PrevInChain);
9340-
else
9341-
NewRed = State.Builder.CreateBinOp(
9342-
(Instruction::BinaryOps)RdxDesc.getOpcode(Kind), PrevInChain,
9343-
NewVecOp);
9344-
PrevInChain = NewRed;
9345-
} else {
9346-
PrevInChain = State.get(getChainOp(), Part, /*IsScalar*/ true);
9347-
NewRed = createTargetReduction(State.Builder, RdxDesc, NewVecOp);
9348-
}
9349-
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
9350-
NextInChain = createMinMaxOp(State.Builder, RdxDesc.getRecurrenceKind(),
9351-
NewRed, PrevInChain);
9352-
} else if (IsOrdered)
9353-
NextInChain = NewRed;
9354-
else
9355-
NextInChain = State.Builder.CreateBinOp(
9356-
(Instruction::BinaryOps)RdxDesc.getOpcode(Kind), NewRed, PrevInChain);
9357-
State.set(this, NextInChain, Part, /*IsScalar*/ true);
9358-
}
9359-
}
9360-
93619302
void VPReplicateRecipe::execute(VPTransformState &State) {
93629303
Instruction *UI = getUnderlyingInstr();
93639304
if (State.Instance) { // Generate a single instance.

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,13 +2075,15 @@ class VPInterleaveRecipe : public VPRecipeBase {
20752075
class VPReductionRecipe : public VPSingleDefRecipe {
20762076
/// The recurrence decriptor for the reduction in question.
20772077
const RecurrenceDescriptor &RdxDesc;
2078+
bool IsOrdered;
20782079

20792080
public:
20802081
VPReductionRecipe(const RecurrenceDescriptor &R, Instruction *I,
2081-
VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp)
2082+
VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp,
2083+
bool IsOrdered)
20822084
: VPSingleDefRecipe(VPDef::VPReductionSC,
20832085
ArrayRef<VPValue *>({ChainOp, VecOp}), I),
2084-
RdxDesc(R) {
2086+
RdxDesc(R), IsOrdered(IsOrdered) {
20852087
if (CondOp)
20862088
addOperand(CondOp);
20872089
}
@@ -2090,7 +2092,7 @@ class VPReductionRecipe : public VPSingleDefRecipe {
20902092

20912093
VPRecipeBase *clone() override {
20922094
return new VPReductionRecipe(RdxDesc, getUnderlyingInstr(), getChainOp(),
2093-
getVecOp(), getCondOp());
2095+
getVecOp(), getCondOp(), IsOrdered);
20942096
}
20952097

20962098
VP_CLASSOF_IMPL(VPDef::VPReductionSC)

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,58 @@ void VPBlendRecipe::print(raw_ostream &O, const Twine &Indent,
15181518
}
15191519
}
15201520
}
1521+
#endif
1522+
1523+
void VPReductionRecipe::execute(VPTransformState &State) {
1524+
assert(!State.Instance && "Reduction being replicated.");
1525+
Value *PrevInChain = State.get(getChainOp(), 0, /*IsScalar*/ true);
1526+
RecurKind Kind = RdxDesc.getRecurrenceKind();
1527+
// Propagate the fast-math flags carried by the underlying instruction.
1528+
IRBuilderBase::FastMathFlagGuard FMFGuard(State.Builder);
1529+
State.Builder.setFastMathFlags(RdxDesc.getFastMathFlags());
1530+
for (unsigned Part = 0; Part < State.UF; ++Part) {
1531+
Value *NewVecOp = State.get(getVecOp(), Part);
1532+
if (VPValue *Cond = getCondOp()) {
1533+
Value *NewCond = State.get(Cond, Part, State.VF.isScalar());
1534+
VectorType *VecTy = dyn_cast<VectorType>(NewVecOp->getType());
1535+
Type *ElementTy = VecTy ? VecTy->getElementType() : NewVecOp->getType();
1536+
Value *Iden = RdxDesc.getRecurrenceIdentity(Kind, ElementTy,
1537+
RdxDesc.getFastMathFlags());
1538+
if (State.VF.isVector()) {
1539+
Iden = State.Builder.CreateVectorSplat(VecTy->getElementCount(), Iden);
1540+
}
1541+
1542+
Value *Select = State.Builder.CreateSelect(NewCond, NewVecOp, Iden);
1543+
NewVecOp = Select;
1544+
}
1545+
Value *NewRed;
1546+
Value *NextInChain;
1547+
if (IsOrdered) {
1548+
if (State.VF.isVector())
1549+
NewRed = createOrderedReduction(State.Builder, RdxDesc, NewVecOp,
1550+
PrevInChain);
1551+
else
1552+
NewRed = State.Builder.CreateBinOp(
1553+
(Instruction::BinaryOps)RdxDesc.getOpcode(Kind), PrevInChain,
1554+
NewVecOp);
1555+
PrevInChain = NewRed;
1556+
} else {
1557+
PrevInChain = State.get(getChainOp(), Part, /*IsScalar*/ true);
1558+
NewRed = createTargetReduction(State.Builder, RdxDesc, NewVecOp);
1559+
}
1560+
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
1561+
NextInChain = createMinMaxOp(State.Builder, RdxDesc.getRecurrenceKind(),
1562+
NewRed, PrevInChain);
1563+
} else if (IsOrdered)
1564+
NextInChain = NewRed;
1565+
else
1566+
NextInChain = State.Builder.CreateBinOp(
1567+
(Instruction::BinaryOps)RdxDesc.getOpcode(Kind), NewRed, PrevInChain);
1568+
State.set(this, NextInChain, Part, /*IsScalar*/ true);
1569+
}
1570+
}
15211571

1572+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
15221573
void VPReductionRecipe::print(raw_ostream &O, const Twine &Indent,
15231574
VPSlotTracker &SlotTracker) const {
15241575
O << Indent << "REDUCE ";

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ TEST(VPRecipeTest, MayHaveSideEffectsAndMayReadWriteMemory) {
11191119
VPValue VecOp;
11201120
VPValue CondOp;
11211121
VPReductionRecipe Recipe(RecurrenceDescriptor(), nullptr, &ChainOp, &CondOp,
1122-
&VecOp);
1122+
&VecOp, false);
11231123
EXPECT_FALSE(Recipe.mayHaveSideEffects());
11241124
EXPECT_FALSE(Recipe.mayReadFromMemory());
11251125
EXPECT_FALSE(Recipe.mayWriteToMemory());
@@ -1287,7 +1287,7 @@ TEST(VPRecipeTest, CastVPReductionRecipeToVPUser) {
12871287
VPValue VecOp;
12881288
VPValue CondOp;
12891289
VPReductionRecipe Recipe(RecurrenceDescriptor(), nullptr, &ChainOp, &CondOp,
1290-
&VecOp);
1290+
&VecOp, false);
12911291
EXPECT_TRUE(isa<VPUser>(&Recipe));
12921292
VPRecipeBase *BaseR = &Recipe;
12931293
EXPECT_TRUE(isa<VPUser>(BaseR));

0 commit comments

Comments
 (0)