LLVM 18 Whole-Program LTO & Profile-Guided Optimization (PGO)
How Nyx achieves maximum native throughput through whole-program Link-Time Optimization (LTO), auto-vectorization, and two-stage Profile-Guided Optimization (PGO).
1. Whole-Program Link-Time Optimization (LTO)
In traditional separate compilation, compilers optimize each source file in isolation. Cross-module calls and region allocations cannot be inlined or elided. Nyx's LLVM 18 backend emits modular bitcode and executes whole-program analysis at link time:
flowchart LR
MA["Module A (.nyx)"]
MB["Module B (.nyx)"]
ST["Stdlib (.nyx)"]
BC["LLVM Bitcode (.bc)"]
LTO["LTO Pass Pipeline
(Cross-Module Inlining & Escape Pruning)"]
BIN["Super-Optimized Binary"]
MA --> BC
MB --> BC
ST --> BC
BC --> LTO
LTO --> BIN
style BC fill:#1e293b,stroke:#38bdf8,stroke-width:2px,color:#f8fafc
style LTO fill:#064e3b,stroke:#10b981,stroke-width:2px,color:#f8fafc
style BIN fill:#701a75,stroke:#f472b6,stroke-width:2px,color:#f8fafc
LTO Optimizations:
- Cross-Module Region Inlining: If a function in module B returns an object allocated in a local region to module A, the LTO pass detects the caller's lifespan and converts heap allocations directly into stack bump allocations.
- Whole-Program Dead Code Elimination (DCE): Eliminates unused standard library functions across the entire dependency graph, producing ultra-compact native binaries.
2. Auto-Vectorization & Target SIMD Dispatch
The backend automatically vectorizes tight loops and tensor operations using the LLVM Loop and SLP Vectorizers:
import std.tooling.llvm
let a: [f64] = [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5]
let b: [f64] = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
// Lowered to 4-wide AVX2 256-bit SIMD instructions
let dot = llvm.simd_vector_dot(a, b, 8)
3. Profile-Guided Optimization (PGO)
PGO bridges compile-time optimizations with actual production runtime behavior:
- Stage 1 (Instrumentation): Compile binary with lightweight execution counters:
nyxc src/main.nyx --pgo-generate -o bin/app_instrumented.exe - Stage 2 (Runtime Profiling): Run typical production workloads to generate
default.profdata:./bin/app_instrumented.exe --benchmark-workload - Stage 3 (Optimized Recompilation): Feed profile data back to LLVM to reorder basic blocks, bias branches, and optimize instruction cache (I-Cache) layout:
nyxc src/main.nyx --pgo-use=default.profdata -O3 -o bin/app_optimized.exe