From ca2898215e1710f075bb393ea3f8d94f2d5d72db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Fri, 11 Sep 2026 05:38:03 -0500 Subject: [PATCH 1/3] Cut allocations when launching CPU kernels A warm launch of a two-argument kernel on the CPU backend allocated about 50 times (2.2 KiB). Most of it came from Julia not specializing on `args...` that a method only splats onward, which boxed every kernel argument on the way from `Kernel` to `clSetKernelArg`; the rest from per-launch vectors for the work sizes and SVM pointers, a keyword splat through the generated kernel call, and a device property query. - Annotate the splatted arguments as `Vararg{Any, N}` in the `Kernel` call, the generated `AbstractKernel` call, `call` and `set_args!`, and unroll `set_args!` by recursion instead of `enumerate`. - Pass each kernel argument to `clSetKernelArg` as a `Ref{T}` `ccall` argument, which copies it to the stack. - Pass the global and local work sizes as three-element tuples through a `ccall` taking `Ref{NTuple{3, Csize_t}}` when there is no offset or device RNG state, skipping the `max_work_item_dims` query. - The generated kernel call takes `global_size` and `local_size` as explicit keywords. - `clconvert` and `call` take `nothing` for the SVM pointers instead of allocating an empty vector; no adaptor rule collects such pointers. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VHciC8x39gm97sABrSvBkt --- src/pocl/backend.jl | 2 +- src/pocl/compiler/execution.jl | 15 ++++---- src/pocl/nanoOpenCL.jl | 63 ++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index 98a606afe..99f834c71 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -185,7 +185,7 @@ function threads_to_workgroupsize(threads, ndrange) end end -function (obj::KA.Kernel{POCLBackend})(args...; ndrange = nothing, workgroupsize = nothing) +function (obj::KA.Kernel{POCLBackend})(args::Vararg{Any, N}; ndrange = nothing, workgroupsize = nothing) where {N} ndrange, workgroupsize, iterspace, dynamic = KA.launch_config(obj, ndrange, workgroupsize) diff --git a/src/pocl/compiler/execution.jl b/src/pocl/compiler/execution.jl index 3e2c78135..5b360842d 100644 --- a/src/pocl/compiler/execution.jl +++ b/src/pocl/compiler/execution.jl @@ -86,7 +86,7 @@ end ## argument conversion struct KernelAdaptor - svm_pointers::Vector{Ptr{Cvoid}} + svm_pointers::Union{Nothing, Vector{Ptr{Cvoid}}} end # # assume directly-passed pointers are SVM pointers @@ -137,7 +137,7 @@ register methods for the the `OpenCL.KernelAdaptor` type. The `pointers` argument is used to collect pointers to indirect SVM buffers, which need to be registered with OpenCL before invoking the kernel. """ -function clconvert(arg, pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[]) +function clconvert(arg, pointers::Union{Nothing, Vector{Ptr{Cvoid}}} = nothing) return adapt(KernelAdaptor(pointers), arg) end @@ -149,11 +149,11 @@ abstract type AbstractKernel{F, TT} end pass_arg(@nospecialize dt) = !(GPUCompiler.isghosttype(dt) || Core.Compiler.isconstType(dt)) @inline @generated function (kernel::AbstractKernel{F, TT})( - args...; - call_kwargs... - ) where {F, TT} + args::Vararg{Any, N}; + global_size = (1,), local_size = nothing + ) where {F, TT, N} sig = Tuple{F, TT.parameters...} # Base.signature_type with a function type - args = (:(kernel.f), (:(clconvert(args[$i], svm_pointers)) for i in 1:length(args))...) + args = (:(kernel.f), (:(clconvert(args[$i])) for i in 1:length(args))...) # filter out ghost arguments that shouldn't be passed to_pass = map(pass_arg, sig.parameters) @@ -175,8 +175,7 @@ pass_arg(@nospecialize dt) = !(GPUCompiler.isghosttype(dt) || Core.Compiler.isco call_tt = Base.to_tuple_type(call_t) return quote - svm_pointers = Ptr{Cvoid}[] - $cl.clcall(kernel.fun, $call_tt, $(call_args...); svm_pointers, kernel.rng_state, call_kwargs...) + $cl.clcall(kernel.fun, $call_tt, $(call_args...); global_size, local_size, kernel.rng_state) end end diff --git a/src/pocl/nanoOpenCL.jl b/src/pocl/nanoOpenCL.jl index 2d196c9fe..e3adadbf1 100644 --- a/src/pocl/nanoOpenCL.jl +++ b/src/pocl/nanoOpenCL.jl @@ -665,6 +665,23 @@ end )::cl_int end +# sizes passed as tuples, which `ccall` copies to the stack +@checked function clEnqueueNDRangeKernel( + command_queue, kernel, work_dim, + global_work_size::NTuple{3, Csize_t}, local_work_size::NTuple{3, Csize_t}, event::Ref{cl_event} + ) + @ccall libopencl.POclEnqueueNDRangeKernel( + command_queue::cl_command_queue, + kernel::cl_kernel, work_dim::cl_uint, + C_NULL::Ptr{Csize_t}, + global_work_size::Ref{NTuple{3, Csize_t}}, + local_work_size::Ref{NTuple{3, Csize_t}}, + 0::cl_uint, + C_NULL::Ptr{cl_event}, + event::Ref{cl_event} + )::cl_int +end + @checked function clEnqueueNDRangeKernel( command_queue, kernel, work_dim, global_work_offset, global_work_size, @@ -1254,9 +1271,10 @@ function set_arg!(k::Kernel, idx::Integer, arg::LocalMem) end function set_arg!(k::Kernel, idx::Integer, arg::T) where {T} - ref = Ref(arg) - tsize = sizeof(ref) - err = unchecked_clSetKernelArg(k, cl_uint(idx - 1), tsize, ref) + # `Ref{T}` makes `ccall` pass a pointer to a stack copy of `arg` + err = @ccall libopencl.POclSetKernelArg( + k::cl_kernel, cl_uint(idx - 1)::cl_uint, sizeof(T)::Csize_t, arg::Ref{T} + )::cl_int if err == CL_INVALID_ARG_SIZE error( """Mismatch between Julia and OpenCL type for kernel argument $idx. @@ -1275,19 +1293,36 @@ function set_arg!(k::Kernel, idx::Integer, arg::T) where {T} return k end -function set_args!(k::Kernel, args...) - for (i, a) in enumerate(args) - set_arg!(k, i, a) - end - return +set_args!(k::Kernel, args::Vararg{Any, N}) where {N} = set_args!(k, 1, args...) +@inline set_args!(k::Kernel, i::Int) = nothing +@inline function set_args!(k::Kernel, i::Int, arg, args::Vararg{Any, N}) where {N} + set_arg!(k, i, arg) + return set_args!(k, i + 1, args...) end +# work sizes padded to the three dimensions OpenCL devices support +const WorkSize = NTuple{3, Csize_t} +@inline work_size(sizes) = ntuple(i -> i <= length(sizes) ? Csize_t(sizes[i]) : Csize_t(0), Val(3)) + function enqueue_kernel( k::Kernel, global_work_size, local_work_size = nothing; global_work_offset = nothing, rng_state = false, nargs = nothing ) - max_work_dim = device().max_work_item_dims work_dim = length(global_work_size) + + if global_work_offset === nothing && local_work_size !== nothing && !rng_state && work_dim <= 3 + if length(local_work_size) != work_dim + throw(ArgumentError("global_work_size and local_work_size have differing dims")) + end + ret_event = Ref{cl_event}() + clEnqueueNDRangeKernel( + queue(), k, cl_uint(work_dim), + work_size(global_work_size), work_size(local_work_size), ret_event + ) + return Event(ret_event[]) + end + + max_work_dim = device().max_work_item_dims if work_dim > max_work_dim throw(ArgumentError("global_work_size has max dim of $max_work_dim")) end @@ -1354,19 +1389,19 @@ function enqueue_kernel( end function call( - k::Kernel, args...; global_size = (1,), local_size = nothing, + k::Kernel, args::Vararg{Any, N}; global_size = (1,), local_size = nothing, global_work_offset = nothing, - svm_pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[], + svm_pointers::Union{Nothing, Vector{Ptr{Cvoid}}} = nothing, rng_state = false - ) + ) where {N} set_args!(k, args...) - if !isempty(svm_pointers) + if svm_pointers !== nothing && !isempty(svm_pointers) clSetKernelExecInfo( k, CL_KERNEL_EXEC_INFO_SVM_PTRS, sizeof(svm_pointers), svm_pointers ) end - return enqueue_kernel(k, global_size, local_size; global_work_offset, rng_state, nargs = length(args)) + return enqueue_kernel(k, global_size, local_size; global_work_offset, rng_state, nargs = N) end # convert the argument values to match the kernel's signature (specified by the user) From 9cb87bbe58fbb76a2215f4530147e0953528821b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Fri, 11 Sep 2026 05:38:03 -0500 Subject: [PATCH 2/3] Resolve compiled CPU kernels through a fast path Every launch went through `clfunction`, which built a compiler config, looked up the method instance, allocated a `CompilerJob`, queried GPUCompiler's cache and hashed its way into `_kernel_instances` before returning the `HostKernel` it had returned last time. Keep the resolved `HostKernel` per `HostKernel{F, tt}` type together with the world age and context it was resolved in, and return it directly while no method has been defined since and the context is unchanged. Reflection (a set `compile_hook`) and explicit compiler keywords bypass the fast path. Reading the `compile_hook` scoped value allocates, so it is only read when a dynamic scope is active. The `Ref` used to find the kernel for the current context and the `@something` in `compile_or_lookup` are replaced with plain control flow, which also removes a boxed variable captured by the `get!` closure. A warm launch now allocates once, for the event handle. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VHciC8x39gm97sABrSvBkt --- src/pocl/compiler/execution.jl | 56 ++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/pocl/compiler/execution.jl b/src/pocl/compiler/execution.jl index 5b360842d..e2ced20c3 100644 --- a/src/pocl/compiler/execution.jl +++ b/src/pocl/compiler/execution.jl @@ -193,8 +193,36 @@ end const clfunction_lock = ReentrantLock() +# `HostKernel` with the world age and context it was resolved in; valid as long as no method +# has been defined since and the context is unchanged. +struct ResolvedKernel + world::UInt + context::nanoOpenCL.Context + kernel::Any +end + +# `HostKernel{F, tt}` instances keyed by their type +const _kernel_fastpath = Dict{DataType, ResolvedKernel}() + +# Reading a `ScopedValue` allocates; outside of any dynamic scope it holds its default. +@static if VERSION >= v"1.11" + @inline compile_hook_set() = Core.current_scope() !== nothing && GPUCompiler.compile_hook[] !== nothing +else + @inline compile_hook_set() = GPUCompiler.compile_hook[] !== nothing +end + function clfunction(f::F, tt::TT = Tuple{}; kwargs...) where {F, TT} Base.@lock clfunction_lock begin + ctx = context() + world = Base.get_world_counter() + cacheable = isempty(kwargs) && !compile_hook_set() + if cacheable + entry = get(_kernel_fastpath, HostKernel{F, tt}, nothing) + if entry !== nothing && entry.world == world && entry.context === ctx + return entry.kernel::HostKernel{F, tt} + end + end + config = compiler_config(device(); kwargs...)::OpenCLCompilerConfig source = methodinstance(F, tt) job = CompilerJob(source, config) @@ -203,28 +231,34 @@ function clfunction(f::F, tt::TT = Tuple{}; kwargs...) where {F, TT} # Resolve the cl.Kernel for the active context. Linear scan over the # session-local cache; almost always n=1, so this is one `===` compare. - ctx = context() - kernel = Ref{nanoOpenCL.Kernel}() + cached = nothing @inbounds for (cached_ctx, cached_kernel) in res.kernels if cached_ctx === ctx - kernel[] = cached_kernel + cached = cached_kernel break end end - if !isassigned(kernel) - kernel[] = link_kernel(job, res.obj::Vector{UInt8}, res.entry::String) + kernel = if cached === nothing + linked = link_kernel(job, res.obj::Vector{UInt8}, res.entry::String) # Don't cache session-local kernel handles while precompiling: the # results struct is serialized into the package image along with its # CodeInstance, and the handles would come back dangling. if ccall(:jl_generating_output, Cint, ()) != 1 - push!(res.kernels, (ctx, kernel[])) + push!(res.kernels, (ctx, linked)) end + linked + else + cached end - h = hash(kernel[], hash(f, hash(tt))) - return get!(_kernel_instances, h) do - HostKernel{F, tt}(f, kernel[], res.device_rng) + h = hash(kernel, hash(f, hash(tt))) + hostkernel = get!(_kernel_instances, h) do + HostKernel{F, tt}(f, kernel, res.device_rng) end::HostKernel{F, tt} + if cacheable + _kernel_fastpath[HostKernel{F, tt}] = ResolvedKernel(world, ctx, hostkernel) + end + return hostkernel end end @@ -241,7 +275,9 @@ function compile_or_lookup(@nospecialize(job::CompilerJob))::OpenCLResults res = GPUCompiler.cached_results(OpenCLResults, job) if res === nothing || res.obj === nothing || GPUCompiler.compile_hook[] !== nothing compiled = compile_to_obj(job) - res = @something res GPUCompiler.cached_results(OpenCLResults, job) + if res === nothing + res = GPUCompiler.cached_results(OpenCLResults, job)::OpenCLResults + end res.obj = compiled.obj res.entry = compiled.entry res.device_rng = compiled.device_rng From 0afb29a9a098f81fd338565b87211c7e86c67e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Fri, 11 Sep 2026 06:05:38 -0500 Subject: [PATCH 3/3] Read the compile hook directly on Julia 1.14 Reading a `ScopedValue` no longer allocates on Julia 1.14, and checking `Core.current_scope()` first costs as much as the read itself there, so the guard is limited to the versions that need it. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VHciC8x39gm97sABrSvBkt --- src/pocl/compiler/execution.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pocl/compiler/execution.jl b/src/pocl/compiler/execution.jl index e2ced20c3..4e128b110 100644 --- a/src/pocl/compiler/execution.jl +++ b/src/pocl/compiler/execution.jl @@ -204,8 +204,9 @@ end # `HostKernel{F, tt}` instances keyed by their type const _kernel_fastpath = Dict{DataType, ResolvedKernel}() -# Reading a `ScopedValue` allocates; outside of any dynamic scope it holds its default. -@static if VERSION >= v"1.11" +# On Julia 1.11 to 1.13 reading a `ScopedValue` allocates; outside of any dynamic scope it +# holds its default, so the read is skipped there. +@static if v"1.11" <= VERSION < v"1.14-" @inline compile_hook_set() = Core.current_scope() !== nothing && GPUCompiler.compile_hook[] !== nothing else @inline compile_hook_set() = GPUCompiler.compile_hook[] !== nothing