Understanding the Effectiveness of Large Language Models in Code Translation

Rangeet Pan, Ali Reza Ibrahimzada, Rahul Krishna, Divya Sankar, Lambert Pouguem Wassi, Michele Merler, Boris Sobolev, Raju Pavuluri, Saurabh Sinha, Reyhaneh Jabbarvand

TL;DR

Large Language Models (LLMs) show promise for automating code translation, but a large-scale study reveals they are not yet reliable for this task, with success rates as low as 2.1%. The research identifies 15 categories of translation bugs and proposes a prompt-crafting technique that improves performance by 5.5%, suggesting context is key for better LLM-driven code translation.

What Problem Does This Paper Address?

Code translation, or transpilation, is the process of converting source code from one programming language (PL) to another while preserving its original functionality. This is crucial for tasks like modernizing legacy software, migrating to cloud-native applications, and facilitating better code synthesis. While LLMs have demonstrated impressive capabilities in code generation, their effectiveness and limitations in code translation remain largely unexplored. This paper aims to bridge this gap by conducting a comprehensive empirical study to understand how well LLMs perform this complex task, what kinds of errors they make, and how their performance compares to traditional methods.

Methods

The researchers conducted a large-scale empirical study involving seven different LLMs (including GPT-4, StarCoder, and Llama 2) to translate code across five programming languages: C, C++, Go, Java, and Python. They used a "vanilla prompting" approach, providing basic instructions, source language, target language, and the code itself. To evaluate the translations, they focused on whether the generated code could compile, run, and pass existing test cases. For unsuccessful translations, they performed a detailed manual investigation to categorize the bugs, leading to the development of a 15-category taxonomy of translation bugs. They also explored a prompt-crafting technique to improve LLM performance by providing more context about previous errors.

Datasets

The study utilized a diverse set of code samples from:

  • CodeNet: A large-scale dataset for AI for code tasks.
  • AVATAR: A parallel corpus for Java-Python program translation.
  • EvalPlus: A benchmark for evaluating code generation models.
  • Apache Commons CLI: A real-world Java project.
  • Python Click: A real-world Python project.

In total, 1,700 code samples were used, covering 31 translation pairs across the five programming languages.

Key Insights and Ideas

  • LLMs are not yet reliable for automated code translation: Despite their advancements, LLMs struggle significantly with code translation, especially for real-world projects.
  • Syntax and semantics are major hurdles: The majority of translation failures stem from LLMs having difficulty understanding code syntax and preserving semantic meaning.
  • Context is king: Providing LLMs with more information about previous errors and the desired outcome dramatically improves translation success.
  • Bug taxonomy is crucial: A structured understanding of the types of errors LLMs introduce is essential for improving their performance.
  • LLMs and traditional tools have complementary strengths: LLMs excel at generating idiomatic code, while traditional transpilers offer broader context and determinism.

Key Results

  • Low success rates: The success rate for code translation across studied LLMs ranged from a mere 2.1% to 47.3%. For real-world projects, the success rates were even lower, with GPT-4 achieving only 8.1% and other models achieving 0%.
  • Compilation errors dominate: A significant majority (77.8%) of unsuccessful translations resulted in compilation errors, indicating LLMs' struggles with code syntax.
  • 15 categories of bugs: Manual analysis identified 15 distinct categories of translation bugs, including issues with data types, input parsing, API mismatches, and removal or inclusion of logic.
  • Prompt crafting improves performance: The proposed iterative prompt-crafting approach, which includes error details and context, improved translation success rates by an average of 5.5%, with GPT-4 seeing a 12% improvement.
  • Comparison with non-LLM approaches: While LLMs like GPT-4 produced more idiomatic code, traditional transpilers like C2Rust achieved higher success rates (95% for C to Rust) but generated less idiomatic and potentially unsafe code.

Limitations and Future Work

The authors acknowledge several limitations:

  • External Validity: The results might not generalize perfectly due to the specific PLs, LLMs, and datasets chosen.
  • Internal Validity: LLMs are non-deterministic, so single translations might not fully represent their capabilities. The lack of formal inter-rater reliability for bug labeling is also noted.
  • Test Suite Rigor: The quality of test suites in datasets can influence the perceived correctness of translations.

Future work suggested includes:

  • Combining LLMs with program analysis: Integrating program analysis techniques could provide LLMs with better context.
  • Iterative and decomposed translation: Developing methods for LLMs to build upon previous translations or break down complex projects into smaller, manageable parts.
  • Fine-tuning open-source LLMs: Tailoring open-source models to specific aspects of code translation, potentially addressing different bug categories.
  • Leveraging language-specific features: Further research into how LLMs handle complex language features like method overloading and inheritance.

Summary updated

Abstract

Code translation aims to convert source code from one programming language (PL) to another. Given the promising abilities of large language models (LLMs) in code synthesis, researchers are exploring their potential to automate code translation. The prerequisite for advancing the state of LLM-based code translation is to understand their promises and limitations over existing techniques. To that end, we present a large-scale empirical study to investigate the ability of general LLMs and code LLMs for code translation across pairs of different languages, including C, C++, Go, Java, and Python. Our study, which involves the translation of 1,700 code samples from three benchmarks and two real-world projects, reveals that LLMs are yet to be reliably used to automate code translation -- with correct translations ranging from 2.1% to 47.3% for the studied LLMs. Further manual investigation of unsuccessful translations identifies 15 categories of translation bugs. We also compare LLM-based code translation with traditional non-LLM-based approaches. Our analysis shows that these two classes of techniques have their own strengths and weaknesses. Finally, insights from our study suggest that providing more context to LLMs during translation can help them produce better results. To that end, we propose a prompt-crafting approach based on the symptoms of erroneous translations; this improves the performance of LLM-based code translation by 5.5% on average. Our study is the first of its kind, in terms of scale and breadth, that provides insights into the current limitations of LLMs in code translation and opportunities for improving them. Our dataset -- consisting of 1,700 code samples in five PLs with 10K+ tests, 43K+ translated code, 1,748 manually labeled bugs, and 1,365 bug-fix pairs -- can help drive research in this area.
BibTeX
@inproceedings{pan2024understanding,
  author = {Rangeet Pan and Ali Reza Ibrahimzada and Rahul Krishna and Divya Sankar and Lambert Pouguem Wassi and Michele Merler and Boris Sobolev and Raju Pavuluri and Saurabh Sinha and Reyhaneh Jabbarvand},
  title = {{Understanding the Effectiveness of Large Language Models in Code Translation}},
  booktitle = {ICSE},
  year = {2024},
  url = {https://openreview.net/forum?id=5v70KONRXy},
}
Figure