CompilerGym: Robust, Performant Compiler Optimization Environments for AI Research

Chris Cummins, Bram Wasti, Jiadong Guo, Brandon Cui, Jason Ansel, Sahir Gomez, Somya Jain, Jia Liu, Olivier Teytaud, Benoit Steiner, Yuandong Tian, Hugh Leather

TL;DR

CompilerGym is an open-source toolkit that brings the ease of OpenAI Gym to the complex world of compiler optimization. By providing high-performance, fault-tolerant environments and a massive dataset of millions of programs, it allows AI researchers to tackle production compiler problems—like LLVM phase ordering and GCC flag tuning—without needing to be compiler experts.

What Problem Does This Paper Address?

Compiler optimization is a "black art" involving high-dimensional search spaces where the order and selection of optimization passes can drastically change a program's performance or size. Historically, this field has had a massive entry barrier. AI researchers wanting to apply Reinforcement Learning (RL) often found themselves bogged down in thousands of lines of "glue code" just to get a compiler to talk to a training script.

Existing research infrastructures were often slow, lacked large-scale datasets, and were prone to crashing when a compiler hit an edge case. CompilerGym aims to be the "Atari for Compilers," providing a standardized, performant, and robust platform that lets researchers focus on the AI, not the infrastructure.

Methods

The core of CompilerGym is its formulation of compiler optimization as a Markov Decision Process (MDP). Using the familiar OpenAI Gym API, a "User" (the agent) interacts with a "CompilerGym Environment" through a simple loop:

The CompilerGym interaction loop
  1. Action: The agent selects an optimization (e.g., an LLVM pass or a GCC flag).
  2. Observation: The environment returns a view of the program (e.g., a feature vector, a control-flow graph, or even the raw IR).
  3. Reward: The environment provides a performance metric (e.g., reduction in instruction count or binary size).

To make this work at scale, the authors implemented a client-server architecture (Figure 2). The Python frontend communicates via RPC with backend compiler services (written in C++ or Python). This design ensures that if a compiler crashes—a common occurrence in experimental autotuning—the training process remains unaffected and can gracefully recover.

Datasets

One of CompilerGym's biggest contributions is its scale. It includes:

  • LLVM Benchmarks: Over 1.1 million compilable C/C++ programs sourced from AnghaBench, GitHub, and the Linux kernel.
  • Program Generators: Integration with tools like Csmith and llvm-stress to generate infinite synthetic benchmarks.
  • Offline Dataset: A 50GB relational database containing over 1 million unique state transitions in LLVM, designed for pre-training agents or learning cost models without running a live compiler.

Key Insights and Ideas

  • Performance through Caching: Unlike previous tools that re-parsed program code at every step, CompilerGym's backend maintains a cache of parsed bitcodes. This allows it to apply optimizations incrementally, resulting in a 27x speedup over prior state-of-the-art frameworks.
  • The "Fork" Operator: Compilers generally don't have an "undo" button. CompilerGym introduces a lightweight fork() operator that creates independent deep copies of environment states, enabling efficient backtracking and tree-search algorithms.
  • Accessibility via Web UI: To help researchers build intuition, the authors released CompilerGym Explorer, a web interface that visualizes the optimization search tree and program feature trends in real-time.
CompilerGym Explorer Web Interface

Key Results

  • Efficiency: CompilerGym is significantly faster than predecessors like Autophase and OpenTuner, reducing the time for a single optimization step from ~200ms down to just ~7.5ms.
  • Autotuning Superiority: Using standard black-box optimization libraries (like Nevergrad) via the CompilerGym API, the authors achieved better code size reduction than the default -Oz heuristic across the cBench suite.
  • RL Performance: The paper demonstrates training various RL agents (PPO, IMPALA, etc.) using the Ray/RLlib library. While these agents excel on the domains they were trained on (e.g., synthetic programs), the results highlight a major open challenge: generalization. An agent trained on synthetic code often struggles when faced with real-world GitHub projects.

Limitations and Future Work

The authors identify cross-domain generalization as the primary hurdle for the field. While CompilerGym provides the data to study this, current AI models still struggle to transfer knowledge between different types of software.

Additionally, while the framework is extensible, adding a completely new compiler still requires implementing the CompilationSession interface. Future work may involve expanding the library of available environments and improving the fidelity of "noisy" rewards like program execution time, which can vary based on hardware noise.

Summary updated

BibTeX
@inproceedings{cummins2022compilergym,
  author = {Chris Cummins and Bram Wasti and Jiadong Guo and Brandon Cui and Jason Ansel and Sahir Gomez and Somya Jain and Jia Liu and Olivier Teytaud and Benoit Steiner and Yuandong Tian and Hugh Leather},
  title = {{CompilerGym: Robust, Performant Compiler Optimization Environments for AI Research}},
  booktitle = {CGO},
  year = {2022},
  doi = {10.1109/cgo53902.2022.9741258},
}
Figure