Commit that relands _Float16 type support on X86 causes correctness error
Last reland of the _Float16 type support on X86 (655ba9c8a1d220) causes correctness error.
Reproducer:
float myabs(float value) {
if (value > 0) return value;
return value * (-1.f);
}
// The test passes if we remove the `__attribute__((noinline))`.
void __attribute__((noinline)) apply_myabs(
const __fp16* input, __fp16* output, int size) {
for (int i = 0; i < size; i++) {
const __fp16 value = input[i];
output[i] = myabs(value);
}
}
bool is_near(float value, float expected, float epsilon) {
auto diff = value - expected;
if (diff < 0) {
diff *= -1;
}
return diff <= epsilon;
}
int main(int argc, char* argv[]) {
const __fp16 input_values[] = {1, -1, 0};
const __fp16 output_ref[] = {1, 1, 0};
__fp16 output[3];
apply_myabs(input_values, output, 3);
for (int i = 0; i < 3; ++i) {
if (!is_near(output[i], output_ref[i], 1e-3f)) {
return 1;
}
}
return 0;
}
Command to build the test:
clang -O3 test.cc -o /tmp/a.out
Building with clang at the specified commit will exit with 1 and at the previous version will exit with 0.
@phoebewang could you revert the culprit commit while the issue is fixed? Please let me know if you need additional info.
@llvm/issue-subscribers-backend-x86
A bit simpler test case:
float __attribute__((noinline)) n(float value) { return -value; }
int main(int argc, char* argv[]) { return (__fp16)n(-1.f) != 1; }
And an even smaller one:
$ cat q.cc
float __attribute__((noinline)) n(float value) { return -value; }
int main(int argc, char* argv[]) { return (__fp16)n(-1.f); }
$ good-clang -O2 q.cc ; ./a.out ; echo $?
1
$ bad-clang -O2 q.cc ; ./a.out ; echo $?
0
And the difference in x86 assembly is:
--- q-good.s 2022-06-25 00:51:08.000000000 +0200
+++ q-bad.s 2022-06-25 00:51:41.000000000 +0200
@@ -1,51 +1,50 @@
.text
.file "q.cc"
.section .rodata.cst16,"aM",@progbits,16
.p2align 4 # -- Begin function _Z1nf
.LCPI0_0:
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.long 0x80000000 # float -0
.text
.globl _Z1nf
.p2align 4, 0x90
.type _Z1nf,@function
_Z1nf: # @_Z1nf
.cfi_startproc
# %bb.0:
xorps .LCPI0_0(%rip), %xmm0
retq
.Lfunc_end0:
.size _Z1nf, .Lfunc_end0-_Z1nf
.cfi_endproc
# -- End function
.section .rodata.cst4,"aM",@progbits,4
.p2align 2 # -- Begin function main
.LCPI1_0:
.long 0xbf800000 # float -1
.text
.globl main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0:
pushq %rax
.cfi_def_cfa_offset 16
movss .LCPI1_0(%rip), %xmm0 # xmm0 = mem[0],zero,zero,zero
callq _Z1nf
- callq __gnu_f2h_ieee@PLT
- movzwl %ax, %edi
- callq __gnu_h2f_ieee@PLT
+ callq __truncsfhf2@PLT
+ callq __extendhfsf2@PLT
cvttss2si %xmm0, %eax
popq %rcx
.cfi_def_cfa_offset 8
retq
.Lfunc_end1:
.size main, .Lfunc_end1-main
.cfi_endproc
# -- End function
- .ident "clang version trunk (a2232da2a5d207126e1777fb33b61a16109ae38e)"
+ .ident "clang version trunk (6f258c0fd34cf4001ffa08c61f6e4e0f1254c50f)"
.section ".note.GNU-stack","",@progbits
.addrsig
The diff in the assembly is correct. @bgra8 @alexfh It works with GCC 12.0. Did you build it with compiler-rt? I knew compiler-rt has a problem due to the ABI change. See https://discourse.llvm.org/t/how-to-build-compiler-rt-for-new-x86-half-float-abi/63366 Maybe I should enable the front support when I reland it so that we can build compiler-rt with the correct ABI.
A patch to enable the _Float16 type: https://reviews.llvm.org/D128571
@phoebewang I'm compiling the program with clang.
I patched https://reviews.llvm.org/D128571 ontop of the culprit reported in this issue and I get the exact same behavior with both my repro and @alexfh shortest reproducer.
Could you please verify for yourself?
@bgra8 Do you mean you compiled your repro with the patched Clang? This is not the right way to solve the problem.
I assuming you are using compiler-rt. If it is true, you need to re-compile compiler-rt with the patched Clang, maybe need to config with COMPILER_RT_HAS_FLOAT16.
If you are using your own runtime, you need to check your implementation of the __truncsfhf2/__extendhfsf2, and solve it in the similar way like #56156.