Skip to content

Commit 59a9e4d

Browse files
[RISCV] Update matchSplatAsGather to convert vectors if they have different sizes (#117878)
This patch updates the matchSplatAsGather function so we can handle vectors of different sizes. The goal is to improve the code gen for @llvm.experimental.vector.match on RISCV. Currently, we use a scalar extract and splat instead of vrgather, and the patch changes that.
1 parent f7e8be7 commit 59a9e4d

File tree

2 files changed

+185
-264
lines changed

2 files changed

+185
-264
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3496,21 +3496,30 @@ static SDValue matchSplatAsGather(SDValue SplatVal, MVT VT, const SDLoc &DL,
34963496
if (SplatVal.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
34973497
return SDValue();
34983498
SDValue Vec = SplatVal.getOperand(0);
3499-
// Only perform this optimization on vectors of the same size for simplicity.
3500-
// Don't perform this optimization for i1 vectors.
3499+
// Don't perform this optimization for i1 vectors, or if the element types are
3500+
// different
35013501
// FIXME: Support i1 vectors, maybe by promoting to i8?
3502-
if (Vec.getValueType() != VT || VT.getVectorElementType() == MVT::i1)
3502+
MVT EltTy = VT.getVectorElementType();
3503+
if (EltTy == MVT::i1 ||
3504+
EltTy != Vec.getSimpleValueType().getVectorElementType())
35033505
return SDValue();
35043506
SDValue Idx = SplatVal.getOperand(1);
35053507
// The index must be a legal type.
35063508
if (Idx.getValueType() != Subtarget.getXLenVT())
35073509
return SDValue();
35083510

3511+
// Check that Index lies within VT
3512+
// TODO: Can we check if the Index is constant and known in-bounds?
3513+
if (!TypeSize::isKnownLE(Vec.getValueSizeInBits(), VT.getSizeInBits()))
3514+
return SDValue();
3515+
35093516
MVT ContainerVT = VT;
3510-
if (VT.isFixedLengthVector()) {
3517+
if (VT.isFixedLengthVector())
35113518
ContainerVT = getContainerForFixedLengthVector(DAG, VT, Subtarget);
3512-
Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
3513-
}
3519+
3520+
Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, ContainerVT,
3521+
DAG.getUNDEF(ContainerVT), Vec,
3522+
DAG.getVectorIdxConstant(0, DL));
35143523

35153524
auto [Mask, VL] = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
35163525

@@ -3523,7 +3532,6 @@ static SDValue matchSplatAsGather(SDValue SplatVal, MVT VT, const SDLoc &DL,
35233532
return convertFromScalableVector(VT, Gather, DAG, Subtarget);
35243533
}
35253534

3526-
35273535
/// Try and optimize BUILD_VECTORs with "dominant values" - these are values
35283536
/// which constitute a large proportion of the elements. In such cases we can
35293537
/// splat a vector with the dominant element and make up the shortfall with

0 commit comments

Comments
 (0)