Skip to content

Add helpers for implementing kernel launch macros - #48

Merged
maleadt merged 2 commits into
mainfrom
tb/kwargs-splat
Dec 2, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/kwargs-splat

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

Back-ends' launch macros (@cuda, @metal, @roc, @oneapi, @opencl, KernelAbstractions' POCL back-end and KI.@launch) each repeat the same parsing around GPUCompiler's split_kwargs and assign_args!, and route keyword arguments to compilation or launch by name when the macro is expanded. That rules out splatting keyword arguments, which an autotuner with NamedTuple configurations needs, because a splat's names are only known at run time:

hints = (; occupancy=2, num_ctas=1)
@cuda backend=cuTile blocks=grid hints... kernel(a, b, c)
# ERROR: ArgumentError: Invalid keyword argument 'hints...'

Several back-ends also carry private split_kwargs_runtime copies for their reflection functions.

This adds two functions, public but not exported:

  • parse_launch_macro(ex; name, options) parses a launch macro's arguments into the called function, its arguments bound to variables (for single evaluation and GC.@preserve), the macro's own literal options such as launch, and the other keyword arguments in source order, splats included.
  • partition_kwargs(kwargs, groups...) partitions keyword arguments by name into NamedTuples.

A macro builds one NamedTuple from its keyword arguments, so they are evaluated in order and repeated names resolve as in a function call, and routes it with partition_kwargs. For constant groups the partition happens at compile time: it is type stable and does not allocate, so launches don't get slower. With @cuda ported to these helpers, a launch on an RTX 5080 took 1.44 µs with written-out keywords, as before, and 1.45–1.47 µs with splatted ones. partition_kwargs can also replace the back-ends' run-time copies.

Routing at run time means that a literal keyword a back-end rejects is reported when the launch executes instead of at macro expansion, like an unknown keyword argument to a function.

The helpers in GPUCompiler stay as they are, deprecated, until the back-ends have moved over; @cuda is the first, in a CUDA.jl PR that needs this one released. Tested on Julia 1.10–1.13.

Back-ends' launch macros (`@cuda`, `@metal`, `@roc`, ...) each parse
their arguments the same way, with GPUCompiler's `split_kwargs` and
`assign_args!` and code around them, and route keyword arguments to
compilation or launch by name at macro expansion time. That rules out
splatted keyword arguments (`@cuda config... kernel(a)`), whose names
are only known at run time, and some back-ends carry private run-time
variants for their reflection functions.

Add two functions for this. `parse_launch_macro` parses a launch
macro's arguments into the call's function, its arguments bound to
variables, the macro's own options, and the other keyword arguments,
splats included, in source order. `partition_kwargs` partitions
keyword arguments by name at run time, returning `NamedTuple`s. A
macro builds one `NamedTuple` from the keyword arguments, which
evaluates them in order and resolves repeated names like the keyword
arguments of a function call, and routes it with `partition_kwargs`.
For constant groups of names, that partition happens at compile time:
it is type stable and doesn't allocate, so launches don't get slower
for it.
@maleadt
maleadt merged commit bc1eeb9 into main Dec 2, 2026
8 of 10 checks passed
@maleadt
maleadt deleted the tb/kwargs-splat branch December 2, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant