Skip to main content
QUICK REVIEW

[Paper Review] Towards Translating Real-World Code with LLMs: A Study of Translating to Rust

Hasan Ferit Enişer, Hanliang Zhang|arXiv (Cornell University)|May 19, 2024
Natural Language Processing TechniquesComputer Science6 citations
TL;DR

This paper studies translating real-world C/Go code to Rust using Flourine, an end-to-end translation tool with cross-language differential fuzzing, across five LLMs and 408 benchmarks (total 8160 experiments), reporting translation success rates per model.

ABSTRACT

Large language models (LLMs) show promise in code translation - the task of translating code written in one programming language to another language - due to their ability to write code in most programming languages. However, LLM's effectiveness on translating real-world code remains largely unstudied. In this work, we perform the first substantial study on LLM-based translation to Rust by assessing the ability of five state-of-the-art LLMs, GPT4, Claude 3, Claude 2.1, Gemini Pro, and Mixtral. We conduct our study on code extracted from real-world open source projects. To enable our study, we develop FLOURINE, an end-to-end code translation tool that uses differential fuzzing to check if a Rust translation is I/O equivalent to the original source program, eliminating the need for pre-existing test cases. As part of our investigation, we assess both the LLM's ability to produce an initially successful translation, as well as their capacity to fix a previously generated buggy one. If the original and the translated programs are not I/O equivalent, we apply a set of automated feedback strategies, including feedback to the LLM with counterexamples. Our results show that the most successful LLM can translate 47% of our benchmarks, and also provides insights into next steps for improvements.

Motivation & Objective

  • Develop Flourine, an end-to-end Rust translation tool that validates translations without hand-written test cases.
  • Assess LLMs' ability to translate real-world code from C/Go to Rust and maintain behavior.
  • Investigate the effectiveness of feedback strategies and compilation-driven repair in achieving correct translations.
  • Provide a cross-language differential fuzzing framework to verify I/O equivalence between source and translated programs.
  • Open-source data, benchmarks, and results to enable reproducibility.

Proposed method

  • Flourine performs iterative translation: obtain a candidate Rust translation from an LLM, repair compilation errors, and verify I/O equivalence via a cross-language differential fuzzer.
  • The fuzzer uses JSON serialization to map program states between Go/C source and Rust, enabling input/output equivalence checks without pre-existing tests.
  • Feedback strategies are used to refine translations when the fuzzer finds counterexamples; strategies include restart, hinted restart, counterexample-guided repair, and conversational repair.
  • Benchmarks are extracted from seven real-world projects (C and Go) into compilable Rust-equivalent units with 1–25 functions and diverse domains.
  • Experiments total 8160 runs across 408 benchmarks and five LLMs (GPT-4-Turbo, Claude 2.1, Claude 3 Sonnet, Gemini Pro, Mixtral).
  • LLMs are prompted with zero-shot translation tasks, followed by a compilation repair step guided by Rust compiler errors.
Figure 8 : Success rate for each LLM on each benchmark. Averaged across all feedback strategies.
Figure 8 : Success rate for each LLM on each benchmark. Averaged across all feedback strategies.

Experimental results

Research questions

  • RQ1How accurately can LLMs translate real-world C/Go code to Rust across diverse benchmarks?
  • RQ2How do program size and complexity affect translation success rates?
  • RQ3Do feedback strategies improve translation success, and which strategies are most effective?
  • RQ4How do LLM translations compare to rule-based translation tools in quality and idiomaticity?
  • RQ5What are the main reasons translations fail, and how can failures be mitigated?

Key findings

  • LLMs achieved overall translation success rates of 47.7% (Claude 2), 43.9% (Claude 3), 21.0% (Mixtral), 36.9% (GPT-4-Turbo), and 33.8% (Gemini Pro).
  • Across 408 benchmarks, the total experiments were 8160, with 408 benchmarks and five LLMs, yielding the reported success rates.
  • The best-performing LLM translated up to 47% of benchmarks, with smaller benchmarks generally easier to translate than larger ones.
  • Feedback strategies contributed up to 8 percentage points in absolute translation success on average for the best LLMs, though counterexample-based prompts sometimes underperformed simple repeats.
  • Compared to rule-based tools like C2Rust, LLM translations were found to be more concise and idiomatic, though not uniformly perfect.
  • Limitations include heuristic nature of fuzzing (no formal equivalence guarantee) and serialization constraints affecting some data types.
Figure 9 : Success rate for each LLM on benchmarks grouped by lines of code.
Figure 9 : Success rate for each LLM on benchmarks grouped by lines of code.

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.