Skip to content

Commit

Permalink
[RISCV] Pack build_vectors into largest available element type (#97351)
Browse files Browse the repository at this point in the history
Our worst case build_vector lowering is a serial chain of vslide1down.vx
operations which creates a serial dependency chain through a relatively
high latency operation. We can instead pack together elements into ELEN
sized chunks, and move them from integer to scalar in a single
operation.

This reduces the length of the serial chain on the vector side, and
costs at most three scalar instructions per element. This is a win for
all cores when the sum of the latencies of the scalar instructions is
less than the vslide1down.vx being replaced, and is particularly
profitable for out-of-order cores which can overlap the scalar
computation.

This patch is restricted to configurations with zba and zbb. Without
both, the zero extend might require two instructions which would bring
the total scalar instructions per element to 4. zba and zba are both
present in the rva22u64 baseline which is looking to be quite common for
hardware in practice; we could extend this to systems without bitmanip
with a bit of extra effort.
  • Loading branch information
preames authored Jul 8, 2024
1 parent 2dadf8d commit 03d4332
Show file tree
Hide file tree
Showing 2 changed files with 958 additions and 327 deletions.
66 changes: 66 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3905,6 +3905,65 @@ static SDValue lowerBuildVectorOfConstants(SDValue Op, SelectionDAG &DAG,
return SDValue();
}

/// Double the element size of the build vector to reduce the number
/// of vslide1down in the build vector chain. In the worst case, this
/// trades three scalar operations for 1 vector operation. Scalar
/// operations are generally lower latency, and for out-of-order cores
/// we also benefit from additional parallelism.
static SDValue lowerBuildVectorViaPacking(SDValue Op, SelectionDAG &DAG,
const RISCVSubtarget &Subtarget) {
SDLoc DL(Op);
MVT VT = Op.getSimpleValueType();
assert(VT.isFixedLengthVector() && "Unexpected vector!");
MVT ElemVT = VT.getVectorElementType();
if (!ElemVT.isInteger())
return SDValue();

// TODO: Relax these architectural restrictions, possibly with costing
// of the actual instructions required.
if (!Subtarget.hasStdExtZbb() || !Subtarget.hasStdExtZba())
return SDValue();

unsigned NumElts = VT.getVectorNumElements();
unsigned ElemSizeInBits = ElemVT.getSizeInBits();
if (ElemSizeInBits >= std::min(Subtarget.getELen(), Subtarget.getXLen()) ||
NumElts % 2 != 0)
return SDValue();

// Produce [B,A] packed into a type twice as wide. Note that all
// scalars are XLenVT, possibly masked (see below).
MVT XLenVT = Subtarget.getXLenVT();
auto pack = [&](SDValue A, SDValue B) {
// Bias the scheduling of the inserted operations to near the
// definition of the element - this tends to reduce register
// pressure overall.
SDLoc ElemDL(B);
SDValue ShtAmt = DAG.getConstant(ElemSizeInBits, ElemDL, XLenVT);
return DAG.getNode(ISD::OR, ElemDL, XLenVT, A,
DAG.getNode(ISD::SHL, ElemDL, XLenVT, B, ShtAmt));
};

SDValue Mask = DAG.getConstant(
APInt::getLowBitsSet(XLenVT.getSizeInBits(), ElemSizeInBits), DL, XLenVT);
SmallVector<SDValue> NewOperands;
NewOperands.reserve(NumElts / 2);
for (unsigned i = 0; i < VT.getVectorNumElements(); i += 2) {
SDValue A = Op.getOperand(i);
SDValue B = Op.getOperand(i + 1);
// Bias the scheduling of the inserted operations to near the
// definition of the element - this tends to reduce register
// pressure overall.
A = DAG.getNode(ISD::AND, SDLoc(A), XLenVT, A, Mask);
B = DAG.getNode(ISD::AND, SDLoc(B), XLenVT, B, Mask);
NewOperands.push_back(pack(A, B));
}
assert(NumElts == NewOperands.size() * 2);
MVT WideVT = MVT::getIntegerVT(ElemSizeInBits * 2);
MVT WideVecVT = MVT::getVectorVT(WideVT, NumElts / 2);
return DAG.getNode(ISD::BITCAST, DL, VT,
DAG.getBuildVector(WideVecVT, DL, NewOperands));
}

// Convert to an vXf16 build_vector to vXi16 with bitcasts.
static SDValue lowerBUILD_VECTORvXf16(SDValue Op, SelectionDAG &DAG) {
MVT VT = Op.getSimpleValueType();
Expand Down Expand Up @@ -4006,6 +4065,13 @@ static SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
return convertFromScalableVector(VT, Vec, DAG, Subtarget);
}

// If we're about to resort to vslide1down (or stack usage), pack our
// elements into the widest scalar type we can. This will force a VL/VTYPE
// toggle, but reduces the critical path, the number of vslide1down ops
// required, and possibly enables scalar folds of the values.
if (SDValue Res = lowerBuildVectorViaPacking(Op, DAG, Subtarget))
return Res;

// For m1 vectors, if we have non-undef values in both halves of our vector,
// split the vector into low and high halves, build them separately, then
// use a vselect to combine them. For long vectors, this cuts the critical
Expand Down
Loading

0 comments on commit 03d4332

Please sign in to comment.