make ccallable
work with system images and precompilation
#35574
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.
fixes #35014
The approach I took is to add a field to
Method
storing the needed return type and signature if a C entry point is requested for that method.jl_compile_extern_c
is called to inform the JIT during loading, or after loading a .ji or system image. The signatures are also added to the data passed tojl_create_native
to tell it to include aliases in the system image.A possible simplification is to require the argument types to be specific enough to be specialized. Currently, we allow
@ccallable f(x::Any) = ...
, in which case we need to generate a wrapper accepting any value and doing dynamic dispatch. If we required specific types, e.g.f(x::Cint)
, then every ccallable would be associated with a single MethodInstance, and we'd just generate the alias whenever the instance is compiled.It's not clear whether this feature is needed in the JIT, or just system images (i.e. only when you want to link a C program with direct calls against sys.so). The JIT case takes a bunch of the code here.
A separate problem to fix in the near future is that I believe ccallable and possibly also cfunction are always using jl_apply_generic when compiled into a system image, since
gen_cfun_wrapper
doesn't use the new mechanism for looking up call targets (there is already a TODO comment about the recursive compilation done there).