[llvm] [VPlan] First step towards VPlan cost modeling. (PR #92555)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 12 14:15:51 PDT 2024
================
@@ -7300,6 +7295,166 @@ LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
return VF;
}
+InstructionCost VPCostContext::getLegacyCost(Instruction *UI,
+ ElementCount VF) const {
+ return CM.getInstructionCost(UI, VF).first;
+}
+
+bool VPCostContext::skipCostComputation(Instruction *UI, bool IsVector) const {
+ return (IsVector && CM.VecValuesToIgnore.contains(UI)) ||
+ SkipCostComputation.contains(UI);
+}
+
+InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
+ ElementCount VF) const {
+ InstructionCost Cost = 0;
+ LLVMContext &LLVMCtx = OrigLoop->getHeader()->getContext();
+ VPCostContext CostCtx(CM.TTI, Legal->getWidestInductionType(), LLVMCtx, CM);
+
+ // Cost modeling for inductions is inaccurate in the legacy cost model
+ // compared to the recipes that are generated. To match here initially during
+ // VPlan cost model bring up directly use the induction costs from the legacy
+ // cost model. Note that we do this as pre-processing; the VPlan may not have
+ // any recipes associated with the original induction increment instruction.
+ // We precompute the cost of both induction increment instructions that are
+ // represented by recipes and those that are not, to avoid distinguishing
+ // between them here, and skip all recipes that represent induction increments
+ // (the former case) later on, if they exist, to avoid counting them twice.
+ // TODO: Switch to more accurate costing based on VPlan.
+ for (const auto &[IV, _] : Legal->getInductionVars()) {
+ Instruction *IVInc = cast<Instruction>(
+ IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
+ assert(!CostCtx.SkipCostComputation.contains(IVInc) &&
+ "Same IV increment for multiple inductions?");
+ CostCtx.SkipCostComputation.insert(IVInc);
+ InstructionCost InductionCost = CostCtx.getLegacyCost(IVInc, VF);
+ LLVM_DEBUG({
+ dbgs() << "Cost of " << InductionCost << " for VF " << VF
+ << ":\n induction increment " << *IVInc << "\n";
+ IVInc->dump();
+ });
+ Cost += InductionCost;
+ }
+
+ /// Compute the cost of all exiting conditions of the loop using the legacy
+ /// cost model. This is to match the legacy behavior, which adds the cost of
+ /// all exit conditions. Note that this over-estimates the cost, as there will
+ /// be a single condition to control the vector loop.
+ SmallVector<BasicBlock *> Exiting;
+ CM.TheLoop->getExitingBlocks(Exiting);
+ // Add the cost of all exit conditions.
+ for (BasicBlock *EB : Exiting) {
+ auto *Term = dyn_cast<BranchInst>(EB->getTerminator());
+ if (!Term)
+ continue;
+ if (auto *CondI = dyn_cast<Instruction>(Term->getOperand(0))) {
+ assert(!CostCtx.SkipCostComputation.contains(CondI) &&
+ "Condition alrady skipped?");
----------------
ayalz wrote:
```suggestion
"Condition already skipped?");
```
https://github.com/llvm/llvm-project/pull/92555
More information about the llvm-commits
mailing list