Query about JIT

Dear llvm-dev list,

Apologies if this list is not the right venue for this query - suitable redirection would be appreciated in that case.

I have a JIT use case that I’d like to know the best way to implement using LLVM.

I am looking to migrate from the existing native compilation option (Tiny C Compiler - TCC) for pLisp, a Lisp dialect and IDE.

At present, the native compilation is done by converting the Lisp code to C, storing the C code in a char buffer, and passing it to TCC programmatically via the API provided. I get a function pointer in return, which I store and invoke as needed.

Delving into the LLVM documentation, I found that one possible way to achieve the same functionality in LLVM is to use clag/libclang to convert the C source to LLVM IR, load this IR into the the JIT context and (skipping some steps I’m yet to figure out) get the desired function pointer.

Is this approach the right one? One issue I foresee is that libclang’s clang_parseTranslateUnit() function expects the C code to be from a file (although the file can be in-memory), whereas in my case the C code needs to be picked up from a char buffer - necessitating fmemopen(), etc.

Thanks for your assistance.

Hi Rajesh,

If I understand correctly, libclang is a C interface to Clang features, not LLVM. That means that you cannot get LLVM IR via libclang: The C Interface to Clang provides a relatively small API that exposes facilities for parsing source code into an abstract syntax tree (AST), loading already-parsed ASTs, traversing the AST, associating physical source locations with elements within the AST, and other facilities that support Clang-based development tools.

To get LLVM IR and perform JIT compilation you need to use C++ API of Clang and LLVM.

Here is some links which might be helpful:

BTW, there is a library for translating OpenCL C source code into a LLVM IR which features in-memory translation:

Hi Alexey,

Thank you very much for your detailed reply, will look into the resources indicated. I’d like to avoid C++ if possible, let me see how it goes.

Regards,
Rajesh Jayaprakash

Hi Rajesh,

Adding the cfe-dev (Clang FrontEnd) mailing list as your question seems to be more targeted towards clang (your input is C code if I understood correctly).

At the LLVM level, there is a (limited but “stable”) C API here: https://github.com/llvm/llvm-project/tree/master/llvm/include/llvm-c
For the JIT, this file seems relevant: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm-c/OrcBindings.h
But this assumes you get LLVM IR in the first place, and I don’t know of a C API for clang for this.

Best,

Thanks Mehdi!

Regards,
Rajesh

Dear cfe-dev mailing list,

I had posted earlier in this list with some queries about JIT. Thanks Alexey and Mehdi for your help.

I have liberally borrowed from the provided clang-interpreter example and have more or less managed to meet my objectives. Two concerns remain, however:

  1. I bundle the Clang/LLVM libraries (along with the modified code from clang-interpreter) into a shared library and am able to invoke the JIT functionality both statically and dynamically. However, the size of the shared library is ~54 MB, which I feel is too large. Am I doing something wrong, and is there a way to strip out unneeded bits and reduce the size? Or, is this the price I have to pay for removing any runtime dependency on Clang/LLVM?

  2. This is more of a general JIT query: my current plan is to dump the generated C code to a file and invoke clang on this file through the API. I haven’t tested out the performance implications of this yet, but wanted to know if this is kosher. This method has the advantage that my interaction with the clang API is quite coarse-grained as opposed to using (other) finer-grained API calls.

Any help would be greatly appreciated.

Thanks,
Rajesh Jayaprakash
(https://github.com/shikantaza/plisp)

Hi Rajesh,

I bundle the Clang/LLVM libraries (along with the modified code from clang-interpreter) into a shared library and am able to invoke the JIT functionality both statically and dynamically. However, the size of the shared library is ~54 MB, which I feel is too large. Am I doing something wrong, and is there a way to strip out unneeded bits and reduce the size? Or, is this the price I have to pay for removing any runtime dependency on Clang/LLVM?

I don’t know which specific numbers you need to expect, but the size of LLVM+Clang might be large. For example, take a look at the slides from the EuroLLVM 2019 0 from Russell Gallop about size of clang compiler on windows

  1. This is more of a general JIT query: my current plan is to dump the generated C code to a file and invoke clang on this file through the API. I haven’t tested out the performance implications of this yet, but wanted to know if this is kosher. This method has the advantage that my interaction with the clang API is quite coarse-grained as opposed to using (other) finer-grained API calls.

I cannot say much here because I’ve never tried to do so, but as I can see, ROCm-OpenCL-Driver 1 uses such method, you could try to reach developers of this library and ask them.

Hi Alexey,

Thanks very much for your pointers, will check them out.

Regards,
Rajesh