Abstract
We explore the novel application of Large Language Models to code optimization. We present a 7B-parameter transformer model trained from scratch to optimize LLVM assembly for code size. The model takes as input unoptimized assembly and outputs a list of compiler options to best optimize the program. Crucially, during training, we ask the model to predict the instruction counts before and after optimization, and the optimized code itself. These auxiliary learning tasks significantly improve the optimization performance of the model and improve the model's depth of understanding. We evaluate on a large suite of test programs. Our approach achieves a 3.0% improvement in reducing instruction counts over the compiler, outperforming two state-of-the-art baselines that require thousands of compilations. Furthermore, the model shows surprisingly strong code reasoning abilities, generating compilable code 91% of the time and perfectly emulating the output of the compiler 70% of the time.
Introduction
Compiler optimization has traditionally been considered a "black art," relying on thousands of hand-tuned heuristics and millions of lines of code. One of the most persistent challenges in this field is the pass ordering problem: determining the optimal sequence of transformations—such as inlining, loop unrolling, or constant folding—to produce the most efficient binary for a specific piece of code.
Previous attempts to apply machine learning to this task often utilized "lossy" representations, such as hand-engineered features or Graph Neural Networks (GNNs). While useful, these representations frequently strip away critical details like specific constant values or type information. This research investigates whether a Large Language Model (LLM), treating code as a complete, lossless text representation, can learn the deep semantics of a compiler well enough to outperform traditional heuristic-based optimization.
Methodology
The researchers developed a model based on the 7B-parameter LLaMA 2 architecture, trained entirely from scratch rather than fine-tuned. The core innovation lies in a multi-task learning strategy designed to force the model to "understand" the code it optimizes.
During the training phase, the model is tasked with more than just suggesting compiler flags (the "Pass Ordering" task). It must also perform two auxiliary tasks:
- Instruction Count Prediction: Estimating the exact number of instructions before and after optimization.
- Code Generation: Directly outputting the optimized LLVM assembly (IR) itself.
By requiring the model to emulate the compiler's output, the researchers found that it developed a significantly deeper grasp of code semantics, which in turn boosted its ability to select optimal optimization flags.
Datasets
The model was trained on a massive corpus of 1 million LLVM-IR functions, comprising approximately 610,000 functions from public C/C++ repositories and 390,000 synthetic functions generated by tools like CSmith and YARPGen. Evaluation was conducted across diverse benchmarks, including AI-SOCO, ExeBench, and Transcoder. To establish a ground truth, an Autotuner performed billions of compilations to find near-optimal pass sequences for the training data.
Key Results and Insights
The model demonstrated that treating code as a lossless text interface allows for reasoning about specific constants and variable relationships often lost in other ML representations.
Key performance metrics include:
- Optimization Performance: The model achieved a 3.0% improvement in code size reduction over the standard LLVM
-Oz(optimize for size) setting. - Compiler Emulation: Surprisingly, the model perfectly emulated the compiler's complex logic 70% of the time, producing character-for-character identical assembly. As shown in the training curves, the model's ability to generate compilable code reached 91% as training progressed.
- Efficiency: Unlike traditional "autotuning" which requires hundreds of trial compilations, this model can predict optimal flags in a single "zero-shot" inference pass, achieving 60% of the potential gains of exhaustive search-based methods.
Limitations and Future Work
While the results are promising, several hurdles remain. The current model is limited by a context window of 2,048 tokens, making it suitable for individual functions but challenging for whole-program optimization. Additionally, the model still exhibits "hallucinations" in complex mathematical reasoning, such as failing to correctly fold certain constants. Future research will focus on expanding the context window and distilling these large models into smaller, faster versions suitable for production compiler toolchains.
Summary updated