Open overmighty opened 1 month ago
@llvm/issue-subscribers-backend-x86
Author: OverMighty (overmighty)
Something simpler like fabs bit-twiddle is just as bad:
_Float16 abs_f16(_Float16 x) {
return static_cast<_Float16>(__builtin_elementwise_abs(x));
}
abs_f16(_Float16): # @abs_f16(_Float16)
vpextrw $0, %xmm0, %eax
vmovd %eax, %xmm0
vcvtph2ps %xmm0, %xmm0
vandps .LCPI0_0(%rip), %xmm0, %xmm0
vcvtps2ph $4, %xmm0, %xmm0
vmovd %xmm0, %eax
vpinsrw $0, %eax, %xmm0, %xmm0
retq
This should probably be using __builtin_ceilf16
but InstCombine takes care of it anyway. I'm wondering why this case needs to set up a stack frame since it doesn't look used, https://godbolt.org/z/v4Y1eh5hv.
I used __builtin_ceilf
because GCC without -mavx512fp16
handles __builtin_ceilf16
by just generating a call to the libc's ceilf16
.
@FreddyLeaf @phoebewang Can you look at this?
The GCC code generation is suboptimal either. The vcvtph2ps/vinsertps
can be eliminated too. Clang here handles it for calling convention more stiffly. It would get better if we exclude the ABI handling https://godbolt.org/z/Wcahzb79E.
That says, the support of FP16 on targets without AVX512FP16 is mainly for verification. The performance is not a urgent goal for us, though it's always better to improve.
https://godbolt.org/z/vc4Y1r6Mq
C++ code:
GCC output with
-O3 -march=raptorlake -fno-omit-frame-pointer
(takes 1.33-1.64 ns on i7-13700H):Clang output with
-O3 -march=raptorlake -fno-omit-frame-pointer
(takes ~9.12 ns on i7-13700H):