From 6d36f0dddea64e0ed3d46ef6c5909b6bc530a429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Fri, 27 Nov 2026 21:39:46 -0600 Subject: [PATCH 1/2] POCL: keep the queue fallback out of the inference of launches `launch_and_wait`, which is compiled for every kernel, runs a host kernel, or falls back to the queue when there is none: when PoCL can't create a host kernel, or when the work-group's stack would exceed `host_kernel_stack_limit`. KernelAbstractions' own launches always pass 3-d sizes, so for them the queue is a rare fallback, but inference analyzed it for every kernel anyway: `launch_queued`, `launch_converted`, `clcall` and nanoOpenCL's `call`, `convert_arguments` and `set_args!`, partly with abstract types, and all of it specialized on the kernel and its arguments. The queue is now a `@noinline` function, `launch_queued_and_wait`. Launches that may run a host kernel call it through `Base.inferencebarrier`, so that it is only compiled for the kernels that fall back to it. Whether a launch may run a host kernel only depends on the types of its keyword arguments (`may_run_on_host`): launches without 3-d `global_size` and `local_size`, like `@opencl` launches with integer sizes, always run on the queue, and keep calling it directly, without a dynamic call or allocations. Host launches are unchanged. The precompile workload's `@kernel` launch no longer compiles the queue, so the workload also launches a kernel on the queue, with `@opencl`, which `@opencl` launches use from the package image. In Oceananigans, with `-O0 --check-bounds=yes` as in its CI, solving a Poisson problem on a new grid size compiles 19 CPU kernels, which took 2.5-2.8 s and now take 2.3-2.4 s; its test of Poisson solvers on stretched grids, which does that for about 270 grid sizes, goes from 606-675 to 550-608 s, and allocates 153 instead of 171 GiB. Launches that fall back to the queue allocate 176 more bytes for the dynamic call (240 instead of 64), and take as long as before; the first `@opencl` launch takes about 165 instead of 250 ms. Co-Authored-By: Claude Opus 5.5 --- Project.toml | 2 +- src/pocl/compiler/execution.jl | 40 ++++++++++++++++++++++++++-------- src/precompile.jl | 7 ++++++ test/runtests.jl | 34 +++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 76fa02930..76a4b3ec9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "KernelAbstractions" uuid = "63c18a36-062a-441e-b654-da1e3ab1ce7c" -version = "0.10.9" +version = "0.10.10" authors = ["Valentin Churavy and contributors"] [workspace] diff --git a/src/pocl/compiler/execution.jl b/src/pocl/compiler/execution.jl index 441dc3822..8f866e89b 100644 --- a/src/pocl/compiler/execution.jl +++ b/src/pocl/compiler/execution.jl @@ -193,14 +193,15 @@ function launch_and_wait( # share conversion, the bulk of each path's per-kernel code converted = converted_arguments(kernel, args) if hp === nothing - info = exception_info() - info[] = ExceptionInfo_st() - GC.@preserve info begin - info_ptr = Base.unsafe_convert(Ptr{ExceptionInfo_st}, info) - event = launch_queued(with_fun(kernel, partial), converted, info_ptr; kwargs...) - wait_for_kernel(event) + queued = with_fun(kernel, partial) + fun = something(partial, kernel.fun) + if may_run_on_host(values(kwargs)) + # (behind a barrier: the queue is a fallback here, and inferring it would + # compile its argument handling for every kernel) + Base.inferencebarrier(launch_queued_and_wait)(queued, converted, fun, values(kwargs)) + else + launch_queued_and_wait(queued, converted, fun, values(kwargs)) end - info[].status == 0 || throw_kernel_exception(info[], something(partial, kernel.fun)) else global_size = values(kwargs).global_size num_groups = something(groups, global_size .÷ hp.local_size) @@ -218,6 +219,22 @@ function launch_and_wait( return nothing end +# launch `kernel` with the arguments `converted` on the queue, and wait for it; `fun` is the +# compiled kernel that reports exceptions +@noinline function launch_queued_and_wait( + kernel::AbstractKernel, converted::Tuple, fun::cl.Kernel, kwargs::NamedTuple + ) + info = exception_info() + info[] = ExceptionInfo_st() + GC.@preserve info begin + info_ptr = Base.unsafe_convert(Ptr{ExceptionInfo_st}, info) + event = launch_queued(kernel, converted, info_ptr; kwargs...) + wait_for_kernel(event) + end + info[].status == 0 || throw_kernel_exception(info[], fun) + return nothing +end + # kept out of `launch_and_wait`, which is compiled for every kernel @noinline function wait_for_kernel(event::cl.Event) try @@ -501,10 +518,15 @@ function host_kernel_max_items(k::cl.Kernel) return prevpow(2, max(host_kernel_stack_limit ÷ k.private_mem_size, 1)) end +# whether a launch with the keyword arguments `kwargs` may run a host kernel; others always +# run on the queue. (decided by the types of `kwargs`) +@inline may_run_on_host(kwargs::NamedTuple) = + haskey(kwargs, :local_size) && haskey(kwargs, :global_size) && + kwargs.local_size isa Dims{3} && kwargs.global_size isa Dims{3} + @inline function host_kernel(kernel::HostKernel, kwargs::NamedTuple, groups) - haskey(kwargs, :local_size) && haskey(kwargs, :global_size) || return nothing + may_run_on_host(kwargs) || return nothing local_size, global_size = kwargs.local_size, kwargs.global_size - local_size isa Dims{3} && global_size isa Dims{3} || return nothing # host kernels only run whole work-groups groups === nothing && global_size .% local_size != (0, 0, 0) && return nothing return host_kernel(kernel.fun, local_size, kernel.rng_state) diff --git a/src/precompile.jl b/src/precompile.jl index cf54ef37c..305664d6f 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -8,6 +8,12 @@ @inbounds A[i] = fma(2, B[i], 1) end +# a kernel for the queue, which launches without a 3-d work-group size take +function precompile_queued_kernel(A) + @inbounds A[1] = 1.0f0 + return +end + # whether the workload below launched its kernel, checked by the tests const precompiled_launch = Ref(false) @@ -27,6 +33,7 @@ PrecompileTools.@setup_workload begin A = Base.zeros(Float32, 4) B = Base.ones(Float32, 4) precompile_kernel(CPU())(A, B; ndrange = length(A)) + POCL.@opencl global_size = 1 local_size = 1 precompile_queued_kernel(A) synchronize(CPU()) end end diff --git a/test/runtests.jl b/test/runtests.jl index e8c08f10f..b880daa6f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1761,6 +1761,40 @@ end @test all(==(-1), view(B, 1:m)) end +@kernel function queue_free_kernel!(A) + i = @index(Global) + @inbounds A[i] = i +end + +# The queue is a fallback, which launches only compile when they take it +@testset "POCL launches don't compile the queue for every kernel" begin + A = zeros(Int, 13) + queue_free_kernel!(POCL.POCLBackend(), 8)(A; ndrange = length(A)) # a split launch + @test A == 1:13 + F = typeof(gpu_queue_free_kernel!) + queued = [ + mi for m in methods(POCL.launch_converted) for mi in Base.specializations(m) + if mi isa Core.MethodInstance && occursin(string(F), string(mi.specTypes)) + ] + @test isempty(queued) +end + +function queue_only_kernel!(A) + @inbounds A[1] = 1 + return +end + +queued_launch_allocations(k, A) = @allocated k(A; global_size = 4, local_size = 4) + +# ... while launches that can only run on the queue (without 3-d sizes) call it directly +@testset "POCL launches on the queue don't allocate" begin + A = zeros(Int, 4) + k = @opencl launch = false queue_only_kernel!(A) + queued_launch_allocations(k, A) + @test queued_launch_allocations(k, A) == 0 + @test A[1] == 1 +end + @kernel function muladd_kernel!(D, @Const(A), @Const(B), @Const(C)) i = @index(Global) @inbounds D[i] = muladd(A[i], B[i], C[i]) From 841d7310cc51f1a3556581383be129352ef5b0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sat, 28 Nov 2026 13:19:36 -0600 Subject: [PATCH 2/2] Skip the test of queued launch allocations where waiting allocates Launching a kernel on the queue waits for it, which allocates on Julia 1.10 and 1.11 (and is shielded from task cancellation on 1.14+), with or without the previous change: the `@opencl` launch of the test allocates 1.9-4.1 kB on 1.10 and 0.4-1.9 kB on 1.11 with KernelAbstractions 0.10.9 as well, and nothing on 1.12 and 1.13. The test now skips the check on those versions, as the other tests of launch allocations do. Co-Authored-By: Claude Opus 5.5 --- test/runtests.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index b880daa6f..47f3fe49c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1791,7 +1791,9 @@ queued_launch_allocations(k, A) = @allocated k(A; global_size = 4, local_size = A = zeros(Int, 4) k = @opencl launch = false queue_only_kernel!(A) queued_launch_allocations(k, A) - @test queued_launch_allocations(k, A) == 0 + # (waiting for the kernel allocates on some versions, see "POCL launch allocations") + skip_wait = VERSION < v"1.12" || isdefined(Base, :CANCEL_TOKEN) + @test queued_launch_allocations(k, A) == 0 skip = skip_wait @test A[1] == 1 end