Repository navigation
POCL: claim the exception output with an atomic max - #976
Conversation
PoCL implements a compare-and-swap with its `atomic_compare_exchange_strong_explicit` builtin, which returns the old value through a stack slot. `claim_output` reads work-item ids, so PoCL inlines it into each of its call sites, two per throw site, before it forms the work-item loops, and those give every copy of the slot an array over the work-group. With LLVM 22, alias analysis treats the acquire-release cmpxchg as clobbering the slot, so the stored value isn't forwarded and the arrays stay: about 8 bytes per bounds check per work-item. PoCL's stack estimate, which keeps host work-groups within 1 MiB, counted the slot only once, so kernels with a few hundred bounds checks overflowed the stack at large work-groups. LLVM 23 knows that a local that never escapes isn't affected by the cmpxchg (llvm/llvm-project#193939) and removes the arrays. An atomic max takes the lock the same way (0 to 1, leaving 1 and 2 as they are) and returns the old value directly. Fixes #974.
Benchmark resultsCompared with 7c56d9a95, the merge base. Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.
All benchmarks
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #976 +/- ##
==========================================
- Coverage 85.62% 84.78% -0.84%
==========================================
Files 31 31
Lines 2970 2925 -45
==========================================
- Hits 2543 2480 -63
- Misses 427 445 +18 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks! As mentioned on Slack this does fix the reproducer in #974 and help with resolving the issue we observed in Oceananigans tests, however there's also another related issue, below is a summary from the bot There's another 8 bytes per throw site per work-item left, which only shows up when the outermost dimension of the work-group is 2. This is a variant of the reproducer from #974 which takes the work-group size as argument: using KernelAbstractions
# a stencil with `M` bounds-checked reads, unrolled
@generated function stencil(a, i, j, k, ::Val{M}) where M
reads = [:(s += a[i + $m, j, k]) for m in 1:M]
return :(s = zero(eltype(a)); $(reads...); s)
end
@kernel function many_reads!(out, a, v)
i, j, k = @index(Global, NTuple)
@inbounds out[i, j, k] = stencil(a, i, j, k, v)
end
M = 256
workgroup = Tuple(parse.(Int, split(ARGS[1], ",")))
a = rand(64 + M, 64, 64)
out = zeros(64, 64, 64)
many_reads!(CPU(), workgroup)(out, a, Val(M); ndrange=size(out))
KernelAbstractions.synchronize(CPU())
println("OK, workgroup = $workgroup")$ julia --project repro.jl 64,64,1
OK, workgroup = (64, 64, 1)
$ julia --project repro.jl 64,32,2
Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.
ERROR: LoadError: StackOverflowError:Frame of the work-group function (
32×32×3 has a 6.3 KB frame and 16×16×8 one of 264 B, so it's specifically a 2 in the outermost dimension (2×64×1 is fine). Before this PR this cost came on top of the cmpxchg slot, so these shapes needed about 16 B per site. 32×32×2 is what KernelAbstractions picks by default for an ndrange of 32×32×10, which is the case of the Oceananigans test: with this PR the frame of its largest kernel (480 throw sites) went from 12.9 MB to 6.5 MB. The test passes, but with less than 2 MB to spare on the 8 MiB stack, and a 4096-item work-group of the same shape would overflow again. In the machine code of that kernel, PoCL reserves 788 arrays of 8 bytes per work-item. They're written only on the throw paths, with the result of the inlined I haven't looked into PoCL to see why a trip count of 2 in the outermost work-item loop makes a difference; my guess is that the loop gets unrolled or peeled, and something per throw site that is otherwise a scalar ends up in a context array. Julia 1.13.3, Linux x86_64, pocl_standalone_jll 7.2.3+8. |
Every bounds check in a kernel carried a copy of the exception reporting code, which PoCL inlines into the work-item loops. Values in that code could get an array over the work-group, so kernels with many bounds checks needed megabytes of stack (#974): with a work-group whose outermost dimension is 2, e.g. 32x32x2, still 8 bytes per bounds check per work-item after #976. Use GPUCompiler's reporting by throw site instead: a throw only records its site's id and work-item, through a single copy of the reporting code per kernel, and the host looks up what was thrown. A kernel with 256 bounds checks now has no stack frame at any work-group shape, instead of up to 4.2 MB, and 6.6 KB of code instead of 83-122 KB. `KernelException` now names the exception, the work-item and the reason, and at debug level 2 has a backtrace that reaches the kernel, instead of the kernel printing them. Kernels that launch work-groups of both the full and the bounds-checked variant look the site up in the table of the variant that ran the work-item.
Every bounds check in a kernel carried a copy of the exception reporting code, which PoCL inlines into the work-item loops. Values in that code could get an array over the work-group, so kernels with many bounds checks needed megabytes of stack (#974): with a work-group whose outermost dimension is 2, e.g. 32x32x2, still 8 bytes per bounds check per work-item after #976. Use GPUCompiler's reporting by throw site instead: a throw only records its site's id and work-item, through a single copy of the reporting code per kernel, and the host looks up what was thrown. A kernel with 256 bounds checks now has no stack frame at any work-group shape, instead of up to 4.2 MB, and 6.6 KB of code instead of 83-122 KB. `KernelException` now names the exception, the work-item and the reason, and at debug level 2 has a backtrace that reaches the kernel, instead of the kernel printing them. Kernels that launch work-groups of both the full and the bounds-checked variant look the site up in the table of the variant that ran the work-item.
Every bounds check in a kernel carried a copy of the exception reporting code, which PoCL inlines into the work-item loops. Values in that code could get an array over the work-group, so kernels with many bounds checks needed megabytes of stack (#974): with a work-group whose outermost dimension is 2, e.g. 32x32x2, still 8 bytes per bounds check per work-item after #976. Use GPUCompiler's reporting by throw site instead: a throw only records its site's id and work-item, through a single copy of the reporting code per kernel, and the host looks up what was thrown. A kernel with 256 bounds checks now has no stack frame at any work-group shape, instead of up to 4.2 MB, and 6.6 KB of code instead of 83-122 KB. `KernelException` now names the exception, the work-item and the reason, and at debug level 2 has a backtrace that reaches the kernel, instead of the kernel printing them. Kernels that launch work-groups of both the full and the bounds-checked variant look the site up in the table of the variant that ran the work-item.
Fixes #974.
Kernels with many bounds checks overflowed the thread stack at large work-groups on the CPU back end. The stack went to the exception lock in
claim_output, not to theBoundsErrorarguments, which #966 already removed.claim_outputtook the lock with a compare-and-swap. PoCL implements that withatomic_compare_exchange_strong_explicit, which returns the old value through a stack slot. From there:claim_outputreads work-item ids, so PoCL inlines it into each of its call sites (two per throw site) before forming the work-item loops. The loops then give every copy of the slot an array over the work-group.CL_KERNEL_PRIVATE_MEM_SIZE, counts the slot only once (4 bytes). Neither KernelAbstractions' 1 MiB check nor PoCL'sCL_KERNEL_WORK_GROUP_SIZElimit caught it.LLVM 23 knows that a local that never escapes isn't affected by the cmpxchg (llvm/llvm-project#193939) and removes the arrays, so PoCL builds on LLVM 23 don't have this problem.
An atomic max takes the lock the same way (0 becomes 1; 1 and 2 stay) and returns the old value directly. The issue's kernel now has a 216-byte frame.
The new test runs 256 bounds checks in a work-group of the device's maximum size. Without this change it fails with a
StackOverflowError.OpenCL.jl has the same lock, and the same problem at debug level 2: JuliaGPU/OpenCL.jl#609.