Introduction
Modern compilers like LLVM rely on hand-written heuristics—rules of thumb—to make complex optimization decisions, such as whether to inline a function to save space. These heuristics are difficult to maintain and often fail to scale as more program properties (features) are added. MLGO is the first framework to systematically integrate machine learning into a production-grade compiler (LLVM) by replacing these heuristics with Reinforcement Learning (RL) policies. By doing so, the authors achieved up to a 7% reduction in binary size while maintaining the strict determinism and performance required for industrial software deployment.
Methodology
The MLGO framework treats compiler optimization as an RL problem, focusing initially on inlining-for-size as a pilot case.
The Framework
MLGO operates in two distinct modes to meet the strict requirements of industrial software deployment:
- Development Mode (Training): Engineers train models and log data using a JIT-linked TensorFlow API. This mode allows for rapid iteration and data collection.
- Release Mode (Normal Use): Uses "Ahead-of-Time" (AOT) compilation to convert the trained neural network into a simple, statically linked C++ function. This ensures determinism and high performance without heavy ML dependencies in the final compiler.
Algorithms and Training
The authors compared two primary approaches: Proximal Policy Optimization (PPO), a policy gradient RL algorithm, and Evolution Strategies (ES), a black-box technique. To accelerate training, they employed Behavioral Cloning, where the model initially learns to mimic existing LLVM heuristics before attempting to optimize further.
State Representation
Instead of raw code, the model receives a distilled state consisting of 11 numerical features. These features capture essential properties of the caller, callee, and call site to inform the inlining decision.
Evaluation and Results
The models were trained on a massive internal corpus of over 28,000 IR (Intermediate Representation) modules and evaluated on Clang (self-hosting), Google internal infrastructure apps, and the SPEC 2006 benchmark suite.
Key Results
- Size Reduction: MLGO achieved up to a 7% reduction in binary size compared to LLVM’s most aggressive size optimization (
-Oz). - Efficiency: PPO was significantly more sample-efficient, requiring 20x fewer computational resources than ES to reach similar quality.
- Low Overhead: In release mode, the ML-guided compiler saw only a 1% increase in total compile time and a negligible 0.08% increase in the compiler's own binary size.
Insights and Future Work
A critical design choice was the separation of correctness and policy; the ML model only provides advice, while the compiler's existing infrastructure handles the actual transformation. This ensures the ML model can never "break" the code. Surprisingly, models trained on one corpus generalized well to unrelated code and remained effective over months of codebase evolution.
Future work includes optimizing for execution speed—a more challenging "noisy" metric—and exploring more complex state representations like Graph Neural Networks (GNNs) or code embeddings to capture program structure more effectively.
Summary updated