diff --git a/Project.toml b/Project.toml index 3f79b2e7..68d9ea27 100644 --- a/Project.toml +++ b/Project.toml @@ -42,7 +42,7 @@ CEnum = "0.5, 1" Crayons = "4" GPUArrays = "12.1" GPUCompiler = "2.24" -GPUToolbox = "3.5" +GPUToolbox = "3.7" IntelITT = "0.2" KernelAbstractions = "0.10" KernelInterface = "0.4" diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl index 8e5a8479..f2853ad0 100644 --- a/src/compiler/compilation.jl +++ b/src/compiler/compilation.jl @@ -40,14 +40,16 @@ GPUCompiler.uses_exception_sites(::oneAPICompilerJob) = true GPUCompiler.kernel_state_type(::oneAPICompilerJob) = KernelState # 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::oneAPICompilerJob) 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 5f701e56..1e1c6b9f 100644 --- a/src/device/quirks.jl +++ b/src/device/quirks.jl @@ -1,17 +1,18 @@ +# GPUToolbox's shared overrides throw through this function, with constant arguments that +# let them report like `@gputhrow` +@device_override @generated function GPUToolbox.Overlays.device_throw(::Type{E}, ::Val{reason}) where {E<:Exception, reason} + return quote + $(Expr(:meta, :noinline)) + @gputhrow $(String(nameof(E))) $(String(reason)) + end +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" @device_override @noinline Base.Math.throw_exp_domainerror(x) = @gputhrow "DomainError" "Exponentiation yielding a complex result requires a complex argument" -# intfuncs.jl -@device_override @noinline Base.throw_domerr_powbysq(::Any, p) = - @gputhrow "DomainError" "Cannot raise an integer to a negative power" -@device_override @noinline Base.throw_domerr_powbysq(::Integer, p) = - @gputhrow "DomainError" "Cannot raise an integer to a negative power" -@device_override @noinline Base.throw_domerr_powbysq(::AbstractMatrix, p) = - @gputhrow "DomainError" "Cannot raise an integer to a negative power" - # checked.jl @device_override @noinline Base.Checked.throw_overflowerr_binaryop(op, x, y) = @gputhrow "OverflowError" "Binary operation overflowed" diff --git a/test/execution.jl b/test/execution.jl index 76ac0813..cf829e86 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -1684,6 +1684,69 @@ end @test Array(y)[] == 0 end +@testset "Rational arithmetic" begin + # Julia 1.10 and 1.11 format the messages of `Rational` errors at run time, which + # fails to compile unless GPUToolbox's shared overrides replace them + function rational_kernel(out, a, b, c) + @inbounds out[1] = a * b + c + @inbounds out[2] = -a + return + end + out = oneArray{Rational{Int}}(undef, 2) + @oneapi rational_kernel(out, 2//3, -3//4, 1//5) + @test Array(out) == [2//3 * -3//4 + 1//5, -2//3] + + construct!(a, num, den) = (@inbounds a[1] = Rational(num, den); return) + a = oneArray([0//1]) + @oneapi construct!(a, 1, -2) + @test Array(a) == [-1//2] + # Julia 1.10 throws an ArgumentError, later versions an OverflowError from negating the + # denominator, which is only reported as an exception + @oneapi debug_level=1 construct!(a, 1, typemin(Int)) + err = kernel_exception() + @test err isa KernelException + if isdefined(Base, :__throw_rational_argerror_typemin) + @test err.name == "ArgumentError" + @test err.reason == "invalid rational: denominator can't be typemin" + @test occursin("An ArgumentError was thrown by", sprint(showerror, err)) + end + @test Array(a) == [-1//2] +end + +# GPUToolbox's shared overrides report their exceptions like `@gputhrow` does +@testset "shared override exceptions" begin + throwing() = (oneAPI.GPUToolbox.Overlays.@device_throw ArgumentError("shared reason"); return) + @oneapi debug_level=0 throwing() + err = kernel_exception() + @test err isa KernelException + @test err.name == "" + @oneapi debug_level=1 throwing() + err = kernel_exception() + @test err.name == "ArgumentError" + @test err.reason == "shared reason" + msg = sprint(showerror, err) + @test occursin("An ArgumentError was thrown by", msg) + @test occursin(": shared reason", msg) + + # integer exponentiation with a negative power + pow!(a, p) = (@inbounds a[1] = a[1]^p; return) + a = oneArray([2]) + @oneapi debug_level=1 pow!(a, -1) + err = kernel_exception() + @test err.name == "DomainError" + @test err.reason == "Cannot raise an integer to a negative power" + @test Array(a) == [2] + + if VERSION < v"1.12-" + negate!(a) = (@inbounds a[1] = -a[1]; return) + a = oneArray([typemin(Int)//1]) + @oneapi debug_level=1 negate!(a) + err = kernel_exception() + @test err.name == "OverflowError" + @test err.reason == "rational numerator is typemin" + end +end + @testset "spills past 128 KB of scratch" begin # Each work-item keeps V vectors of 16 values live until the stores (`out` may alias # `inp`), so at SIMD32 IGC spills them, above a table of P values per work-item that it