[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question regarding management of JIT code
Thanks Dave, that seems perfect for my needs.
On 21 April 2015 at 15:18, David Malcolm <dmalcolm@redhat.com> wrote:
>> Lua is a garbage collected language. The way I have implemented the
>> JIT compiler is that each Lua function gets compiled on its own, and
>> in LLVM parlance is put in its own Module. The Module in LLVM is
>> equivalent to a compilation unit in C. While one can put multiple
>> functions in a Module I do not do so because by creating separate
>> Modules for each function I can simply allow Lua's garbage collector
>> to to get rid of unused functions, releasing the Module and resources.
>>
>> Is there a similar capability in libgccjit?
>
> You can use multiple gcc_jit_context instances to do this.
>
> Presumably you have some one-time initialization to do, e.g. creating
> common types. You can create these within a top-level gcc_jit_context:
>
> gcc_jit_context *shared_ctxt;
>
> shared_ctxt = gcc_jit_context_acquire();
> /* create types etc within shared_ctxt. */
>
>
> Then, for each function you want to JIT-compile, create it as a child
> context of the top-level context, using
> gcc_jit_context_new_child_context:
> https://gcc.gnu.org/onlinedocs/jit/topics/contexts.html#gcc_jit_context_new_child_context
>
> Something like this:
>
> gcc_jit_context *fn_ctxt =
> gcc_jit_context_new_child_context (shared_ctxt);
>
> /* populate fn_ctxt, creating the function within it. */
>
> /* Compile it. */
> gcc_jit_result *fn_result =
> gcc_jit_context_compile ();
>
> /* Error handling. */
> if (!fn_result) {
> do something, querying the error on fn_ctxt.
> return;
> }
>
> /* You can release the context before releasing
> the result. */
> gcc_jit_context_release (fn_ctxt);
>
> /* fn_result encapsulates the machine code; it needs to
> stick around until you're done calling the
> machine code. */
>
> The gcc_jit_result will thus contain just the machine code for the
> individual function, and these can be released individually using
> gcc_jit_result_release, assuming you can get the GC to run a finalizer
> on the function.
>
> Visually you'd have something like this:
>
> * shared_ctxt
> * fn_ctxt for "foo"
> * fn_result for "foo"
> * fn_ctxt for "bar"
> * fn_result for "bar"
>
> if that makes sense.
>
> Hope this is helpful
> Dave
>