| Issue |
235612
|
| Summary |
[RISCV] Zvfbfmin vector sitofp/uitofp i32/i64 -> bf16 converts through f32 and rounds twice
|
| Labels |
backend:RISC-V,
miscompilation,
floating-point,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
// RISC-V V + Zvfbfmin: vector int -> bf16 is lowered as int -> f32 (vfcvt.f.x.v / vfncvt.f.x.w) then
// f32 -> bf16 (vfncvtbf16.f.f.w): two roundings. (__bf16)16842753 = 0x4B81, the vector code gives 0x4B80.
// clang --target=riscv64-linux-gnu -march=rv64gcv_zvfbfmin -O2 (also -O0; run: qemu-riscv64 -cpu rv64,v=true,x-zvfbfmin=true,x-zfbfmin=true)
int printf(const char *, ...);
typedef int v4i __attribute__((ext_vector_type(4)));
typedef long long v4l __attribute__((ext_vector_type(4)));
typedef __bf16 v4bf __attribute__((ext_vector_type(4)));
__attribute__((noinline)) void cv32(v4i *p, v4bf *q) { *q = __builtin_convertvector(*p, v4bf); }
__attribute__((noinline)) void cv64(v4l *p, v4bf *q) { *q = __builtin_convertvector(*p, v4bf); }
int main(void) {
v4i a = {16842753, -16842753, 0x40404040, 1}; /* 2^24 + 2^16 + 1 is just above a bf16 midpoint */
v4l b = {16842753, 0x0101000000000001LL, -16842753, 1};
unsigned short e1[4] = {0x4b81, 0xcb81, 0x4e81, 0x3f80}, e2[4] = {0x4b81, 0x5b81, 0xcb81, 0x3f80};
v4bf r1, r2;
cv32(&a, &r1); cv64(&b, &r2);
int bad = 0;
for (int i = 0; i < 4; i++) {
unsigned short u, v;
__builtin_memcpy(&u, (char *)&r1 + 2 * i, 2); __builtin_memcpy(&v, (char *)&r2 + 2 * i, 2);
printf("lane %d: i32 0x%04x (expected 0x%04x) i64 0x%04x (expected 0x%04x)\n", i, u, e1[i], v, e2[i]);
bad |= u != e1[i] || v != e2[i];
}
printf(bad ? "MISCOMPILE\n" : "OK\n");
return bad;
}
```
Resulted in this output (`clang --target=riscv64-linux-gnu -march=rv64gcv_zvfbfmin -O2`, run under `qemu-riscv64 -cpu rv64,v=true,x-zvfbfmin=true,x-zfbfmin=true` through an equivalent freestanding program, `repro_freestanding.c`; -O0 is the same):
```
4b80 cb80 4e81 3f80 (i32, expected 4b81 cb81 4e81 3f80)
4b80 5b80 cb80 3f80 (i64, expected 4b81 5b81 cb81 3f80)
MISCOMPILE
```
| input | correctly rounded bf16 | vector result |
|---|---|---|
| 16842753 (2^24 + 2^16 + 1) | 0x4B81 | **0x4B80** |
| -16842753 | 0xCB81 | **0xCB80** |
| 0x0101000000000001 (i64) | 0x5B81 | **0x5B80** |
The expected values were computed exactly in Python with `fractions`. The vector code is `vfcvt.f.x.v` / `vfncvt.f.x.w` (int to f32), then `vfncvtbf16.f.f.w` (f32 to bf16).
Trunk: https://godbolt.org/z/zcEPh4d89
Root cause: `RISCVTargetLowering::LowerOperation` (SINT_TO_FP/UINT_TO_FP, "Fall back to zvfbfmin for bf16 case if source type is wider than 8 bits") lowers a vector integer to bf16 conversion as `fp_round(int_to_fp(x) : f32)`. The same path is taken with Zvfbfa for sources wider than 8 bits, and the i64 case also goes through the "narrowing int_to_fp then fp_round" path. For i32/i64 sources the f32 conversion is itself inexact, so the value is rounded twice: an integer just above a bf16 rounding midpoint, like 2^24 + 2^16 + 1, rounds to f32 on the midpoint and then to even. The same fallback is exact for f16 results and for i8/i16 sources.
Suggested fix (not validated): for bf16 results with i32 sources, convert exactly to f64 (`vfwcvt.f.x.v`), narrow to f32 with round-to-odd (`vfncvt.rod.f.f.w`), then narrow to bf16. For i64 sources, do the f32 step with round-to-odd (for example via an integer sticky-bit fixup), or scalarize to a correctly rounded conversion.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs