cp-12 · ORC JIT — compile and execute in-process

cp-11 produced an optimised llvm::Module. cp-12 hands that module to LLVM's modern JIT (ORCv2, wrapped behind LLJIT) which compiles it to native code on the spot, looks up main, and calls it.

Build & run

cmake -S src/cpp -B build
cmake --build build
./build/tests/test_jit         # → 17/17 checks passed

echo 'print 2 + 3 * 4;' | ./build/mljit              # 14
echo 'print 2 + 3 * 4;' | ./build/mljit --emit-llvm  # textual IR instead
./build/mljit -O program.ml                          # run after O2

Headline test (recursive fib)

fn fib(n){ if (n < 2) { return n; } return fib(n-1) + fib(n-2); }
print fib(20);

JIT-compiled with -O6765.

Layout

src/cpp/
├── CMakeLists.txt        # links orcjit + executionengine + native + nativecodegen
├── src/jit.hpp / jit.cpp # initNative() + runMain(ctx, module)
├── src/main.cpp          # `mljit` CLI
└── tests/test_jit.cpp    # 17/17 checks

Reading order

  1. steps/01-why-jit.md
  2. steps/02-orc-overview.md
  3. steps/03-lljit-builder.md
  4. steps/04-thread-safe-module.md
  5. steps/05-symbol-lookup-and-call.md
  6. steps/06-runtime-symbols.md
  7. steps/07-beyond-lljit.md