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
6 changes: 6 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ end
# `cufunction`, `link_kernel`, `compile_or_lookup`, and `cached_results`).
let HIPCompilerJob = Compiler.HIPCompilerJob
precompile(Tuple{typeof(Compiler.hipfunction), typeof(identity), Type{Tuple{Nothing}}})
# as called by `KernelInterface.kernel_function`
precompile(Tuple{typeof(Compiler.hipfunction_link), Core.MethodInstance, HIPDevice,
NamedTuple{(:name,), Tuple{Nothing}}})
precompile(Tuple{typeof(Compiler.hiplink), HIPCompilerJob,
Vector{UInt8}, String, Vector{Symbol}, GPUCompiler.Relocations})

Expand All @@ -77,6 +80,9 @@ let HIPCompilerJob = Compiler.HIPCompilerJob
Type{Compiler.HIPResults}, HIPCompilerJob})
end

# Freeing an array, which otherwise compiles in the first garbage collection that frees one.
precompile(Tuple{typeof(pool_free), Managed{Mem.HIPBuffer}})

# Hot entry points of the bundled ROCm libraries, mirroring CUDA.jl's per-library
# precompile directives. These compile the (GPU-free) Julia wrappers so the first
# `A * B`, factorization, FFT plan, etc. doesn't pay full first-use compilation.
Expand Down
32 changes: 17 additions & 15 deletions src/runtime/hip-execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ struct HIPKernel{F, TT} <: AbstractKernel{F, TT}
fun::HIP.HIPFunction
end

# the tuple type and the argument expressions of the call that `call` generates, built on
# vectors so that this compiles once for all argument counts
function call_arguments(@nospecialize(F), @nospecialize(TT), nargs::Int)
params = Any[F, TT.parameters...] # Base.signature_type with a function type
exprs = Any[:(kernel.f); Any[:(args[$i]) for i in 1:nargs]]
call_t = Type[AMDGPU.KernelState]
call_args = Any[:(AMDGPU.KernelState(stream.device, kernel.fun.global_hostcalls))]
for (dt, ex) in zip(params, exprs)
# filter out ghost arguments that shouldn't be passed
(GPUCompiler.isghosttype(dt) || Core.Compiler.isconstType(dt)) && continue
push!(call_t, dt)
push!(call_args, ex)
end
return Base.to_tuple_type(call_t), call_args
end

@inline @generated function call(
kernel::HIPKernel{F, TT}, args::Tuple;
stream::HIP.HIPStream, call_kwargs...,
) where {F, TT}
sig = Tuple{F, TT.parameters...} # Base.signature_type with a function type
args = (:(kernel.f), (:( args[$i] ) for i in 1:length(args.parameters))...)

# filter out ghost arguments that shouldn't be passed.
predicate = dt -> GPUCompiler.isghosttype(dt) || Core.Compiler.isconstType(dt)
to_pass = map(!predicate, sig.parameters)
call_t = Type[x[1] for x in zip(sig.parameters, to_pass) if x[2]]
call_args = Union{Expr,Symbol}[x[1] for x in zip(args, to_pass) if x[2]]

# add the kernel state
pushfirst!(call_t, AMDGPU.KernelState)
pushfirst!(call_args, :(AMDGPU.KernelState(stream.device, kernel.fun.global_hostcalls)))

# finalize types
call_tt = Base.to_tuple_type(call_t)
call_tt, call_args = call_arguments(F, TT, fieldcount(args))
quote
roccall_tuple(kernel.fun, $call_tt, ($(call_args...),); stream, call_kwargs...)
end
Expand Down