aboutsummaryrefslogblamecommitdiffhomepage
path: root/content/2022-07-07-llvm-orc-jit/index.md
blob: 0fc2f2782bff192868a0a115516daa673c31c58d (plain) (tree)
1
2
3
4
5
6
7
8
9






                                            
         
                                           
 
                                                                         
                                                                               























                                                                             


                                                                    
   


                                                                         


                                                                              
                                                                                   
                                                                                                 
                                                                                                        



                                                                                                   
+++
title = "Jit C in memory using LLVM ORC api"

[taxonomies]
tags = ["llvm", "clang", "c++"]
+++

**EDIT**:
- 2023-12-19: Migrate example to `LLVM-17`.

Based on the in-memory compiler shared in the last post ([C to LLVM IR in
memory using libclang](@/2022-06-18-libclang-c-to-llvm-ir/index.md)), this post
demonstrates a small *just in time (JIT)* compiler which allows to compile C
code to host native code in-memory.

The JIT compiler is based on the LLVM [ORCv2 API][llvm-orc2] (the newest LLVM
JIT API at the time of writing) and the crucial parts are taken from the [JIT
tutorial][llvm-jit-tut].

The sources are available under [llvm-orc-jit][post-src].

### main.cc
```cpp
{{ include(path="content/2022-07-07-llvm-orc-jit/main.cc") }}
```

### jit.h
```cpp
{{ include(path="content/2022-07-07-llvm-orc-jit/jit.h") }}
```

### compiler.h
```cpp
{{ include(path="content/2022-07-07-llvm-orc-jit/ccompiler.h") }}
```

### CMakeLists.txt
```cmake
{{ include(path="content/2022-07-07-llvm-orc-jit/CMakeLists.txt") }}
```
The following [Makefile][post-makefile] provides a convenience wrapper to
configure, build, and run the example with a single `make` invocation.

Additionally, the [build-llvm.sh][post-build-llvm] script is provided to build
specific LLVM versions.

[post-src]: https://git.memzero.de/blog/tree/content/2022-07-07-llvm-orc-jit?h=main
[post-makefile]: https://git.memzero.de/blog/tree/content/2022-07-07-llvm-orc-jit/Makefile?h=main
[post-build-llvm]: https://git.memzero.de/blog/tree/content/2022-07-07-llvm-orc-jit/build-llvm.sh?h=main
[src-clang]: https://github.com/llvm/llvm-project/tree/main/clang
[blog-clang-in-memory]: https://blog.audio-tk.com/2018/09/18/compiling-c-code-in-memory-with-clang/
[llvm-jit-tut]: https://www.llvm.org/docs/tutorial/BuildingAJIT1.html
[llvm-orc2]: https://www.llvm.org/docs/ORCv2.html