llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86] Unnecessary promotion for fneg / fabs half calls

Open RKSimon opened this issue 1 year ago • 15 comments

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

RKSimon avatar Feb 12 '25 11:02 RKSimon

@llvm/issue-subscribers-backend-x86

Author: Simon Pilgrim (RKSimon)

https://gcc.godbolt.org/z/PGshMcsTY
_Float16 fabs_f16(_Float16 x) {
    return static_cast&lt;_Float16&gt;(__builtin_elementwise_abs(x));
}
_Float16 fneg_f16(_Float16 x) {
    return static_cast&lt;_Float16&gt;(-(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

llvmbot avatar Feb 12 '25 11:02 llvmbot

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:

  1. 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.
  2. 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.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. 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.

llvmbot avatar Feb 12 '25 11:02 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Simon Pilgrim (RKSimon)

https://gcc.godbolt.org/z/PGshMcsTY
_Float16 fabs_f16(_Float16 x) {
    return static_cast&lt;_Float16&gt;(__builtin_elementwise_abs(x));
}
_Float16 fneg_f16(_Float16 x) {
    return static_cast&lt;_Float16&gt;(-(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

llvmbot avatar Feb 12 '25 11:02 llvmbot

I'm really interested to work on this! Could i get assigned?

StarOne01 avatar Feb 12 '25 15:02 StarOne01

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

BaoshanPang avatar Feb 15 '25 03:02 BaoshanPang

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?

StarOne01 avatar Feb 15 '25 03:02 StarOne01

Not every x86 processsor is supporting these instructions. They are supported with extension of AVX-512.

BaoshanPang avatar Feb 15 '25 03:02 BaoshanPang

So is this a issue?

StarOne01 avatar Feb 18 '25 12:02 StarOne01

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

RKSimon avatar Feb 18 '25 13:02 RKSimon

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!

StarOne01 avatar Feb 18 '25 13:02 StarOne01

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?

BaoshanPang avatar Feb 18 '25 18:02 BaoshanPang

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.

RKSimon avatar Feb 18 '25 18:02 RKSimon

Now I understand what we want to do. Thanks for the clarification.

BaoshanPang avatar Feb 18 '25 18:02 BaoshanPang

Note for correctness this should needs to be done at -O0 also. Because otherwise signaling nans will change to quite nans.

pinskia avatar Mar 24 '26 06:03 pinskia

@pinskia #188201

RKSimon avatar Mar 24 '26 09:03 RKSimon