Skip to content

Commit 4cba7de

Browse files
committed
[VPlan] Replace RdxDesc with RecurKind in VPReductionPHIRecipe (NFC).
Replace VPReductionPHIRecipe with RecurKind in VPReductionPHIRecipe, as all VPlan analyses and codegen only require the recurrence kind. This enables creating new VPReductionPHIRecipe directly in LV, without needing to construction a whole RecurrenceDescriptor object. Depends on #141860 #141932 #142290 #142291
1 parent 927ed49 commit 4cba7de

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7272,8 +7272,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72727272

72737273
auto *EpiRedHeaderPhi =
72747274
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7275-
const RecurrenceDescriptor &RdxDesc =
7276-
EpiRedHeaderPhi->getRecurrenceDescriptor();
7275+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
72777276
Value *MainResumeValue;
72787277
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
72797278
assert((VPI->getOpcode() == VPInstruction::Broadcast ||
@@ -7282,8 +7281,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72827281
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72837282
} else
72847283
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7285-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7286-
RdxDesc.getRecurrenceKind())) {
7284+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
72877285
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
72887286
(void)StartV;
72897287
auto *Cmp = cast<ICmpInst>(MainResumeValue);
@@ -7293,8 +7291,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72937291
"AnyOf expected to start by comparing main resume value to original "
72947292
"start value");
72957293
MainResumeValue = Cmp->getOperand(0);
7296-
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
7297-
RdxDesc.getRecurrenceKind())) {
7294+
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind)) {
72987295
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72997296
Value *SentinelV = EpiRedResult->getOperand(2)->getLiveInIRValue();
73007297
using namespace llvm::PatternMatch;
@@ -9045,8 +9042,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90459042
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90469043
continue;
90479044

9048-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9049-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9045+
RecurKind Kind = PhiR->getRecurrenceKind();
90509046
assert(
90519047
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90529048
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
@@ -9152,6 +9148,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91529148
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91539149
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91549150

9151+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9152+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91559153
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91569154
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91579155
? RdxDesc.getFastMathFlags()
@@ -9182,7 +9180,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91829180
if (!PhiR)
91839181
continue;
91849182

9185-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9183+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9184+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91869185
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91879186
// If tail is folded by masking, introduce selects between the phi
91889187
// and the users outside the vector region of each reduction, at the
@@ -9825,14 +9824,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98259824
}));
98269825
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98279826
->getIncomingValueForBlock(L->getLoopPreheader());
9828-
const RecurrenceDescriptor &RdxDesc =
9829-
ReductionPhi->getRecurrenceDescriptor();
9830-
RecurKind RK = RdxDesc.getRecurrenceKind();
9827+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98319828
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98329829
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9833-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9834-
"start value from ComputeAnyOfResult must match");
9835-
98369830
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98379831
// start value; compare the final value from the main vector loop
98389832
// to the start value.
@@ -9841,9 +9835,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98419835
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98429836
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
98439837
Value *StartV = getStartValueFromReductionResult(RdxResult);
9844-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9845-
"start value from ComputeFindLastIVResult must match");
9846-
98479838
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98489839
EPI.MainLoopIterationCountCheck);
98499840

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,7 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
21742174
class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21752175
public VPUnrollPartAccessor<2> {
21762176
/// Descriptor for the reduction.
2177-
const RecurrenceDescriptor &RdxDesc;
2177+
const RecurKind Kind;
21782178

21792179
/// The phi is part of an in-loop reduction.
21802180
bool IsInLoop;
@@ -2193,17 +2193,24 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21932193
VPValue &Start, bool IsInLoop = false,
21942194
bool IsOrdered = false, unsigned VFScaleFactor = 1)
21952195
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start),
2196-
RdxDesc(RdxDesc), IsInLoop(IsInLoop), IsOrdered(IsOrdered),
2197-
VFScaleFactor(VFScaleFactor) {
2196+
Kind(RdxDesc.getRecurrenceKind()), IsInLoop(IsInLoop),
2197+
IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
2198+
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
2199+
}
2200+
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2201+
bool IsInLoop = false, bool IsOrdered = false,
2202+
unsigned VFScaleFactor = 1)
2203+
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2204+
IsInLoop(IsInLoop), IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
21982205
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
21992206
}
22002207

22012208
~VPReductionPHIRecipe() override = default;
22022209

22032210
VPReductionPHIRecipe *clone() override {
22042211
auto *R = new VPReductionPHIRecipe(cast<PHINode>(getUnderlyingInstr()),
2205-
RdxDesc, *getOperand(0), IsInLoop,
2206-
IsOrdered, VFScaleFactor);
2212+
getRecurrenceKind(), *getOperand(0),
2213+
IsInLoop, IsOrdered, VFScaleFactor);
22072214
R->addOperand(getBackedgeValue());
22082215
return R;
22092216
}
@@ -2222,9 +2229,7 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22222229
VPSlotTracker &SlotTracker) const override;
22232230
#endif
22242231

2225-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2226-
return RdxDesc;
2227-
}
2232+
RecurKind getRecurrenceKind() const { return Kind; }
22282233

22292234
/// Returns true, if the phi is part of an ordered reduction.
22302235
bool isOrdered() const { return IsOrdered; }

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
697697
// and will be removed by breaking up the recipe further.
698698
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
699699
// Get its reduction variable descriptor.
700-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
701-
RecurKind RK = RdxDesc.getRecurrenceKind();
700+
[[maybe_unused]] RecurKind RK = PhiR->getRecurrenceKind();
702701
assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK) &&
703702
"Unexpected reduction kind");
704703
assert(!PhiR->isInLoop() &&
@@ -725,9 +724,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
725724
// and will be removed by breaking up the recipe further.
726725
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
727726
// Get its reduction variable descriptor.
728-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
729727

730-
RecurKind RK = RdxDesc.getRecurrenceKind();
728+
RecurKind RK = PhiR->getRecurrenceKind();
731729
assert(!RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK) &&
732730
"should be handled by ComputeFindLastIVResult");
733731

@@ -753,9 +751,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
753751
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RK))
754752
ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, RdxPart);
755753
else
756-
ReducedPartRdx =
757-
Builder.CreateBinOp((Instruction::BinaryOps)RdxDesc.getOpcode(),
758-
RdxPart, ReducedPartRdx, "bin.rdx");
754+
ReducedPartRdx = Builder.CreateBinOp(
755+
(Instruction::BinaryOps)RecurrenceDescriptor::getOpcode(RK),
756+
RdxPart, ReducedPartRdx, "bin.rdx");
759757
}
760758
}
761759

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,8 +1717,7 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
17171717
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
17181718
if (!PhiR)
17191719
continue;
1720-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
1721-
RecurKind RK = RdxDesc.getRecurrenceKind();
1720+
RecurKind RK = PhiR->getRecurrenceKind();
17221721
if (RK != RecurKind::Add && RK != RecurKind::Mul)
17231722
continue;
17241723

0 commit comments

Comments
 (0)