CompilerDream: Learning a Compiler World Model for General Code Optimization

Chaoyi Deng, Jialong Wu, Ningya Feng, Jianmin Wang, Mingsheng Long

TL;DR

CompilerDream is the first world-model-based reinforcement learning approach for general compiler optimization. By learning to simulate compiler behavior on a massive dataset of natural programs, it achieves state-of-the-art code size reduction and demonstrates remarkable zero-shot generalization to unseen programs and programming languages.

What Problem Does This Paper Address?

Modern compilers like LLVM rely on a series of "passes"—individual transformations that optimize code for size or speed. The order in which these passes are applied, known as the phase ordering problem, is notoriously difficult because the effect of one pass often depends on the previous ones.

Traditional approaches fall into two camps:

  1. Fixed sequences: Compilers use expert-designed defaults (like -O3 or -Oz), which are sub-optimal for many specific programs.
  2. Iterative search/ML: These methods either take thousands of slow compiler runs to find a good sequence for a single program or struggle to generalize to new, unseen code.

The challenge is to create an optimizer that is both efficient (doesn't need thousands of trials) and general (works well on code it wasn't trained on).

Methods

The authors introduce CompilerDream, which treats compiler optimization as a "world model" problem. Instead of interacting directly with a slow compiler during training, the system learns a predictive model of the compiler itself.

The Compiler World Model

Based on the Dreamer architecture, the world model learns to "imagine" what will happen to a program's Intermediate Representation (IR) when a specific optimization pass is applied. It predicts:

  • Latent States: A compressed representation of the code's features.
  • Rewards: The expected reduction in code size.
Figure 1: CompilerDream performs code optimization by interacting with the compiler using an agent powered by a world model. With strong generalization capabilities, it efficiently optimizes programs from diverse origins.

Reward Smoothing

A major hurdle in compiler RL is "sparse rewards"—many passes don't change the code size immediately but are essential for enabling future optimizations. CompilerDream uses a reward smoothing technique, applying an exponential decay to rewards across steps. This provides the agent with continuous feedback, making it easier to learn which sequences are actually valuable.

Figure 3: Comparison of the original and smoothed rewards during an episode on a program from the BLAS dataset [42].

Optimization Agents

The framework supports two types of agents:

  • Value Prediction Agent: Predicts the final outcome of a given sequence to guide a search.
  • Reinforcement Learning Agent: A policy trained entirely within the "imagination" of the world model to generate optimal sequences in a single trial.

Datasets

To ensure generalization, the authors moved away from small, synthetic datasets.

  • Training: They curated a large-scale dataset from CodeContests (originally from AlphaCode), consisting of over 110,000 high-quality, human-written C++ solutions to competitive programming problems.
  • Evaluation: They tested on standard benchmarks like cBench, CHStone, MiBench, and NPB, as well as production-level code from OpenCV and TensorFlow.
  • Generalization Tests: They even evaluated on AI-generated C code (FormAI) and a custom Objective-C dataset to test cross-language capabilities.

Key Insights and Ideas

  • The "Compiler Simulator": By learning a world model, the agent can train an order of magnitude faster than model-free methods (like PPO) because it doesn't have to wait for the actual compiler to run at every step.
  • Natural Code Matters: Training on diverse, human-written code (CodeContests) proved far superior for generalization than training on synthetic code generators like Csmith, which often produce unrealistic code patterns.
  • Latent Generalization: Because the world model learns to represent the dynamics of optimization, it can effectively "zero-shot" generalize to entirely new domains and even different programming languages.

Key Results

CompilerDream consistently outperformed both standard compiler flags and state-of-the-art ML baselines:

  • Autotuning: It reached the top of the CompilerGym leaderboard, achieving a 1.073x code size reduction on cBench compared to LLVM's -Oz.
  • Zero-Shot Performance: In a single trial (no searching), the RL agent surpassed -Oz on almost all benchmarks, including a 1.108x reduction on the NPB dataset.
  • Efficiency: The RL agent trained on the world model learned significantly faster than model-free counterparts, requiring far fewer interactions with the real compiler environment.
Figure 4: Results of general code optimization with RL: Code size reduction in terms of IR instruction count over LLVM -Oz under different methods. Bars indicate the geometric mean and min-max range across test programs in each benchmark dataset.

Limitations and Future Work

  • Metric Focus: The current work focuses exclusively on code size reduction. The authors note that optimizing for execution time is much harder due to the high variance and noise in runtime measurements on modern hardware.
  • Feature Complexity: While the 56-dimensional Autophase features are efficient, they might miss deeper semantic nuances that a Large Language Model (LLM) could capture.
  • Future Directions: The authors suggest scaling up the world model and exploring multi-objective optimization (balancing size vs. speed) as promising next steps.

Summary updated

BibTeX
@inproceedings{deng2024compilerdream,
  author = {Chaoyi Deng and Jialong Wu and Ningya Feng and Jianmin Wang and Mingsheng Long},
  title = {{CompilerDream: Learning a Compiler World Model for General Code Optimization}},
  booktitle = {ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
  year = {2024},
}
Figure