Skip to content

Report exceptions by throw site - #616

Merged
maleadt merged 1 commit into
mainfrom
tb/exception-sites
Nov 19, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/exception-sites

Conversation

@maleadt

@maleadt maleadt commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Kernels now report device-side exceptions through GPUCompiler's throw sites (JuliaGPU/GPUCompiler.jl#1100): a throwing work-item only records the id of its throw site and its position in the mailbox, and the host looks up the exception's name, reason and backtrace in the table of sites that compilation returns with the kernel. Until now, every throw site stored the exception's name and reason into the mailbox itself, and at debug level 2 copied each backtrace frame's strings with a byte loop, so the reporting code was repeated at each of them.

For users

KernelException keeps its fields, but has more in them:

  • the work-item and work-group that threw are now known at every debug level, not only at -g2;
  • at -g1, exceptions that aren't thrown by one of the quirks now have the name GPUCompiler derives for them (often the generic exception) rather than none;
  • names, reasons and backtraces are no longer truncated (they were limited to 63/191 bytes and 16 frames), as the strings never go through device memory.
julia> @opencl global_size=8 local_size=4 (a -> (a[get_global_id() + 2] = 1f0; return))(OpenCL.zeros(Float32, 4))

julia> OpenCL.synchronize()
ERROR: KernelException: A BoundsError was thrown by work-item 3×1×1 in work-group 1×1×1 on device cpu-...: Out-of-bounds array access
For more details, run Julia with `-g2`, or launch the kernel with `@opencl debug_level=2`

and at -g2:

ERROR: KernelException: A BoundsError was thrown by work-item 3×1×1 in work-group 1×1×1 on device cpu-...: Out-of-bounds array access
Stacktrace:
 [1] throw_boundserror at OpenCL/src/device/quirks.jl:61
 [2] arrayset at OpenCL/src/device/array.jl:137
 [3] setindex! at OpenCL/src/device/array.jl:214
 [4] k at REPL[2]:1

Nothing is lost compared to before.

Implementation

exception_site(id) claims the mailbox with the existing atomic max and has the winner store the site id, the position and the id of the kernel's table of sites. The tables are carried by OpenCLResults (so they're cached with the compiled code) and by the HostKernel. Because the mailbox is shared by all queues on a device and only checked once they have completed, a site id alone doesn't identify the kernel that threw: the mailbox numbers the tables of the kernels launched since its last check (an IdDict lookup per launch, under the lock the launch already takes), passes that number through KernelState (replacing the launch id that level 2's re-entrant reporting needed), and resets the numbering when it is checked.

@gputhrow is now GPUCompiler's, and GPUToolbox's device_throw override expands to it.

Numbers

PoCL 7.2.2 (pocl_jll) on a Ryzen 9 9950X, Julia 1.13, a 1-D kernel summing 64 or 256 bounds-checked reads per work-item over 2^20 work-items in work-groups of 256, compared to the same kernel with @inbounds:

before after @inbounds
M=256, -g1: run time 8.49 ms 7.93 ms 0.65 ms
M=256, -g1: code size 15.1 KB 4.0 KB 3.9 KB
M=256, -g2: run time 11.5 ms 7.3 ms 0.65 ms
M=256, -g2: code size 533 KB 4.0 KB 3.9 KB
M=256, -g2: compile + first launch 13.1 s 2.8 s 2.6 s
M=64, -g1: run time 1.68 ms 1.76 ms 0.13 ms
M=64, -g2: run time 1.73 ms 1.36 ms 0.13 ms

At -g1 the run time of bounds-checked kernels doesn't change on PoCL: what remains is the cost of the checks themselves. At -g2 the code no longer grows with the number of throw sites. The stack frames that PoCL's work-item loops gave level 2's reporting code (56 to 112 bytes for a KernelAbstractions kernel with 64 or 256 bounds checks, after #609) are gone.

Depends on

GPUCompiler 2.23, which has JuliaGPU/GPUCompiler.jl#1100.

@maleadt
maleadt marked this pull request as ready for review November 18, 2026 06:39
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.04%. Comparing base (ffd198d) to head (39f82c6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #616      +/-   ##
==========================================
+ Coverage   89.09%   90.04%   +0.95%     
==========================================
  Files          18       18              
  Lines        1751     1688      -63     
==========================================
- Hits         1560     1520      -40     
+ Misses        191      168      -23     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Every throw site stored the exception's name and reason into the mailbox, and at
debug level 2 copied each backtrace frame's strings with a byte loop, so kernels
grew with their number of bounds checks: with 256 of them, 15 KB of code at -g1
and 530 KB at -g2, which took 13 s instead of 3 s to compile on PoCL.

Use GPUCompiler's reporting by throw site instead. The first work-item that
throws records its site's id, its position and the id of its kernel's table of
sites, through a single copy of the reporting code per kernel, and the host
looks up the name, reason and backtrace in that table. As the mailbox is shared
by the queues of a device and only checked once they have completed, it numbers
the tables of the kernels launched since its last check.

`KernelException` now has the faulting work-item and work-group at every debug
level, and names, reasons and backtraces are no longer truncated. The kernel
with 256 bounds checks now has 4 KB of code at both levels; its run time on
PoCL is unchanged at -g1 (7.9 ms vs 8.5 ms, 0.65 ms with `@inbounds`) and drops
from 11.5 ms to 7.3 ms at -g2.
@maleadt
maleadt merged commit e960c4f into main Nov 19, 2026
13 of 15 checks passed
@maleadt
maleadt deleted the tb/exception-sites branch November 19, 2026 06:02
maleadt added a commit that referenced this pull request Nov 19, 2026
Upstream PoCL miscompiles kernels whose throw sites are reached from
different barrier-separated regions, and declined the fix (pocl/pocl#2281),
which the JLLs carry as a patch. Since #616 every kernel with bounds checks
on both sides of a barrier has that shape: `any`, `all` and `count` give
wrong results on a distribution's PoCL 7.2 under `--check-bounds=yes`, and
the `pocl_local` job, which builds upstream PoCL HEAD, aborts in
`ParallelRegion::chainAfter`.

Stop testing upstream PoCL, drop the test skip for it, and document that
PoCL should be used through pocl_jll.
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