ChatPaper.aiChatPaper

生成式编译:AI生成代码时的即时编译器反馈

Generative Compilation: On-the-Fly Compiler Feedback as AI Generates Code

July 15, 2026
作者: Niels Mündler-Sasahara, Hristo Venev, Dawn Song, Martin Vechev, Jingxuan He
cs.AI

摘要

具有丰富静态语义的语言(如Rust)能为AI生成的代码提供更强的保证,但其严格性使得代码生成更加困难。现成的编译器可以在生成后提供有用的反馈,但无法指导中间生成步骤(例如自回归大语言模型解码过程中的步骤)。约束解码通过拒绝采样过程中的无效token提前介入,但需要白盒模型访问权限,且对语义约束的重新实现成本高昂。我们提出生成式编译,这是首个在生成过程中获取编译器对不完整程序反馈的方法。其核心技术装置是"sealor":一种轻量级、主要基于语法指导的变换,能将不完整程序转换为标准编译器可诊断的完整程序。该设计确保可能被补全的不完整程序永远不会被拒绝,同时保留足够的代码上下文以尽早捕获真正的死结。我们在类Rust核心演算上构建了这样一个"sealor",并证明其满足这些性质,所有证明均在Lean中机械化实现。我们将该方法扩展至首个针对真实Rust的不完整程序检查器。我们使用前沿黑盒模型与开源模型,在具有挑战性的仓库级Rust编码任务上评估了该方法。结果表明,相较于标准的事后生成反馈,生成式编译能减少非编译输出并提升功能正确性。其实现方式是:在生成过程中尽早检测接近错误源的广泛错误类型,从而减少错误级联效应并提供聚焦的诊断信息。更广泛地说,生成式编译是推动编译器成为AI辅助编程中活跃参与者的重要一步——使其在生成阶段发挥作用,而非仅作为独立的生成后检查环节。
English
Languages with rich static semantics, such as Rust, provide stronger guarantees for AI-generated code, but their strictness makes generation more difficult. Off-the-shelf compilers can provide useful feedback post-generation, but does not guide intermediate generation steps, such as those during autoregressive LLM decoding. Constrained decoding intervenes earlier by rejecting invalid tokens during sampling, but requires white-box model access and costly reimplementation for semantic constraints.We introduce generative compilation, the first approach to obtaining compiler feedback on partial programs during generation. The core technical device is a sealor: a lightweight, mostly syntax-guided transformation that converts partial programs into complete ones that standard compilers can diagnose. It is designed such that possible-to-complete partial programs are never rejected, while preserving enough code context to catch genuine dead ends early. We construct such a sealor on a core Rust-like calculus and prove that it satisfies these properties, all mechanized in Lean. We extend it to the first partial-program checker for real Rust. We evaluate our method on challenging repository-level Rust coding tasks, across both frontier black-box and open-weight models. We show that generative compilation reduces non-compiling outputs and improves functional correctness, relative to standard post-generation feedback. It does so by detecting a broad range of errors close to their source and early during generation, thereby reducing errors cascades and enabling focused diagnostics. More broadly, generative compilation is a step toward making compilers a first-class citizen of AI-assisted programming active during generation, rather than a separate post-generation check.