AIResearch AIResearch
Back to articles
Science

AI Optimizes Loops Without Breaking Programs

A new method combines program analysis and rewriting to safely improve cyclic code, outperforming traditional compilers on tricky examples.

AI Research
March 26, 2026
3 min read
AI Optimizes Loops Without Breaking Programs

Compilers face a persistent : optimizing programs with loops without introducing errors or missing opportunities. Traditional approaches often struggle with the order of transformations, a problem known as the phase ordering issue, where each optimization pass can destroy information needed by the next. Equality saturation offers an alternative by using a data structure called an e-graph to non-destructively track all intermediate programs and equivalences, allowing rewrites to be applied without a fixed sequence. However, existing e-graph analyses have been pessimistic, meaning they are too conservative when analyzing cyclic programs like those with loops, often returning imprecise that limit optimization potential.

Researchers from the University of California, Berkeley, have developed a that integrates optimistic analyses into equality saturation for the first time, enabling precise analysis of cyclic programs. They identified that current e-class analysis, used in systems like egg and egglog, is inherently inductive and cannot handle cycles effectively, leading to imprecise . Their approach uses an abstract interpretation algorithm that can soundly and precisely analyze cycles during equality saturation, addressing a core issue where ill-formed cycles created by rewrites could poison analysis . This unified algorithm allows for optimistic analysis and non-destructive rewriting to work together, improving optimization capabilities.

Ology involves embedding a single static assignment (SSA) form program representation into an e-graph, with a novel semantic formulation that supports abstract interpretation. The researchers proposed a dataflow analysis algorithm that computes an abstraction precise for well-formed cycles—those containing control flow loops—while remaining sound in the presence of ill-formed cycles, which arise from rewrites equating terms to their sub-terms. They built a prototype in Rust that combines equality saturation and optimistic analyses, using a sea-of-nodes style representation and implementing analyses like interval and offset abstractions. The algorithm alternates between phases of abstract interpretation and rewriting, ensuring only sound analysis trigger rewrites, thus maintaining correctness throughout the process.

In , the prototype demonstrated superior performance on example programs that require both rewriting and optimistic analysis for full optimization. For instance, in a program with a loop where variables undergo arithmetic operations, the tool could discover that a returned value equals 49, as shown in Figure 6, by using optimistic analysis to determine variable values and rewrites to simplify expressions. Another example, in Figure 7, showed the tool proving a return value of 0 by merging well-formed cycles soundly, a capability that eludes traditional compilers. The researchers reported that production compilers like clang and gcc could not fully optimize these examples, while their tool succeeded, highlighting the practical benefits of their approach.

Of this work are significant for compiler design and program optimization, particularly in areas like high-performance computing and automated code improvement. By enabling precise analysis of loops within an equality saturation framework, this can lead to more aggressive and correct optimizations in compilers, potentially improving software performance and reliability. It also addresses a known problem in e-graph literature: soundly de-duplicating cycles without merging ill-formed ones, which could enhance tools in domains such as circuit synthesis and tensor algebra. The research opens avenues for integrating flow-sensitive analyses and contextual e-graphs, further expanding the scope of optimizations.

Limitations include the current focus on flow-insensitive analyses for rewrite conditions, as flow-sensitive abstractions require more complex contextual e-graphs that are an area for future work. The prototype, while effective on simple examples, needs evaluation on larger, real-world programs to assess scalability and performance. Additionally, the algorithm assumes well-formed SSA programs as input, and handling arbitrary code may require extensions. The paper notes that implementing contextual e-graphs efficiently remains an open research question, which could limit immediate practical adoption in some settings.

Original Source

Read the complete research paper

View on arXiv

About the Author

Guilherme A.

Guilherme A.

Former dentist (MD) from Brazil, 41 years old, husband, and AI enthusiast. In 2020, he transitioned from a decade-long career in dentistry to pursue his passion for technology, entrepreneurship, and helping others grow.

Connect on LinkedIn