
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.htm...