[Haskell-cafe] Caching functions compiled with llvm-hs

Tom Westerhout kot.tom97 at gmail.com
Sun Jul 12 10:33:58 UTC 2020

On 11/07/2020, Henning Thielemann <lemming at henning-thielemann.de> wrote:
> On Sat, 11 Jul 2020, Tom Westerhout wrote:
>> A very primitive JIT compiler. What is the best way to do this with
>> llvm-hs? All standard examples using LLVM.OrcJIT or
>> LLVM.ExecutionEngine show how to compile a function and then
>> immediately execute it. I can't quite figure out a safe way to keep
>> the FunPtr...
> As far as I know, the compiled function is valid as long as the
> LLVM.ExecutionEngine exists. Thus I would define the following:
>    data JITFunPtr f = JITFunPtr (ForeignPtr ExecutionEngine) (FunPtr f)
> with DisposeExecutionEngine as finalizer for the ForeignPtr. After every
> call of the FunPtr function you have to 'touchForeignPtr executionEngine'.
> Alternatively, you could work with 'f' instead of 'FunPtr f' and add a
> (touchForeignPtr executionEngine) to the imported 'f'. This is what I do
> in llvm-tf:
> http://hackage.haskell.org/package/llvm-tf-9.2/docs/LLVM-ExecutionEngine.html#v:getExecutionFunction

Your getExecutionFunction implementation is indeed quite helpful, thank you!


More information about the Haskell-Cafe mailing list