Skip to content

[RISCV] Directly use pack* in build_vector lowering #98084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3905,6 +3905,21 @@ static SDValue lowerBuildVectorOfConstants(SDValue Op, SelectionDAG &DAG,
return SDValue();
}

static unsigned getPACKOpcode(unsigned DestBW,
const RISCVSubtarget &Subtarget) {
switch (DestBW) {
default:
llvm_unreachable("Unsupported pack size");
case 16:
return RISCV::PACKH;
case 32:
return Subtarget.is64Bit() ? RISCV::PACKW : RISCV::PACK;
case 64:
assert(Subtarget.is64Bit());
return RISCV::PACK;
}
}

/// 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
Expand Down Expand Up @@ -3933,30 +3948,34 @@ static SDValue lowerBuildVectorViaPacking(SDValue Op, SelectionDAG &DAG,
// Produce [B,A] packed into a type twice as wide. Note that all
// scalars are XLenVT, possibly masked (see below).
MVT XLenVT = Subtarget.getXLenVT();
SDValue Mask = DAG.getConstant(
APInt::getLowBitsSet(XLenVT.getSizeInBits(), ElemSizeInBits), DL, XLenVT);
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);
if (Subtarget.hasStdExtZbkb())
// Note that we're relying on the high bits of the result being
// don't care. For PACKW, the result is *sign* extended.
return SDValue(
DAG.getMachineNode(getPACKOpcode(ElemSizeInBits * 2, Subtarget),
ElemDL, XLenVT, A, B),
0);

A = DAG.getNode(ISD::AND, SDLoc(A), XLenVT, A, Mask);
B = DAG.getNode(ISD::AND, SDLoc(B), XLenVT, B, Mask);
SDValue ShtAmt = DAG.getConstant(ElemSizeInBits, ElemDL, XLenVT);
SDNodeFlags Flags;
Flags.setDisjoint(true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the disjoint change here is arguably unrelated. I can separate this out if folks want. I did it in one change because I realized it when playing with pack* matching options. By it's own, this line has no codegen effect.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with it.

return DAG.getNode(ISD::OR, ElemDL, XLenVT, A,
DAG.getNode(ISD::SHL, ElemDL, XLenVT, B, ShtAmt));
DAG.getNode(ISD::SHL, ElemDL, XLenVT, B, ShtAmt), Flags);
};

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));
}
for (unsigned i = 0; i < VT.getVectorNumElements(); i += 2)
NewOperands.push_back(pack(Op.getOperand(i), Op.getOperand(i + 1)));
assert(NumElts == NewOperands.size() * 2);
MVT WideVT = MVT::getIntegerVT(ElemSizeInBits * 2);
MVT WideVecVT = MVT::getVectorVT(WideVT, NumElts / 2);
Expand Down
Loading
Loading