Skip to content

Add overrides of Base methods that format exception messages - #43

Merged
maleadt merged 2 commits into
mainfrom
tb/throw-overrides
Nov 10, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/throw-overrides

Conversation

@maleadt

@maleadt maleadt commented Nov 10, 2026

Copy link
Copy Markdown
Member

On Julia 1.10, Rational arithmetic in a kernel fails to compile on most back-ends. Constructing a Rational, which * and + do, can reach Base.__throw_rational_argerror_typemin(T), which interpolates the integer type into its message ("invalid rational: denominator can't be typemin($T)"). GPU compilers can't compile that formatting code: PoCL, oneAPI, OpenCL and AMDGPU report an InvalidIRError with dynamic calls, and Metal crashes in LLVM's dead argument elimination. Negating a Rational has the same problem through __throw_rational_numerator_typemin(T), on Julia 1.10 and 1.11. Julia 1.11 removed the first helper, and Julia 1.12 builds the second message with a LazyString. CUDA.jl overrides the first helper in its own quirks.

This adds a third shared table, throw_overrides, with overrides of these helpers that throw the same exception type with a constant message, which GPUCompiler can compile (it drops the exception and reports the throw through the back-end's exception runtime). Each override is only defined on the Julia versions whose Base needs it, so the table is empty on 1.12 and later. The table's reason is narrow: Base helpers whose message is built at run time. For now it only holds the Rational helpers. Other helpers that interpolate values, like Base._throw_dmrs (reached by reshape, and overridden by CUDA.jl and AMDGPU.jl) or Broadcast.throwdm, are left out of scope; they could be added later.

The test infers each override and checks that it only constructs and throws the exception from literals, which Base's methods don't on 1.10 and 1.11. Stacking the table on KernelAbstractions' PoCL back end and on Metal makes a Rational{Int} multiply-add kernel compile and produce the right results on Julia 1.10, and a typemin denominator raises a KernelException.

@github-actions

Copy link
Copy Markdown

Documentation preview: https://juliagpu.github.io/GPUToolbox.jl/previews/PR43/

Julia 1.10 and 1.11 throw some `Rational` errors with a message that
interpolates the integer type, which GPU compilers can't compile, so that
`Rational` arithmetic fails to compile for most back-ends. Add a table
with overrides of these helpers that throw the same exceptions with a
constant message.
@maleadt
maleadt merged commit 6804040 into main Nov 10, 2026
10 checks passed
@maleadt
maleadt deleted the tb/throw-overrides branch November 10, 2026 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant