diff --git a/Project.toml b/Project.toml index 7109edf5..e6c17c82 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl index 77bc4112..4937741c 100644 --- a/src/compiler/compilation.jl +++ b/src/compiler/compilation.jl @@ -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 diff --git a/src/device/quirks.jl b/src/device/quirks.jl index 6b44c200..32a61a2d 100644 --- a/src/device/quirks.jl +++ b/src/device/quirks.jl @@ -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" diff --git a/test/exceptions.jl b/test/exceptions.jl index 09571b58..1137c4cb 100644 --- a/test/exceptions.jl +++ b/test/exceptions.jl @@ -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 diff --git a/test/intrinsics.jl b/test/intrinsics.jl index 29b8e767..6706df63 100644 --- a/test/intrinsics.jl +++ b/test/intrinsics.jl @@ -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