Issue 238375
Summary [SPIR-V] Legalize bfloat when SPV_KHR_bfloat16 or SPV_INTEL_bfloat16_arithmetic is unavailable
Labels new issue
Assignees
Reporter maleadt
    The SPIR-V back end only accepts `bfloat` when the extensions that make it native are enabled, and aborts otherwise, where other targets without native `bf16` legalize it. There are two cases.

**Storage, without `SPV_KHR_bfloat16`.** Even a copy fails:

```llvm
define spir_kernel void @copy(ptr addrspace(1) %dst, ptr addrspace(1) %src) {
  %v = load bfloat, ptr addrspace(1) %src
  store bfloat %v, ptr addrspace(1) %dst
  ret void
}
```

```
$ llc -mtriple=spirv64-unknown-unknown copy.ll
LLVM ERROR: OpTypeFloat type with bfloat requires the following SPIR-V extension: SPV_KHR_bfloat16
```

Many SPIR-V consumers don't accept that extension (most OpenCL drivers, older Intel GPUs). Without it, the back end could represent `bfloat` as a 16-bit integer: loads, stores, arguments and other uses that only move the value become `i16`; `fpext` from `bfloat` becomes a shift into the top half of an `i32` and a bitcast to `float`; `fptrunc` to `bfloat` becomes an integer round-to-nearest-even of the `float` bits, with NaN handling. This is the storage-only representation `SoftPromoteHalf` uses in SelectionDAG.

**Arithmetic, with only `SPV_KHR_bfloat16`.** Storage and conversions work, but arithmetic fails:

```llvm
define spir_kernel void @k(ptr addrspace(1) %p, bfloat %x, bfloat %y) {
  %r = fadd bfloat %x, %y
 store bfloat %r, ptr addrspace(1) %p
  ret void
}
```

```
$ llc -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_KHR_bfloat16 k.ll
LLVM ERROR: Arithmetic instructions with bfloat16 arguments require the following SPIR-V extension: SPV_INTEL_bfloat16_arithmetic
```

Comparisons and OpenCL.std instructions (`llvm.fabs`, `llvm.fma`, `llvm.minnum`, `llvm.sqrt`, ...) fail the same way. `SPV_INTEL_bfloat16_arithmetic` isn't in the SPIR-V registry, `spirv-val` rejects its capability (`Invalid capability operand: 6226`), and few consumers accept it. Without it, the back end could promote like other targets do: extend the operands to `float`, operate on that, and convert back. For `fadd`/`fsub`/`fmul`/`fdiv` and `sqrt` that gives the correctly rounded `bfloat` result, since `float` has more than twice its precision. `fma` needs more care (computing in `double` and rounding to odd works, or a libcall), and `fneg`/`fabs`/`copysign` are better done on the bits so that NaN payloads survive.

Without either extension, both apply: arithmetic on `float`, storage as `i16`.

A related problem concerns conversions. `SPV_KHR_bfloat16` defines `OpFConvert`, `OpConvertSToF` and `OpConvertUToF` to `BFloat16KHR` as converting to binary32 first. The back end emits `fptrunc double %d to bfloat` and `sitofp i32 %i to bfloat` as a single such instruction, so by the spec the value is rounded twice (e.g. `1 + 2^-8 + 2^-30` and `2^30 + 2^22 + 1` come out one ulp off), whereas LLVM IR specifies a single rounding. Some consumers round once anyway; the Khronos translator, for example, reads the instruction back as a single `fptrunc`. Rounding to `float` with round-to-odd first would match the IR semantics.

Seen with LLVM 23.1.4. For reference, Julia's GPU compiler works around the arithmetic and conversion cases before handing the module to the back end (JuliaGPU/GPUCompiler.jl#1076), and would rather drop that once the back end legalizes `bfloat` itself.

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

Reply via email to