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

Henning Thielemann lemming at henning-thielemann.de
Sat Jul 11 22:08:35 UTC 2020


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


More information about the Haskell-Cafe mailing list