Skip to content

Commit 0f1be97

Browse files
committed
[VP][EVL] Support select instruction with EVL-vectorization
1 parent 145e8fd commit 0f1be97

File tree

7 files changed

+126
-8
lines changed

7 files changed

+126
-8
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
889889
case VPRecipeBase::VPWidenSC:
890890
case VPRecipeBase::VPWidenEVLSC:
891891
case VPRecipeBase::VPWidenSelectSC:
892+
case VPRecipeBase::VPWidenSelectEVLSC:
892893
case VPRecipeBase::VPBlendSC:
893894
case VPRecipeBase::VPPredInstPHISC:
894895
case VPRecipeBase::VPCanonicalIVPHISC:
@@ -1712,10 +1713,17 @@ class VPHistogramRecipe : public VPRecipeBase {
17121713

17131714
/// A recipe for widening select instructions.
17141715
struct VPWidenSelectRecipe : public VPSingleDefRecipe {
1716+
1717+
protected:
1718+
template <typename IterT>
1719+
VPWidenSelectRecipe(unsigned VPDefOpcode, SelectInst &I,
1720+
iterator_range<IterT> Operands)
1721+
: VPSingleDefRecipe(VPDefOpcode, Operands, &I, I.getDebugLoc()) {}
1722+
1723+
public:
17151724
template <typename IterT>
17161725
VPWidenSelectRecipe(SelectInst &I, iterator_range<IterT> Operands)
1717-
: VPSingleDefRecipe(VPDef::VPWidenSelectSC, Operands, &I,
1718-
I.getDebugLoc()) {}
1726+
: VPWidenSelectRecipe(VPDef::VPWidenSelectSC, I, Operands) {}
17191727

17201728
~VPWidenSelectRecipe() override = default;
17211729

@@ -1724,7 +1732,15 @@ struct VPWidenSelectRecipe : public VPSingleDefRecipe {
17241732
operands());
17251733
}
17261734

1727-
VP_CLASSOF_IMPL(VPDef::VPWidenSelectSC)
1735+
static inline bool classof(const VPRecipeBase *R) {
1736+
return R->getVPDefID() == VPRecipeBase::VPWidenSelectSC ||
1737+
R->getVPDefID() == VPRecipeBase::VPWidenSelectEVLSC;
1738+
}
1739+
1740+
static inline bool classof(const VPUser *U) {
1741+
auto *R = dyn_cast<VPRecipeBase>(U);
1742+
return R && classof(R);
1743+
}
17281744

17291745
/// Produce a widened version of the select instruction.
17301746
void execute(VPTransformState &State) override;
@@ -1744,6 +1760,52 @@ struct VPWidenSelectRecipe : public VPSingleDefRecipe {
17441760
}
17451761
};
17461762

1763+
// A recipe for widening select instruction with vector-predication intrinsics
1764+
// with explicit vector length (EVL).
1765+
struct VPWidenSelectEVLRecipe : public VPWidenSelectRecipe {
1766+
1767+
template <typename IterT>
1768+
VPWidenSelectEVLRecipe(SelectInst &I, iterator_range<IterT> Operands,
1769+
VPValue &EVL)
1770+
: VPWidenSelectRecipe(VPDef::VPWidenSelectEVLSC, I, Operands) {
1771+
addOperand(&EVL);
1772+
}
1773+
1774+
VPWidenSelectEVLRecipe(VPWidenSelectRecipe &W, VPValue &EVL)
1775+
: VPWidenSelectEVLRecipe(*cast<SelectInst>(W.getUnderlyingInstr()),
1776+
W.operands(), EVL) {}
1777+
1778+
~VPWidenSelectEVLRecipe() override = default;
1779+
1780+
VPWidenSelectEVLRecipe *clone() final {
1781+
llvm_unreachable("VPWidenSelectEVLRecipe cannot be cloned");
1782+
return nullptr;
1783+
}
1784+
1785+
VP_CLASSOF_IMPL(VPDef::VPWidenSelectEVLSC)
1786+
1787+
VPValue *getEVL() { return getOperand(getNumOperands() - 1); }
1788+
const VPValue *getEVL() const { return getOperand(getNumOperands() - 1); }
1789+
1790+
/// Produce a vp-intrinsic version of the select instruction.
1791+
void execute(VPTransformState &State) final;
1792+
1793+
/// Returns true if the recipe only uses the first lane of operand \p Op.
1794+
bool onlyFirstLaneUsed(const VPValue *Op) const override {
1795+
assert(is_contained(operands(), Op) &&
1796+
"Op must be an operand of the recipe");
1797+
// EVL in that recipe is always the last operand, thus any use before means
1798+
// the VPValue should be vectorized.
1799+
return getEVL() == Op;
1800+
}
1801+
1802+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1803+
/// Print the recipe.
1804+
void print(raw_ostream &O, const Twine &Indent,
1805+
VPSlotTracker &SlotTracker) const final;
1806+
#endif
1807+
};
1808+
17471809
/// A recipe for handling GEP instructions.
17481810
class VPWidenGEPRecipe : public VPRecipeWithIRFlags {
17491811
bool isPointerLoopInvariant() const {

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ bool VPRecipeBase::mayWriteToMemory() const {
9595
case VPWidenPHISC:
9696
case VPWidenSC:
9797
case VPWidenEVLSC:
98-
case VPWidenSelectSC: {
98+
case VPWidenSelectSC:
99+
case VPWidenSelectEVLSC: {
99100
const Instruction *I =
100101
dyn_cast_or_null<Instruction>(getVPSingleValue()->getUnderlyingValue());
101102
(void)I;
@@ -136,7 +137,8 @@ bool VPRecipeBase::mayReadFromMemory() const {
136137
case VPWidenPHISC:
137138
case VPWidenSC:
138139
case VPWidenEVLSC:
139-
case VPWidenSelectSC: {
140+
case VPWidenSelectSC:
141+
case VPWidenSelectEVLSC: {
140142
const Instruction *I =
141143
dyn_cast_or_null<Instruction>(getVPSingleValue()->getUnderlyingValue());
142144
(void)I;
@@ -173,7 +175,8 @@ bool VPRecipeBase::mayHaveSideEffects() const {
173175
case VPWidenPointerInductionSC:
174176
case VPWidenSC:
175177
case VPWidenEVLSC:
176-
case VPWidenSelectSC: {
178+
case VPWidenSelectSC:
179+
case VPWidenSelectEVLSC: {
177180
const Instruction *I =
178181
dyn_cast_or_null<Instruction>(getVPSingleValue()->getUnderlyingValue());
179182
(void)I;
@@ -1127,6 +1130,21 @@ void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent,
11271130
getOperand(2)->printAsOperand(O, SlotTracker);
11281131
O << (isInvariantCond() ? " (condition is loop invariant)" : "");
11291132
}
1133+
1134+
void VPWidenSelectEVLRecipe::print(raw_ostream &O, const Twine &Indent,
1135+
VPSlotTracker &SlotTracker) const {
1136+
O << Indent << "WIDEN-SELECT ";
1137+
printAsOperand(O, SlotTracker);
1138+
O << " = vp.select ";
1139+
getOperand(0)->printAsOperand(O, SlotTracker);
1140+
O << ", ";
1141+
getOperand(1)->printAsOperand(O, SlotTracker);
1142+
O << ", ";
1143+
getOperand(2)->printAsOperand(O, SlotTracker);
1144+
O << ", ";
1145+
getOperand(3)->printAsOperand(O, SlotTracker);
1146+
O << (isInvariantCond() ? " (condition is loop invariant)" : "");
1147+
}
11301148
#endif
11311149

11321150
void VPWidenSelectRecipe::execute(VPTransformState &State) {
@@ -1147,6 +1165,35 @@ void VPWidenSelectRecipe::execute(VPTransformState &State) {
11471165
State.addMetadata(Sel, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
11481166
}
11491167

1168+
void VPWidenSelectEVLRecipe::execute(VPTransformState &State) {
1169+
State.setDebugLocFrom(getDebugLoc());
1170+
assert(State.UF == 1 && "Expected only UF == 1 when vectorizing with "
1171+
"explicit vector length.");
1172+
1173+
Value *EVLArg = State.get(getEVL(), 0, /*NeedsScalar=*/true);
1174+
IRBuilderBase &BuilderIR = State.Builder;
1175+
VectorBuilder Builder(BuilderIR);
1176+
Builder.setEVL(EVLArg);
1177+
// The condition can be loop invariant but still defined inside the
1178+
// loop. This means that we can't just use the original 'cond' value.
1179+
// We have to take the 'vectorized' value and pick the first lane.
1180+
// Instcombine will make this a no-op.
1181+
auto *InvarCond =
1182+
isInvariantCond() ? State.get(getCond(), VPIteration(0, 0)) : nullptr;
1183+
1184+
Value *Cond = InvarCond ? InvarCond : State.get(getCond(), 0);
1185+
if (!isa<VectorType>(Cond->getType()))
1186+
Cond = BuilderIR.CreateVectorSplat(State.VF, Cond, "splat.cond");
1187+
1188+
Value *Op0 = State.get(getOperand(1), 0);
1189+
Value *Op1 = State.get(getOperand(2), 0);
1190+
Value *VPInst = Builder.createVectorInstruction(
1191+
Instruction::Select, Op0->getType(), {Cond, Op0, Op1}, "vp.select");
1192+
State.set(this, VPInst, 0);
1193+
State.addMetadata(VPInst,
1194+
dyn_cast_or_null<Instruction>(getUnderlyingValue()));
1195+
}
1196+
11501197
VPRecipeWithIRFlags::FastMathFlagsTy::FastMathFlagsTy(
11511198
const FastMathFlags &FMF) {
11521199
AllowReassoc = FMF.allowReassoc();

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,10 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
13791379
return nullptr;
13801380
return new VPWidenEVLRecipe(*W, EVL);
13811381
})
1382+
.Case<VPWidenSelectRecipe>(
1383+
[&](VPWidenSelectRecipe *W) -> VPRecipeBase * {
1384+
return new VPWidenSelectEVLRecipe(*W, EVL);
1385+
})
13821386
.Case<VPReductionRecipe>([&](VPReductionRecipe *Red) {
13831387
VPValue *NewMask = GetNewMask(Red->getCondOp());
13841388
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
@@ -357,6 +357,7 @@ class VPDef {
357357
VPWidenSC,
358358
VPWidenEVLSC,
359359
VPWidenSelectSC,
360+
VPWidenSelectEVLSC,
360361
VPBlendSC,
361362
VPHistogramSC,
362363
// START: Phi-like recipes. Need to be kept together.

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ bool VPlanVerifier::verifyEVLRecipe(const VPInstruction &EVL) const {
148148
return VerifyEVLUse(
149149
*W, Instruction::isUnaryOp(W->getOpcode()) ? 1 : 2);
150150
})
151+
.Case<VPWidenSelectEVLRecipe>(
152+
[&](const VPWidenSelectEVLRecipe *S) {
153+
return VerifyEVLUse(*S, 3);
154+
})
151155
.Case<VPReductionEVLRecipe>([&](const VPReductionEVLRecipe *R) {
152156
return VerifyEVLUse(*R, 2);
153157
})

llvm/test/Transforms/LoopVectorize/RISCV/vectorize-force-tail-with-evl-cond-reduction.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ define i32 @cond_add(ptr %a, i64 %n, i32 %start) {
7070
; IF-EVL-INLOOP-NEXT: [[TMP18:%.*]] = getelementptr inbounds i32, ptr [[TMP17]], i32 0
7171
; IF-EVL-INLOOP-NEXT: [[VP_OP_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP18]], <vscale x 4 x i1> shufflevector (<vscale x 4 x i1> insertelement (<vscale x 4 x i1> poison, i1 true, i64 0), <vscale x 4 x i1> poison, <vscale x 4 x i32> zeroinitializer), i32 [[TMP12]])
7272
; IF-EVL-INLOOP-NEXT: [[TMP19:%.*]] = icmp sgt <vscale x 4 x i32> [[VP_OP_LOAD]], shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> poison, i32 3, i64 0), <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer)
73-
; IF-EVL-INLOOP-NEXT: [[TMP20:%.*]] = select <vscale x 4 x i1> [[TMP19]], <vscale x 4 x i32> [[VP_OP_LOAD]], <vscale x 4 x i32> zeroinitializer
73+
; IF-EVL-INLOOP-NEXT: [[TMP20:%.*]] = call <vscale x 4 x i32> @llvm.vp.select.nxv4i32(<vscale x 4 x i1> [[TMP19]], <vscale x 4 x i32> [[VP_OP_LOAD]], <vscale x 4 x i32> zeroinitializer, i32 [[TMP12]])
7474
; IF-EVL-INLOOP-NEXT: [[TMP21:%.*]] = call i32 @llvm.vp.reduce.add.nxv4i32(i32 0, <vscale x 4 x i32> [[TMP20]], <vscale x 4 x i1> shufflevector (<vscale x 4 x i1> insertelement (<vscale x 4 x i1> poison, i1 true, i64 0), <vscale x 4 x i1> poison, <vscale x 4 x i32> zeroinitializer), i32 [[TMP12]])
7575
; IF-EVL-INLOOP-NEXT: [[TMP22]] = add i32 [[TMP21]], [[VEC_PHI]]
7676
; IF-EVL-INLOOP-NEXT: [[TMP23:%.*]] = zext i32 [[TMP12]] to i64

llvm/test/Transforms/LoopVectorize/RISCV/vplan-vp-select-intrinsics.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ define void @vp_select(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
3333
; IF-EVL-NEXT: WIDEN ir<%1> = vp.load vp<%8>, vp<%5>
3434
; IF-EVL-NEXT: WIDEN ir<%cmp4> = icmp sgt ir<%0>, ir<%1>
3535
; IF-EVL-NEXT: WIDEN ir<%2> = vp.sub ir<0>, ir<%1>, vp<%5>
36-
; IF-EVL-NEXT: WIDEN-SELECT ir<%cond.p> = select ir<%cmp4>, ir<%1>, ir<%2>
36+
; IF-EVL-NEXT: WIDEN-SELECT ir<%cond.p> = vp.select ir<%cmp4>, ir<%1>, ir<%2>, vp<%5>
3737
; IF-EVL-NEXT: WIDEN ir<%cond> = vp.add ir<%cond.p>, ir<%0>, vp<%5>
3838
; IF-EVL-NEXT: CLONE ir<%arrayidx15> = getelementptr inbounds ir<%a>, vp<%6>
3939
; IF-EVL-NEXT: vp<%9> = vector-pointer ir<%arrayidx15>

0 commit comments

Comments
 (0)