Issue 244593
Summary [fuzzer-found][NVPTX] LowerINT_TO_FP converts int -> bf16 through f32 below sm_90: double rounding, (__bf16)16842753 is 0x4B80 instead of 0x4B81
Labels miscompilation, backend:NVPTX, floating-point, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```cpp
/* [NVPTX] (unsigned/int) -> __bf16 below sm_90 is double-rounded through float:
 * NVPTXTargetLowering::LowerINT_TO_FP emits int -> f32 (cvt.rn.f32.u32 /
 * cvt.rn.f32.s32, rounds when |x| > 2^24) followed by FP_ROUND f32 -> bf16
 * (cvt.rn.bf16.f32, a second rounding). The result is not the correctly
 * rounded bf16 value. sm_90 has a direct cvt.rn.bf16.u32 and is correct.
 *
 * x = 16842753 = 2^24 + 2^16 + 1 lies just above the midpoint 2^24 + 2^16 between
 * the bf16 neighbours 2^24 (0x4B80) and 2^24 + 2^17 (0x4B81), so the exact
 * result is 0x4B81. Through float, x first rounds (tie, to even) to exactly
 * 2^24 + 2^16 (0x4B808000), the midpoint, which then ties to even: 0x4B80.
 *
 * Godbolt: compiler "x86-64 clang (trunk)" with flags:
 *   --target=nvptx64-nvidia-cuda -march=sm_80 -O2
 * (sm_50..sm_89 are affected; sm_50 rounds f32 -> bf16 with integer ops).
 * Godbolt cannot execute PTX: view the PTX of cvt_u32 / cvt_s32:
 *   buggy: cvt.rn.f32.u32 %r2, %r1 ; cvt.rn.bf16.f32 %rs1, %r2
 *   correct: one rounding, e.g. cvt.rn.f64.u32 (exact), a round-to-odd
 *            f64 -> f32 step, then cvt.rn.bf16.f32 (or sm_90's cvt.rn.bf16.u32).
 * Run: on any sm_80 GPU (e.g. as a CUDA device function); the arithmetic is the same.
 *
 * EXPECTED (correct compiler):  u32 = 19329 (0x4B81), s32 = 52097 (0xCB81)
 * BUGGY (vanilla trunk):        u32 = 19328 (0x4B80), s32 = 52096 (0xCB80)
 *                               MISMATCH (exit 1)
 */
#include <stdio.h>

__attribute__((noinline)) __bf16 cvt_u32(unsigned x) { return (__bf16)x; }
__attribute__((noinline)) __bf16 cvt_s32(int x) { return (__bf16)x; }

volatile unsigned gu = 16842753u;  /* 2^24 + 2^16 + 1 */
volatile int gs = -16842753;

int main(void) {
  __bf16 a = cvt_u32(gu), b = cvt_s32(gs);
  unsigned short ua, ub;
 __builtin_memcpy(&ua, &a, 2);
  __builtin_memcpy(&ub, &b, 2);
 printf("u32 = %d (expected 19329)\n", ua);
  printf("s32 = %d (expected 52097)\n", ub);
  if (ua != 19329 || ub != 52097) {
 printf("MISMATCH\n");
    return 1;
  }
  return 0;
}
```

Resulted in this output:
```
PTX (--target=nvptx64-nvidia-cuda -march=sm_80 -O2):
cvt_u32(...)
	ld.param.b32 	%r1, [cvt_u32_param_0];
	cvt.rn.f32.u32 	%r2, %r1;        // 1st rounding: 16842753 -> 0x4B808000
	cvt.rn.bf16.f32 	%rs1, %r2;   // 2nd rounding: exact midpoint, ties to even -> 0x4B80
	st.param.b16 	[func_retval0], %rs1;
(cvt_s32: cvt.rn.f32.s32 + cvt.rn.bf16.f32; sm_50 uses cvt.rn.f32.u32 + an integer RNE sequence;
 sm_90 emits a single cvt.rn.bf16.u32 and is correct.)

  input      | expected (correctly rounded) | actual
  16842753 | 19329 (0x4B81)               | 19328 (0x4B80)   <-- MISMATCH
  -16842753 | 52097 (0xCB81)               | 52096 (0xCB80)   <-- MISMATCH

(Not executed: no NVIDIA GPU here. PTX cvt.rn is IEEE RNE, so the values follow from the two
conversions; checked with exact arithmetic: RNE8(x) = 0x4B81, RNE8(RNE24(x)) = 0x4B80.)
```

Trunk: https://godbolt.org/z/jo78svTPj (x86-64 clang trunk, --target=nvptx64-nvidia-cuda -march=sm_80 -O2; PTX only)

Root cause: Below sm_90 there is no direct integer -> bf16 cvt, and NVPTXTargetLowering::LowerINT_TO_FP (llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp) lowers it as [su]int_to_fp to f32 followed by FP_ROUND f32 -> bf16. int -> f32 rounds for |x| > 2^24, and the second rounding to 8 significand bits then double-rounds: 2^24 + 2^16 + 1 first ties to exactly the bf16 midpoint and then ties to even downward. sm_50..sm_89 are affected for i32 and i64 sources; this is a separate copy from the generic/X86 path in #234116.

Suggested fix: For sources of <= 32 bits convert to f64 first (exact), then let FP_ROUND f64 -> bf16 expand with a round-to-odd f64 -> f32 step before cvt.rn.bf16.f32; i64 sources need the same round-to-odd treatment or a libcall. Validated locally for i32 (a patched tree carries this fix and the generated PTX computes 0x4B81 / 0xCB81, checked by modeling the PTX since no GPU is available).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to