$npx -y skills add mohitmishra786/low-level-dev-skills --skill jit-compilationJIT compilation skill for runtime code generation. Use when building LLVM ORC JIT, LLJIT, Cranelift JIT, inline caches, trampolines, or Rust dynasm codegen. Activates on queries about ORC JIT, LLJIT, Cranelift, ExecutionSession, inline cache, W^X, or dynasm.
| 1 | # JIT Compilation |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through just-in-time compilation: LLVM ORC JIT v2 (`ExecutionSession`, `IRLayer`, `ObjectLayer`), LLJIT for simpler use cases, Cranelift JIT, inline caches for dynamic dispatch, trampolines for lazy compilation, security considerations (W^X, code signing), and Rust `dynasm` for x86 codegen. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Building an interpreter with a JIT hot-path |
| 10 | - Implementing lazy function compilation on first call |
| 11 | - Embedding dynamic code generation in a REPL or game engine |
| 12 | - Prototyping codegen without writing a full linker |
| 13 | - Creating inline caches for polymorphic call sites |
| 14 | - Generating x86 machine code from Rust with `dynasm` |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. JIT architecture overview |
| 19 | |
| 20 | ``` |
| 21 | Source/AST/Bytecode |
| 22 | → IR (LLVM IR or Cranelift CLIF) |
| 23 | → Object file (in memory) |
| 24 | → Runtime linker (RTDyldObjectLinkingLayer) |
| 25 | → Executable code in R+X memory |
| 26 | → Function pointer call |
| 27 | ``` |
| 28 | |
| 29 | ### 2. LLJIT — simplest LLVM JIT |
| 30 | |
| 31 | ```cpp |
| 32 | #include "llvm/ExecutionEngine/Orc/LLJIT.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/Module.h" |
| 35 | #include "llvm/IR/IRBuilder.h" |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace llvm::orc; |
| 39 | |
| 40 | int main() { |
| 41 | auto JIT = cantFail(LLJITBuilder().create()); |
| 42 | |
| 43 | LLVMContext Context; |
| 44 | auto M = std::make_unique<Module>("jit", Context); |
| 45 | IRBuilder<> Builder(Context); |
| 46 | |
| 47 | // int add(int a, int b) { return a + b; } |
| 48 | Function *AddFn = Function::Create( |
| 49 | FunctionType::get(Builder.getInt32Ty(), |
| 50 | {Builder.getInt32Ty(), Builder.getInt32Ty()}, false), |
| 51 | Function::ExternalLinkage, "add", M.get()); |
| 52 | |
| 53 | BasicBlock *BB = BasicBlock::Create(Context, "entry", AddFn); |
| 54 | Builder.SetInsertPoint(BB); |
| 55 | auto Args = AddFn->arg_begin(); |
| 56 | Value *Sum = Builder.CreateAdd(Args, Args + 1); |
| 57 | Builder.CreateRet(Sum); |
| 58 | |
| 59 | cantFail(JIT->addIRModule(ThreadSafeModule(std::move(M), std::make_unique<LLVMContext>()))); |
| 60 | |
| 61 | auto AddSym = JIT->lookup("add"); |
| 62 | auto *AddPtr = (int (*)(int, int))AddSym->getValue(); |
| 63 | int result = AddPtr(3, 4); // 7 |
| 64 | return 0; |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | ```bash |
| 69 | clang++ -std=c++17 jit.cpp $(llvm-config --cxxflags --ldflags --libs core orcjit native) -o jit |
| 70 | ``` |
| 71 | |
| 72 | ### 3. ORC JIT v2 layers |
| 73 | |
| 74 | ```cpp |
| 75 | ExecutionSession ES; |
| 76 | auto &MainJD = ES.createBareJITDylib("main"); |
| 77 | |
| 78 | RTDyldObjectLinkingLayer ObjectLayer( |
| 79 | ES, []() { return std::make_unique<SectionMemoryManager>(); }); |
| 80 | |
| 81 | IRCompileLayer CompileLayer( |
| 82 | ES, ObjectLayer, std::make_unique<TargetMachineBuilder>()); |
| 83 | |
| 84 | // Add IR module to JITDylib |
| 85 | ThreadSafeModule TSM = ...; |
| 86 | CompileLayer.add(MainJD, std::move(TSM)); |
| 87 | |
| 88 | // Resolve symbol |
| 89 | auto Sym = ES.lookup({&MainJD}, "my_func"); |
| 90 | ``` |
| 91 | |
| 92 | Layers: |
| 93 | - **IRLayer** — compiles LLVM IR to object |
| 94 | - **ObjectLayer** — links relocatable objects |
| 95 | - **ExecutionSession** — symbol lookup and JITDylib management |
| 96 | |
| 97 | ### 4. Lazy compilation with trampolines |
| 98 | |
| 99 | ``` |
| 100 | First call → trampoline → compile function → patch trampoline → direct call |
| 101 | ``` |
| 102 | |
| 103 | ```cpp |
| 104 | // Simplified lazy compile on first invocation |
| 105 | void *LazyCompile(const std::string &Name) { |
| 106 | if (!Compiled.count(Name)) { |
| 107 | auto Fn = CompileFromAST(Name); |
| 108 | Compiled[Name] = Fn; |
| 109 | // Patch call site or update function pointer table |
| 110 | } |
| 111 | return Compiled[Name]; |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | ORC supports lazy reexports and lazy compilation via `LazyCallThroughManager`. |
| 116 | |
| 117 | ### 5. Inline caches |
| 118 | |
| 119 | ```javascript |
| 120 | // Concept: monomorphic call site caches resolved target |
| 121 | // Pseudocode for dynamic language |
| 122 | function call_site(obj, method, args) { |
| 123 | if (obj.class_id === cache.class_id) { |
| 124 | return cache.fn_ptr(args); // fast path |
| 125 | } |
| 126 | cache.class_id = obj.class_id; |
| 127 | cache.fn_ptr = resolve_method(obj, method); |
| 128 | return cache.fn_ptr(args); |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | JIT generates specialized code per cached type; deoptimize on cache miss. |
| 133 | |
| 134 | ### 6. Cranelift JIT |
| 135 | |
| 136 | ```rust |
| 137 | use cranelift::prelude::*; |
| 138 | use cranelift_jit::{JITBuilder, JITModule}; |
| 139 | use cranelift_module::{Linkage, Module}; |
| 140 | |
| 141 | let isa = cranelift_native::builder().finish(settings::Flags::new(settings::builder()))?; |
| 142 | let jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names()); |
| 143 | let mut module = JITModule::new(jit_builder); |
| 144 | |
| 145 | let mut ctx = module.make_context(); |
| 146 | ctx.func = Function::with_name_signature( |
| 147 | module.declare_function("add", Linkage::Export, &sig)?, |
| 148 | sig, |
| 149 | ); |
| 150 | // ... build IR in ctx.func ... |
| 151 | module.define_function(func_id, &mut ctx)?; |
| 152 | module.finalize_definitions()?; |
| 153 | let code = module.get_finalized_function(func_id); |
| 154 | let add_fn: fn(i32, i32) -> i32 = unsafe { std::mem::transmute(code) }; |
| 155 | ``` |
| 156 | |
| 157 | Cranelift: faster compile times than LLVM, good for embeddable JITs. |
| 158 | |
| 159 | ### 7. Security — W^X policy |
| 160 | |
| 161 | ``` |
| 162 | W^X (Write XOR Execute) |
| 163 | ├── Memory page is writable OR exe |