6

I have a software that is able to generate C code that I would like to use in a just-in-time compilation context. From what I understand, LLVM/Clang is the way to go and, for maintainability of the project, I'd like to use the C API of llvm and Clang (libclang).

I started out creating a libclang context using clang_createIndex and a translation unit using createTranslationUnitFromSourceFile (would have been nice to be able to avoid going via the file system and pass the source code as a string instead). But I pretty much get stuck there. How can I go from the libclang translation unit to an LLVM "execution engine" which is what appears to be needed for JIT? Or is this not even possible using the C API?

jtv
  • 128
  • 5
Joel
  • 1,295
  • 15
  • 30

1 Answers1

-1

The best method to learn how to use a body of code is to investigate the examples you're given.

There exist tutorials on how to leverage the clang/llvm tools to compile C++ code and emit LLVM-IR, to compile LLVM-IR to LLVM-Bitcode, and to execute that LLVM-bitcode. All that is necessary to learn to incorporate this functionality in our application is to investigate the execution path of these tools, to find the sequence of methods that accomplish what we want.

Here is an example using the example tools of compiling a cpp file to llvm-bitcode, and executing it.

clang++ -c -O3 -emit-llvm main.cpp -o main.bc
lli main.bc

This is a great start, we can just look at the source behind the tools, and investigate the execution path outlined by the arguments. Since these tools are merely interfaces exposing the underlying functionality available in the llvm/clang libraries that we can add to our project, following the execution path shallowly will give us a sequence of library available methods that we can call within our application to accomplish the same results.

Once the sequence of library methods is trivially established, you can delve into breaking individual library methods into their underlying functionality, and tease out the exact behavior we desire through a relatively small set of modifications here and there, rather than trying to reimplement something from the ground up.

  • 1
    That would be a NO then. As using an internal interface in a project as dynamic as clang/llvm is very, um, short-sighted. – user3710044 Sep 01 '17 at 06:55
  • It's not using an internal interface. The tools are examples of how to use the libraries, and there are multiple levels of abstraction available in the public interfaces of the llvm and clang project. However you're right that if the API is not set in stone you should be careful about using it in your code directly, you should built abstractions away from dynamic apis, such that if it does change you only need to rewrite your abstraction. – SteamyThePunk Feb 09 '18 at 01:40
  • 1
    Just thought I'd add a bit more to this ancient answer. There is actually a tutorial for doing JIT compilation on the LLVM site. https://llvm.org/docs/tutorial/BuildingAJIT1.html Look, you don't have to LIKE the answer, but you have to accept them when they're the right answer or in this case... the ONLY answer. – SteamyThePunk Jul 04 '20 at 18:30