LLVM - Dynamicly load Functions via IR?

Hello LLVM-World,

I always thought, that clang is just a normal C/C++ Compiler, but after some research I’m getting a little confused. Do I’m right with the following?

I have a simple C/C++ program and I compile it with clang.

1.) Clang generates me a LLVM IR file
2.) LLVM uses the IR file to generate the.obj file
3.) My linker creates an executebale file from that.

Is this correct? On Stackoverflow (http://stackoverflow.com/questions/3509215/llvm-jit-and-native) I read:

So how does one use LLVM as a JIT compiler?
You build an application which generates some LLVM IR (in memory), then use the LLVM library to generate native code (still in memory). LLVM hands you back a pointer which you can call afterwards. No clang involved. |

  • |

So… Is it possible to grab the IR-File clang created and load them into a normal C/C++ written program? Could this program now use the LLVM-Library to create functions from it and let me call them? So this would be like a dll/so? How could you perform this? What would happen if I’m using a function like printf? So the created code must have a “undefined reference”.

Kind regards
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789
Geschäftsführer: Hiroshi Kawamura, Dr Hiroshi Nakamura, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima.

Hello Björn,

clang is a C/C++ compiler which uses the LLVM backend to generate object code.
This means you can use clang to create directly the object code or simply emit the LLVM-IR and JIT it.

If you are looking for JIT support using LLVM, please take a look at compiler-RT [1].
You can find a section of the LLVM tutorial [2] dedicated to JIT implementation.

I would also ask you if a JIT is actually what you need for your use-case.
I did something similar to what you want to do (if I understood correctly)
wrapping calls to the compiler/using libclang without using the JIT.
I published my code as a LGPL library on github [3]. Feel free to check it out / contribute.


Stefano Cherubin

[1] https://compiler-rt.llvm.org/
[2] http://llvm.org/docs/tutorial/
[3] https://github.com/skeru/libVersioningCompiler