Skip to content

Commit 85a044b

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 a75587d commit 85a044b

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
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;
@@ -9042,8 +9039,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90429039
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90439040
continue;
90449041

9045-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9046-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9042+
RecurKind Kind = PhiR->getRecurrenceKind();
90479043
assert(
90489044
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90499045
!RecurrenceDescriptor::isFindIVRecurrenceKind(Kind) &&
@@ -9149,6 +9145,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91499145
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91509146
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91519147

9148+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9149+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91529150
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91539151
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91549152
? RdxDesc.getFastMathFlags()
@@ -9179,7 +9177,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91799177
if (!PhiR)
91809178
continue;
91819179

9182-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9180+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9181+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91839182
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
91849183
// If tail is folded by masking, introduce selects between the phi
91859184
// and the users outside the vector region of each reduction, at the
@@ -9825,14 +9824,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98259824
}));
98269825
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98279826
->getIncomingValueForBlock(L->getLoopPreheader());
9828-
const RecurrenceDescriptor &RdxDesc =
9829-
ReductionPhi->getRecurrenceDescriptor();
9830-
RecurKind RK = RdxDesc.getRecurrenceKind();
9827+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98319828
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98329829
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9833-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9834-
"start value from ComputeAnyOfResult must match");
9835-
98369830
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98379831
// start value; compare the final value from the main vector loop
98389832
// to the start value.
@@ -9841,9 +9835,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98419835
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98429836
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(RK)) {
98439837
Value *StartV = getStartValueFromReductionResult(RdxResult);
9844-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9845-
"start value from ComputeFinIVResult must match");
9846-
98479838
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98489839
EPI.MainLoopIterationCountCheck);
98499840

llvm/lib/Transforms/Vectorize/VPlan.h

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

21902190
/// The phi is part of an in-loop reduction.
21912191
bool IsInLoop;
@@ -2204,16 +2204,24 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22042204
VPValue &Start, bool IsInLoop = false,
22052205
bool IsOrdered = false, unsigned VFScaleFactor = 1)
22062206
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start),
2207-
RdxDesc(RdxDesc), IsInLoop(IsInLoop), IsOrdered(IsOrdered),
2208-
VFScaleFactor(VFScaleFactor) {
2207+
Kind(RdxDesc.getRecurrenceKind()), IsInLoop(IsInLoop),
2208+
IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
2209+
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
2210+
}
2211+
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2212+
bool IsInLoop = false, bool IsOrdered = false,
2213+
unsigned VFScaleFactor = 1)
2214+
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2215+
IsInLoop(IsInLoop), IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
22092216
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
22102217
}
22112218

22122219
~VPReductionPHIRecipe() override = default;
22132220

22142221
VPReductionPHIRecipe *clone() override {
2222+
22152223
auto *R = new VPReductionPHIRecipe(
2216-
dyn_cast_or_null<PHINode>(getUnderlyingValue()), RdxDesc,
2224+
dyn_cast_or_null<PHINode>(getUnderlyingValue()), getRecurrenceKind(),
22172225
*getOperand(0), IsInLoop, IsOrdered, VFScaleFactor);
22182226
R->addOperand(getBackedgeValue());
22192227
return R;
@@ -2233,9 +2241,7 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22332241
VPSlotTracker &SlotTracker) const override;
22342242
#endif
22352243

2236-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2237-
return RdxDesc;
2238-
}
2244+
RecurKind getRecurrenceKind() const { return Kind; }
22392245

22402246
/// Returns true, if the phi is part of an ordered reduction.
22412247
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)