skills/compiler-internals/jit-compilation/SKILL.md
JIT 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.
npx skillsauth add mohitmishra786/low-level-dev-skills jit-compilationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
dynasmSource/AST/Bytecode
→ IR (LLVM IR or Cranelift CLIF)
→ Object file (in memory)
→ Runtime linker (RTDyldObjectLinkingLayer)
→ Executable code in R+X memory
→ Function pointer call
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
using namespace llvm;
using namespace llvm::orc;
int main() {
auto JIT = cantFail(LLJITBuilder().create());
LLVMContext Context;
auto M = std::make_unique<Module>("jit", Context);
IRBuilder<> Builder(Context);
// int add(int a, int b) { return a + b; }
Function *AddFn = Function::Create(
FunctionType::get(Builder.getInt32Ty(),
{Builder.getInt32Ty(), Builder.getInt32Ty()}, false),
Function::ExternalLinkage, "add", M.get());
BasicBlock *BB = BasicBlock::Create(Context, "entry", AddFn);
Builder.SetInsertPoint(BB);
auto Args = AddFn->arg_begin();
Value *Sum = Builder.CreateAdd(Args, Args + 1);
Builder.CreateRet(Sum);
cantFail(JIT->addIRModule(ThreadSafeModule(std::move(M), std::make_unique<LLVMContext>())));
auto AddSym = JIT->lookup("add");
auto *AddPtr = (int (*)(int, int))AddSym->getValue();
int result = AddPtr(3, 4); // 7
return 0;
}
clang++ -std=c++17 jit.cpp $(llvm-config --cxxflags --ldflags --libs core orcjit native) -o jit
ExecutionSession ES;
auto &MainJD = ES.createBareJITDylib("main");
RTDyldObjectLinkingLayer ObjectLayer(
ES, []() { return std::make_unique<SectionMemoryManager>(); });
IRCompileLayer CompileLayer(
ES, ObjectLayer, std::make_unique<TargetMachineBuilder>());
// Add IR module to JITDylib
ThreadSafeModule TSM = ...;
CompileLayer.add(MainJD, std::move(TSM));
// Resolve symbol
auto Sym = ES.lookup({&MainJD}, "my_func");
Layers:
First call → trampoline → compile function → patch trampoline → direct call
// Simplified lazy compile on first invocation
void *LazyCompile(const std::string &Name) {
if (!Compiled.count(Name)) {
auto Fn = CompileFromAST(Name);
Compiled[Name] = Fn;
// Patch call site or update function pointer table
}
return Compiled[Name];
}
ORC supports lazy reexports and lazy compilation via LazyCallThroughManager.
// Concept: monomorphic call site caches resolved target
// Pseudocode for dynamic language
function call_site(obj, method, args) {
if (obj.class_id === cache.class_id) {
return cache.fn_ptr(args); // fast path
}
cache.class_id = obj.class_id;
cache.fn_ptr = resolve_method(obj, method);
return cache.fn_ptr(args);
}
JIT generates specialized code per cached type; deoptimize on cache miss.
use cranelift::prelude::*;
use cranelift_jit::{JITBuilder, JITModule};
use cranelift_module::{Linkage, Module};
let isa = cranelift_native::builder().finish(settings::Flags::new(settings::builder()))?;
let jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
let mut module = JITModule::new(jit_builder);
let mut ctx = module.make_context();
ctx.func = Function::with_name_signature(
module.declare_function("add", Linkage::Export, &sig)?,
sig,
);
// ... build IR in ctx.func ...
module.define_function(func_id, &mut ctx)?;
module.finalize_definitions()?;
let code = module.get_finalized_function(func_id);
let add_fn: fn(i32, i32) -> i32 = unsafe { std::mem::transmute(code) };
Cranelift: faster compile times than LLVM, good for embeddable JITs.
W^X (Write XOR Execute)
├── Memory page is writable OR executable, never both
├── JIT: allocate RW → write code → mprotect(RX)
└── macOS hardened runtime requires signed JIT pages
#include <sys/mman.h>
void *mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// write machine code to mem
mprotect(mem, size, PROT_READ | PROT_EXEC);
Linux: MAP_JIT on Apple platforms; sealed memfd on hardened systems.
use dynasm::dynasm;
use dynasmrt::{Assembler, ExecutableBuffer};
let mut asm = Assembler::new().unwrap();
dynasm!(asm
; .arch x86_64
; add:
; add eax, ecx
; ret
);
let buf = asm.finalize().unwrap();
let add_fn: fn(i32, i32) -> i32 = unsafe { std::mem::transmute(buf.ptr(0)) };
Use for lightweight asm snippets without LLVM dependency.
JIT backend choice?
├── Need LLVM optimizations → ORC JIT / LLJIT
├── Fast compile, embeddable → Cranelift
├── Tiny asm snippets → dynasm
└── ML workloads → MLIR → ORC pipeline
| Symptom | Cause | Fix |
|---------|-------|-----|
| Symbol not found on lookup | Name mangling or not exported | Use C linkage; LLVMExternalLinkage |
| Segfault calling JIT code | ABI mismatch | Match calling convention and types |
| W^X mmap failed | SELinux/grsecurity | Use MAP_JIT; check dmesg |
| Stale code after recompile | Old function pointer | Invalidate caches; use trampolines |
| LLVM JIT slow compile | -O2 in JIT | Use -O0 for JIT; optimize hot paths only |
| Cranelift verify error | Invalid CLIF | Enable cranelift_codegen::verify_function |
skills/compiler-internals/llvm-passes — optimize before JITskills/compiler-internals/compiler-frontend — AST to IR for JIT inputskills/compiler-internals/mlir — MLIR lowering to LLVM for JITskills/compilers/llvm — LLVM IR fundamentalsskills/low-level-programming/interpreters — bytecode interpreters using JITskills/low-level-programming/assembly-x86 — hand-written asm contextdevelopment
QEMU/KVM skill for virtualization and kernel development. Use when running qemu-system-x86_64 with KVM, configuring virtio devices, VFIO passthrough, QMP monitor, libvirt, or booting custom kernels. Activates on queries about QEMU, KVM, virtio, VFIO, virsh, virt-install, or -kernel -append.
development
Hardware virtualization internals skill for Intel VT-x and AMD-V. Use when studying VMCS/VMCB, EPT/NPT page tables, VMEXIT handling, APIC virtualization, or building minimal hypervisors. Activates on queries about VMX, SVM, VMCS, EPT, NPT, VMEXIT, or type-1 hypervisor.
testing
Linux containers internals skill for namespaces, cgroups, and OCI. Use when understanding clone/unshare namespaces, cgroups v2 limits, overlayfs, runc, seccomp profiles, capabilities, or escape mitigations. Activates on queries about namespaces, cgroups, overlayfs, runc, seccomp-bpf, OCI spec, or container escape.
tools
Reverse engineering skill for binary analysis. Use when decompiling with Ghidra, analyzing with radare2, scripting RE tools, triaging with strings/file/xxd, or diffing binaries. Activates on queries about Ghidra, radare2, r2, decompiler, Binary Ninja, Diaphora, or stripped binary analysis.