Skip to content

Commit 6a9a16d

Browse files
authored
[VPlan] Replace RdxDesc with RecurKind in VPReductionPHIRecipe (NFC). (#142322)
Replace RdxDesc 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 PR: #142322
1 parent 6d77b82 commit 6a9a16d

File tree

4 files changed

+38
-56
lines changed

4 files changed

+38
-56
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7266,8 +7266,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72667266

72677267
auto *EpiRedHeaderPhi =
72687268
cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7269-
const RecurrenceDescriptor &RdxDesc =
7270-
EpiRedHeaderPhi->getRecurrenceDescriptor();
7269+
RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
72717270
Value *MainResumeValue;
72727271
if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
72737272
assert((VPI->getOpcode() == VPInstruction::Broadcast ||
@@ -7276,8 +7275,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72767275
MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
72777276
} else
72787277
MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7279-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
7280-
RdxDesc.getRecurrenceKind())) {
7278+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind)) {
72817279
[[maybe_unused]] Value *StartV =
72827280
EpiRedResult->getOperand(1)->getLiveInIRValue();
72837281
auto *Cmp = cast<ICmpInst>(MainResumeValue);
@@ -7287,8 +7285,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72877285
"AnyOf expected to start by comparing main resume value to original "
72887286
"start value");
72897287
MainResumeValue = Cmp->getOperand(0);
7290-
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(
7291-
RdxDesc.getRecurrenceKind())) {
7288+
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(Kind)) {
72927289
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
72937290
Value *SentinelV = EpiRedResult->getOperand(2)->getLiveInIRValue();
72947291
using namespace llvm::PatternMatch;
@@ -8308,7 +8305,7 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
83088305
unsigned ScaleFactor =
83098306
getScalingForReduction(RdxDesc.getLoopExitInstr()).value_or(1);
83108307
PhiRecipe = new VPReductionPHIRecipe(
8311-
Phi, RdxDesc, *StartV, CM.isInLoopReduction(Phi),
8308+
Phi, RdxDesc.getRecurrenceKind(), *StartV, CM.isInLoopReduction(Phi),
83128309
CM.useOrderedReductions(RdxDesc), ScaleFactor);
83138310
} else {
83148311
// TODO: Currently fixed-order recurrences are modeled as chains of
@@ -9068,8 +9065,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90689065
if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
90699066
continue;
90709067

9071-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9072-
RecurKind Kind = RdxDesc.getRecurrenceKind();
9068+
RecurKind Kind = PhiR->getRecurrenceKind();
90739069
assert(
90749070
!RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) &&
90759071
!RecurrenceDescriptor::isFindIVRecurrenceKind(Kind) &&
@@ -9175,6 +9171,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91759171
if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
91769172
CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
91779173

9174+
// TODO: Retrieve FMFs from recipes directly.
9175+
RecurrenceDescriptor RdxDesc = Legal->getRecurrenceDescriptor(
9176+
cast<PHINode>(PhiR->getUnderlyingInstr()));
91789177
// Non-FP RdxDescs will have all fast math flags set, so clear them.
91799178
FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
91809179
? RdxDesc.getFastMathFlags()
@@ -9205,7 +9204,8 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92059204
if (!PhiR)
92069205
continue;
92079206

9208-
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
9207+
const RecurrenceDescriptor &RdxDesc = Legal->getRecurrenceDescriptor(
9208+
cast<PHINode>(PhiR->getUnderlyingInstr()));
92099209
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
92109210
// If tail is folded by masking, introduce selects between the phi
92119211
// and the users outside the vector region of each reduction, at the
@@ -9253,24 +9253,23 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92539253
VPInstruction *FinalReductionResult;
92549254
VPBuilder::InsertPointGuard Guard(Builder);
92559255
Builder.setInsertPoint(MiddleVPBB, IP);
9256-
if (RecurrenceDescriptor::isFindIVRecurrenceKind(
9257-
RdxDesc.getRecurrenceKind())) {
9256+
RecurKind RecurrenceKind = PhiR->getRecurrenceKind();
9257+
if (RecurrenceDescriptor::isFindIVRecurrenceKind(RecurrenceKind)) {
92589258
VPValue *Start = PhiR->getStartValue();
92599259
VPValue *Sentinel = Plan->getOrAddLiveIn(RdxDesc.getSentinelValue());
92609260
FinalReductionResult =
92619261
Builder.createNaryOp(VPInstruction::ComputeFindIVResult,
92629262
{PhiR, Start, Sentinel, NewExitingVPV}, ExitDL);
9263-
} else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
9264-
RdxDesc.getRecurrenceKind())) {
9263+
} else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RecurrenceKind)) {
92659264
VPValue *Start = PhiR->getStartValue();
92669265
FinalReductionResult =
92679266
Builder.createNaryOp(VPInstruction::ComputeAnyOfResult,
92689267
{PhiR, Start, NewExitingVPV}, ExitDL);
92699268
} else {
9270-
VPIRFlags Flags = RecurrenceDescriptor::isFloatingPointRecurrenceKind(
9271-
RdxDesc.getRecurrenceKind())
9272-
? VPIRFlags(RdxDesc.getFastMathFlags())
9273-
: VPIRFlags();
9269+
VPIRFlags Flags =
9270+
RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurrenceKind)
9271+
? VPIRFlags(RdxDesc.getFastMathFlags())
9272+
: VPIRFlags();
92749273
FinalReductionResult =
92759274
Builder.createNaryOp(VPInstruction::ComputeReductionResult,
92769275
{PhiR, NewExitingVPV}, Flags, ExitDL);
@@ -9279,11 +9278,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92799278
// then extend the loop exit value to enable InstCombine to evaluate the
92809279
// entire expression in the smaller type.
92819280
if (MinVF.isVector() && PhiTy != RdxDesc.getRecurrenceType() &&
9282-
!RecurrenceDescriptor::isAnyOfRecurrenceKind(
9283-
RdxDesc.getRecurrenceKind())) {
9281+
!RecurrenceDescriptor::isAnyOfRecurrenceKind(RecurrenceKind)) {
92849282
assert(!PhiR->isInLoop() && "Unexpected truncated inloop reduction!");
9285-
assert(!RecurrenceDescriptor::isMinMaxRecurrenceKind(
9286-
RdxDesc.getRecurrenceKind()) &&
9283+
assert(!RecurrenceDescriptor::isMinMaxRecurrenceKind(RecurrenceKind) &&
92879284
"Unexpected truncated min-max recurrence!");
92889285
Type *RdxTy = RdxDesc.getRecurrenceType();
92899286
auto *Trunc =
@@ -9319,8 +9316,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
93199316
// with a boolean reduction phi node to check if the condition is true in
93209317
// any iteration. The final value is selected by the final
93219318
// ComputeReductionResult.
9322-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
9323-
RdxDesc.getRecurrenceKind())) {
9319+
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RecurrenceKind)) {
93249320
auto *Select = cast<VPRecipeBase>(*find_if(PhiR->users(), [](VPUser *U) {
93259321
return isa<VPWidenSelectRecipe>(U) ||
93269322
(isa<VPReplicateRecipe>(U) &&
@@ -9851,14 +9847,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98519847
}));
98529848
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
98539849
->getIncomingValueForBlock(L->getLoopPreheader());
9854-
const RecurrenceDescriptor &RdxDesc =
9855-
ReductionPhi->getRecurrenceDescriptor();
9856-
RecurKind RK = RdxDesc.getRecurrenceKind();
9850+
RecurKind RK = ReductionPhi->getRecurrenceKind();
98579851
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
98589852
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9859-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9860-
"start value from ComputeAnyOfResult must match");
9861-
98629853
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
98639854
// start value; compare the final value from the main vector loop
98649855
// to the start value.
@@ -9867,9 +9858,6 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98679858
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
98689859
} else if (RecurrenceDescriptor::isFindIVRecurrenceKind(RK)) {
98699860
Value *StartV = getStartValueFromReductionResult(RdxResult);
9870-
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
9871-
"start value from ComputeFinIVResult must match");
9872-
98739861
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
98749862
EPI.MainLoopIterationCountCheck);
98759863

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,8 +2191,8 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
21912191
/// operand.
21922192
class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21932193
public VPUnrollPartAccessor<2> {
2194-
/// Descriptor for the reduction.
2195-
const RecurrenceDescriptor &RdxDesc;
2194+
/// The recurrence kind of the reduction.
2195+
const RecurKind Kind;
21962196

21972197
/// The phi is part of an in-loop reduction.
21982198
bool IsInLoop;
@@ -2205,22 +2205,20 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22052205
unsigned VFScaleFactor = 1;
22062206

22072207
public:
2208-
/// Create a new VPReductionPHIRecipe for the reduction \p Phi described by \p
2209-
/// RdxDesc.
2210-
VPReductionPHIRecipe(PHINode *Phi, const RecurrenceDescriptor &RdxDesc,
2211-
VPValue &Start, bool IsInLoop = false,
2212-
bool IsOrdered = false, unsigned VFScaleFactor = 1)
2213-
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start),
2214-
RdxDesc(RdxDesc), IsInLoop(IsInLoop), IsOrdered(IsOrdered),
2215-
VFScaleFactor(VFScaleFactor) {
2208+
/// Create a new VPReductionPHIRecipe for the reduction \p Phi.
2209+
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2210+
bool IsInLoop = false, bool IsOrdered = false,
2211+
unsigned VFScaleFactor = 1)
2212+
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2213+
IsInLoop(IsInLoop), IsOrdered(IsOrdered), VFScaleFactor(VFScaleFactor) {
22162214
assert((!IsOrdered || IsInLoop) && "IsOrdered requires IsInLoop");
22172215
}
22182216

22192217
~VPReductionPHIRecipe() override = default;
22202218

22212219
VPReductionPHIRecipe *clone() override {
22222220
auto *R = new VPReductionPHIRecipe(
2223-
dyn_cast_or_null<PHINode>(getUnderlyingValue()), RdxDesc,
2221+
dyn_cast_or_null<PHINode>(getUnderlyingValue()), getRecurrenceKind(),
22242222
*getOperand(0), IsInLoop, IsOrdered, VFScaleFactor);
22252223
R->addOperand(getBackedgeValue());
22262224
return R;
@@ -2240,9 +2238,8 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
22402238
VPSlotTracker &SlotTracker) const override;
22412239
#endif
22422240

2243-
const RecurrenceDescriptor &getRecurrenceDescriptor() const {
2244-
return RdxDesc;
2245-
}
2241+
/// Returns the recurrence kind of the reduction.
2242+
RecurKind getRecurrenceKind() const { return Kind; }
22462243

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