Skip to content

Commit 567b317

Browse files
authored
[VPlan] Construct initial once and pass clones to tryToBuildVPlan (NFC). (llvm#141363)
Update to only build an initial, plain-CFG VPlan once, and then transform & optimize clones. This requires changes to ::clone() for VPInstruction and VPWidenPHIRecipe to allow for proper cloning of the recipes in the initial VPlan. PR: llvm#141363
1 parent 435d8b1 commit 567b317

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,10 @@ class LoopVectorizationPlanner {
519519
/// \p Range's largest included VF is restricted to the maximum VF the
520520
/// returned VPlan is valid for. If no VPlan can be built for the input range,
521521
/// set the largest included VF to the maximum VF for which no plan could be
522-
/// built.
523-
VPlanPtr tryToBuildVPlanWithVPRecipes(VFRange &Range, LoopVersioning *LVer);
522+
/// built. Each VPlan is built starting from a copy of \p InitialPlan, which
523+
/// is a plain CFG VPlan wrapping the original scalar loop.
524+
VPlanPtr tryToBuildVPlanWithVPRecipes(VPlanPtr InitialPlan, VFRange &Range,
525+
LoopVersioning *LVer);
524526

525527
/// Build VPlans for power-of-2 VF's between \p MinVF and \p MaxVF inclusive,
526528
/// according to the information gathered by Legal when it checked if it is

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8717,9 +8717,11 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
87178717
}
87188718

87198719
auto MaxVFTimes2 = MaxVF * 2;
8720+
auto VPlan0 = VPlanTransforms::buildPlainCFG(OrigLoop, *LI);
87208721
for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {
87218722
VFRange SubRange = {VF, MaxVFTimes2};
8722-
if (auto Plan = tryToBuildVPlanWithVPRecipes(SubRange, &LVer)) {
8723+
if (auto Plan = tryToBuildVPlanWithVPRecipes(
8724+
std::unique_ptr<VPlan>(VPlan0->duplicate()), SubRange, &LVer)) {
87238725
bool HasScalarVF = Plan->hasScalarVFOnly();
87248726
// Now optimize the initial VPlan.
87258727
if (!HasScalarVF)
@@ -8980,9 +8982,8 @@ static void addExitUsersForFirstOrderRecurrences(
89808982
}
89818983
}
89828984

8983-
VPlanPtr
8984-
LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range,
8985-
LoopVersioning *LVer) {
8985+
VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
8986+
VPlanPtr Plan, VFRange &Range, LoopVersioning *LVer) {
89868987

89878988
using namespace llvm::VPlanPatternMatch;
89888989
SmallPtrSet<const InterleaveGroup<Instruction> *, 1> InterleaveGroups;
@@ -9004,7 +9005,6 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range,
90049005
return !CM.requiresScalarEpilogue(VF.isVector());
90059006
},
90069007
Range);
9007-
auto Plan = VPlanTransforms::buildPlainCFG(OrigLoop, *LI);
90089008
VPlanTransforms::prepareForVectorization(
90099009
*Plan, Legal->getWidestInductionType(), PSE, RequiresScalarEpilogueCheck,
90109010
CM.foldTailByMasking(), OrigLoop,

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,10 @@ class VPInstruction : public VPRecipeWithIRFlags,
972972

973973
VPInstruction *clone() override {
974974
SmallVector<VPValue *, 2> Operands(operands());
975-
return new VPInstruction(Opcode, Operands, *this, getDebugLoc(), Name);
975+
auto *New = new VPInstruction(Opcode, Operands, *this, getDebugLoc(), Name);
976+
if (getUnderlyingValue())
977+
New->setUnderlyingValue(getUnderlyingInstr());
978+
return New;
976979
}
977980

978981
unsigned getOpcode() const { return Opcode; }
@@ -2090,7 +2093,11 @@ class VPWidenPHIRecipe : public VPSingleDefRecipe, public VPPhiAccessors {
20902093
}
20912094

20922095
VPWidenPHIRecipe *clone() override {
2093-
llvm_unreachable("cloning not implemented yet");
2096+
auto *C = new VPWidenPHIRecipe(cast<PHINode>(getUnderlyingValue()),
2097+
getOperand(0), getDebugLoc(), Name);
2098+
for (VPValue *Op : make_range(std::next(op_begin()), op_end()))
2099+
C->addOperand(Op);
2100+
return C;
20942101
}
20952102

20962103
~VPWidenPHIRecipe() override = default;

0 commit comments

Comments
 (0)