Skip to content

Commit

Permalink
Add explicit safepoints on 1.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Feb 12, 2024
1 parent b159ef2 commit 9327071
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/cudadrv/occupancy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ end
# HACK: callback function for `launch_configuration` on platforms without support for
# trampolines as used by `@cfunction` (JuliaLang/julia#27174, JuliaLang/julia#32154)
_shmem_cb = nothing
_shmem_cint_cb(x::Cint) = Cint(something(_shmem_cb)(x))
function _shmem_cint_cb(x::Cint)
# see @gcsafe_ccall documentation
@static if VERSION < v"1.9"
GC.safepoint()
end
Cint(something(_shmem_cb)(x))
end
_shmem_cb_lock = Threads.ReentrantLock()

"""
Expand All @@ -58,7 +64,13 @@ function launch_configuration(fun::CuFunction; shmem::Union{Integer,Base.Callabl
if isa(shmem, Integer)
cuOccupancyMaxPotentialBlockSize(blocks_ref, threads_ref, fun, C_NULL, shmem, max_threads)
elseif Sys.ARCH == :x86 || Sys.ARCH == :x86_64
shmem_cint = threads -> Cint(shmem(threads))
function shmem_cint(threads)
# see @gcsafe_ccall documentation
@static if VERSION < v"1.9"
GC.safepoint()
end
Cint(shmem(threads))
end
cb = @cfunction($shmem_cint, Cint, (Cint,))
cuOccupancyMaxPotentialBlockSize(blocks_ref, threads_ref, fun, cb, 0, max_threads)
else
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/call.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ render_arg(io, arg::Union{<:Base.RefValue, AbstractArray}) = summary(io, arg)

# 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:
# - on 1.10 and later, everything is fine because of safepoint_on_entry
# - on 1.9, @cfunction-based callbacks are fine because they transition to gc_unsafe
# - on 1.8 and earlier, the code should explicitly call GC.safepoint()!

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
Expand Down

0 comments on commit 9327071

Please sign in to comment.