How to use clang and llvm for JIT, preferably via the C API

Hi!

I’m trying to add JIT support to our software for mathematical optimization (http://casadi.org), but it’s proving difficult. Since we already support generation of C code, the natural approach is to generate C code (either as temporary files or as strings) and parse this code using clang. Although we’ve managed to implement a proof-of-concept, I’m struggling to implement something more maintainable.

In particular, I’d like to use the C API (libclang), but I don’t see how this can be combined with JIT. If I want to do clang+JIT, am I forced to use the (unstable) C++ API?

Here is a stackoverflow post I posted on the subject (without luck):

http://stackoverflow.com/questions/32254862/just-in-time-compilation-using-libclang-and-llvm-c

Best regards,
Joel

P.S. I’m also struggling to set up our (CMake-based) build system so that it works with multiple llvm versions and multiple platforms. The “llvm-config” doesn’t appear to have anything clang-related. But I guess that’s a separate issue.

+Lang for JIT stuff

Hi Joel,

As far as I know libclang doesn’t provide an API for code generation (though I would double-check this on clang-dev).

If you can get hold of IR (e.g. by exec’ing “clang -c -emit-llvm …”) you can use the stable MCJIT API to JIT that. The performance of such a scheme may not be great though.

Cheers,
Lang.

Hi Lang,

Thanks a lot, I should have thought of that… I can use libclang to get hold of the IR - that way I can avoid the command-line completely.

I don’t think performance will be an issue, our generated code is quite large so the extra overhead will probably be dwarfed by other steps in the compilation chain.

When you refer to the “stable MCJIT API”, you mean the C API, right? From what I can tell, it indeed appears to have the required functionality (http://llvm.org/docs/doxygen/html/c_2ExecutionEngine_8h.html).

Thanks!
Joel

It doesn’t, but the ability to drive LLVM IR and object code generation is pretty close to the top of the list of requested features. Having the ability to produce the LLVM C API’s wrapper of a Module (rather than on disk bitcode) would be relatively easy, but no one wanting such a thing has yet sent patches.

Short version: Patches welcome. I’d be very happy to review such patches.

David