diff --git a/src/precompile.jl b/src/precompile.jl index fd161845f..6ee15ebde 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -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}) @@ -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. diff --git a/src/runtime/hip-execution.jl b/src/runtime/hip-execution.jl index a5bf0907d..26cb91906 100644 --- a/src/runtime/hip-execution.jl +++ b/src/runtime/hip-execution.jl @@ -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