Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ SPIRVIntrinsics = {path = "lib/intrinsics"}
Adapt = "4"
GPUArrays = "12.1"
GPUCompiler = "2.20"
GPUToolbox = "3.5"
GPUToolbox = "3.6"
KernelAbstractions = "0.10"
KernelInterface = "0.4"
LLVM = "10"
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ end
GPUCompiler.runtime_module(::CompilerJob{<:Any,OpenCLCompilerParams}) = OpenCL

# SPIR-V doesn't support 128-bit integers, so keep 64-bit integer math (e.g. `Rational`
# comparisons) that Base computes with them out of Int128
# 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)
if job.config.target.supports_fp64
(method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.int128_overrides)
(method_table, SPIRVIntrinsics.method_table, 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,
GPUToolbox.Overlays.int128_overrides)
GPUToolbox.Overlays.int128_overrides, GPUToolbox.Overlays.throw_overrides)
end
end

Expand Down
9 changes: 9 additions & 0 deletions src/device/quirks.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# GPUToolbox's shared overrides throw through this function, with constant arguments that
# let them report like `@gputhrow`
@device_override @noinline function GPUToolbox.Overlays.device_throw(::Type{E}, ::Val{reason}) where {E<:Exception, reason}
if kernel_debug_level() >= 1
record_exception!(exception_info(), Val(nameof(E)), Val(reason))
end
throw(nothing)
end

# math.jl
@device_override @noinline Base.Math.throw_complex_domainerror(f::Symbol, x) =
@gputhrow "DomainError" "This operation requires a complex input to return a complex result"
Expand Down
32 changes: 32 additions & 0 deletions test/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,38 @@ end
@test exce.name == "DomainError"
end

@testset "exceptions from GPUToolbox's shared overrides" begin
function throw_at(dl, kernel, args...)
k = @opencl launch=false debug_level=dl kernel(args...)
k(args...)
try; cl.finish(cl.queue()); nothing; catch err; err; end
end

# the shared overrides throw through `device_throw`, which reports like a quirk
shared() = (OpenCL.GPUToolbox.Overlays.@device_throw ArgumentError("shared reason"); return)
exc = throw_at(1, shared)
@test exc isa OpenCL.KernelException
@test exc.name == "ArgumentError"
@test exc.reason == "shared reason"
@test isempty(throw_at(0, shared).name)

# a `Rational` with a typemin denominator can't be normalized
rational!(a, d) = (a[1] = 1 // d[1]; return)
a = OpenCL.zeros(Rational{Int}, 1)
d = CLArray([typemin(Int)])
@test throw_at(0, rational!, a, d) isa OpenCL.KernelException
if isdefined(Base, :__throw_rational_argerror_typemin)
# Julia 1.10 throws an `ArgumentError` with the type in its message
exc = throw_at(1, rational!, a, d)
@test exc isa OpenCL.KernelException
@test exc.name == "ArgumentError"
@test exc.reason == "invalid rational: denominator can't be typemin"
msg = sprint(showerror, exc)
@test occursin("ArgumentError", msg)
@test occursin("denominator can't be typemin", msg)
end
end

# The mailbox is shared by all work-groups and queues on the device: claims and stores
# synchronize at device scope, publishing the details recorded before them.
@testset "exception mailbox atomics" begin
Expand Down
18 changes: 18 additions & 0 deletions test/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ end
end
end

# Base throws some of `Rational`'s errors with a formatted message, which doesn't compile, so
# GPUToolbox's overrides throw them with a constant one. Without those, kernels that construct
# a rational with a negative denominator (Julia 1.10) or negate one (1.10 and 1.11) fail.
@testset "rational arithmetic" begin
xs = [1//3, -5//7, 3//2, 2//-9]
ys = [2//5, 7//3, -1//4, 9//2]
zs = [-1//2, 1//7, 3//8, 1//1]
# Intel's CPU runtime can't compile the `OpSMulExtended` of checked `Int` products
if OpenCL.OpenCLKernels.intel_cpu_runtime(cl.device())
@test_skip "checked rational multiplication on Intel's CPU runtime"
else
@test Array(map(muladd, CLArray(xs), CLArray(ys), CLArray(zs))) == muladd.(xs, ys, zs)
end
@test Array(map(-, CLArray(xs))) == -xs
@test Array(map((n, d) -> n // d, CLArray([1, 3, -4]), CLArray([-2, 6, -8]))) ==
[-1//2, 1//2, 1//2]
end

# truncating the 3-D builtins used to produce loads of vector types SPIR-V doesn't have
function truncated_builtins_kernel(a, b, c)
# only the last work-item writes
Expand Down
Loading