Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GPUCompiler"
uuid = "61eb1bfa-7361-4325-ad38-22787b887f55"
version = "2.25.0"
version = "2.26.0"
authors = ["Tim Besard <tim.besard@gmail.com>"]

[workspace]
Expand Down
73 changes: 73 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
70 changes: 0 additions & 70 deletions src/execution.jl
Original file line number Diff line number Diff line change
@@ -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

"""
Expand Down
4 changes: 0 additions & 4 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,4 +26,5 @@ demumble_jll = "1e29f10c-031c-5a83-9565-69cddfc27673"

[compat]
Aqua = "0.8"
GPUToolbox = "3.8"
ParallelTestRunner = "2"
3 changes: 2 additions & 1 deletion test/helpers/bpf.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module BPF

using ..GPUCompiler
import GPUToolbox
import LLVM
import ..TestRuntime

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()
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/gcn.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module GCN

using ..GPUCompiler
import GPUToolbox
import LLVM
import ..TestRuntime

Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions test/helpers/metal.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Metal

using ..GPUCompiler
import GPUToolbox
import LLVM
import ..TestRuntime

Expand Down Expand Up @@ -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...)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/native.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Native

using ..GPUCompiler
import GPUToolbox
using LLVM, LLVM.IR, LLVM.Build, LLVM.ORC
import ..TestRuntime

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/ptx.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module PTX

using ..GPUCompiler
import GPUToolbox
import LLVM
import NVVM
import ..TestRuntime
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/spirv.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module SPIRV

using ..GPUCompiler
import GPUToolbox
import LLVM
import ..TestRuntime

Expand All @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading