| Issue |
235731
|
| Summary |
[X86] sitofp/uitofp i32 -> bfloat double-rounds through f32 (vcvtsi2ss + __truncsfbf2), one ULP off
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
leidian977
|
On x86_64, integer-to-bfloat conversions at every code level (SSE2 baseline
through AVX512-BF16) are lowered as `vcvtsi2ss` / `vcvtusi2ss` (integer ->
f32, rounded) followed by `__truncsfbf2` (f32 -> bf16, rounded again). For
i32 sources whose magnitude is at least 2^24, the f32 conversion is itself
inexact, so the value is rounded twice and the final bf16 can be one ULP off
from the correctly rounded result.
This is the same double-rounding pattern as #235612 (reported there for
RISC-V Zvfbfmin vector codegen) and #234750 (ExpandIRInsts), but this one is
in the ordinary SelectionDAG lowering of `sitofp`/`uitofp` on X86, and it
affects the plain i32 (and i64) source widths, not just wide (>i32) types.
#71658 covers only ">i32 to bf16" and targets the libcall path.
### Reproduction
```llvm
; i32-to-bf16.ll
define bfloat @si32_to_bf16(i32 %x) {
%r = sitofp i32 %x to bfloat
ret bfloat %r
}
define bfloat @ui32_to_bf16(i32 zeroext %x) {
%r = uitofp i32 %x to bfloat
ret bfloat %r
}
define <4 x bfloat> @v_si32_to_bf16(<4 x i32> %x) {
%r = sitofp <4 x i32> %x to <4 x bfloat>
ret <4 x bfloat> %r
}
```
```
$ llc -mtriple=x86_64 -mattr=+avx512bf16,+avx512fp16 i32-to-bf16.ll -o - | grep -E 'vcvt|callq'
vcvtsi2ss %edi, %xmm15, %xmm0
callq __truncsfbf2@PLT
vcvtusi2ss %edi, %xmm15, %xmm0
callq __truncsfbf2@PLT
vcvtdq2ps %xmm0, %xmm0
callq __truncsfbf2@PLT ; x4, one per element
```
The SSE2 baseline (`llc -mtriple=x86_64 i32-to-bf16.ll`) emits the same
`cvtsi2ss` + `__truncsfbf2` sequence. i64 sources emit
`vcvtsi2ss %rdi` / `vcvtusi2ss %rdi` + `__truncsfbf2` with the same issue
(that part is covered by #71658; the i32 case is not).
### Counterexample (bit-exact)
`x = 16842753 = 2^24 + 2^16 + 1`, which is exactly one bf16 ULP above the
bf16 midpoint `2^24 + 2^16` in the binade [2^24, 2^25):
| step | value | f32 bits |
|---|---|---|
| `vcvtsi2ss` (RNE to f32) | 16842752.0 | `0x4B808000` |
| `__truncsfbf2` (RNE to bf16) | 16777216.0 | `0x4B80` |
| correctly rounded bf16 | 16908288.0 | `0x4B81` |
The f32 rounding lands exactly on the bf16 midpoint (low 16 f32 bits are
`0x8000`), and the second ties-to-even rounding then goes the wrong way. The
information is already destroyed by the first rounding, so no
`__truncsfbf2` implementation can recover the correct result. Verified
bit-exactly in Python with `struct` (f32 RNE conversion + RNE truncation of
the top 16 bits).
Any i32 with |x| >= 2^24 whose f32 rounding lands on a bf16 midpoint is
affected; the first such value is 16842753.
### Suggested fix
The f64 route used elsewhere avoids the issue: `vcvtsi2sd`/`vcvtusi2sd`
(i32 -> f64 is **exact**) followed by a correctly rounded f64 -> bf16
conversion (e.g. round-to-odd truncation to f32 then `__truncsfbf2`, the
same trick the RISC-V backend uses with `vfncvt.rod.f.f.w`, or a direct
`__truncdfbf2` call). Alternatively the existing libcall approach from
#71658 could be extended down to i32 sources.
Tested on trunk (`cc2acc33a5b2`, assertions build).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs