[llvm] [VPlan] Add transform to replace VPWidenCanonicalIV with wide IV. (PR #194267)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 28 03:11:18 PDT 2026
================
@@ -691,6 +691,60 @@ static void removeRedundantCanonicalIVs(VPlan &Plan) {
}
}
+void VPlanTransforms::replaceWidenCanonicalIVWithWidenIV(
+ VPlan &Plan, ScalarEvolution &SE, const TargetTransformInfo &TTI,
+ TargetTransformInfo::TargetCostKind CostKind, ElementCount VF, unsigned UF,
+ const SmallPtrSetImpl<const Value *> &ValuesToIgnore) {
+ VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+ if (!LoopRegion || Plan.hasScalarVFOnly())
+ return;
+
+ VPValue *CanonicalIV = LoopRegion->getCanonicalIV();
+ auto *WideCanIV = vputils::findUserOf<VPWidenCanonicalIVRecipe>(CanonicalIV);
+ if (!WideCanIV)
+ return;
+
+ if (vputils::onlyFirstLaneUsed(WideCanIV) ||
+ vputils::onlyScalarValuesUsed(WideCanIV))
+ return;
+
+ // Introduce a new VPWidenIntOrFpInductionRecipe if profitable.
+ Type *CanIVTy = LoopRegion->getCanonicalIVType();
+ auto *VecTy = VectorType::get(CanIVTy, VF);
+ InstructionCost BroadcastCost = TTI.getShuffleCost(
+ TargetTransformInfo::SK_Broadcast, VecTy, VecTy, {}, CostKind);
+ InstructionCost PHICost = TTI.getCFInstrCost(Instruction::PHI, CostKind);
+ if (PHICost > BroadcastCost)
+ return;
+
+ // Bail out if the additional wide induction phi increase the expected spill
+ // cost.
+ VPRegisterUsage UnrolledBase =
+ calculateRegisterUsageForPlan(Plan, {VF}, TTI, ValuesToIgnore)[0];
+ for (auto &Pair : UnrolledBase.MaxLocalUsers)
+ Pair.second *= UF;
+ unsigned RegClass = TTI.getRegisterClassForType(/*Vector=*/true, VecTy);
+ VPRegisterUsage Projected = UnrolledBase;
+ Projected.MaxLocalUsers[RegClass] += 1;
+ if (Projected.spillCost(TTI, CostKind) >
+ UnrolledBase.spillCost(TTI, CostKind))
+ return;
+
+ Constant *Zero = ConstantInt::get(CanIVTy, 0);
+ InductionDescriptor ID(Zero, InductionDescriptor::IK_IntInduction,
+ SE.getOne(CanIVTy));
+ VPIRValue *StartV = Plan.getZero(CanIVTy);
+ VPValue *StepV = Plan.getConstantInt(CanIVTy, 1);
+ auto *NewWideIV = new VPWidenIntOrFpInductionRecipe(
+ /*IV=*/nullptr, StartV, StepV, &Plan.getVF(), ID,
+ VPIRFlags::WrapFlagsTy(/*HasNUW=*/false, /*HasNSW=*/false),
+ WideCanIV->getDebugLoc());
----------------
artagnon wrote:
```suggestion
Constant *Zero = Plan.getZero(CanIVTy);
InductionDescriptor ID(Zero, InductionDescriptor::IK_IntInduction,
SE.getOne(CanIVTy));
VPValue *StepV = Plan.getConstantInt(CanIVTy, 1);
auto *NewWideIV = new VPWidenIntOrFpInductionRecipe(
/*IV=*/nullptr, Zero, StepV, &Plan.getVF(), ID,
VPIRFlags::WrapFlagsTy(/*HasNUW=*/false, /*HasNSW=*/false),
WideCanIV->getDebugLoc());
```
https://github.com/llvm/llvm-project/pull/194267
More information about the llvm-commits
mailing list