Skip to content

Commit b251550

Browse files
committed
[LV][EVL] Support cast instruction with EVL-vectorization
1 parent f41f6ea commit b251550

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
@@ -918,6 +918,7 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
918918
case VPRecipeBase::VPWidenCallSC:
919919
case VPRecipeBase::VPWidenCanonicalIVSC:
920920
case VPRecipeBase::VPWidenCastSC:
921+
case VPRecipeBase::VPWidenCastEVLSC:
921922
case VPRecipeBase::VPWidenGEPSC:
922923
case VPRecipeBase::VPWidenSC:
923924
case VPRecipeBase::VPWidenEVLSC:
@@ -1109,6 +1110,7 @@ class VPRecipeWithIRFlags : public VPSingleDefRecipe {
11091110
R->getVPDefID() == VPRecipeBase::VPWidenEVLSC ||
11101111
R->getVPDefID() == VPRecipeBase::VPWidenGEPSC ||
11111112
R->getVPDefID() == VPRecipeBase::VPWidenCastSC ||
1113+
R->getVPDefID() == VPRecipeBase::VPWidenCastEVLSC ||
11121114
R->getVPDefID() == VPRecipeBase::VPReplicateSC ||
11131115
R->getVPDefID() == VPRecipeBase::VPVectorPointerSC;
11141116
}
@@ -1551,19 +1553,28 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
15511553
/// Result type for the cast.
15521554
Type *ResultTy;
15531555

1554-
public:
1555-
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1556-
CastInst &UI)
1557-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op, UI), Opcode(Opcode),
1556+
protected:
1557+
VPWidenCastRecipe(unsigned VPDefOpcode, Instruction::CastOps Opcode,
1558+
VPValue *Op, Type *ResultTy, CastInst &UI)
1559+
: VPRecipeWithIRFlags(VPDefOpcode, Op, UI), Opcode(Opcode),
15581560
ResultTy(ResultTy) {
15591561
assert(UI.getOpcode() == Opcode &&
15601562
"opcode of underlying cast doesn't match");
15611563
}
15621564

1563-
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
1564-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op), Opcode(Opcode),
1565+
VPWidenCastRecipe(unsigned VPDefOpcode, Instruction::CastOps Opcode,
1566+
VPValue *Op, Type *ResultTy)
1567+
: VPRecipeWithIRFlags(VPDefOpcode, Op), Opcode(Opcode),
15651568
ResultTy(ResultTy) {}
15661569

1570+
public:
1571+
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1572+
CastInst &UI)
1573+
: VPWidenCastRecipe(VPDef::VPWidenCastSC, Opcode, Op, ResultTy, UI) {}
1574+
1575+
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
1576+
: VPWidenCastRecipe(VPDef::VPWidenCastSC, Opcode, Op, ResultTy) {}
1577+
15671578
~VPWidenCastRecipe() override = default;
15681579

15691580
VPWidenCastRecipe *clone() override {
@@ -1574,7 +1585,15 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
15741585
return new VPWidenCastRecipe(Opcode, getOperand(0), ResultTy);
15751586
}
15761587

1577-
VP_CLASSOF_IMPL(VPDef::VPWidenCastSC)
1588+
static inline bool classof(const VPRecipeBase *R) {
1589+
return R->getVPDefID() == VPRecipeBase::VPWidenCastSC ||
1590+
R->getVPDefID() == VPRecipeBase::VPWidenCastEVLSC;
1591+
}
1592+
1593+
static inline bool classof(const VPUser *U) {
1594+
auto *R = dyn_cast<VPRecipeBase>(U);
1595+
return R && classof(R);
1596+
}
15781597

15791598
/// Produce widened copies of the cast.
15801599
void execute(VPTransformState &State) override;
@@ -1591,6 +1610,55 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
15911610
Type *getResultType() const { return ResultTy; }
15921611
};
15931612

1613+
// A recipe for widening cast operation with vector-predication intrinsics with
1614+
/// explicit vector length (EVL).
1615+
class VPWidenCastEVLRecipe : public VPWidenCastRecipe {
1616+
using VPRecipeWithIRFlags::transferFlags;
1617+
1618+
public:
1619+
VPWidenCastEVLRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
1620+
VPValue &EVL)
1621+
: VPWidenCastRecipe(VPDef::VPWidenCastEVLSC, Opcode, Op, ResultTy) {
1622+
addOperand(&EVL);
1623+
}
1624+
1625+
VPWidenCastEVLRecipe(VPWidenCastRecipe &W, VPValue &EVL)
1626+
: VPWidenCastEVLRecipe(W.getOpcode(), W.getOperand(0), W.getResultType(),
1627+
EVL) {
1628+
transferFlags(W);
1629+
}
1630+
1631+
~VPWidenCastEVLRecipe() override = default;
1632+
1633+
VPWidenCastEVLRecipe *clone() final {
1634+
llvm_unreachable("VPWidenEVLRecipe cannot be cloned");
1635+
return nullptr;
1636+
}
1637+
1638+
VP_CLASSOF_IMPL(VPDef::VPWidenCastEVLSC)
1639+
1640+
VPValue *getEVL() { return getOperand(getNumOperands() - 1); }
1641+
const VPValue *getEVL() const { return getOperand(getNumOperands() - 1); }
1642+
1643+
/// Produce a vp-intrinsic copies of the cast.
1644+
void execute(VPTransformState &State) final;
1645+
1646+
/// Returns true if the recipe only uses the first lane of operand \p Op.
1647+
bool onlyFirstLaneUsed(const VPValue *Op) const override {
1648+
assert(is_contained(operands(), Op) &&
1649+
"Op must be an operand of the recipe");
1650+
// EVL in that recipe is always the last operand, thus any use before means
1651+
// the VPValue should be vectorized.
1652+
return getEVL() == Op;
1653+
}
1654+
1655+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1656+
/// Print the recipe.
1657+
void print(raw_ostream &O, const Twine &Indent,
1658+
VPSlotTracker &SlotTracker) const final;
1659+
#endif
1660+
};
1661+
15941662
/// VPScalarCastRecipe is a recipe to create scalar cast instructions.
15951663
class VPScalarCastRecipe : public VPSingleDefRecipe {
15961664
Instruction::CastOps Opcode;

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ bool VPRecipeBase::mayWriteToMemory() const {
6969
case VPReductionSC:
7070
case VPWidenCanonicalIVSC:
7171
case VPWidenCastSC:
72+
case VPWidenCastEVLSC:
7273
case VPWidenGEPSC:
7374
case VPWidenIntOrFpInductionSC:
7475
case VPWidenLoadEVLSC:
@@ -112,6 +113,7 @@ bool VPRecipeBase::mayReadFromMemory() const {
112113
case VPReductionSC:
113114
case VPWidenCanonicalIVSC:
114115
case VPWidenCastSC:
116+
case VPWidenCastEVLSC:
115117
case VPWidenGEPSC:
116118
case VPWidenIntOrFpInductionSC:
117119
case VPWidenPHISC:
@@ -162,6 +164,7 @@ bool VPRecipeBase::mayHaveSideEffects() const {
162164
case VPScalarIVStepsSC:
163165
case VPWidenCanonicalIVSC:
164166
case VPWidenCastSC:
167+
case VPWidenCastEVLSC:
165168
case VPWidenGEPSC:
166169
case VPWidenIntOrFpInductionSC:
167170
case VPWidenPHISC:
@@ -1381,16 +1384,56 @@ void VPWidenCastRecipe::execute(VPTransformState &State) {
13811384
}
13821385
}
13831386

1387+
void VPWidenCastEVLRecipe::execute(VPTransformState &State) {
1388+
unsigned Opcode = getOpcode();
1389+
State.setDebugLocFrom(getDebugLoc());
1390+
assert(State.UF == 1 && "Expected only UF == 1 when vectorizing with "
1391+
"explicit vector length.");
1392+
1393+
// TODO: add more cast instruction, eg: fptoint/inttofp/inttoptr/fptofp
1394+
if (Opcode == Instruction::SExt || Opcode == Instruction::ZExt ||
1395+
Opcode == Instruction::Trunc) {
1396+
Value *SrcVal = State.get(getOperand(0), 0);
1397+
VectorType *DsType = VectorType::get(getResultType(), State.VF);
1398+
1399+
IRBuilderBase &BuilderIR = State.Builder;
1400+
VectorBuilder Builder(BuilderIR);
1401+
Value *Mask = BuilderIR.CreateVectorSplat(State.VF, BuilderIR.getTrue());
1402+
Builder.setMask(Mask).setEVL(State.get(getEVL(), 0, /*NeedsScalar=*/true));
1403+
1404+
Value *VPInst =
1405+
Builder.createVectorInstruction(Opcode, DsType, {SrcVal}, "vp.cast");
1406+
1407+
if (VPInst) {
1408+
if (auto *VecOp = dyn_cast<CastInst>(VPInst))
1409+
VecOp->copyIRFlags(getUnderlyingInstr());
1410+
}
1411+
1412+
State.set(this, VPInst, 0);
1413+
State.addMetadata(VPInst,
1414+
dyn_cast_or_null<Instruction>(getUnderlyingValue()));
1415+
}
1416+
}
1417+
13841418
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
13851419
void VPWidenCastRecipe::print(raw_ostream &O, const Twine &Indent,
13861420
VPSlotTracker &SlotTracker) const {
13871421
O << Indent << "WIDEN-CAST ";
13881422
printAsOperand(O, SlotTracker);
1389-
O << " = " << Instruction::getOpcodeName(Opcode) << " ";
1423+
O << " = " << Instruction::getOpcodeName(Opcode);
13901424
printFlags(O);
13911425
printOperands(O, SlotTracker);
13921426
O << " to " << *getResultType();
13931427
}
1428+
1429+
void VPWidenCastEVLRecipe::print(raw_ostream &O, const Twine &Indent,
1430+
VPSlotTracker &SlotTracker) const {
1431+
O << Indent << "WIDEN-CAST ";
1432+
printAsOperand(O, SlotTracker);
1433+
O << " = vp." << Instruction::getOpcodeName(getOpcode());
1434+
printFlags(O);
1435+
printOperands(O, SlotTracker);
1436+
}
13941437
#endif
13951438

13961439
/// This function adds

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,15 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
13441344
return nullptr;
13451345
return new VPWidenEVLRecipe(*W, EVL);
13461346
})
1347+
.Case<VPWidenCastRecipe>(
1348+
[&](VPWidenCastRecipe *W) -> VPRecipeBase * {
1349+
unsigned Opcode = W->getOpcode();
1350+
if (Opcode != Instruction::SExt &&
1351+
Opcode != Instruction::ZExt &&
1352+
Opcode != Instruction::Trunc)
1353+
return nullptr;
1354+
return new VPWidenCastEVLRecipe(*W, EVL);
1355+
})
13471356
.Case<VPReductionRecipe>([&](VPReductionRecipe *Red) {
13481357
VPValue *NewMask = GetNewMask(Red->getCondOp());
13491358
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
@@ -351,6 +351,7 @@ class VPDef {
351351
VPWidenCallSC,
352352
VPWidenCanonicalIVSC,
353353
VPWidenCastSC,
354+
VPWidenCastEVLSC,
354355
VPWidenGEPSC,
355356
VPWidenLoadEVLSC,
356357
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)