Issue 240103
Summary [AArch64] Vectorized bfloat fadd without +bf16 is not correctly rounded: the round-to-even bias is taken from the operand, not the sum
Labels new issue
Assignees
Reporter CarloLucibello
    On AArch64 without `+bf16` (e.g. Apple M1), a vectorized `fadd <N x bfloat>` is not correctly rounded: about 25% of the sums in the reproducer below differ from the round-to-nearest-even result, always by one ulp at exact ties. The scalar `fadd bfloat`, a non-vectorized loop and `+bf16` targets are all correct.

## Reproducer (C)

```c
// Vectorized __bf16 addition rounds exact ties inconsistently on AArch64 without FEAT_BF16.
// clang -O2 bf16_add.c -o bf16_add && ./bf16_add
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#define N 4096

static __bf16 from_bits(uint16_t u) { __bf16 x; memcpy(&x, &u, 2); return x; }
static uint16_t to_bits(__bf16 x) { uint16_t u; memcpy(&u, &x, 2); return u; }

// round-to-nearest-even of a finite float to bfloat16, with integer arithmetic only
static uint16_t rne(float f) {
    uint32_t u; memcpy(&u, &f, 4);
    u += 0x7fff + ((u >> 16) & 1);
    return (uint16_t)(u >> 16);
}

__attribute__((noinline)) void add(__bf16 *c, const __bf16 *a, const __bf16 *b, int n) {
    for (int i = 0; i < n; i++) c[i] = a[i] + b[i];
}

int main(void) {
    static __bf16 a[N], b[N], c[N];
    uint32_t s = 12345;
    for (int i = 0; i < N; i++) {
        s = s * 1664525u + 1013904223u; a[i] = from_bits(0x3f80 | ((s >> 16) & 0x7f));  // [1, 2)
        s = s * 1664525u + 1013904223u; b[i] = from_bits(0x3f80 | ((s >> 16) & 0x7f));
    }
    add(c, a, b, N);
    int wrong = 0;
    for (int i = 0; i < N; i++) {
        float exact = (float)a[i] + (float)b[i];  // exact: the sum of two bfloat16 fits in a float
        if (to_bits(c[i]) != rne(exact)) {
            if (wrong < 4)
                printf("a=%g b=%g exact=%g got=0x%04x want=0x%04x\n", (float)a[i], (float)b[i],
                       exact, to_bits(c[i]), rne(exact));
            wrong++;
        }
    }
    printf("%d of %d sums are not correctly rounded\n", wrong, N);
    return wrong != 0;
}
```

```
$ clang -O2 bf16_add.c -o bf16_add && ./bf16_add
a=1.75781 b=1.8125 exact=3.57031 got=0x4065 want=0x4064
a=1.75 b=1.41406 exact=3.16406 got=0x404b want=0x404a
1034 of 4096 sums are not correctly rounded
```

- Apple clang 21.0.0 (clang-2100.1.1.101) and Homebrew clang 22.1.8, on an Apple M1 (`apple-m1`, no FEAT_BF16): 1034 of 4096 wrong, with the same two first lines.
- `-O0` (both compilers) and `-O2 -fno-vectorize -fno-slp-vectorize` (Apple clang): 0 of 4096 wrong.
- The same also shows in Julia 1.13.3 (LLVM 20.1.8), whose native `Core.BFloat16` compiles `c[i] = a[i] + b[i]` to `fadd <8 x bfloat>`: 540 of 4096 random sums in [0, 1) wrong; a non-inlined scalar `+` and `Float32 -> BFloat16` conversions are right.
- I have not tried a trunk build.

## Reduced IR

```llvm
define void @vadd(ptr %c, ptr %a, ptr %b) {
  %x = load <8 x bfloat>, ptr %a
  %y = load <8 x bfloat>, ptr %b
  %z = fadd <8 x bfloat> %x, %y
  store <8 x bfloat> %z, ptr %c
  ret void
}
```

`llc -mtriple=arm64-apple-darwin -mcpu=apple-m1 -O2` (22.1.8) gives:

```asm
_vadd:
	ldr	q0, [x1]
	ldr	q1, [x2]
	ushr.4s	v2, v0, #16          // lsb = (v0 >> 16) & 1: v0 is the loaded input x
	movi.4s	v3, #1
	and.16b	v2, v2, v3
	shll2.4s	v3, v1, #16
	shll2.4s	v4, v0, #16
	fadd.4s	v3, v4, v3           // the sum, high half
	movi.4s	v4, #127, msl #8     // 0x7fff
	add.4s	v2, v2, v4           // bias = lsb + 0x7fff
	add.4s	v4, v3, v2           // sum + bias
	fcmeq.4s	v5, v3, v3
	orr.4s	v3, #64, lsl #16
	bit.16b	v3, v4, v5
	shll.4s	v1, v1, #16
	shll.4s	v0, v0, #16
	fadd.4s	v0, v0, v1           // the sum, low half
	add.4s	v1, v0, v2           // same bias again
	fcmeq.4s	v2, v0, v0
	orr.4s	v0, #64, lsl #16
	bit.16b	v0, v1, v2
	uzp2.8h	v0, v0, v3
	str	q0, [x0]
	ret
```

The scalar version is correct: it takes the tie-breaking bit from the sum (`fadd s0, s0, s1; fmov w8, s0; ubfx w9, w8, #16, #1; ...; add w8, w9, w8; add w8, w8, #0x7fff; lsr w8, w8, #16`).

In the vector sequence, the round-to-even bias `((bits >> 16) & 1) + 0x7fff` appears to be computed from the loaded input vector (`v0` before the `shll`/`fadd`), not from either sum, and the one bias is added to both halves. So the lsb used to break ties is an unrelated bit of the input, and ties are sometimes rounded to the odd neighbour instead of the even one. With `-mcpu=apple-m2` (FEAT_BF16) the output is `fadd.4s` followed by `bfcvtn`/`bfcvtn2`, which is correct.

The scalar and `+bf16` paths being right suggests the bug is in the vector `FP_ROUND` f32 to bf16 expansion, or in how its operand is chosen when the `fadd` is promoted, but I haven't looked into the backend.

🧔‍♂️🤖  Assisted by AI

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

Reply via email to