Skip to content

Commit 8c359c7

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 llvm#108378 with an increment of the wide IV step.
1 parent a55fe62 commit 8c359c7

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
@@ -1213,6 +1213,7 @@ class VPInstruction : public VPRecipeWithIRFlags,
12131213
CalculateTripCountMinusVF,
12141214
// Increment the canonical IV separately for each unrolled part.
12151215
CanonicalIVIncrementForPart,
1216+
WideIVStep,
12161217
BranchOnCount,
12171218
BranchOnCond,
12181219
ComputeReductionResult,

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ bool VPInstruction::isFPMathOp() const {
719719
return Opcode == Instruction::FAdd || Opcode == Instruction::FMul ||
720720
Opcode == Instruction::FNeg || Opcode == Instruction::FSub ||
721721
Opcode == Instruction::FDiv || Opcode == Instruction::FRem ||
722-
Opcode == Instruction::FCmp || Opcode == Instruction::Select;
722+
Opcode == Instruction::FCmp || Opcode == Instruction::Select ||
723+
Opcode == VPInstruction::WideIVStep;
723724
}
724725
#endif
725726

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

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

20532053
void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
2054+
Type *CanonicalIVType = Plan.getCanonicalIV()->getScalarType();
2055+
VPTypeAnalysis TypeInfo(CanonicalIVType);
2056+
20542057
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
20552058
vp_depth_first_deep(Plan.getEntry()))) {
2056-
for (VPRecipeBase &R : make_early_inc_range(VPBB->phis())) {
2057-
if (!isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R))
2059+
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
2060+
if (isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R)) {
2061+
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
2062+
StringRef Name =
2063+
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
2064+
auto *ScalarR = new VPScalarPHIRecipe(PhiR->getStartValue(),
2065+
PhiR->getBackedgeValue(),
2066+
PhiR->getDebugLoc(), Name);
2067+
ScalarR->insertBefore(PhiR);
2068+
PhiR->replaceAllUsesWith(ScalarR);
2069+
PhiR->eraseFromParent();
20582070
continue;
2059-
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
2060-
StringRef Name =
2061-
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
2062-
auto *ScalarR =
2063-
new VPScalarPHIRecipe(PhiR->getStartValue(), PhiR->getBackedgeValue(),
2064-
PhiR->getDebugLoc(), Name);
2065-
ScalarR->insertBefore(PhiR);
2066-
PhiR->replaceAllUsesWith(ScalarR);
2067-
PhiR->eraseFromParent();
2071+
}
2072+
2073+
auto *VPI = dyn_cast<VPInstruction>(&R);
2074+
if (VPI && VPI->getOpcode() == VPInstruction::WideIVStep) {
2075+
VPBuilder Builder(VPI->getParent(), VPI->getIterator());
2076+
VPValue *VectorStep = VPI->getOperand(0);
2077+
Type *IVTy = TypeInfo.inferScalarType(VPI->getOperand(2));
2078+
if (TypeInfo.inferScalarType(VectorStep) != IVTy) {
2079+
Instruction::CastOps CastOp = IVTy->isFloatingPointTy()
2080+
? Instruction::UIToFP
2081+
: Instruction::Trunc;
2082+
VectorStep = Builder.createWidenCast(CastOp, VectorStep, IVTy);
2083+
}
2084+
2085+
VPValue *ScalarStep = VPI->getOperand(1);
2086+
auto *ConstStep =
2087+
ScalarStep->isLiveIn()
2088+
? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())
2089+
: nullptr;
2090+
if (!ConstStep || ConstStep->getValue() != 1) {
2091+
if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {
2092+
ScalarStep =
2093+
Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);
2094+
}
2095+
2096+
std::optional<FastMathFlags> FMFs;
2097+
if (IVTy->isFloatingPointTy())
2098+
FMFs = VPI->getFastMathFlags();
2099+
2100+
unsigned MulOpc =
2101+
IVTy->isFloatingPointTy() ? Instruction::FMul : Instruction::Mul;
2102+
VPInstruction *Mul = Builder.createNaryOp(
2103+
MulOpc, {VectorStep, ScalarStep}, FMFs, R.getDebugLoc());
2104+
VectorStep = Mul;
2105+
}
2106+
VPI->replaceAllUsesWith(VectorStep);
2107+
VPI->eraseFromParent();
2108+
}
20682109
}
20692110
}
20702111
}

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)