From c9a98efe436b31c684137046c74eae33ff77528e Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 13 Nov 2026 15:04:11 +0100 Subject: [PATCH] POCL: claim the exception output with an atomic max 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. --- src/pocl/device/runtime.jl | 17 +++++++++++------ test/runtests.jl | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/pocl/device/runtime.jl b/src/pocl/device/runtime.jl index 99cedce53..fd9e5ad50 100644 --- a/src/pocl/device/runtime.jl +++ b/src/pocl/device/runtime.jl @@ -32,12 +32,16 @@ end ) return end -@device_function @inline function atomic_cas_device!(ptr::LLVMPtr{Int32, AS.CrossWorkgroup}, cmp::Int32, val::Int32) - (; old) = UnsafeAtomics.Internal.llvm_cmpxchg!( - ptr, cmp, val, Val(:acq_rel), Val(:acquire), Val(:device), - Val(false), Val(false), Val(sizeof(Int32)), Val(()) +# not a compare-and-swap: PoCL's builtin for it returns the old value through a stack slot. +# `claim_output` reads work-item ids, so PoCL inlines it into every throw site before forming +# the work-item loops, which give each copy of the slot an array over the work-group. LLVM 22 +# can't forward stores across the cmpxchg, so these arrays stay: megabytes of stack for a +# kernel with many bounds checks (JuliaGPU/KernelAbstractions.jl#974). LLVM 23 removes them +# (llvm/llvm-project#193939). +@device_function @inline function atomic_max_device!(ptr::LLVMPtr{Int32, AS.CrossWorkgroup}, val::Int32) + return UnsafeAtomics.Internal.llvm_rmw!( + ptr, Val(:max), val, Val(:acq_rel), Val(:device), Val(false), Val(sizeof(Int32)), Val(()) ) - return old end # only one work-item reports the exception, or the output of all work-items that throw @@ -51,7 +55,8 @@ end @device_function @noinline function claim_output() lock = exception_field(Val(:output_lock)) me = (get_global_id(1), get_global_id(2), get_global_id(3), 0) - state = atomic_cas_device!(lock, 0 % Int32, 1 % Int32) + # 0 → 1 claims the output; 1 and 2 stay + state = atomic_max_device!(lock, 1 % Int32) if state == 0 unsafe_store!(exception_field(Val(:owner)), me) # publish the owner before other work-items compare against it diff --git a/test/runtests.jl b/test/runtests.jl index 6d48a8474..709611ab2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1097,6 +1097,26 @@ end @test occursin("throw_boundserror", out) end +# throw sites cost little or no stack per work-item. this kernel's largest work-group used to +# need 8 MiB of stack, which its launches would get by default on a few threads +# (JuliaGPU/KernelAbstractions.jl#974). +@generated function bounds_checked_sum(a, i, ::Val{N}) where {N} + reads = [:(s += a[i + $n]) for n in 1:N] + return :(s = zero(eltype(a)); $(reads...); s) +end +@testset "POCL many throw sites" begin + @kernel function many_throw_sites!(out, a) + i = @index(Global, Linear) + out[i] = bounds_checked_sum(a, i, Val(256)) + end + n = POCL.device_limits().max_work_group_size + a = rand(n + 256) + out = zeros(n) + many_throw_sites!(CPU(), n)(out, a; ndrange = n) + synchronize(CPU()) + @test out ≈ [sum(@view a[(i + 1):(i + 256)]) for i in 1:n] +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