-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[RISCV] Form vredsum from explode_vector + scalar (left) reduce #67821
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 1 commit
f224bd2
dc9a20c
1345207
ed57234
38a60f0
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 |
---|---|---|
|
@@ -11122,6 +11122,85 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N, | |
} | ||
} | ||
|
||
/// Perform two related transforms whose purpose is to incrementally recognize | ||
/// an explode_vector followed by scalar reduction as a vector reduction node. | ||
/// This exists to recover from a deficiency in SLP which can't handle | ||
/// forrests with multiple roots sharing common nodes. In some cases, one | ||
/// of the trees will be vectorized, and the other will remain (unprofitably) | ||
/// scalarized. | ||
static SDValue combineBinOpOfExtractToReduceTree(SDNode *N, SelectionDAG &DAG, | ||
const RISCVSubtarget &Subtarget) { | ||
|
||
// This transforms need to run before all integer types have been legalized | ||
// to i64 (so that the vector element type matches the add type), and while | ||
// it's safe to introduce odd sized vector types. | ||
if (DAG.NewNodesMustHaveLegalTypes) | ||
return SDValue(); | ||
|
||
const SDLoc DL(N); | ||
const EVT VT = N->getValueType(0); | ||
const unsigned Opc = N->getOpcode(); | ||
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. Unused var in release builds I think? |
||
assert(Opc == ISD::ADD && "extend this to other reduction types"); | ||
const SDValue LHS = N->getOperand(0); | ||
const SDValue RHS = N->getOperand(1); | ||
|
||
if (!LHS.hasOneUse() || !RHS.hasOneUse()) | ||
return SDValue(); | ||
|
||
if (RHS.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | ||
!isa<ConstantSDNode>(RHS.getOperand(1))) | ||
return SDValue(); | ||
|
||
SDValue SrcVec = RHS.getOperand(0); | ||
EVT SrcVecVT = SrcVec.getValueType(); | ||
assert(SrcVecVT.getVectorElementType() == VT); | ||
if (SrcVecVT.isScalableVector()) | ||
return SDValue(); | ||
|
||
if (SrcVecVT.getScalarSizeInBits() > Subtarget.getELen()) | ||
return SDValue(); | ||
|
||
// match binop (extract_vector_elt V, 0), (extract_vector_elt V, 1) to | ||
// reduce_op (extract_subvector [2 x VT] from V). This will form the | ||
// root of our reduction tree. TODO: We could extend this to any two | ||
// adjacent constant indices if desired. | ||
if (LHS.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | ||
LHS.getOperand(0) == SrcVec && isNullConstant(LHS.getOperand(1)) && | ||
isOneConstant(RHS.getOperand(1))) { | ||
EVT ReduceVT = EVT::getVectorVT(*DAG.getContext(), VT, 2); | ||
SDValue Vec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ReduceVT, SrcVec, | ||
DAG.getVectorIdxConstant(0, DL)); | ||
return DAG.getNode(ISD::VECREDUCE_ADD, DL, VT, Vec); | ||
} | ||
|
||
// Match (binop reduce (extract_subvector V, 0), | ||
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. Put parentheses around |
||
// extract_vector_elt V, sizeof(SubVec)) | ||
// into a reduction of one more element from the original vector V. | ||
if (LHS.getOpcode() != ISD::VECREDUCE_ADD) | ||
return SDValue(); | ||
|
||
SDValue ReduceVec = LHS.getOperand(0); | ||
if (ReduceVec.getOpcode() == ISD::EXTRACT_SUBVECTOR && | ||
ReduceVec.hasOneUse() && ReduceVec.getOperand(0) == RHS.getOperand(0) && | ||
isNullConstant(ReduceVec.getOperand(1)) && | ||
isa<ConstantSDNode>(RHS.getOperand(1))) { | ||
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. RHS.getOperand(1) is known to be a ConstantSDNode from line 11151 right? 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. I don't think this was addressed. I think this |
||
uint64_t Idx = cast<ConstantSDNode>(RHS.getOperand(1))->getZExtValue(); | ||
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. I'm not sure you can guarantee Idx fits in 64 bits here. It would definitely be UB if it didn't fit, but the getZExtValue() would assert in that case. |
||
if (ReduceVec.getValueType().getVectorNumElements() == Idx) { | ||
// For illegal types (e.g. 3xi32), most will be combined again into a | ||
// wider (hopefully legal) type. If this is a terminal state, we are | ||
// relying on type legalization here to poduce something reasonable | ||
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. poduce -> produce |
||
// and this lowering quality could probably be improved. (TODO) | ||
EVT ReduceVT = EVT::getVectorVT(*DAG.getContext(), VT, Idx+1); | ||
SDValue Vec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ReduceVT, SrcVec, | ||
DAG.getVectorIdxConstant(0, DL)); | ||
return DAG.getNode(ISD::VECREDUCE_ADD, DL, VT, Vec); | ||
} | ||
} | ||
|
||
return SDValue(); | ||
} | ||
|
||
|
||
// Try to fold (<bop> x, (reduction.<bop> vec, start)) | ||
static SDValue combineBinOpToReduce(SDNode *N, SelectionDAG &DAG, | ||
const RISCVSubtarget &Subtarget) { | ||
|
@@ -11449,6 +11528,9 @@ static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG, | |
return V; | ||
if (SDValue V = combineBinOpToReduce(N, DAG, Subtarget)) | ||
return V; | ||
if (SDValue V = combineBinOpOfExtractToReduceTree(N, DAG, Subtarget)) | ||
return V; | ||
|
||
// fold (add (select lhs, rhs, cc, 0, y), x) -> | ||
// (select lhs, rhs, cc, x, (add x, y)) | ||
return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forrests -> forests