[论文解读] Code Translation with Compiler Representations
本文提出通过引入LLVM中间表示(IR)来增强神经代码翻译,以提高语义准确性并减少语法错误。通过在C++、Java、Rust和Go的源代码及其对应的IR上联合训练序列到序列的Transformer模型,该方法在四种语言的代码翻译任务中平均提升了11%的翻译质量,其中在Java→Rust翻译任务中使用贪婪解码时提升最高达79%。
In this paper, we leverage low-level compiler intermediate representations (IR) to improve code translation. Traditional transpilers rely on syntactic information and handcrafted rules, which limits their applicability and produces unnatural-looking code. Applying neural machine translation (NMT) approaches to code has successfully broadened the set of programs on which one can get a natural-looking translation. However, they treat the code as sequences of text tokens, and still do not differentiate well enough between similar pieces of code which have different semantics in different languages. The consequence is low quality translation, reducing the practicality of NMT, and stressing the need for an approach significantly increasing its accuracy. Here we propose to augment code translation with IRs, specifically LLVM IR, with results on the C++, Java, Rust, and Go languages. Our method improves upon the state of the art for unsupervised code translation, increasing the number of correct translations by 11% on average, and up to 79% for the Java -> Rust pair with greedy decoding. We extend previous test sets for code translation, by adding hundreds of Go and Rust functions. Additionally, we train models with high performance on the problem of IR decompilation, generating programming source code from IR, and study using IRs as intermediary pivot for translation.
研究动机与目标
- 解决神经机器翻译(NMT)在代码翻译中的局限性,即其往往依赖于表层标记序列,导致生成语义错误但语法上看似合理的代码。
- 通过编译器IR提供的语义基础,克服基于规则的转换器生成不自然、难以维护代码的缺陷。
- 通过使用LLVM IR作为多种编程语言间的共享语义表示,提升零样本和低资源代码翻译的性能。
- 通过增加343个Go函数和280个Rust函数及其测试用例,扩展现有代码翻译基准测试集以支持更全面的评估。
- 探索将IR作为翻译中间语言的可行性,并评估基于神经模型的IR反编译为源代码的性能。
提出的方法
- 利用LLVM编译工具链从C++、Java、Rust和Go的源代码中生成LLVM IR表示,并将其作为训练数据的增强内容。
- 在配对的源代码和对应IR序列上训练序列到序列的Transformer模型,以学习语义对齐的代码表示。
- 在训练过程中联合引入代码和IR的监督信号,同时优化翻译结果的语法正确性与语义保真度。
- 设计仅使用IR的基线模型,即先将源代码翻译为IR,再将IR反编译为目标语言,以评估其性能与完整代码+IR模型的对比。
- 训练一个独立的反编译器模型,从LLVM IR生成源代码,在C++反编译任务中达到78%的准确率。
- 利用IR作为共享语义空间,对齐不同编程语言之间的嵌入表示,从而提升跨语言迁移能力和翻译质量。
实验结果
研究问题
- RQ1在神经代码翻译中引入LLVM IR表示,是否能显著提升翻译准确率和语义正确性?
- RQ2在训练中引入IR后,其在低资源翻译场景(如Rust的翻译)中的性能表现如何?
- RQ3在直接进行代码到代码翻译存在困难时,IR在多大程度上可作为有效的中间语言用于代码翻译?
- RQ4神经模型是否能有效学习将LLVM IR反编译为高质量、符合语言习惯的多语言源代码?
- RQ5与最先进的无监督代码翻译模型(如TransCoder)相比,所提出方法在准确率和鲁棒性方面表现如何?
主要发现
- 所提出的IR增强方法在C++、Java、Rust和Go四种语言的无监督代码翻译任务中,平均提升SOTA性能11%。
- 在Java→Rust的翻译任务中,使用贪婪解码时,该方法相比TransCoder实现了79%的正确翻译率提升。
- 在低数据场景下,模型表现尤为突出:翻译到Rust时相对提升26%,从Rust翻译时相对提升19%。
- 仅使用IR的基线方法(即通过IR反编译)相比TransCoder实现了170%的性能提升,证明了语义对齐的价值。
- 反编译器模型在从LLVM IR重建C++源代码任务中达到78%的准确率,表明其在反向转换任务中表现强劲。
- 扩展后的测试集包含343个Go函数和280个Rust函数,显著扩展了评估基准,超越了以往工作。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。