[X86] Unnecessary promotion for fneg / fabs half calls
https://gcc.godbolt.org/z/PGshMcsTY
_Float16 fabs_f16(_Float16 x) {
return static_cast<_Float16>(__builtin_elementwise_abs(x));
}
_Float16 fneg_f16(_Float16 x) {
return static_cast<_Float16>(-(x));
}
These shouldn't require vcvtph2ps/vcvtps2ph or __extendhfsf2/__truncsfhf2 calls - we should be able to use the bitwise ops directly on the v8f16 type.
.LCPI0_0:
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
fabs_f16(_Float16): # @fabs_f16(_Float16)
# %bb.0: # %entry
vcvtph2ps %xmm0, %xmm0
vandps .LCPI0_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
retq
# -- End function
.LCPI1_0:
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
fneg_f16(_Float16): # @fneg_f16(_Float16)
# %bb.0: # %entry
vcvtph2ps %xmm0, %xmm0
vxorps .LCPI1_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
retq
# -- End function
@llvm/issue-subscribers-backend-x86
Author: Simon Pilgrim (RKSimon)
_Float16 fabs_f16(_Float16 x) {
return static_cast<_Float16>(__builtin_elementwise_abs(x));
}
_Float16 fneg_f16(_Float16 x) {
return static_cast<_Float16>(-(x));
}
These shouldn't require vcvtph2ps/vcvtps2ph or __extendhfsf2/__truncsfhf2 calls - we should be able to use the bitwise ops directly on the v8f16 type.
.LCPI0_0:
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
fabs_f16(_Float16): # @<!-- -->fabs_f16(_Float16)
# %bb.0: # %entry
vcvtph2ps %xmm0, %xmm0
vandps .LCPI0_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
retq
# -- End function
.LCPI1_0:
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
fneg_f16(_Float16): # @<!-- -->fneg_f16(_Float16)
# %bb.0: # %entry
vcvtph2ps %xmm0, %xmm0
vxorps .LCPI1_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
retq
# -- End function
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
- In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Simon Pilgrim (RKSimon)
_Float16 fabs_f16(_Float16 x) {
return static_cast<_Float16>(__builtin_elementwise_abs(x));
}
_Float16 fneg_f16(_Float16 x) {
return static_cast<_Float16>(-(x));
}
These shouldn't require vcvtph2ps/vcvtps2ph or __extendhfsf2/__truncsfhf2 calls - we should be able to use the bitwise ops directly on the v8f16 type.
.LCPI0_0:
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
.long 0x7fffffff # float NaN
fabs_f16(_Float16): # @<!-- -->fabs_f16(_Float16)
# %bb.0: # %entry
vcvtph2ps %xmm0, %xmm0
vandps .LCPI0_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
retq
# -- End function
.LCPI1_0:
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
fneg_f16(_Float16): # @<!-- -->fneg_f16(_Float16)
# %bb.0: # %entry
vcvtph2ps %xmm0, %xmm0
vxorps .LCPI1_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
retq
# -- End function
I'm really interested to work on this! Could i get assigned?
This maybe not a bug. If using option -mavx512fp16, then I can see the code would be optimized: https://gcc.godbolt.org/z/9fjv1cKx3
.LCPI0_0:
.short 0x7fff # half NaN
fabs_f16(_Float16): # @fabs_f16(_Float16)
# %bb.0: # %entry
vpbroadcastw .LCPI0_0(%rip), %xmm1 # xmm1 = [NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]
vpand %xmm1, %xmm0, %xmm0
retq
# -- End function
.LCPI1_0:
.short 0x8000 # half -0
fneg_f16(_Float16): # @fneg_f16(_Float16)
# %bb.0: # %entry
vpbroadcastw .LCPI1_0(%rip), %xmm1 # xmm1 = [-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0]
vpxor %xmm1, %xmm0, %xmm0
retq
# -- End function
This maybe not a bug. If using option -mavx512fp16, then I can see the code would be optimized: https://gcc.godbolt.org/z/9fjv1cKx3
.LCPI0_0: .short 0x7fff # half NaN fabs_f16(_Float16): # @fabs_f16(_Float16) # %bb.0: # %entry vpbroadcastw .LCPI0_0(%rip), %xmm1 # xmm1 = [NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN] vpand %xmm1, %xmm0, %xmm0 retq # -- End function .LCPI1_0: .short 0x8000 # half -0 fneg_f16(_Float16): # @fneg_f16(_Float16) # %bb.0: # %entry vpbroadcastw .LCPI1_0(%rip), %xmm1 # xmm1 = [-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0] vpxor %xmm1, %xmm0, %xmm0 retq # -- End function
That's interesting, is there any reason why it is unoptimized in a normal build?
Not every x86 processsor is supporting these instructions. They are supported with extension of AVX-512.
So is this a issue?
Yes this is an issue - we shouldn't require extension/truncation (either by libcall or instruction) via a float type to handle what is just a bitwise operation
Yes this is an issue - we shouldn't require extension/truncation (either by libcall or instruction) via a float type to handle what is just a bitwise operation
Got it!
vandps and vxorps are SIMD (Single Instruction, Multiple Data) instructions in the x86 instruction set, specifically part of the AVX (Advanced Vector Extensions) instruction set.
If the processor doesn't has the AVX module, how can we use them?
We treat MVT::f16 as a legal type on SSE2+ targets, so anything with andps/xorps will be fine. LowerFABSorFNEG is already setup for all of this - it really should be a trivial patch.
Now I understand what we want to do. Thanks for the clarification.
Note for correctness this should needs to be done at -O0 also. Because otherwise signaling nans will change to quite nans.
@pinskia #188201