From ef63e0ecb3e0f59884a2a50c65a58fc97a24fc50 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 7 Oct 2026 10:53:43 +0200 Subject: [PATCH 1/3] Don't infer the rare launch modes for every kernel select_launch returns a Union of launch modes. launch_tuple handles NDLaunch{Int32} statically, but inference union-split the other branch, so the whole launch chain down to the back-end's compiler was inferred three more times for every kernel. Hide the callee of that branch from inference, so those modes are only compiled for kernels that use them. --- src/backend_launch.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend_launch.jl b/src/backend_launch.jl index 28e49b997..3f99c0faf 100644 --- a/src/backend_launch.jl +++ b/src/backend_launch.jl @@ -95,7 +95,9 @@ function launch_tuple(obj::Kernel, args::Tuple; ndrange = nothing, workgroupsize # the common case, specialized statically launch_kernel(obj, NDLaunch{Int32}(), ndrange, workgroupsize, iterspace, args) else - launch_kernel(obj, launch, ndrange, workgroupsize, iterspace, args) + # the other modes are rare: hide the callee from inference, so that it doesn't + # analyze the whole launch chain for each of them when compiling every kernel + Base.inferencebarrier(launch_kernel)(obj, launch, ndrange, workgroupsize, iterspace, args) end return nothing end From 715205a994be21dd43ebca5508e35299f5596451 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 7 Oct 2026 10:53:43 +0200 Subject: [PATCH 2/3] Keep KernelInterface's launch checks out of per-kernel code launch_geometry is inlined into the kernel call, which is specialized on the kernel, and check_work_group_size took the kernel too. So validating the launch keywords was compiled again for every kernel. Only the limit and configuration queries need the kernel: do the rest in functions that don't take it. --- lib/KernelInterface/src/launch.jl | 53 +++++++++++++++++++------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/KernelInterface/src/launch.jl b/lib/KernelInterface/src/launch.jl index 99d181b81..00f9c88d6 100644 --- a/lib/KernelInterface/src/launch.jl +++ b/lib/KernelInterface/src/launch.jl @@ -143,20 +143,13 @@ Validate the launch keywords of a [`Kernel`](@ref) call and turn them into the n work-groups and the work-group size. A zero number of work-groups means nothing is launched. """ @inline function launch_geometry(kernel::Kernel, numgroups, workgroupsize, ndrange, max_work_group_size) - check_dims("numgroups", numgroups) - check_dims("workgroupsize", workgroupsize) - check_dims("ndrange", ndrange) - if ndrange != () && numgroups != () - throw(ArgumentError("Only one of `numgroups` and `ndrange` can be used")) - end - max_work_group_size > 0 || - throw(ArgumentError("`max_work_group_size` must be positive, got $max_work_group_size")) - + check_launch(numgroups, workgroupsize, ndrange, max_work_group_size) items = if workgroupsize != () wgsize = pad3(workgroupsize) - any(iszero, wgsize) && - throw(ArgumentError("`workgroupsize` must be positive, got $(repr(workgroupsize))")) - check_work_group_size(kernel, wgsize) + max_dims = max_work_group_dims(kernel.backend) + all(wgsize .<= max_dims) || throw_work_group_dims_error(wgsize, max_dims) + max_items = KernelInterface.max_work_group_size(kernel) + prod_exceeds(wgsize, max_items) && throw_work_group_size_error(wgsize, max_items) wgsize elseif ndrange == () (1, 1, 1) @@ -170,7 +163,27 @@ work-groups and the work-group size. A zero number of work-groups means nothing ) threads_to_workgroupsize(config.workgroupsize, wanted, max_work_group_dims(kernel.backend)) end + return launch_groups(numgroups, ndrange, items), items +end +# The parts of `launch_geometry` that don't need the kernel. They are kept out of it, which +# is compiled for every kernel. + +function check_launch(numgroups, workgroupsize, ndrange, max_work_group_size) + check_dims("numgroups", numgroups) + check_dims("workgroupsize", workgroupsize) + check_dims("ndrange", ndrange) + if ndrange != () && numgroups != () + throw(ArgumentError("Only one of `numgroups` and `ndrange` can be used")) + end + max_work_group_size > 0 || + throw(ArgumentError("`max_work_group_size` must be positive, got $max_work_group_size")) + any(iszero, workgroupsize) && + throw(ArgumentError("`workgroupsize` must be positive, got $(repr(workgroupsize))")) + return +end + +function launch_groups(numgroups, ndrange, items::Dims{3}) groups = if ndrange != () cld.(pad3(ndrange), items) elseif numgroups != () @@ -183,17 +196,15 @@ work-groups and the work-group size. A zero number of work-groups means nothing if !any(iszero, groups) && any(map((g, i) -> g > typemax(Int) รท i, groups, items)) throw(ArgumentError("Launch of $groups work-groups of $items work-items has more than typemax(Int) work-items in a dimension")) end - return groups, items + return groups end -function check_work_group_size(kernel::Kernel, items::Dims{3}) - max_dims = max_work_group_dims(kernel.backend) - all(items .<= max_dims) || - throw(ArgumentError("Work-group size $items exceeds the maximum of $max_dims per dimension")) - max_items = max_work_group_size(kernel) - prod_exceeds(items, max_items) && - throw(ArgumentError("Work-group size $items has more than $max_items work-items, the maximum for this kernel")) - return +@noinline function throw_work_group_dims_error(items, max_dims) + throw(ArgumentError("Work-group size $items exceeds the maximum of $max_dims per dimension")) +end + +@noinline function throw_work_group_size_error(items, max_items) + throw(ArgumentError("Work-group size $items has more than $max_items work-items, the maximum for this kernel")) end """ From 6cf611db7456149e58637118c9944b33c8e51a1d Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 7 Oct 2026 11:22:56 +0200 Subject: [PATCH 3/3] Launch @kernel kernels through KernelInterface's back-end hook KA computed the launch geometry and then passed it as keywords to the KI kernel call, which validated and normalized it again. That added a keyword call and its geometry code to what is compiled for every kernel. Call KI.launch directly instead: the geometry is valid by construction, except for the kernel's limits, which KA now checks with the function KI uses. --- lib/KernelInterface/src/launch.jl | 22 ++++++++++++++++++---- src/backend_launch.jl | 14 ++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/KernelInterface/src/launch.jl b/lib/KernelInterface/src/launch.jl index 00f9c88d6..e499015d9 100644 --- a/lib/KernelInterface/src/launch.jl +++ b/lib/KernelInterface/src/launch.jl @@ -146,10 +146,7 @@ work-groups and the work-group size. A zero number of work-groups means nothing check_launch(numgroups, workgroupsize, ndrange, max_work_group_size) items = if workgroupsize != () wgsize = pad3(workgroupsize) - max_dims = max_work_group_dims(kernel.backend) - all(wgsize .<= max_dims) || throw_work_group_dims_error(wgsize, max_dims) - max_items = KernelInterface.max_work_group_size(kernel) - prod_exceeds(wgsize, max_items) && throw_work_group_size_error(wgsize, max_items) + check_work_group_size(kernel, wgsize) wgsize elseif ndrange == () (1, 1, 1) @@ -199,6 +196,23 @@ function launch_groups(numgroups, ndrange, items::Dims{3}) return groups end +""" + check_work_group_size(kernel::Kernel, items::Dims{3}) + +Check that work-groups of `items` work-items fit the limits of `kernel`, as +[`launch`](@ref) requires, throwing an `ArgumentError` otherwise. + +Not part of the public interface; used by KernelInterface's and KernelAbstractions' launch +code. +""" +@inline function check_work_group_size(kernel::Kernel, items::Dims{3}) + max_dims = max_work_group_dims(kernel.backend) + all(items .<= max_dims) || throw_work_group_dims_error(items, max_dims) + max_items = max_work_group_size(kernel) + prod_exceeds(items, max_items) && throw_work_group_size_error(items, max_items) + return +end + @noinline function throw_work_group_dims_error(items, max_dims) throw(ArgumentError("Work-group size $items exceeds the maximum of $max_dims per dimension")) end diff --git a/src/backend_launch.jl b/src/backend_launch.jl index 3f99c0faf..65bb040c5 100644 --- a/src/backend_launch.jl +++ b/src/backend_launch.jl @@ -117,14 +117,16 @@ function launch_kernel(obj::Kernel, launch, ndrange, _workgroupsize, iterspace, ctx = mkcontext(obj, ndrange, iterspace, launch) end - # launching through the `KI.Kernel` validates the sizes against the kernel's limits + # the geometry is valid by construction, except for the kernel's limits groups = size(blocks(iterspace)) items = size(workitems(iterspace)) if launch isa NDLaunch - call_kernel(kernel, ctx, args, groups, items) + groups, items = KI.pad3(groups), KI.pad3(items) else - call_kernel(kernel, ctx, args, prod(groups), prod(items)) + groups, items = (prod(groups), 1, 1), (prod(items), 1, 1) end + KI.check_work_group_size(kernel, items) + KI.launch(kernel, groups, items, prepend(ctx, args)) return nothing end @@ -143,8 +145,8 @@ end return :(Tuple{Core.Typeof(KI.argconvert(backend, ctx)), $(types...)}) end -# `kernel(ctx, args...; numgroups, workgroupsize)` -@inline @generated function call_kernel(kernel::KI.Kernel, ctx, args::Tuple, numgroups, workgroupsize) +# `(ctx, args...)` +@inline @generated function prepend(ctx, args::Tuple) argexprs = (:(args[$i]) for i in 1:fieldcount(args)) - return :(kernel(ctx, $(argexprs...); numgroups, workgroupsize)) + return :((ctx, $(argexprs...))) end