Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion lib/intrinsics/src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,52 @@ 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` 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 Base.Experimental.@overlay fastmath_overrides Base.FastMath.$(Symbol(f, :_fast))(x::$T) = $(Symbol(:native_, f))(x)
end
@eval begin
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

# 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
Expand Down
14 changes: 10 additions & 4 deletions src/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
72 changes: 72 additions & 0 deletions test/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,78 @@ 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)
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
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

# 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,
]
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
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
Expand Down
Loading