Skip to content

Support interpolated function pointers in @gcsafe_ccall on Julia < 1.12 - #24

Merged
maleadt merged 1 commit into
mainfrom
tb/gcsafe-fptr
Sep 30, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/gcsafe-fptr

Conversation

@maleadt

@maleadt maleadt commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

@gcsafe_ccall is 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 to ccall instead of evaluating it. As a result:

julia> fptr = cglobal(:jl_rand);

julia> @gcsafe_ccall $fptr()::UInt64
ERROR: syntax: "$" expression outside quote

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=true on 1.12+ and a plain @ccall before that.

This PR makes the fallback handle interpolated pointers the same way Base's @ccall does. It evaluates the expression once, checks that it's a Ptr{Cvoid} (throwing the same ArgumentError as Base's @ccall does 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.

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
maleadt merged commit 9bb32ef into main Sep 30, 2026
10 checks passed
@maleadt
maleadt deleted the tb/gcsafe-fptr branch September 30, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant