Skip to content

Commit 81a7d17

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 a4ccc07 commit 81a7d17

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
@@ -7238,8 +7238,6 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72387238

72397239
auto *EpiRedHeaderPhi =
72407240
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7241-
const RecurrenceDescriptor &RdxDesc =
7242-
EpiRedHeaderPhi->getRecurrenceDescriptor();
72437241
Value *MainResumeValue;
72447242
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
72457243
assert((VPI->getOpcode() == VPInstruction::Broadcast ||
@@ -7248,19 +7246,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72487246
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72497247
} else
72507248
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7251-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7252-
RdxDesc.getRecurrenceKind())) {
7249+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
7250+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
72537251
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
72547252
(void)StartV;
7253+
72557254
auto *Cmp = cast<ICmpInst>(MainResumeValue);
72567255
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
72577256
"AnyOf expected to start with ICMP_NE");
72587257
assert(Cmp->getOperand(1) == StartV &&
72597258
"AnyOf expected to start by comparing main resume value to original "
72607259
"start value");
72617260
MainResumeValue = Cmp->getOperand(0);
7262-
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
7263-
RdxDesc.getRecurrenceKind())) {
7261+
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind)) {
72647262
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72657263
using namespace llvm::PatternMatch;
72667264
Value *Cmp, *OrigResumeV, *CmpOp;
@@ -9037,8 +9035,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90379035
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90389036
continue;
90399037

9040-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9041-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9038+
RecurKind Kind = PhiR->getRecurrenceKind();
90429039
assert(
90439040
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90449041
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
@@ -9144,6 +9141,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91449141
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91459142
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91469143

9144+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9145+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91479146
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91489147
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91499148
? RdxDesc.getFastMathFlags()
@@ -9174,7 +9173,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91749173
if (!PhiR)
91759174
continue;
91769175

9177-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9176+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9177+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91789178
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91799179
// If tail is folded by masking, introduce selects between the phi
91809180
// and the users outside the vector region of each reduction, at the
@@ -9811,14 +9811,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98119811
}));
98129812
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98139813
->getIncomingValueForBlock(L->getLoopPreheader());
9814-
const RecurrenceDescriptor &RdxDesc =
9815-
ReductionPhi->getRecurrenceDescriptor();
9816-
RecurKind RK = RdxDesc.getRecurrenceKind();
9814+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98179815
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98189816
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9819-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9820-
"start value from ComputeAnyOfResult must match");
9821-
98229817
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98239818
// start value; compare the final value from the main vector loop
98249819
// to the start value.
@@ -9827,9 +9822,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98279822
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98289823
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
98299824
Value *StartV = getStartValueFromReductionResult(RdxResult);
9830-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9831-
"start value from ComputeFindLastIVResult must match");
9832-
98339825
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98349826
EPI.MainLoopIterationCountCheck);
98359827

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)