Skip to main content
QUICK REVIEW

[Paper Review] Code Translation with Compiler Representations

Marc Szafraniec, Baptiste Rozière|arXiv (Cornell University)|Jun 30, 2022
Adversarial Robustness in Machine LearningComputer Science18 citations
TL;DR

This paper proposes augmenting neural code translation with LLVM Intermediate Representation (IR) to improve semantic accuracy and reduce syntactic errors. By training sequence-to-sequence transformers on both source code and corresponding IR, the method achieves an 11% average relative improvement in translation quality across C++, Java, Rust, and Go, with up to 79% improvement on Java→Rust using greedy decoding.

ABSTRACT

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.

Motivation & Objective

  • Address the limitation of neural machine translation (NMT) in code translation, which often produces semantically incorrect but syntactically plausible code due to reliance on surface-level token sequences.
  • Overcome the shortcomings of rule-based transpilers that generate unidiomatic, hard-to-maintain code by leveraging semantic grounding via compiler IRs.
  • Improve zero-shot and low-resource code translation by using LLVM IR as a shared semantic representation across multiple programming languages.
  • Extend existing code translation benchmarks by adding 343 Go and 280 Rust functions with test cases to support evaluation.
  • Explore the use of IR as a pivot for translation and evaluate the performance of IR decompilation to source code using neural models.

Proposed method

  • Augment training data with LLVM IR representations generated from source code in C++, Java, Rust, and Go using the LLVM compiler toolchain.
  • Train a sequence-to-sequence Transformer model on paired source code and corresponding IR sequences to learn semantically aligned code representations.
  • Combine code and IR supervision during training to jointly optimize for both syntactic correctness and semantic fidelity in translation.
  • Design an IR-only pivot baseline that translates source code to IR and then decompiles the IR to target language, evaluating its performance against full code+IR models.
  • Train a separate decompiler model to generate source code from LLVM IR, achieving 78% accuracy on C++ decompilation.
  • Use the IR as a shared semantic space to align embeddings across different programming languages, improving cross-lingual transfer and translation quality.

Experimental results

Research questions

  • RQ1Can augmenting neural code translation with LLVM IR representations significantly improve translation accuracy and semantic correctness?
  • RQ2How does the inclusion of IR during training affect performance in low-resource translation scenarios (e.g., translating to or from Rust)?
  • RQ3To what extent can IR serve as a viable pivot language for code translation, especially when direct code-to-code translation is challenging?
  • RQ4Can neural models effectively learn to decompile LLVM IR into high-quality, idiomatic source code in multiple languages?
  • RQ5How does the proposed method compare to state-of-the-art unsupervised code translation models like TransCoder in terms of both accuracy and robustness?

Key findings

  • The proposed IR-augmented method improves the state of the art in unsupervised code translation by 11% on average across C++, Java, Rust, and Go.
  • For the Java→Rust translation pair, the method achieves a 79% improvement in correct translations using greedy decoding compared to TransCoder.
  • The model shows particularly strong gains in low-data regimes, with 26% relative improvement when translating to Rust and 19% when translating from it.
  • The IR-only pivot method, which translates via decompilation from IR, achieves 170% improvement over TransCoder, demonstrating the value of semantic grounding.
  • The decompiler model achieves 78% accuracy in reconstructing C++ source code from LLVM IR, indicating strong performance in the reverse direction.
  • The extended test set includes 343 Go and 280 Rust functions, significantly expanding the evaluation benchmark beyond prior work.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.