-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
199 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.