EditLord: Learning Code Transformation Rules for Code Editing

Weichen Li, Albert Jan, Baishakhi Ray, Chengzhi Mao, Junfeng Yang, Kexin Pei · DOI

Abstract

Code editing is a critical yet challenging task that requires balancing specific transformations—such as performance optimization or security hardening—with the preservation of original program logic. Traditional Large Language Model (LLM) approaches often treat editing as an implicit translation task, leading to issues with fragility, lack of interpretability, and poor generalization. This paper introduces EditLord, a framework that shifts code editing toward an explicit, rule-based process. By inducing a concise library of reusable transformation rules (meta-rules), EditLord achieves a 22.7% improvement in editing performance and a 58.1% boost in robustness compared to state-of-the-art baselines.

Introduction

In modern software engineering, code editing is a high-stakes balancing act. Whether optimizing for execution speed, improving readability through decompilation, or hardening code against security vulnerabilities, the primary constraint remains the same: the functional integrity of the program must be maintained.

Most current methodologies treat code editing as an end-to-end "black-box" generation task. While powerful, this implicit approach suffers from three primary flaws:

  1. Fragility: Models are often sensitive to semantics-preserving changes, such as variable renaming, which can cause the editing process to fail.
  2. Interpretability: It is difficult to ascertain the underlying logic behind a model's specific code modifications.
  3. Generalization: Models frequently struggle to apply learned patterns to code structures that are more complex or longer than those seen during training.

EditLord addresses these challenges by treating code editing as a compositional and discrete process, making the transformation steps explicit through an inductive learning framework.

The EditLord Framework

The core innovation of EditLord is the Inductive Meta-Rule Set. Rather than learning solely from raw code pairs, the system utilizes an LM as an inductive learner to extract high-level, natural language transformation rules. The workflow is divided into three distinct stages:

1. Data-Driven Inductive Rule Discovery

The system analyzes training pairs to generate raw editing rules (e.g., "replace std::cin with scanf for faster I/O"). These raw rules undergo an iterative refinement process—Adding, Merging, and Pruning—to distill a concise, non-redundant Meta-Rule Set that captures the most effective transformation patterns.

2. Functional Specification Discovery

To ensure functional correctness, EditLord generates a high-level natural language description of the code's original intent for every pair. This "functional specification" serves as a semantic anchor, preventing the model from altering the program's core logic during the editing phase.

3. Rule-Based Editing

During inference, the model follows a "plan-then-execute" strategy. It first identifies the relevant meta-rules and functional specifications before generating the edited code. This structured approach can be applied via fine-tuning, zero-shot prompting, or iterative refinement cycles involving execution feedback.

Experimental Evaluation

The researchers evaluated EditLord across three diverse domains to test its versatility:

  • Performance Optimization: Using the HQ (High Quality) dataset of C++ code pairs, evaluated via the gem5 CPU simulator.
  • Decompilation and Readability: Utilizing HumanEval-Decompile, which tasks the model with transforming machine-generated Ghidra output into human-readable source code.
  • Security Hardening: Using SVEN for training and CWEval (covering 31 Common Weakness Enumerations) to ensure security fixes do not break existing functionality.

Key Insights

EditLord’s success stems from several architectural advantages:

  • Actionable Guidance: By requiring the model to articulate a plan (the rules) before execution, EditLord significantly reduces hallucinations.
  • Robustness through Abstraction: Learning abstract rules (e.g., "optimize nested loops") rather than memorizing specific code snippets makes the model resilient to "noise" like variable renaming.
  • Compositionality: The meta-rules are modular. A single complex edit can be broken down into multiple applied rules, such as combining I/O efficiency with algorithmic optimization.
  • Human-in-the-Loop Potential: Because rules are expressed in natural language, human experts can refine the Meta-Rule Set. The study found that human-augmented rules can improve performance by up to 35.5%.

Results and Impact

EditLord consistently outperforms standard fine-tuning and Chain-of-Thought (CoT) baselines across multiple metrics:

  • Performance: An average 22.7% improvement in editing tasks across models like DeepSeek-Coder and GPT-4o mini.
  • Robustness: A 58.1% improvement in maintaining performance when faced with semantics-preserving input transformations.
  • Correctness: A 20.2% increase in functional correctness for security and engineering applications.
  • Efficiency: In performance tasks, EditLord-edited code achieved significant speedups (up to 5x in certain C++ cases) while maintaining 100% functional correctness, whereas baselines often introduced bugs to achieve speed.

Limitations and Future Work

While EditLord represents a significant step forward, several areas for improvement remain. Current execution feedback is relatively coarse (e.g., binary pass/fail); integrating fine-grained, per-statement execution profiles could further enhance refinement. Additionally, while EditLord treats functional correctness as a soft constraint, future iterations could incorporate formal verification methods to provide hard guarantees. Finally, the initial discovery of the meta-rule set is computationally intensive, requiring multiple LM calls during the setup phase.

Conclusion

EditLord demonstrates that making the "how" and "why" of code transformation explicit allows Language Models to perform more robust, interpretable, and effective code editing. By bridging the gap between black-box generation and rule-based logic, EditLord provides a scalable framework for high-integrity software evolution.

Summary updated

BibTeX
@inproceedings{li2025editlord,
  author = {Weichen Li and Albert Jan and Baishakhi Ray and Chengzhi Mao and Junfeng Yang and Kexin Pei},
  title = {{EditLord: Learning Code Transformation Rules for Code Editing}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=CxQPqcxRnx},
}
Figure