Skip to content

Commit f2c0c9f

Browse files
committed
[RISCV] Reduce the LMUL for a vrgather operation if legal
If we're lowering a shuffle to a vrgather (or vcompress), and we know that a prefix of the operation can be done while producing the same (defined) lanes, do the operation with a narrower LMUL.
1 parent ada8adf commit f2c0c9f

File tree

2 files changed

+209
-156
lines changed

2 files changed

+209
-156
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5619,6 +5619,31 @@ static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG,
56195619
}
56205620
}
56215621

5622+
// If only a prefix of the source elements influence a prefix of the
5623+
// destination elements, try to see if we can reduce the required LMUL
5624+
unsigned MinVLen = Subtarget.getRealMinVLen();
5625+
unsigned MinVLMAX = MinVLen / VT.getScalarSizeInBits();
5626+
if (NumElts > MinVLMAX) {
5627+
unsigned MaxIdx = 0;
5628+
for (auto [I, M] : enumerate(Mask)) {
5629+
if (M == -1)
5630+
continue;
5631+
MaxIdx = std::max(std::max((unsigned)I,(unsigned)M), MaxIdx);
5632+
}
5633+
unsigned NewNumElts = NumElts;
5634+
while (MaxIdx < NewNumElts / 2 && NewNumElts != MinVLMAX)
5635+
NewNumElts /= 2;
5636+
if (NewNumElts != NumElts) {
5637+
MVT NewVT = MVT::getVectorVT(VT.getVectorElementType(), NewNumElts);
5638+
SDValue ZeroIdx = DAG.getVectorIdxConstant(0, DL);
5639+
V1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NewVT, V1, ZeroIdx);
5640+
SDValue Res = DAG.getVectorShuffle(NewVT, DL, V1, DAG.getUNDEF(NewVT),
5641+
Mask.take_front(NewNumElts));
5642+
return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT),
5643+
Res, ZeroIdx);
5644+
}
5645+
}
5646+
56225647
// Before hitting generic lowering fallbacks, try to widen the mask
56235648
// to a wider SEW.
56245649
if (SDValue V = tryWidenMaskForShuffle(Op, DAG))

0 commit comments

Comments
 (0)