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 @@ -51,7 +51,7 @@ AMDGPU = "2"
Adapt = "0.4, 1.0, 2.0, 3.0, 4"
Atomix = "1.5"
GPUCompiler = "2.20"
GPUToolbox = "3.5"
GPUToolbox = "3.6"
KernelInterface = "0.4"
LLVM = "10"
LinearAlgebra = "1.6"
Expand Down
8 changes: 5 additions & 3 deletions src/pocl/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ end
GPUCompiler.runtime_module(::CompilerJob{<:Any, OpenCLCompilerParams}) = POCL

# 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 error paths format
# their message at run time, which doesn't compile; GPUToolbox's overrides throw with a
# constant message instead.
GPUCompiler.method_tables(job::OpenCLCompilerJob) =
job.config.params.fuse_muladd ?
(muladd_method_table, method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.int128_overrides) :
(method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.int128_overrides)
(muladd_method_table, method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.int128_overrides, GPUToolbox.Overlays.throw_overrides) :
(method_table, SPIRVIntrinsics.method_table, GPUToolbox.Overlays.int128_overrides, GPUToolbox.Overlays.throw_overrides)

# filter out OpenCL built-ins
# TODO: eagerly lower these using the translator API
Expand Down
8 changes: 8 additions & 0 deletions src/pocl/device/quirks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ macro gputhrow(name::String, reason::String)
end
end

# GPUToolbox's shared overrides throw through this function
@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"
Expand Down
56 changes: 56 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,62 @@ end
@test occursin("throw_boundserror", out)
end

# Julia 1.10 and 1.11 format the messages of some `Rational` errors at run time, which
# GPUToolbox's overrides replace with constant messages
@testset "POCL Rational errors" begin
@kernel function rational_muladd!(out, a, b)
I = @index(Global, Linear)
@inbounds out[I] = a[I] * b[I] + a[I]
end
@kernel function rational_neg!(out, a)
I = @index(Global, Linear)
@inbounds out[I] = -a[I]
end
a = [1 // 2, -3 // 4, 5 // 6]
b = [2 // 3, 4 // 5, -6 // 7]
out = zeros(Rational{Int}, length(a))
rational_muladd!(CPU())(out, a, b; ndrange = length(a))
synchronize(CPU())
@test out == a .* b .+ a
rational_neg!(CPU())(out, a; ndrange = length(a))
synchronize(CPU())
@test out == -a

# the exceptions report their type and message like the back-end's own
function output(f, args...)
return mktemp() do path, io
redirect_stdout(io) do
@test_throws POCL.KernelException @opencl global_size = 1 debug_level = 1 f(args...)
Libc.flush_cstdio()
end
close(io)
read(path, String)
end
end
rational!(A, n, d) = (A[1] = Rational(n, d); return)
out = output(rational!, zeros(Rational{Int}, 1), 1, typemin(Int))
if isdefined(Base, :__throw_rational_argerror_typemin)
@test occursin("ERROR: ArgumentError during kernel execution", out)
@test occursin("denominator can't be typemin", out)
else
@test occursin("ERROR: ", out)
end
neg!(A, x) = (A[1] = -x; return)
out = output(neg!, zeros(Rational{Int}, 1), typemin(Int) // 1)
if VERSION < v"1.12-"
@test occursin("ERROR: OverflowError during kernel execution", out)
@test occursin("rational numerator is typemin", out)
else
@test occursin("ERROR: ", out)
end

# also on Julia versions where no shared override throws
throwing() = (GPUToolbox.Overlays.device_throw(ArgumentError, Val(:var"100% shared reason")); return)
out = output(throwing)
@test occursin("ERROR: ArgumentError during kernel execution", out)
@test occursin("100% shared reason", out)
end

# sysimage builds compile methods for the host, which must not reach device-only code, or
# the sysimage references device intrinsics and fails to load (JuliaGPU/GPUCompiler.jl#611)
@testset "host leaks" begin
Expand Down
Loading