arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2207.03578cs.PLcs.CLcs.LG
收藏

利用编译器中间表示进行代码翻译

Code Translation with Compiler Representations

Meta AI

Marc Szafraniec, Baptiste Roziere, Hugh Leather, Francois Charton, Patrick Labatut, Gabriel Synnaeve

AI总结:

针对神经代码翻译难以区分相似代码语义的问题,提出引入LLVM IR增强无监督翻译,在多语言任务上显著提升正确率并扩展测试集。

摘要:

本文利用底层编译器中间表示(IR)来改进代码翻译。传统转译器依赖语法信息和手工规则,这限制了其适用性,并生成看起来不自然的代码。将神经机器翻译(NMT)方法应用于代码,已成功扩大了可获得自然外观翻译的程序范围。然而,这些方法将代码视为文本标记序列,仍不能充分区分在不同语言中具有不同语义的相似代码片段。其后果是翻译质量较低,降低了NMT的实用性,并凸显出迫切需要一种能显著提高其准确性的方法。在此,我们提出用IR(具体为LLVM IR)增强代码翻译,并给出了在C++、Java、Rust和Go语言上的结果。我们的方法改进了无监督代码翻译的当前最优水平,使正确翻译的数量平均增加11%,在采用贪心解码的Java到Rust语言对上最高增加79%。我们通过增加数百个Go和Rust函数,扩展了以往的代码翻译测试集。此外,我们针对IR反编译(即从IR生成程序源代码)问题训练了高性能模型,并研究了将IR用作翻译的中间枢轴。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑