diff --git a/lib/KernelInterface/src/launch.jl b/lib/KernelInterface/src/launch.jl index e499015d9..568affccb 100644 --- a/lib/KernelInterface/src/launch.jl +++ b/lib/KernelInterface/src/launch.jl @@ -206,13 +206,19 @@ Not part of the public interface; used by KernelInterface's and KernelAbstractio 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) + check_work_group_dims(kernel.backend, items) max_items = max_work_group_size(kernel) prod_exceeds(items, max_items) && throw_work_group_size_error(items, max_items) return end +# kept out of `check_work_group_size`, which is inlined into code compiled for every kernel +@noinline function check_work_group_dims(backend::Backend, items::Dims{3}) + max_dims = max_work_group_dims(backend) + all(items .<= max_dims) || throw_work_group_dims_error(items, max_dims) + 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 991db0a8c..33500b314 100644 --- a/src/backend_launch.jl +++ b/src/backend_launch.jl @@ -96,13 +96,16 @@ Core.kwcall(kwargs::NamedTuple, obj::Kernel{<:KI.Backend}, args::Vararg{Any, N}) launch_tuple(obj, args; kwargs...) function launch_tuple(obj::Kernel, args::Tuple; ndrange = nothing, workgroupsize = nothing) - ndrange, workgroupsize, iterspace, dynamic = launch_config(obj, ndrange, workgroupsize) + setup = launch_setup(obj, ndrange, workgroupsize) # nothing to launch (or compile) for an empty ndrange - any(iszero, size(blocks(iterspace))) && return nothing + setup === nothing && return nothing + ndrange, workgroupsize, iterspace = setup # launch on an N-d grid, computing indices in a narrow type, if possible. this doesn't # depend on the tuned workgroup size, so the context (and thus the kernel) doesn't either. - launch = select_launch(obj, workgroupsize, iterspace) + # (not done by `launch_setup`: the launch mode isn't inferred concretely, so returning + # it would box the result) + launch = @noinline select_launch(obj, workgroupsize, iterspace) T = narrow_index_type(backend(obj)) if launch === NDLaunch{T}() # the common case, specialized statically @@ -115,6 +118,16 @@ function launch_tuple(obj::Kernel, args::Tuple; ndrange = nothing, workgroupsize return nothing end +# The parts of the launch that don't depend on the arguments are kept out of +# `launch_tuple` and `launch_kernel`, which are compiled for every kernel. + +# normalize the launch arguments, or return `nothing` if there is nothing to launch +@noinline function launch_setup(obj::Kernel, ndrange, workgroupsize) + ndrange, workgroupsize, iterspace, dynamic = launch_config(obj, ndrange, workgroupsize) + any(iszero, size(blocks(iterspace))) && return nothing + return ndrange, workgroupsize, iterspace +end + function launch_kernel(obj::Kernel, launch, ndrange, _workgroupsize, iterspace, args::Tuple) # a constant `nothing` for kernels without collectives; for kernels with, the call below # is a function barrier that makes the rest of the launch type stable @@ -132,10 +145,26 @@ function launch_kernel(obj::Kernel, launch, subgroups, ndrange, _workgroupsize, kernel = KI.kernel_function(b, obj.f, tt; compiler_options(obj)...) # tune the workgroup size, keeping the context type (and thus the kernel) the same - if workgroupsize(obj) <: DynamicSize && _workgroupsize === nothing + threads = if workgroupsize(obj) <: DynamicSize && _workgroupsize === nothing + range = something(ndrange, static_ndrange(obj)) + KI.launch_configuration(kernel; nitems = saturated_prod(extents(range))).workgroupsize + else + nothing + end + ctx, groups, items = launch_geometry(obj, launch, subgroups, ndrange, iterspace, ctx, threads) + + KI.check_work_group_size(kernel, items) + KI.launch(kernel, groups, items, prepend(ctx, args)) + return nothing +end + +# the context and launch geometry, re-partitioned for the tuned workgroup size `threads` +@noinline function launch_geometry( + obj::Kernel, launch, subgroups, ndrange, iterspace, ctx, threads + ) + if threads !== nothing range = something(ndrange, static_ndrange(obj)) - threads = KI.launch_configuration(kernel; nitems = saturated_prod(extents(range))).workgroupsize - iterspace, _ = partition(obj, ndrange, launch_workgroupsize(b, launch, threads, range)) + iterspace, _ = partition(obj, ndrange, launch_workgroupsize(backend(obj), launch, threads, range)) ctx = mkcontext(obj, ndrange, iterspace, launch; subgroups) end @@ -147,9 +176,7 @@ function launch_kernel(obj::Kernel, launch, subgroups, ndrange, _workgroupsize, else 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 + return ctx, groups, items end # The helpers below avoid splatting the arguments, and `map`, which isn't type stable for 32