Abstract
Effective code optimization in compilers is crucial for computer and software engineering. The success of these optimizations primarily depends on the selection and ordering of the optimization passes applied to the code. While most compilers rely on a fixed sequence of optimization passes, current methods to find the optimal sequence either employ impractically slow search algorithms or learning methods that struggle to generalize to code unseen during training. We introduce CompilerDream, a model-based reinforcement learning approach to general code optimization. CompilerDream comprises a compiler world model that accurately simulates the intrinsic properties of optimization passes and an agent trained on this model to produce effective optimization strategies. By training on a large-scale program dataset, CompilerDream is equipped to serve as a general code optimizer across various application scenarios and source-code languages. Our extensive experiments first highlight CompilerDream's strong optimization capabilities for autotuning, where it leads the CompilerGym leaderboard. More importantly, the zero-shot generalization ability of large-scale trained compiler world model and agent, excels across diverse datasets, surpassing LLVM's built-in optimizations and other state-of-the-art methods in both settings of value prediction and end-to-end code optimization.
Introduction: Teaching AI to "Imagine" Better Code
CompilerDream is the first approach to apply World Models to the "phase ordering problem" in compiler optimization. By learning to simulate how optimization passes affect code, it creates an agent that can optimize programs it has never seen before, outperforming standard LLVM -Oz settings and topping the CompilerGym leaderboard.
What Problem Does This Paper Address?
Modern compilers like LLVM rely on hundreds of "optimization passes"—small programs that transform code to make it faster or smaller (e.g., removing unused variables or unrolling loops). The phase ordering problem asks: In what order should we apply these passes?
The search space is astronomical, and the "perfect" order varies for every program. Current solutions fall into two camps:
- Search-based methods: These try thousands of combinations on a single program. They find great results but are painfully slow because they have to actually compile and run the code every time.
- Model-free Reinforcement Learning (RL): These train an AI to pick passes. While faster, they often struggle to "generalize"—they perform well on code similar to their training data but fail on new, complex software.
The authors identify a "generalization bottleneck" caused by training on small or synthetic datasets and the lack of a model that understands the underlying dynamics of code transformation.
Methods: The Compiler World Model
The core innovation is the Compiler World Model, inspired by the "Dreamer" architecture used in robotics and gaming.
- The World Model: Instead of interacting with a real, slow compiler during training, the agent interacts with a neural network that simulates the compiler. This model predicts two things: how the code's features will change after a pass (latent state transition) and how much the code size will shrink (reward).
- Reward Smoothing: Optimization is often a "sparse reward" problem. You might apply five passes that do nothing, only for the sixth pass to suddenly shrink the code by 20%. To help the AI learn, the authors use an exponential decay to "smooth" rewards backward in time, giving credit to the "enabling" passes that made the final optimization possible.
- Two Flavors of Agents:
- A Value Prediction Agent that uses the world model to "score" different sequences.
- An RL Agent (Actor-Critic) that learns to pick the next best pass entirely within its own "imagination" (the world model).
Datasets and Generalization
The authors argue that naturalness and scale are key to generalization.
- Training: They used CodeContests, a massive dataset of 110,240 human-written C++ solutions to competitive programming problems. This is significantly more complex than the synthetic "Csmith" programs often used in this field.
- Evaluation: They tested across eight diverse benchmarks, including cBench, CHStone, and real-world kernels from OpenCV and TensorFlow.
- Zero-Shot Testing: To prove generalization, they tested on languages the model never saw during training, including Fortran (NPB/BLAS benchmarks) and Objective-C.
Key Insights
- Compilers as "Worlds": The paper treats the compiler environment like a physics engine. Just as a robot learns the "physics" of a room, CompilerDream learns the "physics" of code transformation.
- Imagination is Efficient: Because the agent trains on the world model's predictions rather than real compilations, it learns an order of magnitude faster than traditional RL (like PPO).
- Human Code > Synthetic Code: Training on human-written algorithmic code allows the model to capture the "logic" of optimization, which translates surprisingly well across different programming languages.
- Smoothing the "Aha!" Moment: The reward smoothing technique is crucial. It prevents the model from getting discouraged during long sequences of passes that don't yield immediate results but are necessary for a later breakthrough.
Key Results
- Leaderboard King: CompilerDream reached #1 on the CompilerGym leaderboard, achieving a 1.073x code size reduction on cBench, surpassing the previous state-of-the-art.
- Zero-Shot Mastery: Even without seeing a specific dataset during training, the model consistently beat LLVM's built-in
-Oz(the standard "optimize for size" flag). - Cross-Language Success: On Fortran-based benchmarks (NPB), it achieved an additional 3% code size reduction over
-Oz, despite being trained only on C++. - Speed: The RL agent can generate an optimized sequence in a single "trial" (inference), making it practical for real-world use where developers can't wait hours for a search algorithm to finish.
Limitations and Future Work
- Metric Focus: The paper focuses exclusively on code size reduction. While important for embedded systems, most developers care more about execution time. The authors note that measuring runtime is "noisy" (affected by background tasks on a CPU), making it harder for models to learn.
- Action Space: The model uses a reduced set of 42-124 LLVM passes. Expanding this to include more granular "fine-tuning" of pass parameters is a logical next step.
- LLM Integration: While this model uses a latent neural representation, the authors suggest that combining world models with Large Language Models (LLMs) could provide even deeper insights into code semantics.
Summary updated