-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[X86][BF16] Improve vectorization of BF16 #88486
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21433,25 +21433,9 @@ SDValue X86TargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { | |
return Res; | ||
} | ||
|
||
if (!SVT.isVector()) | ||
if (!SVT.isVector() || SVT.getVectorElementType() == MVT::bf16) | ||
return Op; | ||
|
||
if (SVT.getVectorElementType() == MVT::bf16) { | ||
// FIXME: Do we need to support strict FP? | ||
assert(!IsStrict && "Strict FP doesn't support BF16"); | ||
if (VT.getVectorElementType() == MVT::f64) { | ||
MVT TmpVT = VT.changeVectorElementType(MVT::f32); | ||
return DAG.getNode(ISD::FP_EXTEND, DL, VT, | ||
DAG.getNode(ISD::FP_EXTEND, DL, TmpVT, In)); | ||
} | ||
assert(VT.getVectorElementType() == MVT::f32 && "Unexpected fpext"); | ||
MVT NVT = SVT.changeVectorElementType(MVT::i32); | ||
In = DAG.getBitcast(SVT.changeTypeToInteger(), In); | ||
In = DAG.getNode(ISD::ZERO_EXTEND, DL, NVT, In); | ||
In = DAG.getNode(ISD::SHL, DL, NVT, In, DAG.getConstant(16, DL, NVT)); | ||
return DAG.getBitcast(VT, In); | ||
} | ||
|
||
if (SVT.getVectorElementType() == MVT::f16) { | ||
if (Subtarget.hasFP16() && isTypeLegal(SVT)) | ||
return Op; | ||
|
@@ -56517,17 +56501,40 @@ static SDValue combineFP16_TO_FP(SDNode *N, SelectionDAG &DAG, | |
|
||
static SDValue combineFP_EXTEND(SDNode *N, SelectionDAG &DAG, | ||
const X86Subtarget &Subtarget) { | ||
EVT VT = N->getValueType(0); | ||
bool IsStrict = N->isStrictFPOpcode(); | ||
SDValue Src = N->getOperand(IsStrict ? 1 : 0); | ||
EVT SrcVT = Src.getValueType(); | ||
|
||
SDLoc dl(N); | ||
if (SrcVT.getScalarType() == MVT::bf16) { | ||
assert(!IsStrict && "Strict FP doesn't support BF16"); | ||
if (Src.getOpcode() == ISD::FP_ROUND && | ||
Src.getOperand(0).getValueType() == VT) | ||
return Src.getOperand(0); | ||
|
||
if (!SrcVT.isVector()) | ||
return SDValue(); | ||
|
||
if (VT.getVectorElementType() == MVT::f64) { | ||
MVT TmpVT = VT.getSimpleVT().changeVectorElementType(MVT::f32); | ||
return DAG.getNode(ISD::FP_EXTEND, dl, VT, | ||
DAG.getNode(ISD::FP_EXTEND, dl, TmpVT, Src)); | ||
} | ||
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. Would this be OK to perform on scalars too? 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. We have handled it https://godbolt.org/z/r7cjPx9xY |
||
assert(VT.getVectorElementType() == MVT::f32 && "Unexpected fpext"); | ||
MVT NVT = SrcVT.getSimpleVT().changeVectorElementType(MVT::i32); | ||
Src = DAG.getBitcast(SrcVT.changeTypeToInteger(), Src); | ||
Src = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Src); | ||
Src = DAG.getNode(ISD::SHL, dl, NVT, Src, DAG.getConstant(16, dl, NVT)); | ||
return DAG.getBitcast(VT, Src); | ||
} | ||
|
||
if (!Subtarget.hasF16C() || Subtarget.useSoftFloat()) | ||
return SDValue(); | ||
|
||
if (Subtarget.hasFP16()) | ||
return SDValue(); | ||
|
||
bool IsStrict = N->isStrictFPOpcode(); | ||
EVT VT = N->getValueType(0); | ||
SDValue Src = N->getOperand(IsStrict ? 1 : 0); | ||
EVT SrcVT = Src.getValueType(); | ||
|
||
if (!SrcVT.isVector() || SrcVT.getVectorElementType() != MVT::f16) | ||
return SDValue(); | ||
|
||
|
@@ -56539,8 +56546,6 @@ static SDValue combineFP_EXTEND(SDNode *N, SelectionDAG &DAG, | |
if (NumElts == 1 || !isPowerOf2_32(NumElts)) | ||
return SDValue(); | ||
|
||
SDLoc dl(N); | ||
|
||
// Convert the input to vXi16. | ||
EVT IntVT = SrcVT.changeVectorElementTypeToInteger(); | ||
Src = DAG.getBitcast(IntVT, Src); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hi, several weeks late, but I don't think this optimization is correct even the absence of some sort of strictness mode.
That is,
extend(round(x))
isn't meaningfully equal tox
to a reasonable level of precision - the code I set this off with is explicitly usingin order to "lose" extra precision from a f32 computation being used as a reference for a bfloat one.
(The high-level structure goes like this
I'll also note that this transformation isn't, from what I can tell, present for
half
Please revert this conditional specifically or justify it, thanks!
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.
There are some discussions in 3cf8535
bfloat
is different fromhalf
in two ways:bfloat
has fewer fraction bits, so precision should not be a concern like other types (evenhalf
) by design;half
is an IEEE type, whilebfloat
is not. We don't necessarily follow it;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.
Discussion noted. However, I as the programmer want to explicitly perform that truncate-extend behavior in one spot in my input (because I'm testing a bfloat function whose result has been
fpext
ed to against a floating-point version that's had its lower bits masked off).This rewrite has caused per-element result errors around 1e-2 (if I remember right 16.25 vs 16.3125 or the like)
I understand that this intermediate elimination improves performance and is numerically useful a lot of the time, so, given that ... what mechanism would you recommend for forcing this optimization to not fire for a particular pair of round and extend operations?
(I don't want to make things strict at the function level if possible - I want to protect a particular fptrunc / fpext pair from this optimization. Would
canoicalize
do it, or should I stick in s more interesting noop?)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.
After #90836 landed, you can use
__arithmetic_fence
to achieve this, e.g., https://godbolt.org/z/vYr1z3h71There 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.
After #90836 landed, you can use
__arithmetic_fence
to achieve this, e.g., https://godbolt.org/z/vYr1z3h71