From 84c4d6e0cbb1604cbc36d3136681e1c64c4ed5ef Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 3 Nov 2026 18:30:11 +0100 Subject: [PATCH 1/2] Support BFloat16 kernels on devices that accept SPV_KHR_bfloat16 On Julia 1.11+, `BFloat16` is `Core.BFloat16`, which lowers to LLVM's `bfloat`, so kernels using it failed GPUCompiler's validation. Add `Device.spirv_extensions` (cl_khr_spirv_queries) and, when it lists SPV_KHR_bfloat16, enable that extension and GPUCompiler's `supports_bfloat16` on the SPIR-V paths. GPUCompiler 2.20 widens the arithmetic to `float`. PoCL reports the extension from pocl_jll 7.2.2+15; the OpenCL C path (spirv2clc) has no bfloat16 and keeps rejecting such kernels. --- Project.toml | 2 +- lib/cl/device.jl | 12 +++- src/compiler/compilation.jl | 8 +++ test/Project.toml | 2 + test/bfloat16.jl | 106 ++++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 test/bfloat16.jl diff --git a/Project.toml b/Project.toml index 9ca6735b..c1ee4538 100644 --- a/Project.toml +++ b/Project.toml @@ -34,7 +34,7 @@ SPIRVIntrinsics = {path = "lib/intrinsics"} [compat] Adapt = "4" GPUArrays = "12.1" -GPUCompiler = "2.18" +GPUCompiler = "2.20" GPUToolbox = "3.5" KernelAbstractions = "0.10" KernelInterface = "0.4" diff --git a/lib/cl/device.jl b/lib/cl/device.jl index a6c30888..3829efae 100644 --- a/lib/cl/device.jl +++ b/lib/cl/device.jl @@ -143,6 +143,16 @@ end # error handling inspired by rusticl # https://gitlab.freedesktop.org/mesa/mesa/-/blob/c4385d6fb0938231114eb3023082cd33788b89b4/src/gallium/frontends/rusticl/api/device.rs#L314-320 + # cl_khr_spirv_queries: the SPIR-V extensions that modules may declare + if s == :spirv_extensions + "cl_khr_spirv_queries" in d.extensions || return String[] + size = Ref{Csize_t}() + clGetDeviceInfo(d, CL_DEVICE_SPIRV_EXTENSIONS_KHR, 0, C_NULL, size) + result = Vector{Ptr{Cchar}}(undef, size[] รท sizeof(Ptr{Cchar})) + clGetDeviceInfo(d, CL_DEVICE_SPIRV_EXTENSIONS_KHR, sizeof(result), result, C_NULL) + return String[unsafe_string(ptr) for ptr in result] + end + if s == :sub_group_sizes res_size = Ref{Csize_t}() err = unchecked_clGetDeviceInfo(d, CL_DEVICE_SUB_GROUP_SIZES_INTEL, C_NULL, C_NULL, res_size) @@ -189,7 +199,7 @@ function Base.propertynames(::Device, private::Bool=false) :has_image_support, :has_local_mem, :host_unified_memory, :available, :compiler_available, :non_uniform_work_group_support # other - :extensions, :platform, :device_type, :max_work_item_size, :sub_group_sizes, + :extensions, :spirv_extensions, :platform, :device_type, :max_work_item_size, :sub_group_sizes, :max_image2d_shape, :max_image3d_shape, ) return private ? (fieldnames(Device)..., props...) : props diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl index 9e9e5874..77bc4112 100644 --- a/src/compiler/compilation.jl +++ b/src/compiler/compilation.jl @@ -215,11 +215,19 @@ end error("Device does not support cl_intel_required_subgroup_size") end + # `BFloat16` loads, stores and conversions; GPUCompiler widens the arithmetic to `float`. + # (spirv2clc has no bfloat16 support.) + supports_bfloat16 = !source && "SPV_KHR_bfloat16" in dev.spirv_extensions + if supports_bfloat16 + extensions = union(something(extensions, String[]), ["SPV_KHR_bfloat16"]) + end + # GPUCompiler adds the extensions that the atomics need spirv_ext = extensions === nothing ? "" : join(("+$ext" for ext in extensions), ",") # create GPUCompiler objects target = SPIRVCompilerTarget(; version=SPIRV_VERSION, supports_fp16, supports_fp64, + supports_bfloat16, validate=true, extensions=spirv_ext, atomics, backend=llvm_to_spirv_backend, driver=spirv_driver(dev), kwargs...) diff --git a/test/Project.toml b/test/Project.toml index c42c3cf9..e9da3bbd 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,5 +1,6 @@ [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b" CompilerCaching = "9db33cc3-5358-4881-8759-fa4194144afd" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" @@ -36,6 +37,7 @@ OpenCL = {path = ".."} SPIRVIntrinsics = {path = "../lib/intrinsics"} [compat] +BFloat16s = "0.6" IOCapture = "1" Statistics = "1" intel_opencl_rt_jll = "2026.1" diff --git a/test/bfloat16.jl b/test/bfloat16.jl new file mode 100644 index 00000000..ee3338b4 --- /dev/null +++ b/test/bfloat16.jl @@ -0,0 +1,106 @@ +# On Julia 1.11+, `BFloat16` is `Core.BFloat16`, which Julia lowers to LLVM's `bfloat`: +# storing it needs a device that accepts `SPV_KHR_bfloat16`, and GPUCompiler widens its +# arithmetic to `float`. On Julia 1.10, BFloat16s.jl defines it as a 16-bit primitive type. + +using BFloat16s + +function bf16_copy(y, x) + i = get_global_id() + @inbounds y[i] = x[i] + return +end +function bf16_ops(out, x, y) + i = get_global_id() + @inbounds begin + a, b = x[i], y[i] + out[1, i] = a * b + b + out[2, i] = a / b + out[3, i] = min(a, b) + out[4, i] = max(a, b) + out[5, i] = abs(a) + out[6, i] = copysign(a, b) + out[7, i] = ifelse(a < b, a, b) + out[8, i] = sqrt(abs(a)) + out[9, i] = -a + end + return +end +function bf16_convert(y, x) + i = get_global_id() + @inbounds y[i] = convert(eltype(y), x[i]) + return +end + +# correctly rounded, unlike the host's conversions from `Float64` and wide integers on Julia +# 1.11 and 1.12 (which round twice, see JuliaMath/BFloat16s.jl#78) +bf16_nearest(x) = BFloat16(Float32(BigFloat(x; precision = 8))) + +# finite values from bit patterns, rather than from rounding on the host (Julia 1.11's +# vectorized `Float32` to `BFloat16` conversion crashes LLVM on AVX512-BF16 hosts) +rand_bf16(n) = map(1:n) do _ + while true + v = reinterpret(BFloat16, rand(UInt16)) + isfinite(v) && return v + end +end + +if !isdefined(Core, :BFloat16) || OpenCL.compiler_config(cl.device()).target.supports_bfloat16 + n = 1024 + hx, hy = rand_bf16(n), rand_bf16(n) + hx[1:4] .= BFloat16.((-0.0, 0.0, 1.0, -2.0)) + hy[1:4] .= BFloat16.((0.0, -0.0, 0.0, 3.0)) + x, y = CLArray(hx), CLArray(hy) + + @testset "copy" begin + out = similar(x) + @opencl global_size = n bf16_copy(out, x) + @test isequal(Array(out), hx) + end + + @testset "arithmetic" begin + out = CLArray{BFloat16}(undef, 9, n) + @opencl global_size = n bf16_ops(out, x, y) + o = Array(out) + # (references in `Float32`: on Julia 1.11, the host miscompiles vectorized `BFloat16` + # operations such as `copysign`) + f32(op) = (xs...) -> BFloat16(op(Float32.(xs)...)) + # (multiplied and added separately, each rounded to bfloat) + @test isequal(o[1, :], map((a, b) -> f32(+)(f32(*)(a, b), b), hx, hy)) + @test isequal(o[2, :], map(f32(/), hx, hy)) + @test isequal(o[3, :], map(f32(min), hx, hy)) + @test isequal(o[4, :], map(f32(max), hx, hy)) + @test isequal(o[5, :], map(f32(abs), hx)) + @test isequal(o[6, :], map(f32(copysign), hx, hy)) + @test isequal(o[7, :], map(f32((a, b) -> ifelse(a < b, a, b)), hx, hy)) + @test isequal(o[8, :], map(f32(a -> sqrt(abs(a))), hx)) + @test isequal(o[9, :], map(f32(-), hx)) + end + + # including inputs that a conversion through `Float32` would round wrongly + conversions = ( + (Float32, Float32[1 + 2.0f0^-8 + 2.0f0^-20, floatmax(Float32)]), + (Float64, [1 + 2.0^-8 + 2.0^-30, -(1 + 2.0^-8 + 2.0^-30), 1.0e39]), + (Int32, Int32[2^30 + 2^22 + 1, typemin(Int32), typemax(Int32)]), + (Int64, Int64[2^62 + 2^54 + 1, typemin(Int64), typemax(Int64)]), + (UInt64, [UInt64(2)^63 + UInt64(2)^55 + 1, typemax(UInt64)]), + ) + fp64 = "cl_khr_fp64" in cl.device().extensions + @testset "$T to BFloat16" for (T, extra) in filter(c -> fp64 || c[1] != Float64, conversions) + vals = T <: AbstractFloat ? T.(randn(n) .* 1000) : rand(T, n) + vals[1:length(extra)] .= extra + dst = CLArray{BFloat16}(undef, n) + @opencl global_size = n bf16_convert(dst, CLArray(vals)) + @test isequal(Array(dst), map(bf16_nearest, vals)) + end + + @testset "BFloat16 to Float32" begin + dst = CLArray{Float32}(undef, n) + @opencl global_size = n bf16_convert(dst, x) + @test isequal(Array(dst), map(Float32, hx)) + end +else + # without `SPV_KHR_bfloat16` (or on the OpenCL C source path), compiling must fail + # cleanly, rather than the driver aborting on a rejected extension + x = CLArray(BFloat16.(Float32[1, 2, 3, 4])) + @test_throws OpenCL.GPUCompiler.InvalidIRError @opencl global_size = 4 bf16_copy(similar(x), x) +end From 9316e0c64c0d5a9d37e354d85958ad2515a9ff54 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 3 Nov 2026 19:14:15 +0100 Subject: [PATCH 2/2] Expect SPV_KHR_bfloat16 in the atomics extension test where supported The bfloat16 change adds the extension on devices that report it, which broke the test's exact comparison of the extension list. Also correct the reason the conversion test avoids the host's BFloat16 conversions: BFloat16s.jl 0.6.4 rounds them correctly, but vectorized ones still crash LLVM on Julia 1.11 on AVX512-BF16 hosts. --- test/atomics.jl | 6 ++++-- test/bfloat16.jl | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/atomics.jl b/test/atomics.jl index a389e36b..a59305cd 100644 --- a/test/atomics.jl +++ b/test/atomics.jl @@ -406,11 +406,13 @@ end # nothing enables native floating-point min/max @test !occursin("min_max", GPUCompiler.spirv_extensions(OpenCL.compiler_config(dev).target)) - # the user's extensions are kept, and the atomics add the ones they need + # the user's extensions are kept, next to the one for bfloat16 (on devices that support + # it), and the atomics add the ones they need config = OpenCL.compiler_config(dev; extensions=["SPV_KHR_expect_assume"], atomics=GPUCompiler.SPIRVAtomics(; fadd_f32_local=true)) + bfloat16 = config.target.supports_bfloat16 ? ",+SPV_KHR_bfloat16" : "" @test GPUCompiler.spirv_extensions(config.target) == - "+SPV_KHR_expect_assume,+SPV_EXT_shader_atomic_float_add" + "+SPV_KHR_expect_assume$bfloat16,+SPV_EXT_shader_atomic_float_add" end # `@atomic` returns the old value when it uses an atomic operation, and the new one when it diff --git a/test/bfloat16.jl b/test/bfloat16.jl index ee3338b4..ffc6b184 100644 --- a/test/bfloat16.jl +++ b/test/bfloat16.jl @@ -31,8 +31,8 @@ function bf16_convert(y, x) return end -# correctly rounded, unlike the host's conversions from `Float64` and wide integers on Julia -# 1.11 and 1.12 (which round twice, see JuliaMath/BFloat16s.jl#78) +# the correctly rounded value, computed without the host's `BFloat16` conversions (on Julia +# 1.11, vectorized ones crash LLVM on AVX512-BF16 hosts) bf16_nearest(x) = BFloat16(Float32(BigFloat(x; precision = 8))) # finite values from bit patterns, rather than from rounding on the host (Julia 1.11's