Skip to content

Commit e454d31

Browse files
committed
[VPlan] Factor out precomputing costs from LVP::cost (NFC).
Move the logic for pre-computing costs of certain instructions to a separate helper function, allowing re-use in a follow-up patch.
1 parent ee08d9c commit e454d31

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ class LoopVectorizationPlanner {
344344
/// been retired.
345345
InstructionCost cost(VPlan &Plan, ElementCount VF) const;
346346

347+
/// Precompute costs for certain instructions using the legacy cost model. The
348+
/// function is used to bring up the VPlan-based cost model to initially avoid
349+
/// taking different decisions due to inaccuracies in the legacy cost model.
350+
InstructionCost precomputeCosts(VPlan &Plan, ElementCount VF,
351+
VPCostContext &CostCtx) const;
352+
347353
public:
348354
LoopVectorizationPlanner(
349355
Loop *L, LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI,

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7072,13 +7072,10 @@ bool VPCostContext::skipCostComputation(Instruction *UI, bool IsVector) const {
70727072
SkipCostComputation.contains(UI);
70737073
}
70747074

7075-
InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
7076-
ElementCount VF) const {
7077-
InstructionCost Cost = 0;
7078-
LLVMContext &LLVMCtx = OrigLoop->getHeader()->getContext();
7079-
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(),
7080-
LLVMCtx, CM);
7081-
7075+
InstructionCost
7076+
LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
7077+
VPCostContext &CostCtx) const {
7078+
InstructionCost Cost;
70827079
// Cost modeling for inductions is inaccurate in the legacy cost model
70837080
// compared to the recipes that are generated. To match here initially during
70847081
// VPlan cost model bring up directly use the induction costs from the legacy
@@ -7224,6 +7221,16 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
72247221
auto BranchCost = CostCtx.getLegacyCost(BB->getTerminator(), VF);
72257222
Cost += BranchCost;
72267223
}
7224+
return Cost;
7225+
}
7226+
7227+
InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
7228+
ElementCount VF) const {
7229+
LLVMContext &LLVMCtx = OrigLoop->getHeader()->getContext();
7230+
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(),
7231+
LLVMCtx, CM);
7232+
InstructionCost Cost = precomputeCosts(Plan, VF, CostCtx);
7233+
72277234
// Now compute and add the VPlan-based cost.
72287235
Cost += Plan.cost(VF, CostCtx);
72297236
LLVM_DEBUG(dbgs() << "Cost for VF " << VF << ": " << Cost << "\n");

0 commit comments

Comments
 (0)