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