[llvm] [VPlan] Compute proper cost for WIDEN-CANONICAL-INDUCTION. (PR #195017)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 01:11:13 PDT 2026


================
@@ -4542,6 +4559,24 @@ void VPWidenCanonicalIVRecipe::execute(VPTransformState &State) {
   State.set(this, CanonicalVectorIV);
 }
 
+InstructionCost
+VPWidenCanonicalIVRecipe::computeCost(ElementCount VF,
+                                      VPCostContext &Ctx) const {
+  if (VF.isScalar())
+    return 0;
+  // execute() emits: broadcast of the scalar IV, a step vector and a vector
+  // add. Charge the broadcast and the add directly. The step vector is
+  // emitted as a VPInstruction::StepVector whose own cost migration is
+  // pending (see VPInstructionWithType::computeCost), so it is not charged
+  // here to avoid front-running that work.
----------------
artagnon wrote:

At the moment, it's not totally straightforward. Using https://github.com/llvm/llvm-project/pull/191219 as reference, it should emit two broadcasts, a StepVector, and an add over the StepVector:

```cpp
        CanIV = Builder.createNaryOp(VPInstruction::Broadcast, CanIV);
        Step = Builder.createNaryOp(VPInstruction::Broadcast, Step);
        Step = Builder.createAdd(
            Step, Builder.createNaryOp(VPInstruction::StepVector, {}, CanIVTy));
        VPValue *CanVecIV =
            Builder.createAdd(CanIV, Step, WideCanIV->getDebugLoc(), "vec.iv");
        WideCanIV->replaceAllUsesWith(CanVecIV);
        ToRemove.push_back(WideCanIV);
```

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


More information about the llvm-commits mailing list