[llvm] [VPlan] Compute scalar cost based on VPlan0 instead of legacy CM. (PR #196845)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 02:12:23 PDT 2026


================
@@ -5745,6 +5703,30 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
   return Cost;
 }
 
+InstructionCost LoopVectorizationPlanner::computeScalarCost() const {
+  ElementCount ScalarVF = ElementCount::getFixed(1);
+  VPCostContext CostCtx(CM.TTI, *CM.TLI, *InitialVPlan0, CM, Config.CostKind,
+                        PSE, OrigLoop);
+  VPBasicBlock *Header =
+      VPBlockUtils::getPlainCFGHeaderAndLatch(*InitialVPlan0).first;
+  InstructionCost Cost = 0;
+
+  for (VPBasicBlock *VPBB : vp_rpo_plain_cfg_loop_body(Header)) {
+    // Look up the divisor via the first underlying IR instruction in the loop.
+    uint64_t Divisor = 1;
+    for (const VPRecipeBase &R : *VPBB) {
+      auto *UI = dyn_cast_if_present<Instruction>(
+          cast<VPSingleDefRecipe>(&R)->getUnderlyingValue());
+      if (!UI)
+        continue;
+      Divisor = CostCtx.getPredBlockCostDivisor(UI->getParent());
+      break;
+    }
+    Cost += VPBB->cost(ScalarVF, CostCtx) / Divisor;
----------------
lukel97 wrote:

Can recipes get hoisted by licm etc. into other blocks in the scalar VPlan? If so then I don't think the divisor will be accurate. 

Is there a way we can somehow extract the `DenseMap<BasicBlock *, VPBasicBlock *> BB2VPBB` map from PlainCFGBuilder and use it to look up the "underlying blocks" here?

https://github.com/llvm/llvm-project/pull/196845


More information about the llvm-commits mailing list