diff --git a/Project.toml b/Project.toml index e549f4094..a7be0fc12 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82" ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" +GPUToolbox = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc" KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c" LLD_jll = "d55e3150-da41-5e91-b323-ecfd1eec6109" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" @@ -51,6 +52,7 @@ EnzymeCore = "0.8" ExprTools = "0.1" GPUArrays = "11.2.1" GPUCompiler = "0.27, 1.0" +GPUToolbox = "0.1.0" KernelAbstractions = "0.9.2" LLD_jll = "15, 16, 17" LLVM = "9" diff --git a/src/hip/HIP.jl b/src/hip/HIP.jl index 889f2d5bb..efb0e36d5 100644 --- a/src/hip/HIP.jl +++ b/src/hip/HIP.jl @@ -11,7 +11,8 @@ import ..AMDGPU import ..AMDGPU.libhip import .AMDGPU: @check, check -include("call.jl") +import GPUToolbox: @gcsafe_ccall + include("libhip_common.jl") include("error.jl") include("libhip.jl") diff --git a/src/hip/call.jl b/src/hip/call.jl deleted file mode 100644 index a1c1dc181..000000000 --- a/src/hip/call.jl +++ /dev/null @@ -1,64 +0,0 @@ -## version of ccall that calls jl_gc_safe_enter|leave around the inner ccall - -# TODO: replace with JuliaLang/julia#49933 once merged - -# note that this is generally only safe with functions that do not call back into Julia. -# when callbacks occur, the code should ensure the GC is not running by wrapping the code -# in the `@gcunsafe` macro - -function ccall_macro_lower(func, rettype, types, args, nreq) - # instead of re-using ccall or Expr(:foreigncall) to perform argument conversion, - # we need to do so ourselves in order to insert a jl_gc_safe_enter|leave - # just around the inner ccall - - cconvert_exprs = [] - cconvert_args = [] - for (typ, arg) in zip(types, args) - var = gensym("$(func)_cconvert") - push!(cconvert_args, var) - push!(cconvert_exprs, quote - $var = Base.cconvert($(esc(typ)), $(esc(arg))) - end) - end - - unsafe_convert_exprs = [] - unsafe_convert_args = [] - for (typ, arg) in zip(types, cconvert_args) - var = gensym("$(func)_unsafe_convert") - push!(unsafe_convert_args, var) - push!(unsafe_convert_exprs, quote - $var = Base.unsafe_convert($(esc(typ)), $arg) - end) - end - - call = quote - $(unsafe_convert_exprs...) - - gc_state = @ccall(jl_gc_safe_enter()::Int8) - ret = ccall($(esc(func)), $(esc(rettype)), $(Expr(:tuple, map(esc, types)...)), - $(unsafe_convert_args...)) - @ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid) - ret - end - - quote - $(cconvert_exprs...) - - GC.@preserve $(cconvert_args...) $(call) - end -end - -""" - @gcsafe_ccall ... - -Call a foreign function just like `@ccall`, but marking it safe for the GC to run. This is -useful for functions that may block, so that the GC isn't blocked from running, but may also -be required to prevent deadlocks (see JuliaGPU/CUDA.jl#2261). - -Note that this is generally only safe with non-Julia C functions that do not call back into -Julia. When using callbacks, the code should make sure to transition back into GC-unsafe -mode using the `@gcunsafe` macro. -""" -macro gcsafe_ccall(expr) - ccall_macro_lower(Base.ccall_macro_parse(expr)...) -end