Skip to content

Commit

Permalink
Add reflection macros and utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Sep 11, 2024
1 parent cd44339 commit 8945b2e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/OpenCL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ include("util.jl")
include("array.jl")
include("compiler/compilation.jl")
include("compiler/execution.jl")
include("compiler/reflection.jl")

end
74 changes: 74 additions & 0 deletions src/compiler/reflection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# code reflection entry-points

#
# code_* replacements
#

# function to split off certain kwargs for selective forwarding, at run time.
# `@opencl` does something similar at parse time, using `GPUCompiler.split_kwargs`.
function split_kwargs_runtime(kwargs, wanted::Vector{Symbol})
remaining = Dict{Symbol, Any}()
extracted = Dict{Symbol, Any}()
for (key, value) in kwargs
if key in wanted
extracted[key] = value
else
remaining[key] = value
end
end
return extracted, remaining
end

for method in (:code_typed, :code_warntype, :code_llvm, :code_native)
# only code_typed doesn't take a io argument
args = method == :code_typed ? (:job,) : (:io, :job)

@eval begin
function $method(io::IO, @nospecialize(func), @nospecialize(types);
kernel::Bool=false, kwargs...)
compiler_kwargs, kwargs = split_kwargs_runtime(kwargs, COMPILER_KWARGS)
source = methodinstance(typeof(func), Base.to_tuple_type(types))
config = compiler_config(device(); kernel, compiler_kwargs...)
job = CompilerJob(source, config)
GPUCompiler.$method($(args...); kwargs...)
end
$method(@nospecialize(func), @nospecialize(types); kwargs...) =
$method(stdout, func, types; kwargs...)
end
end



#
# @device_code_* functions
#

export @device_code_lowered, @device_code_typed, @device_code_warntype, @device_code_llvm,
@device_code_native, @device_code

# forward to GPUCompiler
@eval $(Symbol("@device_code_lowered")) = $(getfield(GPUCompiler, Symbol("@device_code_lowered")))
@eval $(Symbol("@device_code_typed")) = $(getfield(GPUCompiler, Symbol("@device_code_typed")))
@eval $(Symbol("@device_code_warntype")) = $(getfield(GPUCompiler, Symbol("@device_code_warntype")))
@eval $(Symbol("@device_code_llvm")) = $(getfield(GPUCompiler, Symbol("@device_code_llvm")))
@eval $(Symbol("@device_code_native")) = $(getfield(GPUCompiler, Symbol("@device_code_native")))
@eval $(Symbol("@device_code")) = $(getfield(GPUCompiler, Symbol("@device_code")))


#
# other
#

"""
Metal.return_type(f, tt) -> r::Type
Return a type `r` such that `f(args...)::r` where `args::tt`.
"""
function return_type(@nospecialize(func), @nospecialize(tt))
source = methodinstance(typeof(func), tt)
config = compiler_config(device())
job = CompilerJob(source, config)
interp = GPUCompiler.get_interpreter(job)
sig = Base.signature_type(func, tt)
Core.Compiler.return_type(interp, sig)
end

0 comments on commit 8945b2e

Please sign in to comment.