From d2597fb06f7874c5d0062d0a9d5ad3ba01e8d138 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 09:51:35 +0100 Subject: [PATCH] Accept callable objects as kernels Remove `hipfunction`'s `Function` constraint so callable objects (structs with call methods) can be launched as kernels without a `MethodError`. JuliaGPU/KernelAbstractions.jl#931 uses such kernels. Nothing depends on the constraint, and CUDA.jl's `cufunction` has none. --- src/compiler/codegen.jl | 2 +- test/device/launch.jl | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/compiler/codegen.jl b/src/compiler/codegen.jl index 64e9463e7..97335e07d 100644 --- a/src/compiler/codegen.jl +++ b/src/compiler/codegen.jl @@ -292,7 +292,7 @@ The following kwargs are supported: target") and fall back to no limit, so pick the value per architecture. Negative values throw an `ArgumentError`. """ -function hipfunction(f::F, tt::TT = Tuple{}; kwargs...) where {F <: Core.Function, TT} +function hipfunction(f::F, tt::TT = Tuple{}; kwargs...) where {F, TT} kw = values(kwargs) dev = AMDGPU.device() fun = GPUCompiler.cached_lookup(HIP.HIPFunction, F, tt, (dev, kw)) do diff --git a/test/device/launch.jl b/test/device/launch.jl index f8628fb8a..612b43a64 100644 --- a/test/device/launch.jl +++ b/test/device/launch.jl @@ -79,7 +79,25 @@ end @test Array(map(x -> x[1] + x[2] + x[3], ROCArray(xs))) == map(x -> x[1] + x[2] + x[3], xs) end +# a callable object used as a kernel +struct StoreValue + val::Float64 +end +function (f::StoreValue)(a) + a[1] = f.val + return +end + @testset "Function/Argument Conversion" begin + @testset "Callable object as kernel" begin + a_dev = ROCArray([1.0]) + @roc StoreValue(2.0)(a_dev) + @test Array(a_dev) == [2.0] + # reuses the compiled kernel, with the new instance's state + @roc StoreValue(3.0)(a_dev) + @test Array(a_dev) == [3.0] + end + @testset "Closure as Argument" begin function kernel(closure) closure()