From 4a75b889c8c1c65055e50b2bae98dfc97eff8dc9 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 14:36:24 +0100 Subject: [PATCH 1/2] Deprecate `split_kwargs` and `assign_args!` These helpers for back-ends' launch macros are superseded by GPUToolbox's `parse_launch_macro` and `partition_kwargs`, which support splatted keyword arguments and partition keyword arguments into `NamedTuple`s at run time, also for code that isn't a macro. They don't belong in GPUCompiler either, as they have nothing to do with compilation. Move them to deprecated.jl. They keep working and don't warn, as released back-ends use them. Remove them from the precompile workload, and use `partition_kwargs` in the test helpers. --- src/deprecated.jl | 73 ++++++++++++++++++++++++++++++++++++++++++ src/execution.jl | 70 ---------------------------------------- src/precompile.jl | 4 --- test/Project.toml | 2 ++ test/helpers/bpf.jl | 3 +- test/helpers/gcn.jl | 3 +- test/helpers/metal.jl | 7 ++-- test/helpers/native.jl | 3 +- test/helpers/ptx.jl | 3 +- test/helpers/spirv.jl | 3 +- test/utils.jl | 12 ++++--- 11 files changed, 96 insertions(+), 87 deletions(-) diff --git a/src/deprecated.jl b/src/deprecated.jl index f395eb39..03a8a77b 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -333,3 +333,76 @@ const deferred_codegen_jobs = Dict{Int, Any}() # the job registered under a marker's id, if any legacy_deferred_spec(id::Int) = get(deferred_codegen_jobs, id, nothing) + + +## launch macro helpers + +export split_kwargs, assign_args! + +# Deprecated in favor of GPUToolbox's `parse_launch_macro` and `partition_kwargs`, which +# support splatted keyword arguments. Released back-ends still use these, so they don't +# warn, and keep working until the next breaking release. + +# split keyword arguments expressions into groups. returns vectors of keyword argument +# values, one more than the number of groups (unmatched keywords in the last vector). +# intended for use in macros; the resulting groups can be used in expressions. +# can be used at run time, but not in performance critical code. +function split_kwargs(kwargs, kw_groups...) + # called by launch macros with differently shaped arguments, so don't compile it for each + @nospecialize + kwarg_groups = ntuple(_->[], length(kw_groups) + 1) + for kwarg in kwargs + # decode + if Meta.isexpr(kwarg, :(=)) + # use in macros + key, val = kwarg.args + elseif kwarg isa Pair{Symbol,<:Any} + # use in functions + key, val = kwarg + else + throw(ArgumentError("non-keyword argument like option '$kwarg'")) + end + isa(key, Symbol) || throw(ArgumentError("non-symbolic keyword '$key'")) + + # find a matching group + group = length(kwarg_groups) + for (i, kws) in enumerate(kw_groups) + if key in kws + group = i + break + end + end + push!(kwarg_groups[group], kwarg) + end + + return kwarg_groups +end + +# assign arguments to variables, handle splatting +function assign_args!(code, _args) + nargs = length(_args) + + # handle splatting + splats = Vector{Bool}(undef, nargs) + args = Vector{Any}(undef, nargs) + for i in 1:nargs + splats[i] = Meta.isexpr(_args[i], :(...)) + args[i] = splats[i] ? _args[i].args[1] : _args[i] + end + + # assign arguments to variables + vars = Vector{Symbol}(undef, nargs) + for i in 1:nargs + vars[i] = gensym() + push!(code.args, :($(vars[i]) = $(args[i]))) + end + + # convert the arguments, compile the function and call the kernel + # while keeping the original arguments alive + var_exprs = Vector{Any}(undef, nargs) + for i in 1:nargs + var_exprs[i] = splats[i] ? Expr(:(...), vars[i]) : vars[i] + end + + return vars, var_exprs +end diff --git a/src/execution.jl b/src/execution.jl index e37e9925..883d1ca9 100644 --- a/src/execution.jl +++ b/src/execution.jl @@ -1,75 +1,5 @@ # reusable functionality to implement code execution -export split_kwargs, assign_args! - - -## macro tools - -# split keyword arguments expressions into groups. returns vectors of keyword argument -# values, one more than the number of groups (unmatched keywords in the last vector). -# intended for use in macros; the resulting groups can be used in expressions. -# can be used at run time, but not in performance critical code. -function split_kwargs(kwargs, kw_groups...) - # called by launch macros with differently shaped arguments, so don't compile it for each - @nospecialize - kwarg_groups = ntuple(_->[], length(kw_groups) + 1) - for kwarg in kwargs - # decode - if Meta.isexpr(kwarg, :(=)) - # use in macros - key, val = kwarg.args - elseif kwarg isa Pair{Symbol,<:Any} - # use in functions - key, val = kwarg - else - throw(ArgumentError("non-keyword argument like option '$kwarg'")) - end - isa(key, Symbol) || throw(ArgumentError("non-symbolic keyword '$key'")) - - # find a matching group - group = length(kwarg_groups) - for (i, kws) in enumerate(kw_groups) - if key in kws - group = i - break - end - end - push!(kwarg_groups[group], kwarg) - end - - return kwarg_groups -end - -# assign arguments to variables, handle splatting -function assign_args!(code, _args) - nargs = length(_args) - - # handle splatting - splats = Vector{Bool}(undef, nargs) - args = Vector{Any}(undef, nargs) - for i in 1:nargs - splats[i] = Meta.isexpr(_args[i], :(...)) - args[i] = splats[i] ? _args[i].args[1] : _args[i] - end - - # assign arguments to variables - vars = Vector{Symbol}(undef, nargs) - for i in 1:nargs - vars[i] = gensym() - push!(code.args, :($(vars[i]) = $(args[i]))) - end - - # convert the arguments, compile the function and call the kernel - # while keeping the original arguments alive - var_exprs = Vector{Any}(undef, nargs) - for i in 1:nargs - var_exprs[i] = splats[i] ? Expr(:(...), vars[i]) : vars[i] - end - - return vars, var_exprs -end - - ## kernel lookup cache """ diff --git a/src/precompile.jl b/src/precompile.jl index f3267a20..fc4c7aa1 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -75,9 +75,5 @@ import REPL _, meta = GPUCompiler.compile(:asm, job) dispose(meta.ir) end - - # helpers for back-ends' launch macros, which are expanded in the user's session - split_kwargs((:(a = 1), :(b = 2)), [:a], [:c]) - assign_args!(quote end, Any[:x, :(y...)]) end end diff --git a/test/Project.toml b/test/Project.toml index 121b9a3b..f60efd82 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -5,6 +5,7 @@ CUDA_Compiler_jll = "d1e2174e-dfdc-576e-b43e-73b79eb1aca8" CompilerCaching = "9db33cc3-5358-4881-8759-fa4194144afd" FileCheck = "4e644321-382b-4b05-b0b6-5d23c3d944fb" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" +GPUToolbox = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc" Highlights = "eafb193a-b7ab-5a9e-9068-77385905fa72" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" @@ -25,4 +26,5 @@ demumble_jll = "1e29f10c-031c-5a83-9565-69cddfc27673" [compat] Aqua = "0.8" +GPUToolbox = "3.8" ParallelTestRunner = "2" diff --git a/test/helpers/bpf.jl b/test/helpers/bpf.jl index a6535f93..bad48664 100644 --- a/test/helpers/bpf.jl +++ b/test/helpers/bpf.jl @@ -1,6 +1,7 @@ module BPF using ..GPUCompiler +import GPUToolbox import LLVM import ..TestRuntime @@ -8,7 +9,7 @@ struct CompilerParams <: AbstractCompilerParams end GPUCompiler.runtime_module(::CompilerJob{<:Any,CompilerParams}) = TestRuntime function create_job(@nospecialize(func), @nospecialize(types); kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = BPFCompilerTarget() params = CompilerParams() diff --git a/test/helpers/gcn.jl b/test/helpers/gcn.jl index d7b885d6..fa161034 100644 --- a/test/helpers/gcn.jl +++ b/test/helpers/gcn.jl @@ -1,6 +1,7 @@ module GCN using ..GPUCompiler +import GPUToolbox import LLVM import ..TestRuntime @@ -21,7 +22,7 @@ function create_job(@nospecialize(func), @nospecialize(types); dev_isa="gfx900", backend::Symbol=:external, minthreads=nothing, maxthreads=nothing, patch::Bool=false, exception_sites::Bool=false, kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = GCNCompilerTarget(; dev_isa, backend, minthreads, maxthreads) params = CompilerParams(patch, exception_sites) diff --git a/test/helpers/metal.jl b/test/helpers/metal.jl index 05a642a5..1ea3ce1a 100644 --- a/test/helpers/metal.jl +++ b/test/helpers/metal.jl @@ -1,6 +1,7 @@ module Metal using ..GPUCompiler +import GPUToolbox import LLVM import ..TestRuntime @@ -61,7 +62,7 @@ GPUCompiler.relocation_lowering(job::TableCompilerJob) = GPUCompiler.kernel_state_type(::TableCompilerJob) = TableKernelState function create_table_job(@nospecialize(func), @nospecialize(types); kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = MetalCompilerTarget(; macos=v"12.2", metal=v"3.0", air=v"3.0") config = CompilerConfig(target, TableCompilerParams(); kernel=false, config_kwargs...) @@ -77,7 +78,7 @@ code_native_table(@nospecialize(func), @nospecialize(types); kwargs...) = function create_job(@nospecialize(func), @nospecialize(types); minthreads=nothing, maxthreads=nothing, exception_sites::Bool=false, kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = MetalCompilerTarget(; macos=v"12.2", metal=v"3.0", air=v"3.0", minthreads, maxthreads) params = CompilerParams(exception_sites) @@ -87,7 +88,7 @@ end function create_threaded_runtime_job(@nospecialize(func), @nospecialize(types); exception_sites::Bool=false, kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = MetalCompilerTarget(; macos=v"12.2", metal=v"3.0", air=v"3.0") params = ThreadedRuntimeCompilerParams(exception_sites) diff --git a/test/helpers/native.jl b/test/helpers/native.jl index 751d761f..7bf587da 100644 --- a/test/helpers/native.jl +++ b/test/helpers/native.jl @@ -1,6 +1,7 @@ module Native using ..GPUCompiler +import GPUToolbox using LLVM, LLVM.IR, LLVM.Build, LLVM.ORC import ..TestRuntime @@ -78,7 +79,7 @@ end function create_job(@nospecialize(func), @nospecialize(types); entry_safepoint::Bool=false, method_table=test_method_table, relocations::Symbol=:bake, jlruntime::Bool=true, kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = NativeCompilerTarget(;jlruntime) params = CompilerParams(entry_safepoint, method_table, relocations) diff --git a/test/helpers/ptx.jl b/test/helpers/ptx.jl index 1e24c1ad..d17151da 100644 --- a/test/helpers/ptx.jl +++ b/test/helpers/ptx.jl @@ -1,6 +1,7 @@ module PTX using ..GPUCompiler +import GPUToolbox import LLVM import NVVM import ..TestRuntime @@ -68,7 +69,7 @@ function create_job(@nospecialize(func), @nospecialize(types); fastmath=false, system_atomics=true, target_debuginfo::Bool=false, patch::Bool=false, exception_sites::Bool=false, kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = PTXCompilerTarget(; cap, ptx, feature_set, backend, minthreads, maxthreads, blocks_per_sm, maxregs, diff --git a/test/helpers/spirv.jl b/test/helpers/spirv.jl index 8ef70b60..79a7ddb3 100644 --- a/test/helpers/spirv.jl +++ b/test/helpers/spirv.jl @@ -1,6 +1,7 @@ module SPIRV using ..GPUCompiler +import GPUToolbox import LLVM import ..TestRuntime @@ -17,7 +18,7 @@ function create_job(@nospecialize(func), @nospecialize(types); backend::Symbol, driver::Symbol=:generic, extensions::String="", atomics::SPIRVAtomics=SPIRVAtomics(), exception_sites::Bool=false, kwargs...) - config_kwargs, kwargs = split_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) + config_kwargs, kwargs = GPUToolbox.partition_kwargs(kwargs, GPUCompiler.CONFIG_KWARGS) source = methodinstance(typeof(func), Base.to_tuple_type(types), Base.get_world_counter()) target = SPIRVCompilerTarget(; backend, validate=true, optimize=true, extensions, atomics, supports_fp16, supports_fp64, supports_bfloat16, driver) diff --git a/test/utils.jl b/test/utils.jl index 8af8ae55..be9e7f8c 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,13 +1,15 @@ # loading Highlights activates the HighlightsExt extension providing the renderer import Highlights -@testset "split_kwargs" begin +@testset "deprecated launch-macro helpers" begin kwargs = [:(a=1), :(b=2), :(c=3), :(d=4)] groups = GPUCompiler.split_kwargs(kwargs, [:a], [:b, :c]) - @test length(groups) == 3 - @test groups[1] == [:(a=1)] - @test groups[2] == [:(b=2), :(c=3)] - @test groups[3] == [:(d=4)] + @test groups == ([:(a=1)], [:(b=2), :(c=3)], [:(d=4)]) + + code = Expr(:block) + vars, var_exprs = GPUCompiler.assign_args!(code, Any[:x, :(y...)]) + @test code.args == [:($(vars[1]) = x), :($(vars[2]) = y)] + @test var_exprs == [vars[1], :($(vars[2])...)] end @testset "failure dumps" begin From a98c2190bb200160ed32070b6d26abecdce47e7f Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 14:36:24 +0100 Subject: [PATCH 2/2] Bump version. --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 48ba3a47..f9d22447 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "GPUCompiler" uuid = "61eb1bfa-7361-4325-ad38-22787b887f55" -version = "2.25.0" +version = "2.26.0" authors = ["Tim Besard "] [workspace]