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

Don't Transform the Code, Code the Transforms: Towards Precise Code Rewriting using LLMs

Chris Cummins, Volker Seeker, Jordi Armengol-Estapé, Aram H. Markosyan, Gabriel Synnaeve, Hugh LeatherarXiv 2024 · DOI

Introduction

Instead of asking Large Language Models (LLMs) to rewrite code directly—a process that is often opaque and error-prone—this research proposes asking them to write the transformation logic itself. By using a chain-of-thought approach to synthesize Python AST (Abstract Syntax Tree) rewrite scripts, the authors achieved significantly higher precision compared to direct rewriting. This approach creates tools that are easier to debug, faster to run at scale, and more reliable.

LLMs are increasingly used for code refactoring and optimization, but they suffer from two major flaws: they are "black boxes" with no correctness guarantees, and they are computationally expensive to run on large codebases. Traditional rule-based tools (like compilers) are fast and correct but incredibly difficult for humans to write. This paper seeks a middle ground: using the LLM’s reasoning capabilities to generate the "rules" (the transformation code) which can then be verified once and applied to millions of lines of code instantly.

Methods: The CTT Pipeline

The authors introduce an approach called Code the Transform (CTT). Instead of a single prompt, they use a multi-step "chain-of-thought" pipeline designed to iterate toward a correct solution:

  1. Natural Language Description: The LLM is shown a few input/output examples and asked to describe the transformation in plain English.
  2. Implementation: Using that description, the LLM generates a Python script using the ast (Abstract Syntax Tree) module.
  3. Sandboxed Testing: The generated script is run against test cases to verify its behavior.
  4. Failure Analysis & Loopback: If the script fails, the LLM is given the error and asked to explain why it failed before attempting a fix.
  5. Iteration: This loop continues until the transformation works perfectly on all provided examples.
The iterative pipeline for generating transformation scripts through description, implementation, and failure analysis.

Datasets and Evaluation

The researchers assembled a benchmark of 480 input/output Python program pairs covering 16 types of transformations. These categories include arithmetic simplification, boolean logic (e.g., De Morgan’s law), dead code removal, and loop unrolling. The programs were adapted from HumanEval solutions, averaging about 11 lines of code each.

Key Results and Insights

The CTT approach significantly outperformed the baseline Transform the Code (TTC) method, where the LLM is asked to rewrite code directly.

  • Superior Precision: CTT achieved an overall F1 score of 0.97, compared to 0.75 for direct LLM rewriting. For 7 out of the 16 transformation types, the CTT-generated scripts were perfectly precise (1.00 precision).
  • Scalability: Once a script is generated, applying it to new code takes milliseconds of CPU time, avoiding the expensive GPU inference required for every file in the TTC approach.
  • Model Agnostic Quality: While smaller models (like Llama 3.1 8B) required more inference steps to reach a solution, the quality of the final transform they produced was nearly identical to that of the 405B model.
Comparison of pass rates over inference steps and F1 scores across different model sizes for CTT vs. TTC.

Key Insights

  • The "Meta-Programming" Advantage: By generating a script rather than a rewrite, the LLM creates a "white box" that developers can audit.
  • Introspection Matters: The "Failure Analysis" step—where the model explains its own mistake—was vital for the efficiency of the synthesis process.
  • Precision over Recall: CTT is "precise but picky." It might miss some transformation opportunities, but when it does act, it is almost always correct.

Limitations and Future Work

The model still struggles with complex "Optimization" transforms, such as converting loops to high-performance library calls. Future directions include using Reinforcement Learning and bootstrapped fine-tuning to help models learn to write even more robust and complex transformation scripts.

Summary updated

BibTeX
@misc{cummins2024don,
  author = {Chris Cummins and Volker Seeker and Jordi Armengol-Estapé and Aram H. Markosyan and Gabriel Synnaeve and Hugh Leather},
  title = {{Don't Transform the Code, Code the Transforms: Towards Precise Code Rewriting using LLMs}},
  year = {2024},
  eprint = {2410.08806},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.08806},
}
Figure