From 01aa26c186c442a5da6abf6828e815bf4d9aaa78 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 10 Nov 2026 11:31:51 +0100 Subject: [PATCH] Use GPUToolbox's throw overrides on the PoCL back end Some of Base's `Rational` helpers format their error message at run time, which can't be compiled for the device: on Julia 1.10 this broke all signed `Rational` arithmetic in kernels on `CPU()`, and on Julia 1.11 negation. GPUToolbox 3.6 provides `throw_overrides`, which throw these errors with a constant message through `Overlays.device_throw`; stack that table, and implement the hook with `@gputhrow` so these exceptions report their type and message like the back end's own quirks. --- Project.toml | 2 +- src/pocl/compiler/compilation.jl | 8 +++-- src/pocl/device/quirks.jl | 8 +++++ test/runtests.jl | 56 ++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index b334e4ba7..8205945ae 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/pocl/compiler/compilation.jl b/src/pocl/compiler/compilation.jl index 36633b061..d8c548632 100644 --- a/src/pocl/compiler/compilation.jl +++ b/src/pocl/compiler/compilation.jl @@ -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 diff --git a/src/pocl/device/quirks.jl b/src/pocl/device/quirks.jl index 8c563294d..9939be05d 100644 --- a/src/pocl/device/quirks.jl +++ b/src/pocl/device/quirks.jl @@ -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" diff --git a/test/runtests.jl b/test/runtests.jl index 7febebff4..f318d44d1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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