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

Tom Westerhout kot.tom97 at gmail.com
Sat Jul 11 21:26:28 UTC 2020


Hello,


Suppose I've written some function f :: Foo -> Bar using LLVM IR. Now
I'd like to compile it on first invocation with llvm-hs and cache the
obtained function pointer. Basically something like this

f :: Foo -> Bar
f x = fImpl x
  where
    fImpl = unsafePerformIO $
      flag <- isAlreadyCompiled -- check the cache
      if flag
        fetchFunc -- get the compiled code from cache
      else
        compileWithLLVM -- build LLVM IR, compile it, and update the cache

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... Big code bases like Accelerate or JuliaLang do achieve
this somehow, but are quite difficult to understand for the
uninitiated.

Any advice is highly appreciated!


Cheers,
Tom


More information about the Haskell-Cafe mailing list