Skip to content

Commit ddafb4a

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 70f4ecd commit ddafb4a

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

72347234
auto *EpiRedHeaderPhi =
72357235
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7236-
const RecurrenceDescriptor &RdxDesc =
7237-
EpiRedHeaderPhi->getRecurrenceDescriptor();
72387236
Value *MainResumeValue;
72397237
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
72407238
assert((VPI->getOpcode() == VPInstruction::Broadcast ||
@@ -7243,19 +7241,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72437241
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72447242
} else
72457243
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7246-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7247-
RdxDesc.getRecurrenceKind())) {
7244+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
7245+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
72487246
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
72497247
(void)StartV;
7248+
72507249
auto *Cmp = cast<ICmpInst>(MainResumeValue);
72517250
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
72527251
"AnyOf expected to start with ICMP_NE");
72537252
assert(Cmp->getOperand(1) == StartV &&
72547253
"AnyOf expected to start by comparing main resume value to original "
72557254
"start value");
72567255
MainResumeValue = Cmp->getOperand(0);
7257-
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
7258-
RdxDesc.getRecurrenceKind())) {
7256+
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind)) {
72597257
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72607258
using namespace llvm::PatternMatch;
72617259
Value *Cmp, *OrigResumeV, *CmpOp;
@@ -9050,8 +9048,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90509048
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90519049
continue;
90529050

9053-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9054-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9051+
RecurKind Kind = PhiR->getRecurrenceKind();
90559052
assert(
90569053
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90579054
!RecurrenceDescriptor::isFindLastIVRecurrenceKind(Kind) &&
@@ -9157,6 +9154,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91579154
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91589155
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91599156

9157+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9158+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91609159
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91619160
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91629161
? RdxDesc.getFastMathFlags()
@@ -9187,7 +9186,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91879186
if (!PhiR)
91889187
continue;
91899188

9190-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9189+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9190+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91919191
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91929192
// If tail is folded by masking, introduce selects between the phi
91939193
// and the users outside the vector region of each reduction, at the
@@ -9833,14 +9833,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98339833
}));
98349834
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98359835
->getIncomingValueForBlock(L->getLoopPreheader());
9836-
const RecurrenceDescriptor &RdxDesc =
9837-
ReductionPhi->getRecurrenceDescriptor();
9838-
RecurKind RK = RdxDesc.getRecurrenceKind();
9836+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98399837
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98409838
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9841-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9842-
"start value from ComputeAnyOfResult must match");
9843-
98449839
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98459840
// start value; compare the final value from the main vector loop
98469841
// to the start value.
@@ -9849,9 +9844,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98499844
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98509845
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
98519846
Value *StartV = getStartValueFromReductionResult(RdxResult);
9852-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9853-
"start value from ComputeFindLastIVResult must match");
9854-
98559847
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98569848
EPI.MainLoopIterationCountCheck);
98579849

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)