Learning Reusable Program Transformations via LLM-Guided Rule Synthesis

Avaljot Singh, Dushyant Bharadwaj, Stefanos Baziotis, Kaushik Varadharajan, Charith MendisCOLM 2026

Optimizing data science code is a notoriously difficult task. While the Python library Pandas is the industry standard for data manipulation, it is often inefficient, and traditional compilers struggle to navigate the vast space of possible optimizations. Large Language Models (LLMs) offer a potential solution through their ability to "hallucinate" clever code rewrites, but using them directly on every script is slow, expensive, and prone to errors.

In their paper "Learning Reusable Program Transformations via LLM-Guided Rule Synthesis," researchers from UIUC introduce RuleFlow, a framework that captures the creative spark of LLMs and distills it into a high-performance, deterministic compiler.

TL;DR

RuleFlow uses LLMs to discover high-impact Pandas optimizations offline, then automatically converts these discoveries into generalized, reusable rewrite rules. By moving the LLM out of the deployment path, RuleFlow achieves state-of-the-art speedups (up to 4.3× over previous compilers) while eliminating the cost and unreliability of real-time AI inference.

What Problem Does This Paper Address?

Pandas programs are often the bottleneck in data science pipelines. Existing optimization attempts generally fall into two camps:

  1. System-based approaches (like Modin or Dask) provide alternative implementations of the Pandas API, but often struggle with the messy, real-world complexity of Jupyter notebooks.
  2. Compiler-based approaches (like DIAS) use fixed rewrite rules, but these rules are hand-crafted and limited in scope.

LLMs could optimize code on a per-program basis, but the authors found a "low yield" problem: only about 5.7% of LLM-proposed optimizations are both correct and actually faster. Relying on an LLM at runtime is therefore a gamble—it's expensive, non-deterministic, and often produces incorrect code. RuleFlow aims to bridge this gap by using LLMs for discovery but not for deployment.

Methods

RuleFlow operates in three distinct stages, effectively decoupling the "creative" discovery process from the "reliable" execution process.

Figure 2: RULEFLOW is a 3-stage optimization framework: (i) SNIPPETGEN discovers candi- date optimizations from code snippets, (ii) RULEGEN converts them into rewrite rules, and (iii) CODEGEN applies the rules to unseen programs. The framework leverages LLMs for optimization discovery and compiler techniques for efficient deployment.
  1. Discovery (SnippetGen): The system takes a corpus of real-world code and uses an LLM to propose multiple optimized variants. These are automatically tested for semantic equivalence (EquivCheck) and performance gains (OptCheck). An adversarial agent (FeedbackGen) tries to find edge cases to break the optimization, forcing the LLM to refine its work.
  2. The Bridge (RuleGen): This is the core innovation. A multi-agent system "lifts" a concrete code rewrite (e.g., "change this specific line") into a generalized rule (e.g., "change any instance of this pattern"). Four specialized agents handle variable generalization, AST (Abstract Syntax Tree) type resolution, rule construction, and the synthesis of runtime preconditions.
  3. Deployment (CodeGen): The resulting rules are stored in a library. When a new, unseen notebook arrives, a lightweight, deterministic compiler applies these rules using pattern matching. No LLM calls are made during this stage.

Datasets

The authors utilized two primary sources:

  • Learning Set: 158 real-world Pandas notebooks collected from Kaggle via KGTorrent, used to discover and synthesize the initial rule set.
  • Evaluation Set: PANDASBENCH, a challenging benchmark of 102 real-world Python notebooks. This benchmark is particularly difficult because it reflects the "in-the-wild" coding styles of data scientists rather than clean, synthetic examples.

Key Insights and Ideas

  • Low Yield, High Reuse: While most LLM optimizations fail, the successful ones represent recurring patterns. By distilling these into rules, the "discovery cost" of the LLM is amortized across thousands of future deployments.
  • Multi-Agent Superiority: The authors found that breaking the rule-generation task into four specialized sub-tasks (like AST resolution and precondition synthesis) was critical. A single-agent LLM approach only produced valid rules 18% of the time, whereas the multi-agent RuleGen achieved a 68% success rate.
  • Preconditions are Vital: Many optimizations are only safe under certain conditions (e.g., "this column must exist" or "the data must not contain NaNs"). RuleFlow automatically synthesizes these checks to ensure the optimized code remains correct.

Key Results

RuleFlow established a new State-of-the-Art (SOTA) on PANDASBENCH, outperforming both compiler-based and systems-based predecessors.

Table 1: Performance of RuleFlow against baselines.
  • Speedups: RuleFlow achieved a mean speedup of 1.54× over DIAS (the previous compiler SOTA) and a staggering 112.79× over Modin. In specific cases, it reached speedups of up to 1914×.
  • Rule Impact: Individual rules can be incredibly powerful. For example, a rule that replaces df.drop with df.pop for single columns achieved a maximum speedup of 115.6×.
  • Cost Efficiency: Because RuleFlow uses LLMs offline, it reaches a "break-even" point quickly. Compared to using GPT-4.1 to optimize every notebook, RuleFlow becomes cheaper after processing just 60 to 771 notebooks, depending on the model used.
Figure 5: Cumulative per-program optimization cost against notebook count.

Limitations and Future Work

  • Precondition Overhead: In some cases, the time taken to check if an optimization is safe (the precondition) can outweigh the speedup of the optimization itself. The authors suggest "cost-aware precondition synthesis" as a future research path.
  • Subtle Semantic Issues: Some rules failed because they didn't account for how Pandas handles NaN values differently than native Python, or they missed data-flow dependencies across different notebook cells.
  • Generalization: While focused on Pandas, the authors believe this "Discovery-Bridge-Deployment" pattern could be applied to other libraries like NumPy or even general Python optimization.

Summary updated

BibTeX
@inproceedings{singh2026learning,
  author = {Avaljot Singh and Dushyant Bharadwaj and Stefanos Baziotis and Kaushik Varadharajan and Charith Mendis},
  title = {{Learning Reusable Program Transformations via LLM-Guided Rule Synthesis}},
  booktitle = {COLM},
  year = {2026},
  url = {https://openreview.net/forum?id=Zmh7XeI4h2},
}
Figure