Skip to content

Commit 164862c

Browse files
committed
[LV][EVL] Support sext/zext/truncate of cast instruction with EVL-vectorization
1 parent d435acb commit 164862c

File tree

7 files changed

+369
-18
lines changed

7 files changed

+369
-18
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
885885
case VPRecipeBase::VPWidenCallSC:
886886
case VPRecipeBase::VPWidenCanonicalIVSC:
887887
case VPRecipeBase::VPWidenCastSC:
888+
case VPRecipeBase::VPWidenCastEVLSC:
888889
case VPRecipeBase::VPWidenGEPSC:
889890
case VPRecipeBase::VPWidenSC:
890891
case VPRecipeBase::VPWidenEVLSC:
@@ -1076,6 +1077,7 @@ class VPRecipeWithIRFlags : public VPSingleDefRecipe {
10761077
R->getVPDefID() == VPRecipeBase::VPWidenEVLSC ||
10771078
R->getVPDefID() == VPRecipeBase::VPWidenGEPSC ||
10781079
R->getVPDefID() == VPRecipeBase::VPWidenCastSC ||
1080+
R->getVPDefID() == VPRecipeBase::VPWidenCastEVLSC ||
10791081
R->getVPDefID() == VPRecipeBase::VPReplicateSC ||
10801082
R->getVPDefID() == VPRecipeBase::VPVectorPointerSC;
10811083
}
@@ -1528,19 +1530,28 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
15281530
/// Result type for the cast.
15291531
Type *ResultTy;
15301532

1531-
public:
1532-
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1533-
CastInst &UI)
1534-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op, UI), Opcode(Opcode),
1533+
protected:
1534+
VPWidenCastRecipe(unsigned VPDefOpcode, Instruction::CastOps Opcode,
1535+
VPValue *Op, Type *ResultTy, CastInst &UI)
1536+
: VPRecipeWithIRFlags(VPDefOpcode, Op, UI), Opcode(Opcode),
15351537
ResultTy(ResultTy) {
15361538
assert(UI.getOpcode() == Opcode &&
15371539
"opcode of underlying cast doesn't match");
15381540
}
15391541

1540-
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
1541-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op), Opcode(Opcode),
1542+
VPWidenCastRecipe(unsigned VPDefOpcode, Instruction::CastOps Opcode,
1543+
VPValue *Op, Type *ResultTy)
1544+
: VPRecipeWithIRFlags(VPDefOpcode, Op), Opcode(Opcode),
15421545
ResultTy(ResultTy) {}
15431546

1547+
public:
1548+
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1549+
CastInst &UI)
1550+
: VPWidenCastRecipe(VPDef::VPWidenCastSC, Opcode, Op, ResultTy, UI) {}
1551+
1552+
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
1553+
: VPWidenCastRecipe(VPDef::VPWidenCastSC, Opcode, Op, ResultTy) {}
1554+
15441555
~VPWidenCastRecipe() override = default;
15451556

15461557
VPWidenCastRecipe *clone() override {
@@ -1551,7 +1562,15 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
15511562
return new VPWidenCastRecipe(Opcode, getOperand(0), ResultTy);
15521563
}
15531564

1554-
VP_CLASSOF_IMPL(VPDef::VPWidenCastSC)
1565+
static inline bool classof(const VPRecipeBase *R) {
1566+
return R->getVPDefID() == VPRecipeBase::VPWidenCastSC ||
1567+
R->getVPDefID() == VPRecipeBase::VPWidenCastEVLSC;
1568+
}
1569+
1570+
static inline bool classof(const VPUser *U) {
1571+
auto *R = dyn_cast<VPRecipeBase>(U);
1572+
return R && classof(R);
1573+
}
15551574

15561575
/// Produce widened copies of the cast.
15571576
void execute(VPTransformState &State) override;
@@ -1568,6 +1587,55 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
15681587
Type *getResultType() const { return ResultTy; }
15691588
};
15701589

1590+
// A recipe for widening cast operation with vector-predication intrinsics with
1591+
/// explicit vector length (EVL).
1592+
class VPWidenCastEVLRecipe : public VPWidenCastRecipe {
1593+
using VPRecipeWithIRFlags::transferFlags;
1594+
1595+
public:
1596+
VPWidenCastEVLRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1597+
VPValue &EVL)
1598+
: VPWidenCastRecipe(VPDef::VPWidenCastEVLSC, Opcode, Op, ResultTy) {
1599+
addOperand(&EVL);
1600+
}
1601+
1602+
VPWidenCastEVLRecipe(VPWidenCastRecipe &W, VPValue &EVL)
1603+
: VPWidenCastEVLRecipe(W.getOpcode(), W.getOperand(0), W.getResultType(),
1604+
EVL) {
1605+
transferFlags(W);
1606+
}
1607+
1608+
~VPWidenCastEVLRecipe() override = default;
1609+
1610+
VPWidenCastEVLRecipe *clone() final {
1611+
llvm_unreachable("VPWidenEVLRecipe cannot be cloned");
1612+
return nullptr;
1613+
}
1614+
1615+
VP_CLASSOF_IMPL(VPDef::VPWidenCastEVLSC)
1616+
1617+
VPValue *getEVL() { return getOperand(getNumOperands() - 1); }
1618+
const VPValue *getEVL() const { return getOperand(getNumOperands() - 1); }
1619+
1620+
/// Produce a vp-intrinsic copies of the cast.
1621+
void execute(VPTransformState &State) final;
1622+
1623+
/// Returns true if the recipe only uses the first lane of operand \p Op.
1624+
bool onlyFirstLaneUsed(const VPValue *Op) const override {
1625+
assert(is_contained(operands(), Op) &&
1626+
"Op must be an operand of the recipe");
1627+
// EVL in that recipe is always the last operand, thus any use before means
1628+
// the VPValue should be vectorized.
1629+
return getEVL() == Op;
1630+
}
1631+
1632+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1633+
/// Print the recipe.
1634+
void print(raw_ostream &O, const Twine &Indent,
1635+
VPSlotTracker &SlotTracker) const final;
1636+
#endif
1637+
};
1638+
15711639
/// VPScalarCastRecipe is a recipe to create scalar cast instructions.
15721640
class VPScalarCastRecipe : public VPSingleDefRecipe {
15731641
Instruction::CastOps Opcode;

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ bool VPRecipeBase::mayWriteToMemory() const {
8787
case VPReductionSC:
8888
case VPWidenCanonicalIVSC:
8989
case VPWidenCastSC:
90+
case VPWidenCastEVLSC:
9091
case VPWidenGEPSC:
9192
case VPWidenIntOrFpInductionSC:
9293
case VPWidenLoadEVLSC:
@@ -130,6 +131,7 @@ bool VPRecipeBase::mayReadFromMemory() const {
130131
case VPReductionSC:
131132
case VPWidenCanonicalIVSC:
132133
case VPWidenCastSC:
134+
case VPWidenCastEVLSC:
133135
case VPWidenGEPSC:
134136
case VPWidenIntOrFpInductionSC:
135137
case VPWidenPHISC:
@@ -166,6 +168,7 @@ bool VPRecipeBase::mayHaveSideEffects() const {
166168
case VPScalarIVStepsSC:
167169
case VPWidenCanonicalIVSC:
168170
case VPWidenCastSC:
171+
case VPWidenCastEVLSC:
169172
case VPWidenGEPSC:
170173
case VPWidenIntOrFpInductionSC:
171174
case VPWidenPHISC:
@@ -1340,16 +1343,56 @@ void VPWidenCastRecipe::execute(VPTransformState &State) {
13401343
State.addMetadata(Cast, cast_or_null<Instruction>(getUnderlyingValue()));
13411344
}
13421345

1346+
void VPWidenCastEVLRecipe::execute(VPTransformState &State) {
1347+
unsigned Opcode = getOpcode();
1348+
State.setDebugLocFrom(getDebugLoc());
1349+
assert(State.UF == 1 && "Expected only UF == 1 when vectorizing with "
1350+
"explicit vector length.");
1351+
1352+
// TODO: add more cast instruction, eg: fptoint/inttofp/inttoptr/fptofp
1353+
if (Opcode == Instruction::SExt || Opcode == Instruction::ZExt ||
1354+
Opcode == Instruction::Trunc) {
1355+
Value *SrcVal = State.get(getOperand(0), 0);
1356+
VectorType *DsType = VectorType::get(getResultType(), State.VF);
1357+
1358+
IRBuilderBase &BuilderIR = State.Builder;
1359+
VectorBuilder Builder(BuilderIR);
1360+
Value *Mask = BuilderIR.CreateVectorSplat(State.VF, BuilderIR.getTrue());
1361+
Builder.setMask(Mask).setEVL(State.get(getEVL(), 0, /*NeedsScalar=*/true));
1362+
1363+
Value *VPInst =
1364+
Builder.createVectorInstruction(Opcode, DsType, {SrcVal}, "vp.cast");
1365+
1366+
if (VPInst) {
1367+
if (auto *VecOp = dyn_cast<CastInst>(VPInst))
1368+
VecOp->copyIRFlags(getUnderlyingInstr());
1369+
}
1370+
1371+
State.set(this, VPInst, 0);
1372+
State.addMetadata(VPInst,
1373+
dyn_cast_or_null<Instruction>(getUnderlyingValue()));
1374+
}
1375+
}
1376+
13431377
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
13441378
void VPWidenCastRecipe::print(raw_ostream &O, const Twine &Indent,
13451379
VPSlotTracker &SlotTracker) const {
13461380
O << Indent << "WIDEN-CAST ";
13471381
printAsOperand(O, SlotTracker);
1348-
O << " = " << Instruction::getOpcodeName(Opcode) << " ";
1382+
O << " = " << Instruction::getOpcodeName(Opcode);
13491383
printFlags(O);
13501384
printOperands(O, SlotTracker);
13511385
O << " to " << *getResultType();
13521386
}
1387+
1388+
void VPWidenCastEVLRecipe::print(raw_ostream &O, const Twine &Indent,
1389+
VPSlotTracker &SlotTracker) const {
1390+
O << Indent << "WIDEN-CAST ";
1391+
printAsOperand(O, SlotTracker);
1392+
O << " = vp." << Instruction::getOpcodeName(getOpcode());
1393+
printFlags(O);
1394+
printOperands(O, SlotTracker);
1395+
}
13531396
#endif
13541397

13551398
/// This function adds

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,15 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
13791379
return nullptr;
13801380
return new VPWidenEVLRecipe(*W, EVL);
13811381
})
1382+
.Case<VPWidenCastRecipe>(
1383+
[&](VPWidenCastRecipe *W) -> VPRecipeBase * {
1384+
unsigned Opcode = W->getOpcode();
1385+
if (Opcode != Instruction::SExt &&
1386+
Opcode != Instruction::ZExt &&
1387+
Opcode != Instruction::Trunc)
1388+
return nullptr;
1389+
return new VPWidenCastEVLRecipe(*W, EVL);
1390+
})
13821391
.Case<VPReductionRecipe>([&](VPReductionRecipe *Red) {
13831392
VPValue *NewMask = GetNewMask(Red->getCondOp());
13841393
return new VPReductionEVLRecipe(*Red, EVL, NewMask);

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class VPDef {
349349
VPWidenCallSC,
350350
VPWidenCanonicalIVSC,
351351
VPWidenCastSC,
352+
VPWidenCastEVLSC,
352353
VPWidenGEPSC,
353354
VPWidenLoadEVLSC,
354355
VPWidenLoadSC,

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ bool VPlanVerifier::verifyEVLRecipe(const VPInstruction &EVL) const {
148148
return VerifyEVLUse(
149149
*W, Instruction::isUnaryOp(W->getOpcode()) ? 1 : 2);
150150
})
151+
.Case<VPWidenCastEVLRecipe>([&](const VPWidenCastEVLRecipe *C) {
152+
return VerifyEVLUse(*C, 1);
153+
})
151154
.Case<VPReductionEVLRecipe>([&](const VPReductionEVLRecipe *R) {
152155
return VerifyEVLUse(*R, 2);
153156
})

llvm/test/Transforms/LoopVectorize/RISCV/inloop-reduction.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,38 +159,38 @@ define i32 @add_i16_i32(ptr nocapture readonly %x, i32 %n) {
159159
; IF-EVL-INLOOP: vector.body:
160160
; IF-EVL-INLOOP-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
161161
; IF-EVL-INLOOP-NEXT: [[EVL_BASED_IV:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
162-
; IF-EVL-INLOOP-NEXT: [[VEC_PHI:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[TMP12:%.*]], [[VECTOR_BODY]] ]
162+
; IF-EVL-INLOOP-NEXT: [[VEC_PHI:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[TMP11:%.*]], [[VECTOR_BODY]] ]
163163
; IF-EVL-INLOOP-NEXT: [[TMP5:%.*]] = sub i32 [[N]], [[EVL_BASED_IV]]
164164
; IF-EVL-INLOOP-NEXT: [[TMP6:%.*]] = call i32 @llvm.experimental.get.vector.length.i32(i32 [[TMP5]], i32 8, i1 true)
165165
; IF-EVL-INLOOP-NEXT: [[TMP7:%.*]] = add i32 [[EVL_BASED_IV]], 0
166166
; IF-EVL-INLOOP-NEXT: [[TMP8:%.*]] = getelementptr inbounds i16, ptr [[X:%.*]], i32 [[TMP7]]
167167
; IF-EVL-INLOOP-NEXT: [[TMP9:%.*]] = getelementptr inbounds i16, ptr [[TMP8]], i32 0
168168
; IF-EVL-INLOOP-NEXT: [[VP_OP_LOAD:%.*]] = call <vscale x 8 x i16> @llvm.vp.load.nxv8i16.p0(ptr align 2 [[TMP9]], <vscale x 8 x i1> shufflevector (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), <vscale x 8 x i1> poison, <vscale x 8 x i32> zeroinitializer), i32 [[TMP6]])
169-
; IF-EVL-INLOOP-NEXT: [[TMP10:%.*]] = sext <vscale x 8 x i16> [[VP_OP_LOAD]] to <vscale x 8 x i32>
170-
; IF-EVL-INLOOP-NEXT: [[TMP11:%.*]] = call i32 @llvm.vp.reduce.add.nxv8i32(i32 0, <vscale x 8 x i32> [[TMP10]], <vscale x 8 x i1> shufflevector (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), <vscale x 8 x i1> poison, <vscale x 8 x i32> zeroinitializer), i32 [[TMP6]])
171-
; IF-EVL-INLOOP-NEXT: [[TMP12]] = add i32 [[TMP11]], [[VEC_PHI]]
169+
; IF-EVL-INLOOP-NEXT: [[VP_CAST:%.*]] = call <vscale x 8 x i32> @llvm.vp.sext.nxv8i32.nxv8i16(<vscale x 8 x i16> [[VP_OP_LOAD]], <vscale x 8 x i1> shufflevector (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), <vscale x 8 x i1> poison, <vscale x 8 x i32> zeroinitializer), i32 [[TMP6]])
170+
; IF-EVL-INLOOP-NEXT: [[TMP10:%.*]] = call i32 @llvm.vp.reduce.add.nxv8i32(i32 0, <vscale x 8 x i32> [[VP_CAST]], <vscale x 8 x i1> shufflevector (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), <vscale x 8 x i1> poison, <vscale x 8 x i32> zeroinitializer), i32 [[TMP6]])
171+
; IF-EVL-INLOOP-NEXT: [[TMP11]] = add i32 [[TMP10]], [[VEC_PHI]]
172172
; IF-EVL-INLOOP-NEXT: [[INDEX_EVL_NEXT]] = add i32 [[TMP6]], [[EVL_BASED_IV]]
173173
; IF-EVL-INLOOP-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], [[TMP4]]
174-
; IF-EVL-INLOOP-NEXT: [[TMP13:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
175-
; IF-EVL-INLOOP-NEXT: br i1 [[TMP13]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
174+
; IF-EVL-INLOOP-NEXT: [[TMP12:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
175+
; IF-EVL-INLOOP-NEXT: br i1 [[TMP12]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
176176
; IF-EVL-INLOOP: middle.block:
177177
; IF-EVL-INLOOP-NEXT: br i1 true, label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[SCALAR_PH]]
178178
; IF-EVL-INLOOP: scalar.ph:
179179
; IF-EVL-INLOOP-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
180-
; IF-EVL-INLOOP-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP12]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
180+
; IF-EVL-INLOOP-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP11]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY_PREHEADER]] ]
181181
; IF-EVL-INLOOP-NEXT: br label [[FOR_BODY:%.*]]
182182
; IF-EVL-INLOOP: for.body:
183183
; IF-EVL-INLOOP-NEXT: [[I_08:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
184184
; IF-EVL-INLOOP-NEXT: [[R_07:%.*]] = phi i32 [ [[ADD:%.*]], [[FOR_BODY]] ], [ [[BC_MERGE_RDX]], [[SCALAR_PH]] ]
185185
; IF-EVL-INLOOP-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i16, ptr [[X]], i32 [[I_08]]
186-
; IF-EVL-INLOOP-NEXT: [[TMP14:%.*]] = load i16, ptr [[ARRAYIDX]], align 2
187-
; IF-EVL-INLOOP-NEXT: [[CONV:%.*]] = sext i16 [[TMP14]] to i32
186+
; IF-EVL-INLOOP-NEXT: [[TMP13:%.*]] = load i16, ptr [[ARRAYIDX]], align 2
187+
; IF-EVL-INLOOP-NEXT: [[CONV:%.*]] = sext i16 [[TMP13]] to i32
188188
; IF-EVL-INLOOP-NEXT: [[ADD]] = add nsw i32 [[R_07]], [[CONV]]
189189
; IF-EVL-INLOOP-NEXT: [[INC]] = add nuw nsw i32 [[I_08]], 1
190190
; IF-EVL-INLOOP-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], [[N]]
191191
; IF-EVL-INLOOP-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP_LOOPEXIT]], label [[FOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
192192
; IF-EVL-INLOOP: for.cond.cleanup.loopexit:
193-
; IF-EVL-INLOOP-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD]], [[FOR_BODY]] ], [ [[TMP12]], [[MIDDLE_BLOCK]] ]
193+
; IF-EVL-INLOOP-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[ADD]], [[FOR_BODY]] ], [ [[TMP11]], [[MIDDLE_BLOCK]] ]
194194
; IF-EVL-INLOOP-NEXT: br label [[FOR_COND_CLEANUP]]
195195
; IF-EVL-INLOOP: for.cond.cleanup:
196196
; IF-EVL-INLOOP-NEXT: [[R_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[ADD_LCSSA]], [[FOR_COND_CLEANUP_LOOPEXIT]] ]

0 commit comments

Comments
 (0)