[llvm] [VPlan] First step towards VPlan cost modeling (LegacyCM in CostCtx) (PR #92555)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 2 07:47:38 PDT 2024
================
@@ -7391,6 +7384,124 @@ 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::computeCost(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 for both cases, and always skip recipes for
+ // induction increments later on, if they exist.
+ // TODO: Switch to more accurate costing based on VPlan.
+ for (const auto &[IV, _] : Legal->getInductionVars()) {
+ Instruction *IVInc = cast<Instruction>(
+ IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
+ InstructionCost InductionCost = CM.getInstructionCost(IVInc, VF).first;
+ LLVM_DEBUG({
+ dbgs() << "Cost of " << InductionCost << " for VF " << VF
+ << ":\n induction increment " << *IVInc << "\n";
+ IVInc->dump();
+ });
+ Cost += InductionCost;
+ assert(!CostCtx.SkipCostComputation.contains(IVInc) &&
+ "Same IV increment for multiple inductions?");
+ CostCtx.SkipCostComputation.insert(IVInc);
----------------
ayalz wrote:
```suggestion
assert(!CostCtx.SkipCostComputation.contains(IVInc) &&
"Same IV increment for multiple inductions?");
CostCtx.SkipCostComputation.insert(IVInc);
LLVM_DEBUG({
dbgs() << "Cost of " << InductionCost << " for VF " << VF
<< ":\n induction increment " << *IVInc << "\n";
IVInc->dump();
});
Cost += InductionCost;
```
consistent with the order of asserting/marking-dumping-accumulating the costs of reductions below; there they depend on having a cost, here it is independent of cost.
https://github.com/llvm/llvm-project/pull/92555
More information about the llvm-commits
mailing list