-
Notifications
You must be signed in to change notification settings - Fork 14k
[RISCV] Initial codegen support for zvqdotq extension #137039
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
Changes from all commits
381c8ac
f3e724d
231fed6
1426d0e
ef5189f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6971,7 +6971,7 @@ static bool hasPassthruOp(unsigned Opcode) { | |
Opcode <= RISCVISD::LAST_STRICTFP_OPCODE && | ||
"not a RISC-V target specific op"); | ||
static_assert( | ||
RISCVISD::LAST_VL_VECTOR_OP - RISCVISD::FIRST_VL_VECTOR_OP == 134 && | ||
RISCVISD::LAST_VL_VECTOR_OP - RISCVISD::FIRST_VL_VECTOR_OP == 139 && | ||
RISCVISD::LAST_STRICTFP_OPCODE - RISCVISD::FIRST_STRICTFP_OPCODE == 21 && | ||
"adding target specific op should update this function"); | ||
if (Opcode >= RISCVISD::ADD_VL && Opcode <= RISCVISD::VFMAX_VL) | ||
|
@@ -6995,7 +6995,7 @@ static bool hasMaskOp(unsigned Opcode) { | |
Opcode <= RISCVISD::LAST_STRICTFP_OPCODE && | ||
"not a RISC-V target specific op"); | ||
static_assert( | ||
RISCVISD::LAST_VL_VECTOR_OP - RISCVISD::FIRST_VL_VECTOR_OP == 134 && | ||
RISCVISD::LAST_VL_VECTOR_OP - RISCVISD::FIRST_VL_VECTOR_OP == 139 && | ||
RISCVISD::LAST_STRICTFP_OPCODE - RISCVISD::FIRST_STRICTFP_OPCODE == 21 && | ||
"adding target specific op should update this function"); | ||
if (Opcode >= RISCVISD::TRUNCATE_VECTOR_VL && Opcode <= RISCVISD::SETCC_VL) | ||
|
@@ -18101,6 +18101,118 @@ static SDValue performBUILD_VECTORCombine(SDNode *N, SelectionDAG &DAG, | |
DAG.getBuildVector(VT, DL, RHSOps)); | ||
} | ||
|
||
static SDValue lowerVQDOT(unsigned Opc, SDValue Op0, SDValue Op1, | ||
const SDLoc &DL, SelectionDAG &DAG, | ||
const RISCVSubtarget &Subtarget) { | ||
assert(RISCVISD::VQDOT_VL == Opc || RISCVISD::VQDOTU_VL == Opc || | ||
RISCVISD::VQDOTSU_VL == Opc); | ||
MVT VT = Op0.getSimpleValueType(); | ||
assert(VT == Op1.getSimpleValueType() && | ||
VT.getVectorElementType() == MVT::i32); | ||
|
||
assert(VT.isFixedLengthVector()); | ||
MVT ContainerVT = getContainerForFixedLengthVector(DAG, VT, Subtarget); | ||
SDValue Passthru = convertToScalableVector( | ||
ContainerVT, DAG.getConstant(0, DL, VT), DAG, Subtarget); | ||
Op0 = convertToScalableVector(ContainerVT, Op0, DAG, Subtarget); | ||
Op1 = convertToScalableVector(ContainerVT, Op1, DAG, Subtarget); | ||
|
||
auto [Mask, VL] = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget); | ||
const unsigned Policy = RISCVVType::TAIL_AGNOSTIC | RISCVVType::MASK_AGNOSTIC; | ||
SDValue PolicyOp = DAG.getTargetConstant(Policy, DL, Subtarget.getXLenVT()); | ||
SDValue LocalAccum = DAG.getNode(Opc, DL, ContainerVT, | ||
{Op0, Op1, Passthru, Mask, VL, PolicyOp}); | ||
preames marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return convertFromScalableVector(VT, LocalAccum, DAG, Subtarget); | ||
} | ||
|
||
static MVT getQDOTXResultType(MVT OpVT) { | ||
ElementCount OpEC = OpVT.getVectorElementCount(); | ||
assert(OpEC.isKnownMultipleOf(4) && OpVT.getVectorElementType() == MVT::i8); | ||
return MVT::getVectorVT(MVT::i32, OpEC.divideCoefficientBy(4)); | ||
} | ||
|
||
static SDValue foldReduceOperandViaVQDOT(SDValue InVec, const SDLoc &DL, | ||
SelectionDAG &DAG, | ||
const RISCVSubtarget &Subtarget, | ||
const RISCVTargetLowering &TLI) { | ||
// Note: We intentionally do not check the legality of the reduction type. | ||
// We want to handle the m4/m8 *src* types, and thus need to let illegal | ||
// intermediate types flow through here. | ||
if (InVec.getValueType().getVectorElementType() != MVT::i32 || | ||
!InVec.getValueType().getVectorElementCount().isKnownMultipleOf(4)) | ||
return SDValue(); | ||
|
||
// reduce (zext a) <--> reduce (mul zext a. zext 1) | ||
// reduce (sext a) <--> reduce (mul sext a. sext 1) | ||
if (InVec.getOpcode() == ISD::ZERO_EXTEND || | ||
InVec.getOpcode() == ISD::SIGN_EXTEND) { | ||
SDValue A = InVec.getOperand(0); | ||
if (A.getValueType().getVectorElementType() != MVT::i8 || | ||
!TLI.isTypeLegal(A.getValueType())) | ||
return SDValue(); | ||
|
||
MVT ResVT = getQDOTXResultType(A.getSimpleValueType()); | ||
A = DAG.getBitcast(ResVT, A); | ||
SDValue B = DAG.getConstant(0x01010101, DL, ResVT); | ||
|
||
bool IsSigned = InVec.getOpcode() == ISD::SIGN_EXTEND; | ||
unsigned Opc = IsSigned ? RISCVISD::VQDOT_VL : RISCVISD::VQDOTU_VL; | ||
return lowerVQDOT(Opc, A, B, DL, DAG, Subtarget); | ||
} | ||
|
||
// mul (sext, sext) -> vqdot | ||
// mul (zext, zext) -> vqdotu | ||
// mul (sext, zext) -> vqdotsu | ||
// mul (zext, sext) -> vqdotsu (swapped) | ||
// TODO: Improve .vx handling - we end up with a sub-vector insert | ||
// which confuses the splat pattern matching. Also, match vqdotus.vx | ||
if (InVec.getOpcode() != ISD::MUL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just curious, what about left shifts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annoyingly complicated, possible future work. The problem is that we have to expand the shift as a multiply by 2^N, and the range of shift amounts we can handle is very limited due to the input being an i8. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p.s. I don't have a real case which benefits from this, if you do, please share and I'll re-prioritize. |
||
return SDValue(); | ||
|
||
SDValue A = InVec.getOperand(0); | ||
SDValue B = InVec.getOperand(1); | ||
unsigned Opc = 0; | ||
if (A.getOpcode() == B.getOpcode()) { | ||
if (A.getOpcode() == ISD::SIGN_EXTEND) | ||
Opc = RISCVISD::VQDOT_VL; | ||
else if (A.getOpcode() == ISD::ZERO_EXTEND) | ||
Opc = RISCVISD::VQDOTU_VL; | ||
else | ||
return SDValue(); | ||
} else { | ||
if (B.getOpcode() != ISD::ZERO_EXTEND) | ||
std::swap(A, B); | ||
if (A.getOpcode() != ISD::SIGN_EXTEND || B.getOpcode() != ISD::ZERO_EXTEND) | ||
return SDValue(); | ||
Opc = RISCVISD::VQDOTSU_VL; | ||
} | ||
assert(Opc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this ever got triggered? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't with the current code structure, but asserts exist to check assumptions? |
||
|
||
if (A.getOperand(0).getValueType().getVectorElementType() != MVT::i8 || | ||
A.getOperand(0).getValueType() != B.getOperand(0).getValueType() || | ||
!TLI.isTypeLegal(A.getValueType())) | ||
return SDValue(); | ||
|
||
MVT ResVT = getQDOTXResultType(A.getOperand(0).getSimpleValueType()); | ||
A = DAG.getBitcast(ResVT, A.getOperand(0)); | ||
B = DAG.getBitcast(ResVT, B.getOperand(0)); | ||
return lowerVQDOT(Opc, A, B, DL, DAG, Subtarget); | ||
} | ||
|
||
static SDValue performVECREDUCECombine(SDNode *N, SelectionDAG &DAG, | ||
const RISCVSubtarget &Subtarget, | ||
const RISCVTargetLowering &TLI) { | ||
if (!Subtarget.hasStdExtZvqdotq()) | ||
return SDValue(); | ||
|
||
SDLoc DL(N); | ||
EVT VT = N->getValueType(0); | ||
SDValue InVec = N->getOperand(0); | ||
if (SDValue V = foldReduceOperandViaVQDOT(InVec, DL, DAG, Subtarget, TLI)) | ||
return DAG.getNode(ISD::VECREDUCE_ADD, DL, VT, V); | ||
return SDValue(); | ||
} | ||
|
||
static SDValue performINSERT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG, | ||
const RISCVSubtarget &Subtarget, | ||
const RISCVTargetLowering &TLI) { | ||
|
@@ -19878,8 +19990,11 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N, | |
|
||
return SDValue(); | ||
} | ||
case ISD::CTPOP: | ||
case ISD::VECREDUCE_ADD: | ||
if (SDValue V = performVECREDUCECombine(N, DAG, Subtarget, *this)) | ||
return V; | ||
[[fallthrough]]; | ||
case ISD::CTPOP: | ||
if (SDValue V = combineToVCPOP(N, DAG, Subtarget)) | ||
return V; | ||
break; | ||
|
@@ -22401,6 +22516,9 @@ const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const { | |
NODE_NAME_CASE(RI_VUNZIP2A_VL) | ||
NODE_NAME_CASE(RI_VUNZIP2B_VL) | ||
NODE_NAME_CASE(RI_VEXTRACT) | ||
NODE_NAME_CASE(VQDOT_VL) | ||
NODE_NAME_CASE(VQDOTU_VL) | ||
NODE_NAME_CASE(VQDOTSU_VL) | ||
NODE_NAME_CASE(READ_CSR) | ||
NODE_NAME_CASE(WRITE_CSR) | ||
NODE_NAME_CASE(SWAP_CSR) | ||
|
Uh oh!
There was an error while loading. Please reload this page.