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生成的程式碼提供更強的保證,但其嚴謹性也使得程式碼生成更加困難。現成的編譯器雖可在生成後提供有用的回饋,卻無法引導中間生成步驟(例如自迴歸大型語言模型解碼過程中的步驟)。約束解碼透過在取樣時拒絕無效的詞元來提前介入,但需要白箱模型存取權限,並為實現語意約束而付出高昂的重製成本。 我們提出「生成式編譯」,這是第一種在生成過程中取得編譯器對部分程式回饋的方法。其核心技術工具是「密封器」:一種輕量級、主要基於語法引導的轉換,能將部分程式轉換為標準編譯器可診斷的完整程式。此轉換的設計確保可能完成的部分程式永不被拒絕,同時保留足夠的程式碼上下文,以便及早捕捉真正的死路。我們在一個核心的類Rust運算上建構了這樣的密封器,並證明了它滿足這些性質(全部在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.