Skip to content

Commit 3d66d69

Browse files
authored
[VPlan] Support live-ins without underlying IR in type analysis. (#80723)
A VPlan contains multiple live-ins without underlying IR, like VFxUF or VectorTripCount. Trying to infer the scalar type of those causes a crash at the moment. Update VPTypeAnalysis to take a VPlan in its constructor and assign types to those live-ins up front. All those live-ins share the type of the canonical IV. PR: #80723
1 parent ddc0f1d commit 3d66d69

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi() {
212212
return It;
213213
}
214214

215+
VPTransformState::VPTransformState(ElementCount VF, unsigned UF, LoopInfo *LI,
216+
DominatorTree *DT, IRBuilderBase &Builder,
217+
InnerLoopVectorizer *ILV, VPlan *Plan,
218+
LLVMContext &Ctx)
219+
: VF(VF), UF(UF), LI(LI), DT(DT), Builder(Builder), ILV(ILV), Plan(Plan),
220+
LVer(nullptr),
221+
TypeAnalysis(Plan->getCanonicalIV()->getScalarType(), Ctx) {}
222+
215223
Value *VPTransformState::get(VPValue *Def, const VPIteration &Instance) {
216224
if (Def->isLiveIn())
217225
return Def->getLiveInIRValue();

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ struct VPIteration {
236236
struct VPTransformState {
237237
VPTransformState(ElementCount VF, unsigned UF, LoopInfo *LI,
238238
DominatorTree *DT, IRBuilderBase &Builder,
239-
InnerLoopVectorizer *ILV, VPlan *Plan, LLVMContext &Ctx)
240-
: VF(VF), UF(UF), LI(LI), DT(DT), Builder(Builder), ILV(ILV), Plan(Plan),
241-
LVer(nullptr), TypeAnalysis(Ctx) {}
239+
InnerLoopVectorizer *ILV, VPlan *Plan, LLVMContext &Ctx);
242240

243241
/// The chosen Vectorization and Unroll Factors of the loop being vectorized.
244242
ElementCount VF;

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
3535
CachedTypes[OtherV] = ResTy;
3636
return ResTy;
3737
}
38-
case Instruction::ICmp: {
39-
// TODO: Check if types for both operands agree. This also requires
40-
// type-inference for the vector-trip-count, which is missing at the moment.
41-
Type *ResTy = inferScalarType(R->getOperand(0));
42-
return ResTy;
43-
}
38+
case Instruction::ICmp:
4439
case VPInstruction::FirstOrderRecurrenceSplice: {
4540
Type *ResTy = inferScalarType(R->getOperand(0));
4641
VPValue *OtherV = R->getOperand(1);
@@ -207,8 +202,13 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
207202
if (Type *CachedTy = CachedTypes.lookup(V))
208203
return CachedTy;
209204

210-
if (V->isLiveIn())
211-
return V->getLiveInIRValue()->getType();
205+
if (V->isLiveIn()) {
206+
if (auto *IRValue = V->getLiveInIRValue())
207+
return IRValue->getType();
208+
// All VPValues without any underlying IR value (like the vector trip count
209+
// or the backedge-taken count) have the same type as the canonical IV.
210+
return CanonicalIVTy;
211+
}
212212

213213
Type *ResultTy =
214214
TypeSwitch<const VPRecipeBase *, Type *>(V->getDefiningRecipe())

llvm/lib/Transforms/Vectorize/VPlanAnalysis.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class Type;
3535
/// of the previously inferred types.
3636
class VPTypeAnalysis {
3737
DenseMap<const VPValue *, Type *> CachedTypes;
38+
/// Type of the canonical induction variable. Used for all VPValues without
39+
/// any underlying IR value (like the vector trip count or the backedge-taken
40+
/// count).
41+
Type *CanonicalIVTy;
3842
LLVMContext &Ctx;
3943

4044
Type *inferScalarTypeForRecipe(const VPBlendRecipe *R);
@@ -47,7 +51,8 @@ class VPTypeAnalysis {
4751
Type *inferScalarTypeForRecipe(const VPReplicateRecipe *R);
4852

4953
public:
50-
VPTypeAnalysis(LLVMContext &Ctx) : Ctx(Ctx) {}
54+
VPTypeAnalysis(Type *CanonicalIVTy, LLVMContext &Ctx)
55+
: CanonicalIVTy(CanonicalIVTy), Ctx(Ctx) {}
5156

5257
/// Infer the type of \p V. Returns the scalar type of \p V.
5358
Type *inferScalarType(const VPValue *V);

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ static VPValue *createScalarIVSteps(VPlan &Plan, const InductionDescriptor &ID,
513513
}
514514

515515
// Truncate base induction if needed.
516-
VPTypeAnalysis TypeInfo(SE.getContext());
516+
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType(),
517+
SE.getContext());
517518
Type *ResultTy = TypeInfo.inferScalarType(BaseIV);
518519
if (TruncI) {
519520
Type *TruncTy = TruncI->getType();
@@ -897,7 +898,9 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
897898
#ifndef NDEBUG
898899
// Verify that the cached type info is for both A and its users is still
899900
// accurate by comparing it to freshly computed types.
900-
VPTypeAnalysis TypeInfo2(TypeInfo.getContext());
901+
VPTypeAnalysis TypeInfo2(
902+
R.getParent()->getPlan()->getCanonicalIV()->getScalarType(),
903+
TypeInfo.getContext());
901904
assert(TypeInfo.inferScalarType(A) == TypeInfo2.inferScalarType(A));
902905
for (VPUser *U : A->users()) {
903906
auto *R = dyn_cast<VPRecipeBase>(U);
@@ -918,7 +921,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
918921
static void simplifyRecipes(VPlan &Plan, LLVMContext &Ctx) {
919922
ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
920923
Plan.getEntry());
921-
VPTypeAnalysis TypeInfo(Ctx);
924+
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType(), Ctx);
922925
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
923926
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
924927
simplifyRecipe(R, TypeInfo);
@@ -939,7 +942,7 @@ void VPlanTransforms::truncateToMinimalBitwidths(
939942
// other uses have different types for their operands, making them invalidly
940943
// typed.
941944
DenseMap<VPValue *, VPWidenCastRecipe *> ProcessedTruncs;
942-
VPTypeAnalysis TypeInfo(Ctx);
945+
VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType(), Ctx);
943946
VPBasicBlock *PH = Plan.getEntry();
944947
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
945948
vp_depth_first_deep(Plan.getVectorLoopRegion()))) {

0 commit comments

Comments
 (0)