Skip to content

Commit bcbbe2c

Browse files
committed
[VPlan] Pass backedge value directly to FOR and reduction phis (NFC).
Pass backedge values directly to VPFirstOrderRecurrencePHIRecipe and VPReductionPHIRecipe directly, as they must be provided and availbale. Split off from #168291.
1 parent 4cdec92 commit bcbbe2c

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8234,11 +8234,11 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
82348234
if ((Recipe = tryToOptimizeInductionPHI(PhiR)))
82358235
return Recipe;
82368236

8237-
VPHeaderPHIRecipe *PhiRecipe = nullptr;
82388237
assert((Legal->isReductionVariable(Phi) ||
82398238
Legal->isFixedOrderRecurrence(Phi)) &&
82408239
"can only widen reductions and fixed-order recurrences here");
82418240
VPValue *StartV = R->getOperand(0);
8241+
VPValue *BackedgeValue = R->getOperand(1);
82428242
if (Legal->isReductionVariable(Phi)) {
82438243
const RecurrenceDescriptor &RdxDesc = Legal->getRecurrenceDescriptor(Phi);
82448244
assert(RdxDesc.getRecurrenceStartValue() ==
@@ -8250,22 +8250,20 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
82508250
unsigned ScaleFactor =
82518251
getScalingForReduction(RdxDesc.getLoopExitInstr()).value_or(1);
82528252

8253-
PhiRecipe = new VPReductionPHIRecipe(
8254-
Phi, RdxDesc.getRecurrenceKind(), *StartV,
8253+
return new VPReductionPHIRecipe(
8254+
Phi, RdxDesc.getRecurrenceKind(), *StartV, *BackedgeValue,
82558255
getReductionStyle(UseInLoopReduction, UseOrderedReductions,
82568256
ScaleFactor),
82578257
RdxDesc.hasUsesOutsideReductionChain());
8258-
} else {
8259-
// TODO: Currently fixed-order recurrences are modeled as chains of
8260-
// first-order recurrences. If there are no users of the intermediate
8261-
// recurrences in the chain, the fixed order recurrence should be modeled
8262-
// directly, enabling more efficient codegen.
8263-
PhiRecipe = new VPFirstOrderRecurrencePHIRecipe(Phi, *StartV);
82648258
}
8265-
// Add backedge value.
8266-
PhiRecipe->addOperand(R->getOperand(1));
8267-
return PhiRecipe;
8259+
8260+
// TODO: Currently fixed-order recurrences are modeled as chains of
8261+
// first-order recurrences. If there are no users of the intermediate
8262+
// recurrences in the chain, the fixed order recurrence should be modeled
8263+
// directly, enabling more efficient codegen.
8264+
return new VPFirstOrderRecurrencePHIRecipe(Phi, *StartV, *BackedgeValue);
82688265
}
8266+
82698267
assert(!R->isPhi() && "only VPPhi nodes expected at this point");
82708268

82718269
auto *VPI = cast<VPInstruction>(R);

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,14 +2366,17 @@ class LLVM_ABI_FOR_TEST VPWidenPHIRecipe : public VPSingleDefRecipe,
23662366
/// first operand of the recipe and the incoming value from the backedge is the
23672367
/// second operand.
23682368
struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
2369-
VPFirstOrderRecurrencePHIRecipe(PHINode *Phi, VPValue &Start)
2370-
: VPHeaderPHIRecipe(VPDef::VPFirstOrderRecurrencePHISC, Phi, &Start) {}
2369+
VPFirstOrderRecurrencePHIRecipe(PHINode *Phi, VPValue &Start,
2370+
VPValue &BackedgeValue)
2371+
: VPHeaderPHIRecipe(VPDef::VPFirstOrderRecurrencePHISC, Phi, &Start) {
2372+
addOperand(&BackedgeValue);
2373+
}
23712374

23722375
VP_CLASSOF_IMPL(VPDef::VPFirstOrderRecurrencePHISC)
23732376

23742377
VPFirstOrderRecurrencePHIRecipe *clone() override {
23752378
return new VPFirstOrderRecurrencePHIRecipe(
2376-
cast<PHINode>(getUnderlyingInstr()), *getOperand(0));
2379+
cast<PHINode>(getUnderlyingInstr()), *getOperand(0), *getOperand(1));
23772380
}
23782381

23792382
void execute(VPTransformState &State) override;
@@ -2439,20 +2442,21 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
24392442
public:
24402443
/// Create a new VPReductionPHIRecipe for the reduction \p Phi.
24412444
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start,
2442-
ReductionStyle Style,
2445+
VPValue &BackedgeValue, ReductionStyle Style,
24432446
bool HasUsesOutsideReductionChain = false)
24442447
: VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
24452448
Style(Style),
2446-
HasUsesOutsideReductionChain(HasUsesOutsideReductionChain) {}
2449+
HasUsesOutsideReductionChain(HasUsesOutsideReductionChain) {
2450+
addOperand(&BackedgeValue);
2451+
}
24472452

24482453
~VPReductionPHIRecipe() override = default;
24492454

24502455
VPReductionPHIRecipe *clone() override {
2451-
auto *R = new VPReductionPHIRecipe(
2456+
return new VPReductionPHIRecipe(
24522457
dyn_cast_or_null<PHINode>(getUnderlyingValue()), getRecurrenceKind(),
2453-
*getOperand(0), Style, HasUsesOutsideReductionChain);
2454-
R->addOperand(getBackedgeValue());
2455-
return R;
2458+
*getOperand(0), *getBackedgeValue(), Style,
2459+
HasUsesOutsideReductionChain);
24562460
}
24572461

24582462
VP_CLASSOF_IMPL(VPDef::VPReductionPHISC)

0 commit comments

Comments
 (0)