Skip to content

Commit 5c79627

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 263d23b commit 5c79627

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7267,8 +7267,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72677267

72687268
auto *EpiRedHeaderPhi =
72697269
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7270-
const RecurrenceDescriptor &RdxDesc =
7271-
EpiRedHeaderPhi->getRecurrenceDescriptor();
7270+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
72727271
Value *MainResumeValue;
72737272
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
72747273
assert((VPI->getOpcode() == VPInstruction::Broadcast ||
@@ -7277,8 +7276,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72777276
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72787277
} else
72797278
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7280-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7281-
RdxDesc.getRecurrenceKind())) {
7279+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
72827280
[[maybe_unused]] Value *StartV =
72837281
EpiRedResult->getOperand(1)->getLiveInIRValue();
72847282
auto *Cmp = cast<ICmpInst>(MainResumeValue);
@@ -7288,8 +7286,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72887286
"AnyOf expected to start by comparing main resume value to original "
72897287
"start value");
72907288
MainResumeValue = Cmp->getOperand(0);
7291-
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(
7292-
RdxDesc.getRecurrenceKind())) {
7289+
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(Kind)) {
72937290
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72947291
Value *SentinelV = EpiRedResult->getOperand(2)->getLiveInIRValue();
72957292
using namespace llvm::PatternMatch;
@@ -9040,8 +9037,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90409037
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90419038
continue;
90429039

9043-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9044-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9040+
RecurKind Kind = PhiR->getRecurrenceKind();
90459041
assert(
90469042
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90479043
!RecurrenceDescriptor::isFindIVRecurrenceKind(Kind) &&
@@ -9147,6 +9143,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91479143
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91489144
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91499145

9146+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9147+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91509148
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91519149
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91529150
? RdxDesc.getFastMathFlags()
@@ -9177,7 +9175,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91779175
if (!PhiR)
91789176
continue;
91799177

9180-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9178+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9179+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91819180
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91829181
// If tail is folded by masking, introduce selects between the phi
91839182
// and the users outside the vector region of each reduction, at the
@@ -9820,14 +9819,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98209819
}));
98219820
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98229821
->getIncomingValueForBlock(L->getLoopPreheader());
9823-
const RecurrenceDescriptor &RdxDesc =
9824-
ReductionPhi->getRecurrenceDescriptor();
9825-
RecurKind RK = RdxDesc.getRecurrenceKind();
9822+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98269823
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98279824
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9828-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9829-
"start value from ComputeAnyOfResult must match");
9830-
98319825
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98329826
// start value; compare the final value from the main vector loop
98339827
// to the start value.
@@ -9836,9 +9830,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98369830
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98379831
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(RK)) {
98389832
Value *StartV = getStartValueFromReductionResult(RdxResult);
9839-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9840-
"start value from ComputeFinIVResult must match");
9841-
98429833
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98439834
EPI.MainLoopIterationCountCheck);
98449835

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,7 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
21872187
class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21882188
public VPUnrollPartAccessor<2> {
21892189
/// Descriptor for the reduction.
2190-
const RecurrenceDescriptor &RdxDesc;
2190+
const RecurKind Kind;
21912191

21922192
/// The phi is part of an in-loop reduction.
21932193
bool IsInLoop;
@@ -2206,17 +2206,24 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22062206
VPValue &Start, bool IsInLoop = false,
22072207
bool IsOrdered = false, unsigned VFScaleFactor = 1)
22082208
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start),
2209-
RdxDesc(RdxDesc), IsInLoop(IsInLoop), IsOrdered(IsOrdered),
2210-
VFScaleFactor(VFScaleFactor) {
2209+
Kind(RdxDesc.getRecurrenceKind()), IsInLoop(IsInLoop),
2210+
IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
2211+
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
2212+
}
2213+
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2214+
bool IsInLoop = false, bool IsOrdered = false,
2215+
unsigned VFScaleFactor = 1)
2216+
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2217+
IsInLoop(IsInLoop), IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
22112218
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
22122219
}
22132220

22142221
~VPReductionPHIRecipe() override = default;
22152222

22162223
VPReductionPHIRecipe *clone() override {
22172224
auto *R = new VPReductionPHIRecipe(cast<PHINode>(getUnderlyingInstr()),
2218-
RdxDesc, *getOperand(0), IsInLoop,
2219-
IsOrdered, VFScaleFactor);
2225+
getRecurrenceKind(), *getOperand(0),
2226+
IsInLoop, IsOrdered, VFScaleFactor);
22202227
R->addOperand(getBackedgeValue());
22212228
return R;
22222229
}
@@ -2235,9 +2242,7 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22352242
VPSlotTracker &SlotTracker) const override;
22362243
#endif
22372244

2238-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2239-
return RdxDesc;
2240-
}
2245+
RecurKind getRecurrenceKind() const { return Kind; }
22412246

22422247
/// Returns true, if the phi is part of an ordered reduction.
22432248
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
@@ -730,8 +730,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
730730
// and will be removed by breaking up the recipe further.
731731
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
732732
// Get its reduction variable descriptor.
733-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
734-
RecurKind RK = RdxDesc.getRecurrenceKind();
733+
[[maybe_unused]] RecurKind RK = PhiR->getRecurrenceKind();
735734
assert(RecurrenceDescriptor::isFindIVRecurrenceKind(RK) &&
736735
"Unexpected reduction kind");
737736
assert(!PhiR->isInLoop() &&
@@ -765,9 +764,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
765764
// and will be removed by breaking up the recipe further.
766765
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
767766
// Get its reduction variable descriptor.
768-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
769767

770-
RecurKind RK = RdxDesc.getRecurrenceKind();
768+
RecurKind RK = PhiR->getRecurrenceKind();
771769
assert(!RecurrenceDescriptor::isFindIVRecurrenceKind(RK) &&
772770
"should be handled by ComputeFindIVResult");
773771

@@ -793,9 +791,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
793791
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RK))
794792
ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, RdxPart);
795793
else
796-
ReducedPartRdx =
797-
Builder.CreateBinOp((Instruction::BinaryOps)RdxDesc.getOpcode(),
798-
RdxPart, ReducedPartRdx, "bin.rdx");
794+
ReducedPartRdx = Builder.CreateBinOp(
795+
(Instruction::BinaryOps)RecurrenceDescriptor::getOpcode(RK),
796+
RdxPart, ReducedPartRdx, "bin.rdx");
799797
}
800798
}
801799

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,7 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
17351735
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
17361736
if (!PhiR)
17371737
continue;
1738-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
1739-
RecurKind RK = RdxDesc.getRecurrenceKind();
1738+
RecurKind RK = PhiR->getRecurrenceKind();
17401739
if (RK != RecurKind::Add && RK != RecurKind::Mul)
17411740
continue;
17421741

0 commit comments

Comments
 (0)