| Issue |
244147
|
| Summary |
[X86] LowerFLDEXP with AVX512-FP16 converts the i32 exponent to f16, which overflows: ldexp(0.0f16, 70000) and ldexp(inf, -70000) return NaN
|
| Labels |
backend:X86,
miscompilation,
floating-point,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```c
// clang -O2 -fno-math-errno -march=sapphirerapids repro.c && sde64 -spr -- ./a.out
// expected: ldexp(0, 70000) = 0, ldexp(inf, -70000) = inf; actual: NaN for both
#include <stdio.h>
#include <string.h>
__attribute__((noinline)) _Float16 f(_Float16 x, int e) { return __builtin_ldexpf16(x, e); }
int main(void) {
volatile int big = 70000;
_Float16 a = f((_Float16)0.0f, big), b = f((_Float16)__builtin_inff(), -big);
unsigned short ua, ub; memcpy(&ua, &a, 2); memcpy(&ub, &b, 2);
printf("ldexp(0,70000)=%04x ldexp(inf,-70000)=%04x\n", ua, ub);
if (ua != 0x0000 || ub != 0x7c00) { puts("MISCOMPILE"); return 1; }
puts("OK"); return 0;
}
```
Resulted in this output:
```
$ clang -O2 -fno-math-errno -march=sapphirerapids repro.c -o r && sde64 -spr -- ./r
ldexp(0,70000)=fe00 ldexp(inf,-70000)=fe00
MISCOMPILE
```
The expected output is `ldexp(0,70000)=0000 ldexp(inf,-70000)=7c00`, followed by `OK` (which `-march=x86-64-v3` prints). The vector forms (v8f16/v16f16/v32f16 with i32 or i64 exponents, e.g. `__builtin_elementwise_ldexp`) are affected the same way.
Trunk: https://godbolt.org/z/8T9z64Yo6 (clang trunk `-O2 -fno-math-errno -march=sapphirerapids`: `f` is `vcvtsi2sh xmm1, xmm31, edi; vscalefsh xmm0, xmm0, xmm1`).
Root cause: for f16 with FP16 (scalar, site "case MVT::f16", and the v8f16/v16f16/v32f16 VLX/512-bit cases), `LowerFLDEXP` (llvm/lib/Target/X86/X86ISelLowering.cpp) converts the integer exponent with `SINT_TO_FP` to **f16**. Any |e| > 65519 becomes ±inf. `vscalefph`/`vscalefsh` of (±0, +inf) and of (±inf, -inf) are invalid operations and return the default NaN, while `ldexp` must return ±0 and ±inf. The f32 and f64 paths are fine because the converted exponent stays finite.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs