Skip to content

Commit aeb882a

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 cbfd0d6 commit aeb882a

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;
@@ -9070,8 +9067,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90709067
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90719068
continue;
90729069

9073-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9074-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9070+
RecurKind Kind = PhiR->getRecurrenceKind();
90759071
assert(
90769072
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90779073
!RecurrenceDescriptor::isFindIVRecurrenceKind(Kind) &&
@@ -9177,6 +9173,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91779173
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91789174
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91799175

9176+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9177+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91809178
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91819179
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91829180
? RdxDesc.getFastMathFlags()
@@ -9207,7 +9205,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92079205
if (!PhiR)
92089206
continue;
92099207

9210-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9208+
const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars().lookup(
9209+
cast<PHINode>(PhiR->getUnderlyingInstr()));
92119210
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
92129211
// If tail is folded by masking, introduce selects between the phi
92139212
// and the users outside the vector region of each reduction, at the
@@ -9853,14 +9852,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98539852
}));
98549853
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98559854
->getIncomingValueForBlock(L->getLoopPreheader());
9856-
const RecurrenceDescriptor &RdxDesc =
9857-
ReductionPhi->getRecurrenceDescriptor();
9858-
RecurKind RK = RdxDesc.getRecurrenceKind();
9855+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98599856
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98609857
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9861-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9862-
"start value from ComputeAnyOfResult must match");
9863-
98649858
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98659859
// start value; compare the final value from the main vector loop
98669860
// to the start value.
@@ -9869,9 +9863,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98699863
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98709864
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(RK)) {
98719865
Value *StartV = getStartValueFromReductionResult(RdxResult);
9872-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9873-
"start value from ComputeFinIVResult must match");
9874-
98759866
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98769867
EPI.MainLoopIterationCountCheck);
98779868

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
21922192
class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21932193
public VPUnrollPartAccessor<2> {
21942194
/// Descriptor for the reduction.
2195-
const RecurrenceDescriptor &RdxDesc;
2195+
const RecurKind Kind;
21962196

21972197
/// The phi is part of an in-loop reduction.
21982198
bool IsInLoop;
@@ -2211,16 +2211,24 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22112211
VPValue &Start, bool IsInLoop = false,
22122212
bool IsOrdered = false, unsigned VFScaleFactor = 1)
22132213
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start),
2214-
RdxDesc(RdxDesc), IsInLoop(IsInLoop), IsOrdered(IsOrdered),
2215-
VFScaleFactor(VFScaleFactor) {
2214+
Kind(RdxDesc.getRecurrenceKind()), IsInLoop(IsInLoop),
2215+
IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
2216+
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
2217+
}
2218+
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2219+
bool IsInLoop = false, bool IsOrdered = false,
2220+
unsigned VFScaleFactor = 1)
2221+
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2222+
IsInLoop(IsInLoop), IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
22162223
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
22172224
}
22182225

22192226
~VPReductionPHIRecipe() override = default;
22202227

22212228
VPReductionPHIRecipe *clone() override {
2229+
22222230
auto *R = new VPReductionPHIRecipe(
2223-
dyn_cast_or_null<PHINode>(getUnderlyingValue()), RdxDesc,
2231+
dyn_cast_or_null<PHINode>(getUnderlyingValue()), getRecurrenceKind(),
22242232
*getOperand(0), IsInLoop, IsOrdered, VFScaleFactor);
22252233
R->addOperand(getBackedgeValue());
22262234
return R;
@@ -2240,9 +2248,7 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22402248
VPSlotTracker &SlotTracker) const override;
22412249
#endif
22422250

2243-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2244-
return RdxDesc;
2245-
}
2251+
RecurKind getRecurrenceKind() const { return Kind; }
22462252

22472253
/// Returns true, if the phi is part of an ordered reduction.
22482254
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
@@ -751,8 +751,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
751751
// and will be removed by breaking up the recipe further.
752752
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
753753
// Get its reduction variable descriptor.
754-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
755-
RecurKind RK = RdxDesc.getRecurrenceKind();
754+
[[maybe_unused]] RecurKind RK = PhiR->getRecurrenceKind();
756755
assert(RecurrenceDescriptor::isFindIVRecurrenceKind(RK) &&
757756
"Unexpected reduction kind");
758757
assert(!PhiR->isInLoop() &&
@@ -786,9 +785,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
786785
// and will be removed by breaking up the recipe further.
787786
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
788787
// Get its reduction variable descriptor.
789-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
790788

791-
RecurKind RK = RdxDesc.getRecurrenceKind();
789+
RecurKind RK = PhiR->getRecurrenceKind();
792790
assert(!RecurrenceDescriptor::isFindIVRecurrenceKind(RK) &&
793791
"should be handled by ComputeFindIVResult");
794792

@@ -814,9 +812,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
814812
if (RecurrenceDescriptor::isMinMaxRecurrenceKind(RK))
815813
ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, RdxPart);
816814
else
817-
ReducedPartRdx =
818-
Builder.CreateBinOp((Instruction::BinaryOps)RdxDesc.getOpcode(),
819-
RdxPart, ReducedPartRdx, "bin.rdx");
815+
ReducedPartRdx = Builder.CreateBinOp(
816+
(Instruction::BinaryOps)RecurrenceDescriptor::getOpcode(RK),
817+
RdxPart, ReducedPartRdx, "bin.rdx");
820818
}
821819
}
822820

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)