JIT function compile to file
David Malcolm
dmalcolm@redhat.com
Thu Aug 3 15:47:18 GMT 2023
On Thu, 2023-08-03 at 15:07 +0000, Li, Yicheng via Jit wrote:
> Hi,
>
> I’m looking for examples that can let me build a jit function on one
> run, save it to a file, and grab from file again on the second run
> without rebuilding and recompilation.
> I’ve seen the function, gcc_jit_context_compile_to_file, and I’m
> wondering how to use it.
gcc_jit_context_compile_to_file is for ahead-of-time compilation. You
could compile the function into a shared library, and then load the
shared library when the process restarts.
Emacs does something like this for its Lisp files; see:
https://akrl.sdf.org/gccemacs.html
> Throughout the documentation, I see the gcc_jit_location type
> variable, I’m unsure how to use it, and if it is related to calling a
> saved-to-file jit function.
gcc_jit_location is for providing "source location" information for use
when stepping through the code in a debugger. It doesn't affect the
destination where code is compiled to.
> It would be great if there’s any example that can be provided.
I don't think there's an existing example in the documentation or
testsuite, but use gcc_jit_context_compile_to_file with
GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY to make a .so file, then on
rerunning the process use dlopen to load the .so file, and dlsym to
extract the function.
FWIW the testsuite
has:https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/testsuite/jit.dg/test-compile-to-dynamic-library.c
but that testcase merely does the compilation-to-dynamic-library, not
the loading of the built library, and it heavily uses the preprocessor
so probably isn't good as an example.
Hope this is helpful
Dave
More information about the Jit
mailing list