Skip to content

Commit 1789f34

Browse files
committed
[VPlan] Add opcode to create step for wide inductions.
This patch adds a WideIVStep opcode that can be used to create a vector with the steps to increment a wide induction. The opcode has 3 operands * the vector step * the scale of the vector step * a constant indicating the target type of the VPInstruction (this is working around having explicit types for VPInstructions, we could also introduce a dedicated recipe, at the cost of a lot more scaffolding) The opcode is later converted into a sequence of recipes that convert the scale and step to the target type, if needed, and then multiply vector step by scale. This simplifies code that needs to materialize step vectors, e.g. replacing wide IVs as follow up to #108378 with an increment of the wide IV step.
1 parent adfe54f commit 1789f34

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ class VPInstruction : public VPRecipeWithIRFlags,
12201220
CalculateTripCountMinusVF,
12211221
// Increment the canonical IV separately for each unrolled part.
12221222
CanonicalIVIncrementForPart,
1223+
WideIVStep,
12231224
BranchOnCount,
12241225
BranchOnCond,
12251226
ComputeReductionResult,

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ bool VPInstruction::isFPMathOp() const {
661661
return Opcode == Instruction::FAdd || Opcode == Instruction::FMul ||
662662
Opcode == Instruction::FNeg || Opcode == Instruction::FSub ||
663663
Opcode == Instruction::FDiv || Opcode == Instruction::FRem ||
664-
Opcode == Instruction::FCmp || Opcode == Instruction::Select;
664+
Opcode == Instruction::FCmp || Opcode == Instruction::Select ||
665+
Opcode == VPInstruction::WideIVStep;
665666
}
666667
#endif
667668

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,20 +1821,61 @@ void VPlanTransforms::createInterleaveGroups(
18211821
}
18221822

18231823
void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
1824+
Type *CanonicalIVType = Plan.getCanonicalIV()->getScalarType();
1825+
VPTypeAnalysis TypeInfo(CanonicalIVType);
1826+
18241827
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
18251828
vp_depth_first_deep(Plan.getEntry()))) {
1826-
for (VPRecipeBase &R : make_early_inc_range(VPBB->phis())) {
1827-
if (!isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R))
1829+
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
1830+
if (isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R)) {
1831+
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
1832+
StringRef Name =
1833+
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
1834+
auto *ScalarR = new VPScalarPHIRecipe(PhiR->getStartValue(),
1835+
PhiR->getBackedgeValue(),
1836+
PhiR->getDebugLoc(), Name);
1837+
ScalarR->insertBefore(PhiR);
1838+
PhiR->replaceAllUsesWith(ScalarR);
1839+
PhiR->eraseFromParent();
18281840
continue;
1829-
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
1830-
StringRef Name =
1831-
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
1832-
auto *ScalarR =
1833-
new VPScalarPHIRecipe(PhiR->getStartValue(), PhiR->getBackedgeValue(),
1834-
PhiR->getDebugLoc(), Name);
1835-
ScalarR->insertBefore(PhiR);
1836-
PhiR->replaceAllUsesWith(ScalarR);
1837-
PhiR->eraseFromParent();
1841+
}
1842+
1843+
auto *VPI = dyn_cast<VPInstruction>(&R);
1844+
if (VPI && VPI->getOpcode() == VPInstruction::WideIVStep) {
1845+
VPBuilder Builder(VPI->getParent(), VPI->getIterator());
1846+
VPValue *VectorStep = VPI->getOperand(0);
1847+
Type *IVTy = TypeInfo.inferScalarType(VPI->getOperand(2));
1848+
if (TypeInfo.inferScalarType(VectorStep) != IVTy) {
1849+
Instruction::CastOps CastOp = IVTy->isFloatingPointTy()
1850+
? Instruction::UIToFP
1851+
: Instruction::Trunc;
1852+
VectorStep = Builder.createWidenCast(CastOp, VectorStep, IVTy);
1853+
}
1854+
1855+
VPValue *ScalarStep = VPI->getOperand(1);
1856+
auto *ConstStep =
1857+
ScalarStep->isLiveIn()
1858+
? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())
1859+
: nullptr;
1860+
if (!ConstStep || ConstStep->getValue() != 1) {
1861+
if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {
1862+
ScalarStep =
1863+
Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);
1864+
}
1865+
1866+
std::optional<FastMathFlags> FMFs;
1867+
if (IVTy->isFloatingPointTy())
1868+
FMFs = VPI->getFastMathFlags();
1869+
1870+
unsigned MulOpc =
1871+
IVTy->isFloatingPointTy() ? Instruction::FMul : Instruction::Mul;
1872+
VPInstruction *Mul = Builder.createNaryOp(
1873+
MulOpc, {VectorStep, ScalarStep}, FMFs, R.getDebugLoc());
1874+
VectorStep = Mul;
1875+
}
1876+
VPI->replaceAllUsesWith(VectorStep);
1877+
VPI->eraseFromParent();
1878+
}
18381879
}
18391880
}
18401881
}

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,33 +155,15 @@ void UnrollState::unrollWidenInductionByUF(
155155
if (isa_and_present<FPMathOperator>(ID.getInductionBinOp()))
156156
FMFs = ID.getInductionBinOp()->getFastMathFlags();
157157

158-
VPValue *VectorStep = &Plan.getVF();
159-
VPBuilder Builder(PH);
160-
if (TypeInfo.inferScalarType(VectorStep) != IVTy) {
161-
Instruction::CastOps CastOp =
162-
IVTy->isFloatingPointTy() ? Instruction::UIToFP : Instruction::Trunc;
163-
VectorStep = Builder.createWidenCast(CastOp, VectorStep, IVTy);
164-
ToSkip.insert(VectorStep->getDefiningRecipe());
165-
}
166-
167158
VPValue *ScalarStep = IV->getStepValue();
168-
auto *ConstStep = ScalarStep->isLiveIn()
169-
? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())
170-
: nullptr;
171-
if (!ConstStep || ConstStep->getValue() != 1) {
172-
if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {
173-
ScalarStep =
174-
Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);
175-
ToSkip.insert(ScalarStep->getDefiningRecipe());
176-
}
159+
VPBuilder Builder(PH);
160+
VPInstruction *VectorStep =
161+
Builder.createNaryOp(VPInstruction::WideIVStep,
162+
{&Plan.getVF(), ScalarStep,
163+
Plan.getOrAddLiveIn(Constant::getNullValue(IVTy))},
164+
FMFs, IV->getDebugLoc());
177165

178-
unsigned MulOpc =
179-
IVTy->isFloatingPointTy() ? Instruction::FMul : Instruction::Mul;
180-
VPInstruction *Mul = Builder.createNaryOp(MulOpc, {VectorStep, ScalarStep},
181-
FMFs, IV->getDebugLoc());
182-
VectorStep = Mul;
183-
ToSkip.insert(Mul);
184-
}
166+
ToSkip.insert(VectorStep);
185167

186168
// Now create recipes to compute the induction steps for part 1 .. UF. Part 0
187169
// remains the header phi. Parts > 0 are computed by adding Step to the

0 commit comments

Comments
 (0)