Static Neural Compiler Optimization via Deep Reinforcement Learning

Rahim Mammadli, Ali Jannesari, Felix WolfarXiv 2020

The "phase-ordering problem" has long been a challenge in compiler design. Modern compilers like LLVM rely on hundreds of optimization passes—such as constant folding or loop unrolling—to improve code performance. However, the sequence in which these passes are applied significantly impacts the final execution speed. Traditionally, compiler developers have relied on hand-crafted, fixed sequences like -O2 or -O3. While these provide a baseline, they are "one-size-fits-all" solutions that often miss potential performance gains.

This research introduces CORL, a framework that leverages Deep Reinforcement Learning (DRL) to automatically discover optimal optimization sequences based solely on the static intermediate representation (IR) of the code.

The CORL Framework

The authors frame the phase-ordering problem as a Reinforcement Learning task where an agent learns to navigate the complex space of compiler transformations.

The CORL reinforcement learning loop, showing the interaction between the DQN agent, the LLVM optimizer, and the action history.

The system operates in a closed loop:

  1. State Representation: The agent "observes" the code through its LLVM IR, encoded using pre-trained embeddings (inst2vec). To provide context, this state is augmented with an "action history" of previously applied optimizations.
  2. The Agent: A deep residual neural network (DQN) acts as the decision-maker, predicting the expected reward for various optimization actions.
  3. Reward Mechanism: Rewards are calculated based on the logarithm of the achieved speedup ($ln(Speedup)$). This additive property allows the agent to treat the total speedup of a sequence as the sum of its parts.

Defining the Action Space

One of the key contributions of this work is the exploration of different levels of abstraction for the agent's actions. The complexity of the learning task depends heavily on how these actions are defined.

Three levels of action abstraction: High (sub-sequences), Medium (individual passes), and Low (passes with specific parameters).

The researchers experimented with three tiers:

  • High (H): The agent chooses between 8 pre-defined sub-sequences derived from the standard -O3 pipeline.
  • Medium (M): The agent selects from 42 individual transformation passes.
  • Low (L): The agent controls both the selection of passes and their specific parameters, such as loop unroll factors.

The study found a significant trade-off: while the "Low" space offers the highest theoretical performance ceiling, it is significantly harder for the agent to master. The "High" action space proved to be the most stable and converged the fastest during training.

Experimental Results and Insights

The CORL agent was trained and validated on 109 benchmarks from the LLVM test suite, including Polybench and Linpack. The results demonstrate that a static approach—one that does not require running the code during the decision process—is highly effective.

  • Performance Gains: On unseen programs, the agent achieved speedups of up to 1.32x over LLVM’s -O3.
  • Specialization: Unlike fixed sequences, the agent learned program-specific strategies. For the puzzle.c benchmark, it achieved a 12.82x speedup over unoptimized code, nearly doubling the performance of -O3 (6.80x).
  • The Importance of History: Because some optimization passes do not immediately change the IR in a way the neural network can easily detect, maintaining a history of previous actions was found to be crucial for the agent's decision-making process.

Limitations and Future Directions

While promising, the approach faces challenges. The current dataset size (109 benchmarks) is relatively small for deep learning, leading to some generalization issues where -O3 still outperformed the agent on average across the entire validation set. Furthermore, benchmarking is inherently "noisy" due to system interference, requiring multiple runs to obtain reliable data.

Future work aims to incorporate more sophisticated Graph Neural Networks (GNNs) to better capture the structural nuances of code and utilize self-supervised learning to pre-train agents on massive codebases before fine-tuning them for optimization tasks.

Summary updated

BibTeX
@misc{mammadli2020static,
  author = {Rahim Mammadli and Ali Jannesari and Felix Wolf},
  title = {{Static Neural Compiler Optimization via Deep Reinforcement Learning}},
  year = {2020},
  eprint = {2008.08951},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2008.08951},
}
Figure