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