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.
翻译:编译器中的高效代码优化对计算机与软件工程至关重要。这些优化的成败主要取决于代码所应用的优化遍的选择与排序。尽管多数编译器采用固定的优化遍序列,但当前寻找最优序列的方法要么依赖执行效率极低的搜索算法,要么采用难以泛化到训练时未见代码的学习方法。我们提出CompilerDream——一种基于模型的强化学习方法,用于通用代码优化。CompilerDream包含一个能精准模拟优化遍内在属性的编译器世界模型,以及基于该模型训练的、可生成高效优化策略的智能体。通过在大规模程序数据集上训练,CompilerDream具备跨多种应用场景与源代码语言的通用代码优化能力。广泛实验首先揭示了CompilerDream在自动调优中的强大优化能力——其领跑CompilerGym排行榜。更重要的是,大规模训练的编译器世界模型与智能体展现出卓越的零样本泛化能力:在值预测与端到端代码优化两个场景中,其性能均显著超越不同数据集上的LLVM内置优化方法及其他前沿技术。