| Issue |
235264
|
| Summary |
[clang][AMDGPU][R600] PerformDAGCombine folds `(f32 fp_round (f64 uint_to_fp i64 a))` into `(f32 uint_to_fp a)`: `(float)(double)a` loses its double rounding
|
| Labels |
backend:AMDGPU,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
/* [AMDGPU][R600] R600TargetLowering::PerformDAGCombine folds (f32 fp_round (f64 uint_to_fp a)) into
* (f32 uint_to_fp a). That is only valid when `a` converts to double exactly (<= 53 significant bits).
* For a 64-bit `a` the C _expression_ (float)(double)a rounds twice; the fold rounds once, so the result changes.
* a = 0x8000008000000001: (double)a = 2^63 + 2^39 (exact tie for float) -> (float) = 2^63 (0x5f000000)
* emitted r600 code computes the single-rounded (float)a = 2^63 + 2^40 (0x5f000001)
*
* Godbolt: compiler "x86-64 clang (trunk)", flags:
* --target=r600 -mcpu=cayman -O2 (also -mcpu=cypress / redwood; any R600 GPU)
* View the assembly of `conv`: buggy code has no f64 at all -- FFBH_UINT / BIT_ALIGN_INT / MIN_UINT (sticky) /
* UINT_TO_FLT / ADD_INT of the exponent: the single-rounding u64 -> f32 sequence.
* (Without the fold, R600 has no f64 conversion support; a correct lowering must round `a` to 53 bits first,
* e.g. with integer ops, before the u64 -> f32 sequence.)
* GPU code cannot run on Godbolt. The same file compiled for the host (x86-64, plain -O2, "Execute the code")
* has a main() that prints the value a correct compile must produce and the value the r600 code produces
* (= single rounding, reproduced on the host with (float)a), and returns 1 because they differ.
*
* EXPECTED (host): conv(0x8000008000000001) = 0x5f000000 ; r600 code computes 0x5f000001 -> MISMATCH, exit 1
*/
#ifdef __R600__
__attribute__((device_kernel)) void conv(__attribute__((address_space(1))) float *out, unsigned long long a) {
*out = (float)(double)a; /* must round twice */
}
#else
#include <stdio.h>
#include <string.h>
static unsigned bits(float f) { unsigned u; memcpy(&u, &f, 4); return u; }
int main(void) {
volatile unsigned long long a = 0x8000008000000001ULL;
float expected = (float)(double)a; /* C semantics: what conv must store */
float r600 = (float)a; /* what the r600 code (single rounding) stores */
printf("conv(%#llx): expected %#010x, r600 code computes %#010x\n", (unsigned long long)a, bits(expected), bits(r600));
if (bits(expected) != bits(r600)) { printf("MISMATCH (miscompile on r600)\n"); return 1; }
return 0;
}
#endif
```
Resulted in this output:
```
$ clang -O2 repro.c && ./a.out # host model of the check
conv(0x8000008000000001): expected 0x5f000000, r600 code computes 0x5f000001
MISMATCH (miscompile on r600)
(exit 1)
; conv, clang --target=r600 -mcpu=cayman -O2: no f64 step, only the single-rounding u64 -> f32 sequence
FFBH_UINT * T0.W, KC0[3].X,
BIT_ALIGN_INT T0.Z, KC0[3].X, KC0[2].W, 1,
BIT_ALIGN_INT T0.Z, PV.W, PV.Z, PV.Y,
MIN_UINT T1.Z, PV.W, 1,
UINT_TO_FLT T1.Z, PV.W,
ADD_INT * T0.X, PV.Z, PV.W,
```
| a | expected (float)(double)a | r600 code |
|---|---------------------------|-----------|
| 0x8000008000000001 | 0x5f000000 (2^63) | 0x5f000001 (2^63 + 2^40) |
(No R600 hardware was available. The r600 value comes from a bit-exact model of the emitted integer sequence, which equals the single-rounded `(float)a`.)
Trunk: https://godbolt.org/z/dYPPoKozh (x86-64 clang trunk, `--target=r600 -mcpu=cayman -O2`, assembly view; reproduces as of 2026-10-23)
Root cause: `R600TargetLowering::PerformDAGCombine` (llvm/lib/Target/AMDGPU/R600ISelLowering.cpp) rewrites `(f32 fp_round (f64 uint_to_fp a))` to `(f32 uint_to_fp a)` for an operand of any width. The IR rounds twice, first to double (53-bit significand) and then to float. The fold rounds once. The two results differ when the first rounding lands exactly on a float midpoint. For a = 2^63 + 2^39 + 1, `(double)a` is 2^63 + 2^39, which is a tie that rounds to even, giving 2^63. A single rounding of `a` gives 2^63 + 2^40. The fold is exact only when `a` has at most 53 significant bits (e.g. a zero-extended i32).
Suggested fix: Apply the fold only when the integer operand is known to have at most 53 significant bits. Otherwise, round the i64 to 53 significant bits with integer operations (nearest-even, with a sticky bit) before the u64 -> f32 sequence. Simply removing the fold would leave the conversion unselectable, because R600 has no f64 conversions. Not validated.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs