Introduction
Program optimization traditionally relies on rewrite systems to transform code into more efficient forms. However, existing methods face a significant "capability gap." Large Language Models (LLMs) excel at high-level "sketches" but lack formal rigor, often hallucinating rules or failing to guarantee equivalence. Conversely, Equality Saturation (ES) uses e-graphs to represent equivalent programs, ensuring correctness but suffering from "combinatorial explosion" in complex systems. In these cases, the optimizer often gets lost in a sea of low-level, trivial rewrites and fails to find the global optimum.
LGuess is a novel framework designed to bridge this gap by combining the high-level intuition of LLMs with the formal correctness of equality saturation. By using an LLM to guide the search through an e-graph, LGuess avoids the search space explosion while maintaining formal verification for every step.
Methodology: The LGuess Framework
LGuess treats the LLM as a high-level heuristic guide and the e-graph as a low-level execution engine. The system operates in a continuous loop consisting of several key phases:
- Saturation: The process begins with standard equality saturation for a limited number of iterations to build a local e-graph.
- Probabilistic Extraction: To overcome the LLM's context window limitations, LGuess maintains a probabilistic bigram model that scores different code structures within the e-graph.
- LLM Feedback: The system samples candidate programs and queries the LLM to determine which "looks" more promising.
- Model Refinement: LLM preferences are used to update the bigram model's weights, effectively distilling the LLM's intuition into the search process.
- Checkpointing: The most probable program is extracted as a new starting point for the next iteration.
This "Bigram Bridge" allows LGuess to generalize the LLM's advice across the entire e-graph, pruning up to 70% of the search space after only a few queries. It allows the system to query the LLM about specific choices rather than dumping raw, massive graph data into a prompt.
Experimental Evaluation
The researchers evaluated LGuess on the task of factorizing multi-variable polynomials, a classic "search nightmare" due to the massive number of redundant paths created by associative and commutative laws. The benchmark consisted of 320 tasks varying in polynomial degree (2 to 5) and number of variables (2 to 5).
Key Results
LGuess significantly outperformed both pure symbolic (DirectES) and pure neural (DirectLLM) approaches.
- LGuess: Solved 255 out of 320 tasks.
- DirectES: Solved 204 out of 320 tasks. Notably, on the most complex tasks (high degree and many variables), LGuess solved 30 instances compared to only 2 for DirectES.
- DirectLLM (GPT-4o): Solved only 3 out of 320 tasks, primarily due to its inability to maintain formal correctness and tendency to skip steps.
Conclusion and Future Work
LGuess demonstrates that LLMs can be highly effective heuristic guides even when they lack perfect mathematical precision. By bridging neural intuition with symbolic rigor, the framework achieves state-of-the-art results in complex program optimization tasks.
Future work will focus on addressing potential syntactic biases and expanding the framework to practical domains such as Computer-Aided Design (CAD) optimization and array programming. The authors also intend to investigate more sophisticated probabilistic models to capture deeper optimization patterns beyond simple bigrams.
Summary updated