Skip to content

Commit 1f774e1

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 llvm#141860 llvm#141932 llvm#142290 llvm#142291
1 parent 1590361 commit 1f774e1

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7233,28 +7233,29 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72337233

72347234
auto *EpiRedHeaderPhi =
72357235
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7236-
const RecurrenceDescriptor &RdxDesc =
7237-
EpiRedHeaderPhi->getRecurrenceDescriptor();
7238-
Value *MainResumeValue;
7239-
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue()))
7240-
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
7241-
else
7242-
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7243-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7244-
RdxDesc.getRecurrenceKind())) {
7236+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
7237+
Value *MainResumeValue =
7238+
EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7239+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
7240+
MainResumeValue = cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())
7241+
->getOperand(0)
7242+
->getUnderlyingValue();
72457243
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
72467244
(void)StartV;
7245+
72477246
auto *Cmp = cast<ICmpInst>(MainResumeValue);
72487247
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
72497248
"AnyOf expected to start with ICMP_NE");
72507249
assert(Cmp->getOperand(1) == StartV &&
72517250
"AnyOf expected to start by comparing main resume value to original "
72527251
"start value");
72537252
MainResumeValue = Cmp->getOperand(0);
7254-
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
7255-
RdxDesc.getRecurrenceKind())) {
7253+
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind)) {
72567254
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72577255
using namespace llvm::PatternMatch;
7256+
MainResumeValue = cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())
7257+
->getOperand(0)
7258+
->getUnderlyingValue();
72587259
Value *Cmp, *OrigResumeV, *CmpOp;
72597260
bool IsExpectedPattern =
72607261
match(MainResumeValue,
@@ -7268,6 +7269,9 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72687269
assert(IsExpectedPattern && "Unexpected reduction resume pattern");
72697270
(void)IsExpectedPattern;
72707271
MainResumeValue = OrigResumeV;
7272+
} else {
7273+
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue()))
7274+
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72717275
}
72727276
PHINode *MainResumePhi = cast<PHINode>(MainResumeValue);
72737277

@@ -9048,8 +9052,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90489052
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90499053
continue;
90509054

9051-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9052-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9055+
RecurKind Kind = PhiR->getRecurrenceKind();
90539056
assert(
90549057
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90559058
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
@@ -9155,6 +9158,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91559158
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91569159
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91579160

9161+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9162+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91589163
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91599164
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91609165
? RdxDesc.getFastMathFlags()
@@ -9185,7 +9190,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91859190
if (!PhiR)
91869191
continue;
91879192

9188-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9193+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9194+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91899195
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91909196
// If tail is folded by masking, introduce selects between the phi
91919197
// and the users outside the vector region of each reduction, at the
@@ -9832,14 +9838,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98329838
}));
98339839
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98349840
->getIncomingValueForBlock(L->getLoopPreheader());
9835-
const RecurrenceDescriptor &RdxDesc =
9836-
ReductionPhi->getRecurrenceDescriptor();
9837-
RecurKind RK = RdxDesc.getRecurrenceKind();
9841+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98389842
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98399843
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9840-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9841-
"start value from ComputeAnyOfResult must match");
9842-
98439844
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98449845
// start value; compare the final value from the main vector loop
98459846
// to the start value.
@@ -9848,9 +9849,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98489849
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98499850
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
98509851
Value *StartV = getStartValueFromReductionResult(RdxResult);
9851-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9852-
"start value from ComputeFindLastIVResult must match");
9853-
98549852
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98559853
EPI.MainLoopIterationCountCheck);
98569854

llvm/lib/Transforms/Vectorize/VPlan.h

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

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

22002207
~VPReductionPHIRecipe() override = default;
22012208

22022209
VPReductionPHIRecipe *clone() override {
22032210
auto *R = new VPReductionPHIRecipe(cast<PHINode>(getUnderlyingInstr()),
2204-
RdxDesc, *getOperand(0), IsInLoop,
2205-
IsOrdered, VFScaleFactor);
2211+
getRecurrenceKind(), *getOperand(0),
2212+
IsInLoop, IsOrdered, VFScaleFactor);
22062213
R->addOperand(getBackedgeValue());
22072214
return R;
22082215
}
@@ -2221,9 +2228,7 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22212228
VPSlotTracker &SlotTracker) const override;
22222229
#endif
22232230

2224-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2225-
return RdxDesc;
2226-
}
2231+
RecurKind getRecurrenceKind() const { return Kind; }
22272232

22282233
/// Returns true, if the phi is part of an ordered reduction.
22292234
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
@@ -637,8 +637,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
637637
// and will be removed by breaking up the recipe further.
638638
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
639639
// Get its reduction variable descriptor.
640-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
641-
[[maybe_unused]] RecurKind RK = RdxDesc.getRecurrenceKind();
640+
[[maybe_unused]] RecurKind RK = PhiR->getRecurrenceKind();
642641
assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK) &&
643642
"Unexpected reduction kind");
644643
assert(!PhiR->isInLoop() &&
@@ -662,9 +661,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
662661
// and will be removed by breaking up the recipe further.
663662
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
664663
// Get its reduction variable descriptor.
665-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
666664

667-
RecurKind RK = RdxDesc.getRecurrenceKind();
665+
RecurKind RK = PhiR->getRecurrenceKind();
668666
assert(!RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK) &&
669667
"should be handled by ComputeFindLastIVResult");
670668

@@ -690,9 +688,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
690688
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RK))
691689
ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, RdxPart);
692690
else
693-
ReducedPartRdx =
694-
Builder.CreateBinOp((Instruction::BinaryOps)RdxDesc.getOpcode(),
695-
RdxPart, ReducedPartRdx, "bin.rdx");
691+
ReducedPartRdx = Builder.CreateBinOp(
692+
(Instruction::BinaryOps)RecurrenceDescriptor::getOpcode(RK),
693+
RdxPart, ReducedPartRdx, "bin.rdx");
696694
}
697695
}
698696

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,8 +1713,7 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
17131713
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
17141714
if (!PhiR)
17151715
continue;
1716-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
1717-
RecurKind RK = RdxDesc.getRecurrenceKind();
1716+
RecurKind RK = PhiR->getRecurrenceKind();
17181717
if (RK != RecurKind::Add && RK != RecurKind::Mul)
17191718
continue;
17201719

0 commit comments

Comments
 (0)