Issue 243687
Summary [X86] strictfp `__bf16` ↔ float libcalls use i16/GPR ABI while libgcc helpers use XMM0
Labels new issue
Assignees
Reporter 20000419
    Under `#pragma STDC FENV_ACCESS ON` or `-ffp-model=strict`, x86-64 Clang lowers `__bf16` ↔ `float` to `__extendbfsf2` / `__truncsfbf2` and marshals the bf16 side as a softened `i16` in a GPR. libgcc's helpers, and Clang's own C ABI for `__bf16`, use the low 16 bits of XMM0. The value 1.0 is exact in bfloat16, so this is not a rounding-mode difference.

```c
#pragma STDC FENV_ACCESS ON
#include <stdio.h>
#include <string.h>

__attribute__((noinline)) unsigned extend_bits(unsigned short bits) {
  __bf16 b;
  memcpy(&b, &bits, 2);
  union { float f; unsigned u; } v;
  v.f = (float)b;
  return v.u;
}

__attribute__((noinline)) unsigned short trunc_bits(float f) {
  __bf16 b = (__bf16)f;
  unsigned short bits;
  memcpy(&bits, &b, 2);
  return bits;
}

int main(void) {
  unsigned e = extend_bits(0x3f80);
  unsigned short t = trunc_bits(1.0f);
  printf("ext %08x trunc %04x\n", e, t);
  /* 1.0 is exact in bf16: bits 0x3f80 extend to 0x3f800000. */
  return (e == 0x3f800000u && t == 0x3f80) ? 0 : 1;
}
```

```console
$ clang -O0 -ffp-model=strict t.c && ./a.out
ext 00000000 trunc 0000
$ clang -O2 -ffp-model=strict t.c && ./a.out
ext 00000000 trunc 0000
$ gcc -O2 -frounding-math t.c && ./a.out
ext 3f800000 trunc 3f80
```

libgcc (`extendbfsf2.o` / `truncsfbf2.o`): `__extendbfsf2` starts with `movd %xmm0, %eax` and returns the float with `movd %ebx, %xmm0`; `__truncsfbf2` reads the float from XMM0 and every return path ends with `movd %ebx, %xmm0` (bf16 result is not left in EAX). For `+1.0f` a caller that returns EAX observes `0x0000`.

clang `-O2 -ffp-model=strict -S`:

```asm
extend_bits:
  callq __extendbfsf2@PLT   # i16 argument stays in %edi; never moved to %xmm0
  movd  %xmm0, %eax
  retq
trunc_bits:
  callq __truncsfbf2@PLT    # float argument correctly in %xmm0
  retq                      # returns %eax; helper wrote the bf16 to %xmm0
```

`-O0` extend shifts the bits into the high half of XMM0 and then passes the original bits in `%edi`; the helper reads the low half, which that shift cleared. The x86-64 psABI gives `__bf16` the same classification as `_Float16` (SSE). Clang agrees (`X86_64ABIInfo::classify`, `BuiltinType::BFloat16` → `SSE`). `_Float16` under the same flags is correct (`0x3c00` ↔ `0x3f800000`) because scalar `f16` is in `RetCC_X86_64_C` / `CC_X86_64`.

Where (main `65406a394f38`):

- `LegalizeDAG.cpp` `ConvertNodeToLibcall`: `STRICT_BF16_TO_FP` (line 5097) calls `makeLibCall(DAG, RTLIB::FPEXT_BF16_F32, MVT::f32, Node->getOperand(1), ...)` with the softened `i16` operand, and `STRICT_FP_TO_BF16` (line 5244) uses `Node->getValueType(0)` as the result type, so both sides of the call follow the integer rules.
- `X86ISelLowering.cpp`: `STRICT_FP_TO_BF16` / `STRICT_BF16_TO_FP` are `Expand` (lines 362-363). Non-strict `FP_TO_BF16` is `Custom`, and its libcall fallback in `LowerFP_TO_BF16` (line 23089) already calls `makeLibCall(..., MVT::f16, ...)` and bitcasts the XMM result to `i16`. The strict path has no such handling.
- `X86CallingConv.td` `RetCC_X86_64_C` / `CC_X86_64` list scalar `f16` for XMM; there is no scalar `bf16` entry, and a softened `i16` follows the GPR rules.
- `-mllvm -opt-bisect-limit=0` still fails; no `opt -passes=` reproduces it. `llc` of the clang IR reproduces the bad calls.

Versions (good `ext 3f800000 trunc 3f80`):

- clang 15–16: does not compile (`cannot type-cast from __bf16`)
- clang-17: `-O0` fails differently (`ext 3ff00000 trunc 0000`; extend calls `__extendhfsf2`); `-O1+` `ext 00000000 trunc 0000`
- clang 19–23: `-O0` `ext 3f800000 trunc 0000` (extend accidentally OK via leftover XMM / shift path; trunc still 0000); `-O1+` both 0000
- clang 24 (`cf44dd9e317e` and `58561153d5fc`): `-O0` through `-Os` both 0000; `-mavx512bf16` / `-mavx512fp16` still fail; without pragma and without `-ffp-model=strict`: OK; `_Float16` sibling under strict: OK
- gcc 14 `-frounding-math` `-O0` through `-Os`: OK

Related but different: #104465 (closed in 2024) was `__bf16` being lowered like `_Float16` under `FENV_ACCESS`; #80056 added `STRICT_BF16_TO_FP` / `STRICT_FP_TO_BF16`, and this is the libcall ABI of those nodes on x86-64. #78540 is missing strict bf16 promotion (AMDGPU). #56854 was the intentional half ABI change (`__extendhfsf2` / `__truncsfhf2` from RDI to XMM); the half sibling of this repro is correct. #97896 is an undefined reference to `__truncsfbf2`, not a wrong value. #160832 is Windows half (caller XMM, compiler-rt ECX), the opposite direction. #234116 is i32→bf16 double rounding via `__truncdfbf2`. #167694 is a bf16 `strict_fsqrt` crash. Open PR #230889 only adds softening of `STRICT_BF16_TO_FP` for targets where `f32` is softened (RISC-V without F); it does not change the x86-64 libcall ABI.

The problem seems to be that the strict bf16 conversion libcalls are emitted with the softened `i16` type instead of the C `__bf16` (XMM) ABI that the non-strict `LowerFP_TO_BF16` fallback uses.

Godbolt (executed, clang trunk `abcbd442e332`): https://godbolt.org/z/cd5EWdY1o
There the output is `ext 00000000 trunc 3f80` at `-O0` and `-O2 -ffp-model=strict`. The extend is wrong as above. The generated `trunc_bits` is the same as above (it returns `%eax` right after `callq __truncsfbf2`), so the trunc value printed there is whatever that runtime's `__truncsfbf2` leaves in EAX; with libgcc 14 on x86-64 Linux it is `0000`.

cc @phoebewang @RKSimon

Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.

Assisted-by: Grok

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to