Skip to content

Commit 8578b6e

Browse files
committed
[VPlan] Store VPlan directly in VPRecipeBuilder (NFCI).
Instead of passing VPlan in a number of places, just store it directly in VPRecipeBuilder. A single instance is only used for a single VPlan. This simplifies the code and was suggested by @nikolaypanchenko in #84464.
1 parent 20f5bcf commit 8578b6e

File tree

2 files changed

+83
-84
lines changed

2 files changed

+83
-84
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7898,8 +7898,7 @@ void LoopVectorizationPlanner::buildVPlans(ElementCount MinVF,
78987898
}
78997899
}
79007900

7901-
VPValue *VPRecipeBuilder::createEdgeMask(BasicBlock *Src, BasicBlock *Dst,
7902-
VPlan &Plan) {
7901+
VPValue *VPRecipeBuilder::createEdgeMask(BasicBlock *Src, BasicBlock *Dst) {
79037902
assert(is_contained(predecessors(Dst), Src) && "Invalid edge");
79047903

79057904
// Look for cached value.
@@ -7954,7 +7953,7 @@ VPValue *VPRecipeBuilder::getEdgeMask(BasicBlock *Src, BasicBlock *Dst) const {
79547953
return ECEntryIt->second;
79557954
}
79567955

7957-
void VPRecipeBuilder::createHeaderMask(VPlan &Plan) {
7956+
void VPRecipeBuilder::createHeaderMask() {
79587957
BasicBlock *Header = OrigLoop->getHeader();
79597958

79607959
// When not folding the tail, use nullptr to model all-true mask.
@@ -7989,7 +7988,7 @@ VPValue *VPRecipeBuilder::getBlockInMask(BasicBlock *BB) const {
79897988
return BCEntryIt->second;
79907989
}
79917990

7992-
void VPRecipeBuilder::createBlockInMask(BasicBlock *BB, VPlan &Plan) {
7991+
void VPRecipeBuilder::createBlockInMask(BasicBlock *BB) {
79937992
assert(OrigLoop->contains(BB) && "Block is not a part of a loop");
79947993
assert(BlockMaskCache.count(BB) == 0 && "Mask for block already computed");
79957994
assert(OrigLoop->getHeader() != BB &&
@@ -8000,7 +7999,7 @@ void VPRecipeBuilder::createBlockInMask(BasicBlock *BB, VPlan &Plan) {
80007999
VPValue *BlockMask = nullptr;
80018000
// This is the block mask. We OR all incoming edges.
80028001
for (auto *Predecessor : predecessors(BB)) {
8003-
VPValue *EdgeMask = createEdgeMask(Predecessor, BB, Plan);
8002+
VPValue *EdgeMask = createEdgeMask(Predecessor, BB);
80048003
if (!EdgeMask) { // Mask of predecessor is all-one so mask of block is too.
80058004
BlockMaskCache[BB] = EdgeMask;
80068005
return;
@@ -8019,7 +8018,7 @@ void VPRecipeBuilder::createBlockInMask(BasicBlock *BB, VPlan &Plan) {
80198018

80208019
VPWidenMemoryInstructionRecipe *
80218020
VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
8022-
VFRange &Range, VPlanPtr &Plan) {
8021+
VFRange &Range) {
80238022
assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
80248023
"Must be called with either a load or store");
80258024

@@ -8092,7 +8091,7 @@ createWidenInductionRecipes(PHINode *Phi, Instruction *PhiOrTrunc,
80928091
}
80938092

80948093
VPHeaderPHIRecipe *VPRecipeBuilder::tryToOptimizeInductionPHI(
8095-
PHINode *Phi, ArrayRef<VPValue *> Operands, VPlan &Plan, VFRange &Range) {
8094+
PHINode *Phi, ArrayRef<VPValue *> Operands, VFRange &Range) {
80968095

80978096
// Check if this is an integer or fp induction. If so, build the recipe that
80988097
// produces its scalar and vector values.
@@ -8116,7 +8115,7 @@ VPHeaderPHIRecipe *VPRecipeBuilder::tryToOptimizeInductionPHI(
81168115
}
81178116

81188117
VPWidenIntOrFpInductionRecipe *VPRecipeBuilder::tryToOptimizeInductionTruncate(
8119-
TruncInst *I, ArrayRef<VPValue *> Operands, VFRange &Range, VPlan &Plan) {
8118+
TruncInst *I, ArrayRef<VPValue *> Operands, VFRange &Range) {
81208119
// Optimize the special case where the source is a constant integer
81218120
// induction variable. Notice that we can only optimize the 'trunc' case
81228121
// because (a) FP conversions lose precision, (b) sext/zext may wrap, and
@@ -8144,8 +8143,7 @@ VPWidenIntOrFpInductionRecipe *VPRecipeBuilder::tryToOptimizeInductionTruncate(
81448143
}
81458144

81468145
VPBlendRecipe *VPRecipeBuilder::tryToBlend(PHINode *Phi,
8147-
ArrayRef<VPValue *> Operands,
8148-
VPlanPtr &Plan) {
8146+
ArrayRef<VPValue *> Operands) {
81498147
unsigned NumIncoming = Phi->getNumIncomingValues();
81508148

81518149
// We know that all PHIs in non-header blocks are converted into selects, so
@@ -8158,7 +8156,7 @@ VPBlendRecipe *VPRecipeBuilder::tryToBlend(PHINode *Phi,
81588156
for (unsigned In = 0; In < NumIncoming; In++) {
81598157
OperandsWithMask.push_back(Operands[In]);
81608158
VPValue *EdgeMask =
8161-
createEdgeMask(Phi->getIncomingBlock(In), Phi->getParent(), *Plan);
8159+
createEdgeMask(Phi->getIncomingBlock(In), Phi->getParent());
81628160
if (!EdgeMask) {
81638161
assert(In == 0 && "Both null and non-null edge masks found");
81648162
assert(all_equal(Operands) &&
@@ -8172,8 +8170,7 @@ VPBlendRecipe *VPRecipeBuilder::tryToBlend(PHINode *Phi,
81728170

81738171
VPWidenCallRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
81748172
ArrayRef<VPValue *> Operands,
8175-
VFRange &Range,
8176-
VPlanPtr &Plan) {
8173+
VFRange &Range) {
81778174
bool IsPredicated = LoopVectorizationPlanner::getDecisionAndClampRange(
81788175
[this, CI](ElementCount VF) {
81798176
return CM.isScalarWithPredication(CI, VF);
@@ -8248,7 +8245,7 @@ VPWidenCallRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
82488245
if (Legal->isMaskRequired(CI))
82498246
Mask = getBlockInMask(CI->getParent());
82508247
else
8251-
Mask = Plan->getVPValueOrAddLiveIn(ConstantInt::getTrue(
8248+
Mask = Plan.getVPValueOrAddLiveIn(ConstantInt::getTrue(
82528249
IntegerType::getInt1Ty(Variant->getFunctionType()->getContext())));
82538250

82548251
Ops.insert(Ops.begin() + *MaskPos, Mask);
@@ -8278,7 +8275,7 @@ bool VPRecipeBuilder::shouldWiden(Instruction *I, VFRange &Range) const {
82788275

82798276
VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
82808277
ArrayRef<VPValue *> Operands,
8281-
VPBasicBlock *VPBB, VPlanPtr &Plan) {
8278+
VPBasicBlock *VPBB) {
82828279
switch (I->getOpcode()) {
82838280
default:
82848281
return nullptr;
@@ -8291,8 +8288,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
82918288
if (CM.isPredicatedInst(I)) {
82928289
SmallVector<VPValue *> Ops(Operands.begin(), Operands.end());
82938290
VPValue *Mask = getBlockInMask(I->getParent());
8294-
VPValue *One = Plan->getVPValueOrAddLiveIn(
8295-
ConstantInt::get(I->getType(), 1u, false));
8291+
VPValue *One =
8292+
Plan.getVPValueOrAddLiveIn(ConstantInt::get(I->getType(), 1u, false));
82968293
auto *SafeRHS =
82978294
new VPInstruction(Instruction::Select, {Mask, Ops[1], One},
82988295
I->getDebugLoc());
@@ -8336,8 +8333,7 @@ void VPRecipeBuilder::fixHeaderPhis() {
83368333
}
83378334

83388335
VPReplicateRecipe *VPRecipeBuilder::handleReplication(Instruction *I,
8339-
VFRange &Range,
8340-
VPlan &Plan) {
8336+
VFRange &Range) {
83418337
bool IsUniform = LoopVectorizationPlanner::getDecisionAndClampRange(
83428338
[&](ElementCount VF) { return CM.isUniformAfterVectorization(I, VF); },
83438339
Range);
@@ -8392,21 +8388,22 @@ VPReplicateRecipe *VPRecipeBuilder::handleReplication(Instruction *I,
83928388
return Recipe;
83938389
}
83948390

8395-
VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
8396-
Instruction *Instr, ArrayRef<VPValue *> Operands, VFRange &Range,
8397-
VPBasicBlock *VPBB, VPlanPtr &Plan) {
8391+
VPRecipeBase *
8392+
VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
8393+
ArrayRef<VPValue *> Operands,
8394+
VFRange &Range, VPBasicBlock *VPBB) {
83988395
// First, check for specific widening recipes that deal with inductions, Phi
83998396
// nodes, calls and memory operations.
84008397
VPRecipeBase *Recipe;
84018398
if (auto Phi = dyn_cast<PHINode>(Instr)) {
84028399
if (Phi->getParent() != OrigLoop->getHeader())
8403-
return tryToBlend(Phi, Operands, Plan);
8400+
return tryToBlend(Phi, Operands);
84048401

84058402
// Always record recipes for header phis. Later first-order recurrence phis
84068403
// can have earlier phis as incoming values.
84078404
recordRecipeOf(Phi);
84088405

8409-
if ((Recipe = tryToOptimizeInductionPHI(Phi, Operands, *Plan, Range)))
8406+
if ((Recipe = tryToOptimizeInductionPHI(Phi, Operands, Range)))
84108407
return Recipe;
84118408

84128409
VPHeaderPHIRecipe *PhiRecipe = nullptr;
@@ -8442,9 +8439,8 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
84428439
return PhiRecipe;
84438440
}
84448441

8445-
if (isa<TruncInst>(Instr) &&
8446-
(Recipe = tryToOptimizeInductionTruncate(cast<TruncInst>(Instr), Operands,
8447-
Range, *Plan)))
8442+
if (isa<TruncInst>(Instr) && (Recipe = tryToOptimizeInductionTruncate(
8443+
cast<TruncInst>(Instr), Operands, Range)))
84488444
return Recipe;
84498445

84508446
// All widen recipes below deal only with VF > 1.
@@ -8453,10 +8449,10 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
84538449
return nullptr;
84548450

84558451
if (auto *CI = dyn_cast<CallInst>(Instr))
8456-
return tryToWidenCall(CI, Operands, Range, Plan);
8452+
return tryToWidenCall(CI, Operands, Range);
84578453

84588454
if (isa<LoadInst>(Instr) || isa<StoreInst>(Instr))
8459-
return tryToWidenMemory(Instr, Operands, Range, Plan);
8455+
return tryToWidenMemory(Instr, Operands, Range);
84608456

84618457
if (!shouldWiden(Instr, Range))
84628458
return nullptr;
@@ -8475,7 +8471,7 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
84758471
*CI);
84768472
}
84778473

8478-
return tryToWiden(Instr, Operands, VPBB, Plan);
8474+
return tryToWiden(Instr, Operands, VPBB);
84798475
}
84808476

84818477
void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
@@ -8547,37 +8543,6 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
85478543

85488544
SmallPtrSet<const InterleaveGroup<Instruction> *, 1> InterleaveGroups;
85498545

8550-
VPRecipeBuilder RecipeBuilder(OrigLoop, TLI, Legal, CM, PSE, Builder);
8551-
8552-
// ---------------------------------------------------------------------------
8553-
// Pre-construction: record ingredients whose recipes we'll need to further
8554-
// process after constructing the initial VPlan.
8555-
// ---------------------------------------------------------------------------
8556-
8557-
// For each interleave group which is relevant for this (possibly trimmed)
8558-
// Range, add it to the set of groups to be later applied to the VPlan and add
8559-
// placeholders for its members' Recipes which we'll be replacing with a
8560-
// single VPInterleaveRecipe.
8561-
for (InterleaveGroup<Instruction> *IG : IAI.getInterleaveGroups()) {
8562-
auto applyIG = [IG, this](ElementCount VF) -> bool {
8563-
bool Result = (VF.isVector() && // Query is illegal for VF == 1
8564-
CM.getWideningDecision(IG->getInsertPos(), VF) ==
8565-
LoopVectorizationCostModel::CM_Interleave);
8566-
// For scalable vectors, the only interleave factor currently supported
8567-
// is 2 since we require the (de)interleave2 intrinsics instead of
8568-
// shufflevectors.
8569-
assert((!Result || !VF.isScalable() || IG->getFactor() == 2) &&
8570-
"Unsupported interleave factor for scalable vectors");
8571-
return Result;
8572-
};
8573-
if (!getDecisionAndClampRange(applyIG, Range))
8574-
continue;
8575-
InterleaveGroups.insert(IG);
8576-
for (unsigned i = 0; i < IG->getFactor(); i++)
8577-
if (Instruction *Member = IG->getMember(i))
8578-
RecipeBuilder.recordRecipeOf(Member);
8579-
};
8580-
85818546
// ---------------------------------------------------------------------------
85828547
// Build initial VPlan: Scan the body of the loop in a topological order to
85838548
// visit each basic block after having visited its predecessor basic blocks.
@@ -8612,6 +8577,41 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
86128577
bool HasNUW = Style == TailFoldingStyle::None;
86138578
addCanonicalIVRecipes(*Plan, Legal->getWidestInductionType(), HasNUW, DL);
86148579

8580+
VPRecipeBuilder RecipeBuilder(*Plan, OrigLoop, TLI, Legal, CM, PSE, Builder);
8581+
8582+
// ---------------------------------------------------------------------------
8583+
// Pre-construction: record ingredients whose recipes we'll need to further
8584+
// process after constructing the initial VPlan.
8585+
// ---------------------------------------------------------------------------
8586+
8587+
// For each interleave group which is relevant for this (possibly trimmed)
8588+
// Range, add it to the set of groups to be later applied to the VPlan and add
8589+
// placeholders for its members' Recipes which we'll be replacing with a
8590+
// single VPInterleaveRecipe.
8591+
for (InterleaveGroup<Instruction> *IG : IAI.getInterleaveGroups()) {
8592+
auto applyIG = [IG, this](ElementCount VF) -> bool {
8593+
bool Result = (VF.isVector() && // Query is illegal for VF == 1
8594+
CM.getWideningDecision(IG->getInsertPos(), VF) ==
8595+
LoopVectorizationCostModel::CM_Interleave);
8596+
// For scalable vectors, the only interleave factor currently supported
8597+
// is 2 since we require the (de)interleave2 intrinsics instead of
8598+
// shufflevectors.
8599+
assert((!Result || !VF.isScalable() || IG->getFactor() == 2) &&
8600+
"Unsupported interleave factor for scalable vectors");
8601+
return Result;
8602+
};
8603+
if (!getDecisionAndClampRange(applyIG, Range))
8604+
continue;
8605+
InterleaveGroups.insert(IG);
8606+
for (unsigned i = 0; i < IG->getFactor(); i++)
8607+
if (Instruction *Member = IG->getMember(i))
8608+
RecipeBuilder.recordRecipeOf(Member);
8609+
};
8610+
8611+
// ---------------------------------------------------------------------------
8612+
// Construct recipes for the instructions in the loop
8613+
// ---------------------------------------------------------------------------
8614+
86158615
// Scan the body of the loop in a topological order to visit each basic block
86168616
// after having visited its predecessor basic blocks.
86178617
LoopBlocksDFS DFS(OrigLoop);
@@ -8633,9 +8633,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
86338633
Builder.setInsertPoint(VPBB);
86348634

86358635
if (VPBB == HeaderVPBB)
8636-
RecipeBuilder.createHeaderMask(*Plan);
8636+
RecipeBuilder.createHeaderMask();
86378637
else if (NeedsMasks)
8638-
RecipeBuilder.createBlockInMask(BB, *Plan);
8638+
RecipeBuilder.createBlockInMask(BB);
86398639

86408640
// Introduce each ingredient into VPlan.
86418641
// TODO: Model and preserve debug intrinsics in VPlan.
@@ -8658,10 +8658,10 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
86588658
Legal->isInvariantAddressOfReduction(SI->getPointerOperand()))
86598659
continue;
86608660

8661-
VPRecipeBase *Recipe = RecipeBuilder.tryToCreateWidenRecipe(
8662-
Instr, Operands, Range, VPBB, Plan);
8661+
VPRecipeBase *Recipe =
8662+
RecipeBuilder.tryToCreateWidenRecipe(Instr, Operands, Range, VPBB);
86638663
if (!Recipe)
8664-
Recipe = RecipeBuilder.handleReplication(Instr, Range, *Plan);
8664+
Recipe = RecipeBuilder.handleReplication(Instr, Range);
86658665
for (auto *Def : Recipe->definedValues()) {
86668666
auto *UV = Def->getUnderlyingValue();
86678667
Plan->addVPValue(UV, Def);

0 commit comments

Comments
 (0)