From f3ecc06e23246ddc577e842f09da9644f6c95da1 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Wed, 2 Dec 2026 20:48:09 -0700 Subject: [PATCH 1/3] SPIRVIntrinsics: native math builtins, used by @fastmath Add the OpenCL native_* functions (cos, divide, exp, exp2, exp10, log, log2, log10, powr, recip, rsqrt, sin, sqrt, tan) for Float32 and Float16, and route Base.FastMath's exp/log/div/inv/trig variants to them, like CUDA.jl's fast intrinsics. Without this, @fastmath falls back to the correctly rounded builtins. The Float16 native cos/sin/tan go through Float32: PoCL lacks the half versions, and on an Intel Arc B390 the detour is within 7% of the half natives (both 2.8-3.5x faster than the accurate Float16 builtins). Co-Authored-By: Claude Opus 5.5 --- lib/intrinsics/src/math.jl | 35 ++++++++++++++++++++++++++++++++++- test/intrinsics.jl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/intrinsics/src/math.jl b/lib/intrinsics/src/math.jl index 19ebd1ef..21436969 100644 --- a/lib/intrinsics/src/math.jl +++ b/lib/intrinsics/src/math.jl @@ -233,7 +233,40 @@ end @device_function rootn(x::Float64, y::Int32) = @builtin_ccall("rootn", Float64, (Float64, Int32), x, y) -# TODO: half and native +# native: implementation-defined precision, typically a single hardware instruction +# (float, and half with cl_khr_fp16; no double) + +for T in (Float16, Float32) + for f in (:cos, :exp, :exp2, :exp10, :log, :log2, :log10, :recip, :rsqrt, :sin, :sqrt, :tan) + nf = Symbol(:native_, f) + if T == Float16 && f in (:cos, :sin, :tan) + # through Float32: PoCL lacks the half versions, and on Intel Xe they are + # within 7% of the Float32 detour + @eval @device_function $nf(x::Float16) = Float16($nf(Float32(x))) + else + @eval @device_function $nf(x::$T) = @builtin_ccall($(String(nf)), $T, ($T,), x) + end + end + for f in (:divide, :powr) + nf = Symbol(:native_, f) + @eval @device_function $nf(x::$T, y::$T) = @builtin_ccall($(String(nf)), $T, ($T, $T), x, y) + end +end + +# `@fastmath` uses the native functions, like CUDA.jl's fast intrinsics. Without these, the +# fast variants fall back to the correctly rounded builtins. +for T in (Float16, Float32) + for f in (:cos, :exp, :exp2, :exp10, :log, :log2, :log10, :sin, :tan) + @eval @device_override Base.FastMath.$(Symbol(f, :_fast))(x::$T) = $(Symbol(:native_, f))(x) + end + @eval begin + @device_override Base.FastMath.sincos_fast(x::$T) = (native_sin(x), native_cos(x)) + @device_override Base.FastMath.div_fast(x::$T, y::$T) = native_divide(x, y) + @device_override Base.FastMath.inv_fast(x::$T) = native_recip(x) + end +end + +# TODO: half_ functions function _mulhi(a::Int64, b::Int64) shift = sizeof(a) * 4 diff --git a/test/intrinsics.jl b/test/intrinsics.jl index ad633c09..a41ac734 100644 --- a/test/intrinsics.jl +++ b/test/intrinsics.jl @@ -205,6 +205,40 @@ end @test call_on_device(OpenCL.mad, x, y, z) ≈ x * y + z end +fastmath_kernel(a, b) = (@inbounds a[1] = @fastmath exp2(b[1]) / log2(b[2]); return) +# spirv2clc doesn't implement the native_* extended instructions +if OpenCL.resolve_program_backend(cl.device(), OpenCL.program_backend()) !== :spirv + @test_skip "native math through spirv2clc" +else + # native precision is implementation-defined: only check that the result is close + @testset "native - $T" for T in filter(T -> T in (Float16, Float32), float_types) + x = T(0.5) + rand(T) + y = T(0.5) + rand(T) + rtol = T == Float16 ? 1.0e-2 : 1.0e-3 + @testset "$f" for (f, ref) in [ + OpenCL.native_cos => cos, OpenCL.native_sin => sin, OpenCL.native_tan => tan, + OpenCL.native_exp => exp, OpenCL.native_exp2 => exp2, OpenCL.native_exp10 => exp10, + OpenCL.native_log => log, OpenCL.native_log2 => log2, OpenCL.native_log10 => log10, + OpenCL.native_recip => inv, OpenCL.native_rsqrt => x -> 1 / sqrt(x), + OpenCL.native_sqrt => sqrt, + ] + @test call_on_device(f, x) ≈ ref(x) rtol = rtol + end + @test call_on_device(OpenCL.native_divide, x, y) ≈ x / y rtol = rtol + @test call_on_device(OpenCL.native_powr, x, y) ≈ x^y rtol = rtol + end + + @testset "@fastmath uses native functions" begin + a = CLArray(zeros(Float32, 1)) + b = CLArray(Float32[0.5, 3]) + ir = sprint(io -> @device_code_llvm io=io @opencl fastmath_kernel(a, b)) + @test occursin("native_exp2", ir) + @test occursin("native_log2", ir) + @test occursin("native_divide", ir) + @test Array(a)[1] ≈ exp2(0.5f0) / log2(3.0f0) rtol = 1.0f-3 + end +end + # Base compares rationals using the 128-bit products of their numerators and denominators, # which GPUToolbox computes in 64-bit words instead @testset "rational comparisons" begin From 041b3bd2abde1c31044756dbf3cd26b252676a7e Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Wed, 2 Dec 2026 21:22:16 -0700 Subject: [PATCH 2/3] SPIRVIntrinsics: half_ math builtins Add the OpenCL half_* functions (Float32 with at most 8192 ulp error) for completeness. They are not faster than native_*: on an Intel Arc B390, half_exp and half_log2 compile to the native instructions, while half_sin, half_tan and half_powr take the correctly rounded paths (as slow as the accurate builtins). @fastmath keeps using native_*. Co-Authored-By: Claude Opus 5.5 --- lib/intrinsics/src/math.jl | 11 ++++++++++- test/intrinsics.jl | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/intrinsics/src/math.jl b/lib/intrinsics/src/math.jl index 21436969..2d3f984e 100644 --- a/lib/intrinsics/src/math.jl +++ b/lib/intrinsics/src/math.jl @@ -266,7 +266,16 @@ for T in (Float16, Float32) end end -# TODO: half_ functions +# half_: Float32 with at least 11 bits of precision (not half arguments) + +for f in (:cos, :exp, :exp2, :exp10, :log, :log2, :log10, :recip, :rsqrt, :sin, :sqrt, :tan) + hf = Symbol(:half_, f) + @eval @device_function $hf(x::Float32) = @builtin_ccall($(String(hf)), Float32, (Float32,), x) +end +for f in (:divide, :powr) + hf = Symbol(:half_, f) + @eval @device_function $hf(x::Float32, y::Float32) = @builtin_ccall($(String(hf)), Float32, (Float32, Float32), x, y) +end function _mulhi(a::Int64, b::Int64) shift = sizeof(a) * 4 diff --git a/test/intrinsics.jl b/test/intrinsics.jl index a41ac734..c29915e0 100644 --- a/test/intrinsics.jl +++ b/test/intrinsics.jl @@ -239,6 +239,23 @@ else end end +# half_ allows up to 8192 ulp, ≈ 2^-10 relative error +@testset "half_" begin + x = 0.5f0 + rand(Float32) + y = 0.5f0 + rand(Float32) + @testset "$f" for (f, ref) in [ + OpenCL.half_cos => cos, OpenCL.half_sin => sin, OpenCL.half_tan => tan, + OpenCL.half_exp => exp, OpenCL.half_exp2 => exp2, OpenCL.half_exp10 => exp10, + OpenCL.half_log => log, OpenCL.half_log2 => log2, OpenCL.half_log10 => log10, + OpenCL.half_recip => inv, OpenCL.half_rsqrt => x -> 1 / sqrt(x), + OpenCL.half_sqrt => sqrt, + ] + @test call_on_device(f, x) ≈ ref(x) rtol = 2.0f-3 + end + @test call_on_device(OpenCL.half_divide, x, y) ≈ x / y rtol = 2.0f-3 + @test call_on_device(OpenCL.half_powr, x, y) ≈ x^y rtol = 2.0f-3 +end + # Base compares rationals using the 128-bit products of their numerators and denominators, # which GPUToolbox computes in 64-bit words instead @testset "rational comparisons" begin From 33a0aa195c5250c1a9b988944b80f68f922c3caa Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Wed, 2 Dec 2026 23:39:51 -0700 Subject: [PATCH 3/3] SPIRVIntrinsics: separate, non-consistent @fastmath overlay table The native_* results differ from Base's fast variants, so overlay them with plain `@overlay` rather than `@device_override`'s consistent overlay. They go in their own `fastmath_overrides` table, which OpenCL leaves out of the stack on the OpenCL C source path: spirv2clc fails on the native_* extended instructions ("UNIMPLEMENTED extended instruction"), which broke `@fastmath` kernels there. Tests: keep log inputs away from 1, and cover @fastmath sincos/inv, Float16, and the spirv2clc path. Co-Authored-By: Claude Opus 5.5 --- lib/intrinsics/src/math.jl | 15 ++++--- src/compiler/compilation.jl | 14 +++++-- test/intrinsics.jl | 79 +++++++++++++++++++++++-------------- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/lib/intrinsics/src/math.jl b/lib/intrinsics/src/math.jl index 2d3f984e..cab0e349 100644 --- a/lib/intrinsics/src/math.jl +++ b/lib/intrinsics/src/math.jl @@ -253,16 +253,19 @@ for T in (Float16, Float32) end end -# `@fastmath` uses the native functions, like CUDA.jl's fast intrinsics. Without these, the -# fast variants fall back to the correctly rounded builtins. +# `@fastmath` using the native functions, like CUDA.jl's fast intrinsics. A separate table, so +# back ends whose toolchain can't lower native_* (e.g. spirv2clc) can leave it out of the stack. +# Plain (not consistent) overlays: native results differ from what Base's fast variants compute. +Base.Experimental.@MethodTable(fastmath_overrides) + for T in (Float16, Float32) for f in (:cos, :exp, :exp2, :exp10, :log, :log2, :log10, :sin, :tan) - @eval @device_override Base.FastMath.$(Symbol(f, :_fast))(x::$T) = $(Symbol(:native_, f))(x) + @eval Base.Experimental.@overlay fastmath_overrides Base.FastMath.$(Symbol(f, :_fast))(x::$T) = $(Symbol(:native_, f))(x) end @eval begin - @device_override Base.FastMath.sincos_fast(x::$T) = (native_sin(x), native_cos(x)) - @device_override Base.FastMath.div_fast(x::$T, y::$T) = native_divide(x, y) - @device_override Base.FastMath.inv_fast(x::$T) = native_recip(x) + Base.Experimental.@overlay fastmath_overrides Base.FastMath.sincos_fast(x::$T) = (native_sin(x), native_cos(x)) + Base.Experimental.@overlay fastmath_overrides Base.FastMath.div_fast(x::$T, y::$T) = native_divide(x, y) + Base.Experimental.@overlay fastmath_overrides Base.FastMath.inv_fast(x::$T) = native_recip(x) end end diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl index 5cb45d06..2d818d30 100644 --- a/src/compiler/compilation.jl +++ b/src/compiler/compilation.jl @@ -7,6 +7,8 @@ Base.@kwdef struct OpenCLCompilerParams <: AbstractCompilerParams features::FeatureSet = zero(FeatureSet) # requested backend policy; kept here so the cache keys on it (the backends share a context) program_backend::Symbol = :auto + # lower `@fastmath` to the native_* builtins, which spirv2clc can't translate + native_fastmath::Bool = true end const OpenCLCompilerConfig = CompilerConfig{SPIRVCompilerTarget, OpenCLCompilerParams} @@ -48,12 +50,15 @@ GPUCompiler.uses_exception_sites(::CompilerJob{<:Any,OpenCLCompilerParams}) = tr # comparisons) that Base computes with them out of Int128. Some of Base's exceptions format # their message with `string`, which doesn't compile, so throw those with a constant message. function GPUCompiler.method_tables(job::OpenCLCompilerJob) + tables = (method_table, SPIRVIntrinsics.method_table) + if job.config.params.native_fastmath + tables = (tables..., SPIRVIntrinsics.fastmath_overrides) + end if job.config.target.supports_fp64 - (method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.int128_overrides, - GPUToolbox.Overlays.throw_overrides) + (tables..., GPUToolbox.Overlays.int128_overrides, GPUToolbox.Overlays.throw_overrides) else # keep single-precision math that Base computes in Float64 out of double precision - (method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.float64_overrides, + (tables..., GPUToolbox.Overlays.float64_overrides, GPUToolbox.Overlays.int128_overrides, GPUToolbox.Overlays.throw_overrides) end end @@ -222,7 +227,8 @@ end validate=true, extensions=spirv_ext, atomics, backend=llvm_to_spirv_backend, driver=spirv_driver(dev), kwargs...) - params = OpenCLCompilerParams(; sub_group_size, features, program_backend=backend) + params = OpenCLCompilerParams(; sub_group_size, features, program_backend=backend, + native_fastmath=!source) CompilerConfig(target, params; kernel, name, always_inline, debug_level) end diff --git a/test/intrinsics.jl b/test/intrinsics.jl index c29915e0..fc018165 100644 --- a/test/intrinsics.jl +++ b/test/intrinsics.jl @@ -206,36 +206,56 @@ end end fastmath_kernel(a, b) = (@inbounds a[1] = @fastmath exp2(b[1]) / log2(b[2]); return) -# spirv2clc doesn't implement the native_* extended instructions -if OpenCL.resolve_program_backend(cl.device(), OpenCL.program_backend()) !== :spirv - @test_skip "native math through spirv2clc" -else - # native precision is implementation-defined: only check that the result is close - @testset "native - $T" for T in filter(T -> T in (Float16, Float32), float_types) - x = T(0.5) + rand(T) - y = T(0.5) + rand(T) - rtol = T == Float16 ? 1.0e-2 : 1.0e-3 - @testset "$f" for (f, ref) in [ - OpenCL.native_cos => cos, OpenCL.native_sin => sin, OpenCL.native_tan => tan, - OpenCL.native_exp => exp, OpenCL.native_exp2 => exp2, OpenCL.native_exp10 => exp10, - OpenCL.native_log => log, OpenCL.native_log2 => log2, OpenCL.native_log10 => log10, - OpenCL.native_recip => inv, OpenCL.native_rsqrt => x -> 1 / sqrt(x), - OpenCL.native_sqrt => sqrt, - ] - @test call_on_device(f, x) ≈ ref(x) rtol = rtol - end - @test call_on_device(OpenCL.native_divide, x, y) ≈ x / y rtol = rtol - @test call_on_device(OpenCL.native_powr, x, y) ≈ x^y rtol = rtol +fastmath_sincos_kernel(a, b) = (@inbounds (s, c) = @fastmath sincos(b[1]); @inbounds a[1] = @fastmath inv(s + c); return) + +# spirv2clc doesn't implement the native_* extended instructions (`@fastmath` avoids them there) +source_backend = OpenCL.resolve_program_backend(cl.device(), OpenCL.program_backend()) === :opencl +source_backend && @test_skip "native math through spirv2clc" + +# native precision is implementation-defined: only check that the result is close +source_backend || @testset "native - $T" for T in filter(T -> T in (Float16, Float32), float_types) + x = T(0.5) + rand(T) + y = T(0.5) + rand(T) + rtol = T == Float16 ? 1.0e-2 : 1.0e-3 + @testset "$f" for (f, ref) in [ + OpenCL.native_cos => cos, OpenCL.native_sin => sin, OpenCL.native_tan => tan, + OpenCL.native_exp => exp, OpenCL.native_exp2 => exp2, OpenCL.native_exp10 => exp10, + OpenCL.native_log => log, OpenCL.native_log2 => log2, OpenCL.native_log10 => log10, + OpenCL.native_recip => inv, OpenCL.native_rsqrt => x -> 1 / sqrt(x), + OpenCL.native_sqrt => sqrt, + ] + # keep log away from its root, where a relative tolerance is meaningless + xi = ref in (log, log2, log10) ? x + one(T) : x + @test call_on_device(f, xi) ≈ ref(xi) rtol = rtol end + @test call_on_device(OpenCL.native_divide, x, y) ≈ x / y rtol = rtol + @test call_on_device(OpenCL.native_powr, x, y) ≈ x^y rtol = rtol +end - @testset "@fastmath uses native functions" begin - a = CLArray(zeros(Float32, 1)) - b = CLArray(Float32[0.5, 3]) - ir = sprint(io -> @device_code_llvm io=io @opencl fastmath_kernel(a, b)) - @test occursin("native_exp2", ir) - @test occursin("native_log2", ir) - @test occursin("native_divide", ir) - @test Array(a)[1] ≈ exp2(0.5f0) / log2(3.0f0) rtol = 1.0f-3 +@testset "@fastmath uses native functions" begin + native(f, ir) = occursin("native_$f", ir) == !source_backend + + a = CLArray(zeros(Float32, 1)) + b = CLArray(Float32[0.5, 3]) + ir = sprint(io -> @device_code_llvm io=io @opencl fastmath_kernel(a, b)) + @test native("exp2", ir) + @test native("log2", ir) + @test native("divide", ir) + @test Array(a)[1] ≈ exp2(0.5f0) / log2(3.0f0) rtol = 1.0f-3 + + ir = sprint(io -> @device_code_llvm io=io @opencl fastmath_sincos_kernel(a, b)) + @test native("sin", ir) + @test native("cos", ir) + @test native("recip", ir) + @test Array(a)[1] ≈ inv(sin(0.5f0) + cos(0.5f0)) rtol = 1.0f-3 + + if Float16 in float_types + a16 = CLArray(zeros(Float16, 1)) + b16 = CLArray(Float16[0.5, 3]) + ir = sprint(io -> @device_code_llvm io=io @opencl fastmath_sincos_kernel(a16, b16)) + @test native("sin", ir) + @test native("recip", ir) + @test Array(a16)[1] ≈ inv(sin(Float16(0.5)) + cos(Float16(0.5))) rtol = 1.0e-2 end end @@ -250,7 +270,8 @@ end OpenCL.half_recip => inv, OpenCL.half_rsqrt => x -> 1 / sqrt(x), OpenCL.half_sqrt => sqrt, ] - @test call_on_device(f, x) ≈ ref(x) rtol = 2.0f-3 + xi = f in (OpenCL.half_log, OpenCL.half_log2, OpenCL.half_log10) ? x + 1.0f0 : x + @test call_on_device(f, xi) ≈ ref(xi) rtol = 2.0f-3 end @test call_on_device(OpenCL.half_divide, x, y) ≈ x / y rtol = 2.0f-3 @test call_on_device(OpenCL.half_powr, x, y) ≈ x^y rtol = 2.0f-3