[llvm] [VPlan] Add transform to replace VPWidenCanonicalIV with wide IV. (PR #194267)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 03:11:57 PDT 2026
================
@@ -749,6 +749,56 @@ static void removeRedundantCanonicalIVs(VPlan &Plan) {
WidenNewIV->eraseFromParent();
}
+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 || 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 (unsigned &NumUsers : make_second_range(UnrolledBase.MaxLocalUsers))
+ NumUsers *= 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 = Constant::getNullValue(CanIVTy);
+ InductionDescriptor ID(Zero, InductionDescriptor::IK_IntInduction,
+ SE.getOne(CanIVTy));
+ VPValue *StepV = Plan.getConstantInt(CanIVTy, 1);
+ auto *NewWideIV = new VPWidenIntOrFpInductionRecipe(
+ /*IV=*/nullptr, Plan.getOrAddLiveIn(Zero), StepV, &Plan.getVF(), ID,
+ VPIRFlags::WrapFlagsTy(/*HasNUW=*/false, /*HasNSW=*/false),
----------------
fhahn wrote:
updated thanks
https://github.com/llvm/llvm-project/pull/194267
More information about the llvm-commits
mailing list