Repository navigation
Report exceptions by throw site, for back-ends that opt in - #1100
Merged
Merged
Conversation
This was referenced Nov 17, 2026
GC lowering introduces calls to `gc_pool_alloc` during optimization, so the runtime library was linked a second time after it. By then the runtime functions already in the module were internal, so the linker imported fresh external copies of everything `gc_pool_alloc` calls and renamed the originals: kernels that allocate and throw carried two copies of the reporting functions, and the relinked functions were never internalized, ending up as exported functions in the PTX, GCN and SPIR-V output. Instead, link `gc_pool_alloc` with the rest of the runtime when the module allocates, keep it external (and thus alive and with its ABI intact) during optimization, and internalize it right after GC lowering.
Every throw site reports its exception with a sequence of runtime calls that pass the exception's name, and at debug level 2 every frame of its backtrace. On SPIR-V and Metal, throwing code is inlined into kernels, so a kernel with many bounds checks carries many copies of that sequence. That costs run time (Metal and oneAPI kernels with 256 bounds checks run 5-7x slower than without), code size, and on PoCL stack: per-site values in the inlined code get an array over the work-group (JuliaGPU/KernelAbstractions.jl#974). With `uses_exception_sites`, a throw site instead passes an `Int32` id to the `exception_site` runtime function, and compilation returns the table that maps ids to the exception's name, reason and backtrace as `meta.exception_sites`. The ids stay symbolic, as an intrinsic taking the name and reason, until the module is final, which keeps them valid through caching of the runtime library, deferred compilation and linking. Every function then gets a single call that its throw sites branch to, which is inlined. `GPUCompiler.@gputhrow` lets back-ends name an exception and its reason. Backtraces are derived after inlining, so where throwing code is inlined into kernels (SPIR-V, Metal) they reach the kernel. Back-ends that don't opt in are unaffected. The other runtime functions will be removed in the next breaking release.
`lower_throw_extra!` replaces calls to functions such as `julia_throw_boundserror` by a report named after the function, before back-ends' overrides of those functions are inlined. When an override names its exception with `@gputhrow`, that replaced its name and reason by the mangled function name, so skip functions that report a site themselves.
maleadt
force-pushed
the
tb/exception-sites
branch
from
November 17, 2026 14:43
9816663 to
9cf9144
Compare
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every throw site currently reports its exception with a sequence of runtime calls (
report_exception, at debug level 2 areport_exception_frameper frame,signal_exception). On SPIR-V and Metal, throwing code is inlined into kernels, so a kernel with many bounds checks carries many copies of that sequence, and that is expensive. With 256 bounds checks per thread, compared to@inbounds:On PoCL, values in the inlined code get an array over the work-group, which made KernelAbstractions' CPU kernels overflow the stack (JuliaGPU/KernelAbstractions.jl#974).
This adds an opt-in alternative, without changing anything for back-ends that don't opt in:
uses_exception_sites(job)(defaultfalse). An opted-in back-end implements one runtime function,exception_site(id::Int32), instead ofreport_exception,report_exception_name,report_exception_frameandsignal_exception.meta.exception_sites(aVector{ExceptionSite}, plain data, so it can be stored with cached results). Id 0 means nothing is known (debug level 0).finalize_exception_sites!then numbers them, deriving the backtrace at debug level 2 from the reporting call's location after inlining. Where throwing code is inlined into kernels (SPIR-V, and now early infinish_ir!for opted-in Metal jobs), backtraces therefore reach the kernel.merge_exception_sites!makes every function branch to a single reporting call, with a phi of ids, and inlines it.GPUCompiler.@gputhrow name reason(not exported) names a throw's exception and reason, for back-ends' overrides.noinlineandnomerge, which keep a report out of line until it's finalized.The old runtime functions stay, and remain the default; they can go in the next breaking release.
Two fixes this needed:
gc_pool_alloc. By then the runtime functions already in the module were internal, so the linker imported fresh external copies of everythinggc_pool_alloccalls and renamed the originals. Every kernel that allocates and throws carried two copies of the reporting functions, which ended up exported in the PTX, GCN and SPIR-V output; with exception sites the kernel's ids went to the renamed copy and could not be resolved.gc_pool_allocis now linked with the rest of the runtime when the module allocates, kept external during optimization, and internalized after GC lowering, so nothing is linked during optimization any more.lower_throw_extra!replaced calls tothrow_boundserrorand similar functions before back-ends' overrides were inlined, so a@gputhrowin an override got named after the mangled function.KernelAbstractions' PoCL back-end is the first user (JuliaGPU/KernelAbstractions.jl, branch
tb/pocl-exception-sites): a kernel with 256 bounds checks goes from up to 4.2 MB of stack to no frame, and from 83-122 KB of code to 6.6 KB. Prototypes for Metal, oneAPI and AMDGPU showed run time going from 6.8x to 1.4-1.8x on Metal, and from 5.7x to 3.0x on oneAPI.Tested on Julia 1.13 (full suite) and 1.10 (ptx, spirv, metal, gcn and native; the
precompiletests fail there onmastertoo).