Skip to content

Commit 0a5bd5f

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 5431f7e commit 0a5bd5f

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7507,26 +7507,21 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
75077507

75087508
auto *EpiRedHeaderPhi =
75097509
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7510-
const RecurrenceDescriptor &RdxDesc =
7511-
EpiRedHeaderPhi->getRecurrenceDescriptor();
7512-
Value *MainResumeValue;
7513-
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue()))
7514-
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
7515-
else
7516-
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7517-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7518-
RdxDesc.getRecurrenceKind())) {
7510+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
7511+
Value *MainResumeValue =
7512+
EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7513+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
75197514
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
75207515
(void)StartV;
7516+
75217517
auto *Cmp = cast<ICmpInst>(MainResumeValue);
75227518
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
75237519
"AnyOf expected to start with ICMP_NE");
75247520
assert(Cmp->getOperand(1) == StartV &&
75257521
"AnyOf expected to start by comparing main resume value to original "
75267522
"start value");
75277523
MainResumeValue = Cmp->getOperand(0);
7528-
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
7529-
RdxDesc.getRecurrenceKind())) {
7524+
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind)) {
75307525
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
75317526
(void)StartV;
75327527
using namespace llvm::PatternMatch;
@@ -9323,8 +9318,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
93239318
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
93249319
continue;
93259320

9326-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9327-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9321+
RecurKind Kind = PhiR->getRecurrenceKind();
93289322
assert(
93299323
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
93309324
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
@@ -9430,6 +9424,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
94309424
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
94319425
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
94329426

9427+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9428+
cast<PHINode>(PhiR->getUnderlyingInstr()));
94339429
// Non-FP RdxDescs will have all fast math flags set, so clear them.
94349430
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
94359431
? RdxDesc.getFastMathFlags()
@@ -9460,7 +9456,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
94609456
if (!PhiR)
94619457
continue;
94629458

9463-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9459+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9460+
cast<PHINode>(PhiR->getUnderlyingInstr()));
94649461
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
94659462
// If tail is folded by masking, introduce selects between the phi
94669463
// and the users outside the vector region of each reduction, at the
@@ -10106,14 +10103,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
1010610103
}));
1010710104
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
1010810105
->getIncomingValueForBlock(L->getLoopPreheader());
10109-
const RecurrenceDescriptor &RdxDesc =
10110-
ReductionPhi->getRecurrenceDescriptor();
10111-
RecurKind RK = RdxDesc.getRecurrenceKind();
10106+
RecurKind RK = ReductionPhi->getRecurrenceKind();
1011210107
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
1011310108
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
10114-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
10115-
"start value from ComputeAnyOfResult must match");
10116-
1011710109
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
1011810110
// start value; compare the final value from the main vector loop
1011910111
// to the start value.
@@ -10122,9 +10114,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
1012210114
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
1012310115
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
1012410116
Value *StartV = getStartValueFromReductionResult(RdxResult);
10125-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
10126-
"start value from ComputeFindLastIVResult must match");
10127-
1012810117
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
1012910118
EPI.MainLoopIterationCountCheck);
1013010119

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
@@ -1715,8 +1715,7 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
17151715
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
17161716
if (!PhiR)
17171717
continue;
1718-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
1719-
RecurKind RK = RdxDesc.getRecurrenceKind();
1718+
RecurKind RK = PhiR->getRecurrenceKind();
17201719
if (RK != RecurKind::Add && RK != RecurKind::Mul)
17211720
continue;
17221721

0 commit comments

Comments
 (0)