Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark ccalls as gc safe #605

Merged
merged 4 commits into from
Feb 28, 2024
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
24 changes: 24 additions & 0 deletions gen/hip/generator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# NOTE: for this to work, in /opt/rocm/include/hip/hip_runtime_api.h
# add the following:
# #define __HIP_PLATFORM_AMD__
# right before:
# #if defined(__HIP_PLATFORM_AMD__) && !defined(__HIP_PLATFORM_NVIDIA__)

using Clang.Generators

include_dir = normpath("/opt/rocm/include")
hip_dir = joinpath(include_dir, "hip")
options = load_options("hip/hip-generator.toml")

args = get_default_args()
push!(args, "-I$include_dir")
push!(args, "-I$hip_dir")

headers = [
joinpath(hip_dir, header)
for header in readdir(hip_dir)
if header == "hip_runtime_api.h"
]

ctx = create_context(headers, args, options)
build!(ctx)
7 changes: 7 additions & 0 deletions gen/hip/hip-generator.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
library_name = "libhip"
output_file_path = "./libhip.jl"

[codegen]
use_ccall_macro = true
always_NUL_terminated_string = true
6 changes: 1 addition & 5 deletions src/hip/HIP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import PrettyTables
import ..AMDGPU
import ..AMDGPU.libhip

include("call.jl")
include("libhip_common.jl")
include("error.jl")
include("libhip.jl")
Expand Down Expand Up @@ -35,12 +36,7 @@ function HIPContext(device::HIPDevice)
context_ref = Ref{hipContext_t}()
hipCtxCreate(context_ref, Cuint(0), device.device) |> check
context = HIPContext(context_ref[], true)

device!(device)
finalizer(context) do c
c.valid = false
hipCtxDestroy(c.context) |> check
end
return context
end
end
Expand Down
64 changes: 64 additions & 0 deletions src/hip/call.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
## 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
Loading