Skip to content

Commit 9333b97

Browse files
committed
[VPlan] Replace AlsoPack field with shouldPack() method (NFC).
There is no need to update the AlsoPack field when creating VPReplicateRecipes. It can be easily computed based on the VP def-use chains when it is needed. Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D143864
1 parent da94a2b commit 9333b97

File tree

4 files changed

+20
-35
lines changed

4 files changed

+20
-35
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8583,21 +8583,6 @@ VPBasicBlock *VPRecipeBuilder::handleReplication(Instruction *I, VFRange &Range,
85838583
auto *Recipe = new VPReplicateRecipe(I, Plan.mapToVPValues(I->operands()),
85848584
IsUniform, IsPredicated);
85858585

8586-
// Find if I uses a predicated instruction. If so, it will use its scalar
8587-
// value. Avoid hoisting the insert-element which packs the scalar value into
8588-
// a vector value, as that happens iff all users use the vector value.
8589-
for (VPValue *Op : Recipe->operands()) {
8590-
auto *PredR =
8591-
dyn_cast_or_null<VPPredInstPHIRecipe>(Op->getDefiningRecipe());
8592-
if (!PredR)
8593-
continue;
8594-
auto *RepR = cast<VPReplicateRecipe>(
8595-
PredR->getOperand(0)->getDefiningRecipe());
8596-
assert(RepR->isPredicated() &&
8597-
"expected Replicate recipe to be predicated");
8598-
RepR->setAlsoPack(false);
8599-
}
8600-
86018586
// Finalize the recipe for Instr, first if it is not predicated.
86028587
if (!IsPredicated) {
86038588
LLVM_DEBUG(dbgs() << "LV: Scalarizing:" << *I << "\n");
@@ -9585,7 +9570,7 @@ void VPReplicateRecipe::execute(VPTransformState &State) {
95859570
State.ILV->scalarizeInstruction(UI, this, *State.Instance,
95869571
IsPredicated, State);
95879572
// Insert scalar instance packing it into a vector.
9588-
if (AlsoPack && State.VF.isVector()) {
9573+
if (State.VF.isVector() && shouldPack()) {
95899574
// If we're constructing lane 0, initialize to start from poison.
95909575
if (State.Instance->Lane.isFirstLane()) {
95919576
assert(!State.VF.isScalable() && "VF is assumed to be non scalable.");

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ class VPRecipeBuilder {
166166
/// Build a VPReplicationRecipe for \p I and enclose it within a Region if it
167167
/// is predicated. \return \p VPBB augmented with this new recipe if \p I is
168168
/// not predicated, otherwise \return a new VPBasicBlock that succeeds the new
169-
/// Region. Update the packing decision of predicated instructions if they
170-
/// feed \p I. Range.End may be decreased to ensure same recipe behavior from
171-
/// \p Range.Start to \p Range.End.
169+
/// Region. Range.End may be decreased to ensure same recipe behavior from \p
170+
/// Range.Start to \p Range.End.
172171
VPBasicBlock *handleReplication(Instruction *I, VFRange &Range,
173172
VPBasicBlock *VPBB, VPlan &Plan);
174173

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,22 +1503,12 @@ class VPReplicateRecipe : public VPRecipeBase, public VPValue {
15031503
/// Indicator if the replicas are also predicated.
15041504
bool IsPredicated;
15051505

1506-
/// Indicator if the scalar values should also be packed into a vector.
1507-
bool AlsoPack;
1508-
15091506
public:
15101507
template <typename IterT>
15111508
VPReplicateRecipe(Instruction *I, iterator_range<IterT> Operands,
15121509
bool IsUniform, bool IsPredicated = false)
15131510
: VPRecipeBase(VPDef::VPReplicateSC, Operands), VPValue(this, I),
1514-
IsUniform(IsUniform), IsPredicated(IsPredicated) {
1515-
// Retain the previous behavior of predicateInstructions(), where an
1516-
// insert-element of a predicated instruction got hoisted into the
1517-
// predicated basic block iff it was its only user. This is achieved by
1518-
// having predicated instructions also pack their values into a vector by
1519-
// default unless they have a replicated user which uses their scalar value.
1520-
AlsoPack = IsPredicated && !I->use_empty();
1521-
}
1511+
IsUniform(IsUniform), IsPredicated(IsPredicated) {}
15221512

15231513
~VPReplicateRecipe() override = default;
15241514

@@ -1529,8 +1519,6 @@ class VPReplicateRecipe : public VPRecipeBase, public VPValue {
15291519
/// the \p State.
15301520
void execute(VPTransformState &State) override;
15311521

1532-
void setAlsoPack(bool Pack) { AlsoPack = Pack; }
1533-
15341522
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
15351523
/// Print the recipe.
15361524
void print(raw_ostream &O, const Twine &Indent,
@@ -1539,8 +1527,6 @@ class VPReplicateRecipe : public VPRecipeBase, public VPValue {
15391527

15401528
bool isUniform() const { return IsUniform; }
15411529

1542-
bool isPacked() const { return AlsoPack; }
1543-
15441530
bool isPredicated() const { return IsPredicated; }
15451531

15461532
/// Returns true if the recipe only uses the first lane of operand \p Op.
@@ -1556,6 +1542,11 @@ class VPReplicateRecipe : public VPRecipeBase, public VPValue {
15561542
"Op must be an operand of the recipe");
15571543
return true;
15581544
}
1545+
1546+
/// Returns true if the recipe is used by a widened recipe via an intervening
1547+
/// VPPredInstPHIRecipe. In this case, the scalar values should also be packed
1548+
/// in a vector.
1549+
bool shouldPack() const;
15591550
};
15601551

15611552
/// A recipe for generating conditional branches on the bits of a mask.

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,16 @@ void VPReductionRecipe::print(raw_ostream &O, const Twine &Indent,
920920
"outside of loop)";
921921
}
922922

923+
bool VPReplicateRecipe::shouldPack() const {
924+
// Find if the recipe is used by a widened recipe via an intervening
925+
// VPPredInstPHIRecipe. In this case, also pack the scalar values in a vector.
926+
return any_of(users(), [](const VPUser *U) {
927+
if (auto *PredR = dyn_cast<VPPredInstPHIRecipe>(U))
928+
return any_of(PredR->users(),
929+
[](const VPUser *U) { return !isa<VPReplicateRecipe>(U); });
930+
return false;
931+
});
932+
}
923933
void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent,
924934
VPSlotTracker &SlotTracker) const {
925935
O << Indent << (IsUniform ? "CLONE " : "REPLICATE ");
@@ -940,7 +950,7 @@ void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent,
940950
printOperands(O, SlotTracker);
941951
}
942952

943-
if (AlsoPack)
953+
if (shouldPack())
944954
O << " (S->V)";
945955
}
946956
#endif

0 commit comments

Comments
 (0)