Skip to content

Commit c925a3e

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 be660f5 commit c925a3e

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7271,8 +7271,6 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72717271

72727272
auto *EpiRedHeaderPhi =
72737273
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7274-
const RecurrenceDescriptor &RdxDesc =
7275-
EpiRedHeaderPhi->getRecurrenceDescriptor();
72767274
Value *MainResumeValue;
72777275
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
72787276
assert((VPI->getOpcode() == VPInstruction::Broadcast ||
@@ -7281,19 +7279,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72817279
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72827280
} else
72837281
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7284-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7285-
RdxDesc.getRecurrenceKind())) {
7282+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
7283+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
72867284
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
72877285
(void)StartV;
7286+
72887287
auto *Cmp = cast<ICmpInst>(MainResumeValue);
72897288
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
72907289
"AnyOf expected to start with ICMP_NE");
72917290
assert(Cmp->getOperand(1) == StartV &&
72927291
"AnyOf expected to start by comparing main resume value to original "
72937292
"start value");
72947293
MainResumeValue = Cmp->getOperand(0);
7295-
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
7296-
RdxDesc.getRecurrenceKind())) {
7294+
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind)) {
72977295
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72987296
Value *SentinelV = EpiRedResult->getOperand(2)->getLiveInIRValue();
72997297
using namespace llvm::PatternMatch;
@@ -9029,8 +9027,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90299027
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90309028
continue;
90319029

9032-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9033-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9030+
RecurKind Kind = PhiR->getRecurrenceKind();
90349031
assert(
90359032
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90369033
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
@@ -9136,6 +9133,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91369133
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91379134
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91389135

9136+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9137+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91399138
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91409139
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91419140
? RdxDesc.getFastMathFlags()
@@ -9166,7 +9165,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91669165
if (!PhiR)
91679166
continue;
91689167

9169-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9168+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9169+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91709170
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91719171
// If tail is folded by masking, introduce selects between the phi
91729172
// and the users outside the vector region of each reduction, at the
@@ -9805,14 +9805,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98059805
}));
98069806
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98079807
->getIncomingValueForBlock(L->getLoopPreheader());
9808-
const RecurrenceDescriptor &RdxDesc =
9809-
ReductionPhi->getRecurrenceDescriptor();
9810-
RecurKind RK = RdxDesc.getRecurrenceKind();
9808+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98119809
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98129810
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9813-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9814-
"start value from ComputeAnyOfResult must match");
9815-
98169811
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98179812
// start value; compare the final value from the main vector loop
98189813
// to the start value.
@@ -9821,9 +9816,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98219816
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98229817
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
98239818
Value *StartV = getStartValueFromReductionResult(RdxResult);
9824-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9825-
"start value from ComputeFindLastIVResult must match");
9826-
98279819
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98289820
EPI.MainLoopIterationCountCheck);
98299821

llvm/lib/Transforms/Vectorize/VPlan.h

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

21762176
/// The phi is part of an in-loop reduction.
21772177
bool IsInLoop;
@@ -2190,17 +2190,24 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21902190
VPValue &Start, bool IsInLoop = false,
21912191
bool IsOrdered = false, unsigned VFScaleFactor = 1)
21922192
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start),
2193-
RdxDesc(RdxDesc), IsInLoop(IsInLoop), IsOrdered(IsOrdered),
2194-
VFScaleFactor(VFScaleFactor) {
2193+
Kind(RdxDesc.getRecurrenceKind()), IsInLoop(IsInLoop),
2194+
IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
2195+
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
2196+
}
2197+
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2198+
bool IsInLoop = false, bool IsOrdered = false,
2199+
unsigned VFScaleFactor = 1)
2200+
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2201+
IsInLoop(IsInLoop), IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
21952202
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
21962203
}
21972204

21982205
~VPReductionPHIRecipe() override = default;
21992206

22002207
VPReductionPHIRecipe *clone() override {
22012208
auto *R = new VPReductionPHIRecipe(cast<PHINode>(getUnderlyingInstr()),
2202-
RdxDesc, *getOperand(0), IsInLoop,
2203-
IsOrdered, VFScaleFactor);
2209+
getRecurrenceKind(), *getOperand(0),
2210+
IsInLoop, IsOrdered, VFScaleFactor);
22042211
R->addOperand(getBackedgeValue());
22052212
return R;
22062213
}
@@ -2219,9 +2226,7 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22192226
VPSlotTracker &SlotTracker) const override;
22202227
#endif
22212228

2222-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2223-
return RdxDesc;
2224-
}
2229+
RecurKind getRecurrenceKind() const { return Kind; }
22252230

22262231
/// Returns true, if the phi is part of an ordered reduction.
22272232
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
@@ -641,8 +641,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
641641
// and will be removed by breaking up the recipe further.
642642
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
643643
// Get its reduction variable descriptor.
644-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
645-
[[maybe_unused]] RecurKind RK = RdxDesc.getRecurrenceKind();
644+
[[maybe_unused]] RecurKind RK = PhiR->getRecurrenceKind();
646645
assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK) &&
647646
"Unexpected reduction kind");
648647
assert(!PhiR->isInLoop() &&
@@ -666,9 +665,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
666665
// and will be removed by breaking up the recipe further.
667666
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
668667
// Get its reduction variable descriptor.
669-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
670668

671-
RecurKind RK = RdxDesc.getRecurrenceKind();
669+
RecurKind RK = PhiR->getRecurrenceKind();
672670
assert(!RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK) &&
673671
"should be handled by ComputeFindLastIVResult");
674672

@@ -694,9 +692,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
694692
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RK))
695693
ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, RdxPart);
696694
else
697-
ReducedPartRdx =
698-
Builder.CreateBinOp((Instruction::BinaryOps)RdxDesc.getOpcode(),
699-
RdxPart, ReducedPartRdx, "bin.rdx");
695+
ReducedPartRdx = Builder.CreateBinOp(
696+
(Instruction::BinaryOps)RecurrenceDescriptor::getOpcode(RK),
697+
RdxPart, ReducedPartRdx, "bin.rdx");
700698
}
701699
}
702700

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,7 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
17091709
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
17101710
if (!PhiR)
17111711
continue;
1712-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
1713-
RecurKind RK = RdxDesc.getRecurrenceKind();
1712+
RecurKind RK = PhiR->getRecurrenceKind();
17141713
if (RK != RecurKind::Add && RK != RecurKind::Mul)
17151714
continue;
17161715

0 commit comments

Comments
 (0)