Skip to content

Commit 5861191

Browse files
committed
Clone once
1 parent a06af46 commit 5861191

File tree

4 files changed

+59
-35
lines changed

4 files changed

+59
-35
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ class LoopVectorizationPlanner {
507507
/// returned VPlan is valid for. If no VPlan can be built for the input range,
508508
/// set the largest included VF to the maximum VF for which no plan could be
509509
/// built.
510-
VPlanPtr tryToBuildVPlanWithVPRecipes(VFRange &Range);
510+
VPlanPtr tryToBuildVPlanWithVPRecipes(VPlanPtr InitialPlan, VFRange &Range);
511511

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

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8786,10 +8786,13 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
87868786
ElementCount MaxVF) {
87878787
assert(OrigLoop->isInnermost() && "Inner loop expected.");
87888788

8789+
auto VPlan0 = VPlanTransforms::buildPlainCFG(OrigLoop, *LI);
8790+
87898791
auto MaxVFTimes2 = MaxVF * 2;
87908792
for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {
87918793
VFRange SubRange = {VF, MaxVFTimes2};
8792-
if (auto Plan = tryToBuildVPlanWithVPRecipes(SubRange)) {
8794+
if (auto Plan = tryToBuildVPlanWithVPRecipes(
8795+
std::unique_ptr<VPlan>(VPlan0->duplicate()), SubRange)) {
87938796
bool HasScalarVF = Plan->hasScalarVFOnly();
87948797
// Now optimize the initial VPlan.
87958798
if (!HasScalarVF)
@@ -9093,7 +9096,8 @@ static void addExitUsersForFirstOrderRecurrences(
90939096
}
90949097

90959098
VPlanPtr
9096-
LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
9099+
LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VPlanPtr Plan,
9100+
VFRange &Range) {
90979101

90989102
using namespace llvm::VPlanPatternMatch;
90999103
SmallPtrSet<const InterleaveGroup<Instruction> *, 1> InterleaveGroups;
@@ -9115,7 +9119,6 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
91159119
return !CM.requiresScalarEpilogue(VF.isVector());
91169120
},
91179121
Range);
9118-
auto Plan = VPlanTransforms::buildPlainCFG(OrigLoop, *LI);
91199122
VPlanTransforms::introduceRegions(*Plan, Legal->getWidestInductionType(), PSE,
91209123
RequiresScalarEpilogueCheck,
91219124
CM.foldTailByMasking(), OrigLoop);

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ VPBasicBlock *VPBlockBase::getEntryBasicBlock() {
173173
}
174174

175175
void VPBlockBase::setPlan(VPlan *ParentPlan) {
176-
assert(ParentPlan->getEntry() == this && "Can only set plan on its entry.");
177176
Plan = ParentPlan;
178177
}
179178

@@ -1184,11 +1183,16 @@ VPlan *VPlan::duplicate() {
11841183
const auto &[NewEntry, __] = cloneFrom(Entry);
11851184

11861185
BasicBlock *ScalarHeaderIRBB = getScalarHeader()->getIRBasicBlock();
1187-
VPIRBasicBlock *NewScalarHeader = cast<VPIRBasicBlock>(*find_if(
1188-
vp_depth_first_shallow(NewEntry), [ScalarHeaderIRBB](VPBlockBase *VPB) {
1189-
auto *VPIRBB = dyn_cast<VPIRBasicBlock>(VPB);
1190-
return VPIRBB && VPIRBB->getIRBasicBlock() == ScalarHeaderIRBB;
1191-
}));
1186+
VPIRBasicBlock *NewScalarHeader = nullptr;
1187+
if (getScalarHeader()->getNumPredecessors() == 0) {
1188+
NewScalarHeader = getScalarHeader()->clone();
1189+
} else {
1190+
NewScalarHeader = cast<VPIRBasicBlock>(*find_if(
1191+
vp_depth_first_shallow(NewEntry), [ScalarHeaderIRBB](VPBlockBase *VPB) {
1192+
auto *VPIRBB = dyn_cast<VPIRBasicBlock>(VPB);
1193+
return VPIRBB && VPIRBB->getIRBasicBlock() == ScalarHeaderIRBB;
1194+
}));
1195+
}
11921196
// Create VPlan, clone live-ins and remap operands in the cloned blocks.
11931197
auto *NewPlan = new VPlan(cast<VPBasicBlock>(NewEntry), NewScalarHeader);
11941198
DenseMap<VPValue *, VPValue *> Old2NewVPValues;
@@ -1203,8 +1207,7 @@ VPlan *VPlan::duplicate() {
12031207
NewPlan->BackedgeTakenCount = new VPValue();
12041208
Old2NewVPValues[BackedgeTakenCount] = NewPlan->BackedgeTakenCount;
12051209
}
1206-
assert(TripCount && "trip count must be set");
1207-
if (TripCount->isLiveIn())
1210+
if (TripCount && TripCount->isLiveIn())
12081211
Old2NewVPValues[TripCount] =
12091212
NewPlan->getOrAddLiveIn(TripCount->getLiveInIRValue());
12101213
// else NewTripCount will be created and inserted into Old2NewVPValues when
@@ -1217,23 +1220,32 @@ VPlan *VPlan::duplicate() {
12171220
NewPlan->UFs = UFs;
12181221
// TODO: Adjust names.
12191222
NewPlan->Name = Name;
1220-
assert(Old2NewVPValues.contains(TripCount) &&
1221-
"TripCount must have been added to Old2NewVPValues");
1222-
NewPlan->TripCount = Old2NewVPValues[TripCount];
1223+
if (TripCount) {
1224+
assert(Old2NewVPValues.contains(TripCount) &&
1225+
"TripCount must have been added to Old2NewVPValues");
1226+
NewPlan->TripCount = Old2NewVPValues[TripCount];
1227+
}
12231228

12241229
// Transfer all cloned blocks (the second half of all current blocks) from
12251230
// current to new VPlan.
12261231
unsigned NumBlocksAfterCloning = CreatedBlocks.size();
12271232
for (unsigned I :
1228-
seq<unsigned>(NumBlocksBeforeCloning, NumBlocksAfterCloning))
1233+
seq<unsigned>(NumBlocksBeforeCloning, NumBlocksAfterCloning)) {
1234+
this->CreatedBlocks[I]->setPlan(NewPlan);
12291235
NewPlan->CreatedBlocks.push_back(this->CreatedBlocks[I]);
1236+
}
12301237
CreatedBlocks.truncate(NumBlocksBeforeCloning);
12311238

1239+
for (VPBlockBase *VPB : NewPlan->CreatedBlocks) {
1240+
if (VPB->getNumSuccessors() == 0 && isa<VPIRBasicBlock>(VPB) &&
1241+
VPB != NewScalarHeader)
1242+
NewPlan->ExitBlocks.push_back(cast<VPIRBasicBlock>(VPB));
1243+
}
12321244
return NewPlan;
12331245
}
12341246

12351247
VPIRBasicBlock *VPlan::createEmptyVPIRBasicBlock(BasicBlock *IRBB) {
1236-
auto *VPIRBB = new VPIRBasicBlock(IRBB);
1248+
auto *VPIRBB = new VPIRBasicBlock(IRBB, this);
12371249
CreatedBlocks.push_back(VPIRBB);
12381250
return VPIRBB;
12391251
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ class VPBlockBase {
145145
}
146146

147147
protected:
148-
VPBlockBase(const unsigned char SC, const std::string &N)
149-
: SubclassID(SC), Name(N) {}
148+
VPBlockBase(const unsigned char SC, const std::string &N, VPlan *ParentPlan)
149+
: SubclassID(SC), Name(N), Plan(ParentPlan) {}
150150

151151
public:
152152
/// An enumeration for keeping track of the concrete subclass of VPBlockBase
@@ -959,6 +959,8 @@ class VPInstruction : public VPRecipeWithIRFlags,
959959
VPInstruction *clone() override {
960960
SmallVector<VPValue *, 2> Operands(operands());
961961
auto *New = new VPInstruction(Opcode, Operands, getDebugLoc(), Name);
962+
if (getUnderlyingValue())
963+
New->setUnderlyingValue(getUnderlyingInstr());
962964
New->transferFlags(*this);
963965
return New;
964966
}
@@ -1932,7 +1934,11 @@ class VPWidenPHIRecipe : public VPSingleDefRecipe {
19321934
}
19331935

19341936
VPWidenPHIRecipe *clone() override {
1935-
llvm_unreachable("cloning not implemented yet");
1937+
auto *C = new VPWidenPHIRecipe(cast<PHINode>(getUnderlyingValue()),
1938+
getOperand(0), getDebugLoc(), Name);
1939+
for (VPValue *Op : make_range(std::next(op_begin()), op_end()))
1940+
C->addOperand(Op);
1941+
return C;
19361942
}
19371943

19381944
~VPWidenPHIRecipe() override = default;
@@ -3140,8 +3146,9 @@ class VPBasicBlock : public VPBlockBase {
31403146
friend class VPlan;
31413147

31423148
/// Use VPlan::createVPBasicBlock to create VPBasicBlocks.
3143-
VPBasicBlock(const Twine &Name = "", VPRecipeBase *Recipe = nullptr)
3144-
: VPBlockBase(VPBasicBlockSC, Name.str()) {
3149+
VPBasicBlock(VPlan *ParentPlan, const Twine &Name = "",
3150+
VPRecipeBase *Recipe = nullptr)
3151+
: VPBlockBase(VPBasicBlockSC, Name.str(), ParentPlan) {
31453152
if (Recipe)
31463153
appendRecipe(Recipe);
31473154
}
@@ -3153,8 +3160,9 @@ class VPBasicBlock : public VPBlockBase {
31533160
/// The VPRecipes held in the order of output instructions to generate.
31543161
RecipeListTy Recipes;
31553162

3156-
VPBasicBlock(const unsigned char BlockSC, const Twine &Name = "")
3157-
: VPBlockBase(BlockSC, Name.str()) {}
3163+
VPBasicBlock(const unsigned char BlockSC, VPlan *ParentPlan,
3164+
const Twine &Name = "")
3165+
: VPBlockBase(BlockSC, Name.str(), ParentPlan) {}
31583166

31593167
public:
31603168
~VPBasicBlock() override {
@@ -3292,8 +3300,8 @@ class VPIRBasicBlock : public VPBasicBlock {
32923300
BasicBlock *IRBB;
32933301

32943302
/// Use VPlan::createVPIRBasicBlock to create VPIRBasicBlocks.
3295-
VPIRBasicBlock(BasicBlock *IRBB)
3296-
: VPBasicBlock(VPIRBasicBlockSC,
3303+
VPIRBasicBlock(BasicBlock *IRBB, VPlan *ParentPlan)
3304+
: VPBasicBlock(VPIRBasicBlockSC, ParentPlan,
32973305
(Twine("ir-bb<") + IRBB->getName() + Twine(">")).str()),
32983306
IRBB(IRBB) {}
32993307

@@ -3336,18 +3344,19 @@ class VPRegionBlock : public VPBlockBase {
33363344
bool IsReplicator;
33373345

33383346
/// Use VPlan::createVPRegionBlock to create VPRegionBlocks.
3339-
VPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exiting,
3347+
VPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exiting, VPlan *ParentPlan,
33403348
const std::string &Name = "", bool IsReplicator = false)
3341-
: VPBlockBase(VPRegionBlockSC, Name), Entry(Entry), Exiting(Exiting),
3342-
IsReplicator(IsReplicator) {
3349+
: VPBlockBase(VPRegionBlockSC, Name, ParentPlan), Entry(Entry),
3350+
Exiting(Exiting), IsReplicator(IsReplicator) {
33433351
assert(Entry->getPredecessors().empty() && "Entry block has predecessors.");
33443352
assert(Exiting->getSuccessors().empty() && "Exit block has successors.");
33453353
Entry->setParent(this);
33463354
Exiting->setParent(this);
33473355
}
3348-
VPRegionBlock(const std::string &Name = "", bool IsReplicator = false)
3349-
: VPBlockBase(VPRegionBlockSC, Name), Entry(nullptr), Exiting(nullptr),
3350-
IsReplicator(IsReplicator) {}
3356+
VPRegionBlock(VPlan *ParentPlan, const std::string &Name = "",
3357+
bool IsReplicator = false)
3358+
: VPBlockBase(VPRegionBlockSC, Name, ParentPlan), Entry(nullptr),
3359+
Exiting(nullptr), IsReplicator(IsReplicator) {}
33513360

33523361
public:
33533362
~VPRegionBlock() override {}
@@ -3740,7 +3749,7 @@ class VPlan {
37403749
/// VPlan is destroyed.
37413750
VPBasicBlock *createVPBasicBlock(const Twine &Name,
37423751
VPRecipeBase *Recipe = nullptr) {
3743-
auto *VPB = new VPBasicBlock(Name, Recipe);
3752+
auto *VPB = new VPBasicBlock(this, Name, Recipe);
37443753
CreatedBlocks.push_back(VPB);
37453754
return VPB;
37463755
}
@@ -3751,7 +3760,7 @@ class VPlan {
37513760
VPRegionBlock *createVPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exiting,
37523761
const std::string &Name = "",
37533762
bool IsReplicator = false) {
3754-
auto *VPB = new VPRegionBlock(Entry, Exiting, Name, IsReplicator);
3763+
auto *VPB = new VPRegionBlock(Entry, Exiting, this, Name, IsReplicator);
37553764
CreatedBlocks.push_back(VPB);
37563765
return VPB;
37573766
}
@@ -3762,7 +3771,7 @@ class VPlan {
37623771
/// destroyed.
37633772
VPRegionBlock *createVPRegionBlock(const std::string &Name = "",
37643773
bool IsReplicator = false) {
3765-
auto *VPB = new VPRegionBlock(Name, IsReplicator);
3774+
auto *VPB = new VPRegionBlock(this, Name, IsReplicator);
37663775
CreatedBlocks.push_back(VPB);
37673776
return VPB;
37683777
}

0 commit comments

Comments
 (0)