Enable Link-Time Optimization (LTO and ThinLTO) for an LLVM 22-based compiler. Covers full LTO vs ThinLTO trade-offs, bitcode emission from a frontend, LLD integration, out-of-tree whole-program optimization via ModulePassManager, and common debugging tips.
Use this skill when you want cross-module optimization — inlining, devirtualization, dead-code elimination, or whole-program alias analysis — across separately compiled translation units.
| Full LTO | ThinLTO | |
|---|---|---|
| How it works | All bitcode merged into one giant module, then optimized | Per-module summaries at compile time; parallel backends at link time |
| Memory use | Very high (all IR in RAM at once) | Much lower (only imported pieces) |
| Link time | Slow for large programs | Fast, parallelism-friendly |
| Optimization quality | Maximum (whole-program view) | Close to full LTO for most workloads |
| When to use | Small-to-medium programs, maximum perf | Large codebases (Chromium-scale) |
If your compiler emits bitcode files (.bc) or hands off to Clang for linking:
#include "llvm/Bitcode/BitcodeWriter.h"
// After all IR is generated and verified: