NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Code Translation with Compiler Representations

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

Translating code between programming languages—often called transpilation—is essential for migrating legacy systems or porting high-level logic to performance-critical languages. Traditional transpilers rely on handcrafted, rigid rules that often produce "unidiomatic" code that works but looks nothing like what a human would write.

Recent Neural Machine Translation (NMT) approaches, like TransCoder, treat code as text. While they produce more natural-looking results, they often fail on semantic details. For example, a model might confuse a signed integer with an unsigned one or misplace a parenthesis. These small textual errors lead to large functional failures because NMT models often lack a deep understanding of what the code actually does across different languages.

To address this, researchers from Meta AI have significantly improved neural code translation by augmenting training with compiler Intermediate Representations (IR). By teaching models to associate source code with its underlying LLVM IR, they grounded the translation process in semantics rather than just syntax.

Bridging the Semantic Gap with LLVM IR

The core of the authors' method is leveraging LLVM Intermediate Representation (IR) as a semantic bridge. IR is a low-level, language-agnostic "pseudocode" used by compilers. Because different languages (like C++ and Rust) often compile to very similar IR, it serves as a perfect "pivot" for understanding shared logic.

Compilation pipeline showing C code translated into LLVM IR

To make the IR an even better semantic anchor, the authors used size-optimized IR (-Oz). This optimization strips away language-specific "fluff" and makes the IR more uniform across different source languages.

The researchers extended the baseline TransCoder model with three new training objectives:

  1. Translation Language Modeling (TLM): The model is fed a concatenated sequence of source code and its corresponding IR, then tasked with predicting randomly masked tokens. This forces the model to align the embeddings of the source language and the IR.
Translation Language Modeling (TLM) objective masking both source code and IR
  1. Translation Auto-Encoding (TAE): A denoising objective where the model must reconstruct the original source and IR pair from a corrupted version.
  2. IR Generation (MT): The model learns to translate source code directly into IR, further strengthening its grasp of program semantics.

Crucially, IR is only required during training. At inference time, the model translates directly from source language A to source language B, meaning no compiler toolchain is needed to use the final model.

Datasets and Evaluation

The models were trained on functions extracted from 2.8 million GitHub repositories and the CodeNet dataset (14 million competitive programming solutions). To rigorously evaluate their approach, the authors significantly expanded existing benchmarks, adding 343 Go functions and 280 Rust functions complete with unit tests to verify "Computational Accuracy" (CA)—the gold standard for code translation.

Key Results and Insights

By grounding the model in the actual logic of the program, the IR-augmented approach yielded significant improvements:

  • Accuracy Boost: The method improved the state-of-the-art for unsupervised code translation by 11% on average using greedy decoding. When using beam search (size 5), the model achieved a 5.5% average improvement, reaching a 47.8% average accuracy across all tested language pairs.
  • Massive Gains for Low-Resource Languages: The approach is particularly powerful for languages with less training data. For instance, the Java to Rust translation pair saw a massive 79% improvement in correct translations compared to the baseline.
  • Improved Type Safety: One of the most common NMT failures is incorrect type mapping (e.g., translating a signed int to an unsigned usize). IR representations make types explicit, which helped reduce type-related errors by 49%.
  • Decompilation: The model also proved highly effective at "decompilation" (generating source code from IR). It achieved 78% accuracy when generating C++ from LLVM IR, significantly outperforming rule-based tools like RetDec.

Limitations and Future Work

While promising, the approach has a few notable limitations. A major bottleneck is that only a fraction of open-source code can be successfully compiled into IR to create training data. For example, only 3.3% of the Rust functions in their dataset compiled successfully due to missing dependencies, limiting the amount of parallel Source-IR data available.

Additionally, the current work operates strictly at the function level. Translating entire projects or classes remains a challenge due to long-range dependencies. Finally, the method currently requires languages with an LLVM front-end. While this covers many popular languages (C++, Rust, Go, Swift), it excludes others like Python, though the authors suggest that interpreter byte-code could serve a similar purpose in future research.

Summary updated

BibTeX
@inproceedings{szafraniec2023code,
  author = {Marc Szafraniec and Baptiste Roziere and Hugh James Leather and Patrick Labatut and Francois Charton and Gabriel Synnaeve},
  title = {{Code Translation with Compiler Representations}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=XomEU3eNeSQ},
}
Figure