Support interpolated function pointers in @gcsafe_ccall on Julia < 1.12 - #24
Merged
Merged
Conversation
On Julia versions without native support for GC-safe calls, @gcsafe_ccall lowers the call itself, but it did not handle function pointers interpolated with `$`, as Base's @CCall does. Calling one failed with a syntax error ("$" expression outside quote).
maleadt
force-pushed
the
tb/gcsafe-fptr
branch
from
September 30, 2026 17:15
1c18d8d to
a1f827a
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@gcsafe_ccallis meant to be a drop-in replacement for@ccall, but it failed on function pointers interpolated with$, at least on Julia versions without native support for GC-safe calls (anything before 1.12). Those versions go through GPUToolbox's own lowering, which passed the$expression straight toccallinstead of evaluating it. As a result:On Julia 1.12+ the macro defers to Base's lowering, which handles this fine, so the same code works there and fails on 1.10 and 1.11.
This comes up with APIs that are only reachable through function pointers. OpenCL extension functions are an example: they're looked up with
clGetExtensionFunctionAddressForPlatform, and OpenCL.jl wants to call them GC-safely like its other API calls (JuliaGPU/OpenCL.jl#518). For now it has to fall back to Base's@ccall gc_safe=trueon 1.12+ and a plain@ccallbefore that.This PR makes the fallback handle interpolated pointers the same way Base's
@ccalldoes. It evaluates the expression once, checks that it's aPtr{Cvoid}(throwing the sameArgumentErroras Base's@ccalldoes on those Julia versions), and calls through it inside the GC-safe region.The tests now call an interpolated pointer, and on the fallback path also check the error for a non-pointer value. They pass on Julia 1.10 through 1.13 and on nightly.