From 22e7c908237a01da5df74c4f4e5dc6d82ac749dc Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 09:03:05 +0100 Subject: [PATCH 1/3] Build kernel calls with vectors to compile once for all argument counts The `call` generator zipped tuples of argument expressions, recompiling for each new number of kernel arguments at about 35 ms each. Build the call's types and arguments with vectors, as JuliaGPU/KernelAbstractions.jl#931 did for PoCL. --- src/runtime/hip-execution.jl | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) 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 From efe33381fb91715f53ede53fb76ee4fa1f69b598 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 09:05:03 +0100 Subject: [PATCH 2/3] Precompile array freeing The first garbage collection that freed a `ROCArray` compiled `pool_free`, taking about 0.9 s. Precompile it as CUDA.jl does for its equivalent. --- src/precompile.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/precompile.jl b/src/precompile.jl index fd161845f..f76eddab3 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -77,6 +77,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. From 62673a6ffc455792dac85564dc8d1b367374b265 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 09:30:44 +0100 Subject: [PATCH 3/3] Precompile kernel lookup for KernelInterface's arguments `KernelInterface.kernel_function` passes a `name` keyword argument, so its first launch compiled `hipfunction_link` for that argument type. Add the specialization missing from the existing precompile directive. --- src/precompile.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/precompile.jl b/src/precompile.jl index f76eddab3..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})