Automated Code Optimization with E-Graphs

Alessandro Cheli, Christopher RackauckasarXiv 2021

Abstract: This thesis proposes an advanced, generic and high-level code rewriting and analysis system in the Julia programming language, providing applied equality saturation in the presence of multiple dispatch and metaprogramming. We show how our system can practically solve some challenging problems: Can programmers implement their own high-level compiler optimizations for their domain-specific scientific programs, without the requirement of them being compiler experts at all? Can these optimizers be implemented by users in the same language and inside the same programs they want to optimize, solving the two-language problem? Can these compiler optimizers be written in a high-level fashion, as equations, without the need to worry about the rewriting ordering? Thus, can symbolic mathematics do high-level compiler optimizations or vice-versa?

Introduction

In the realm of high-performance scientific computing, a persistent "two-language problem" exists. While researchers often design models using high-level symbolic mathematics, translating those models into efficient machine code typically requires deep expertise in low-level compiler internals, often involving a shift from high-level languages like Python or Julia to low-level languages like C++.

Traditional term-rewriting systems used in compilers are "destructive." When a rule such as $a + b \to b + a$ is applied, the original expression is replaced. This approach makes optimization highly sensitive to the order of rule application and susceptible to infinite loops. This research introduces Metatheory.jl, a general-purpose algebraic rewriting framework for Julia that leverages e-graphs and equality saturation to bridge the gap between human-readable mathematics and machine-efficient execution.

Methodology: E-Graphs and Equality Saturation

The core of this work centers on two sophisticated computer science concepts adapted for the Julia ecosystem:

  1. E-Graphs (Equality Graphs): Unlike traditional trees, an e-graph is a data structure that compactly represents a large set of equivalent expressions. By sharing nodes between equivalent forms, it can store a massive space of mathematical possibilities simultaneously without picking a single "correct" version prematurely.
  2. Equality Saturation: This algorithm iteratively applies rewrite rules to an e-graph. Instead of replacing terms, it adds new equivalent nodes until no further information can be discovered (saturation). Finally, a cost function is applied to extract the most efficient version of the expression from the graph.

The Julia Advantage

While these techniques were popularized by the egg library in Rust, Metatheory.jl introduces several innovations by leveraging Julia’s unique features:

  • Homoiconicity: Since Julia code can be treated as data, the framework can manipulate expressions directly in the host language.
  • Multiple Dispatch: Rewrite rules in Metatheory.jl can include complex logic and type assertions. This allows the system to apply different optimizations based on whether a variable is a Real, an Integer, or a custom symbolic type at runtime.
  • Modularity: The introduction of TermInterface.jl provides a common "glue" layer, allowing disparate symbolic packages to share the same high-performance rewriting engine.

Experimental Evaluation

The framework was evaluated across three distinct domains to test its versatility and performance impact:

1. Robot Dynamics

The system was used to optimize a symbolic mass matrix for a KUKA IIWA 14 robotic arm (7 degrees of freedom). By symbolically pruning terms that were effectively zero within a specific tolerance, the framework achieved a 8.47x speedup in numerical evaluations.

2. Systems Biology

In a large-scale model of B Cell Antigen Signaling involving 1122 ODEs, Metatheory.jl was applied to optimize the underlying equations. The resulting code effectively halved the execution time required to solve these highly stiff differential equations.

3. Compiler Instruction Reduction

To test general-purpose compiler optimization, 100 fuzzed symbolic expressions (AST depth of 12) were processed. With standard compiler optimizations disabled, the framework reduced the average number of LLVM IR instructions from 7228 to 4983 (a ~31% reduction). This demonstrates that e-graphs can catch high-level redundancies that traditional, lower-level compilers often miss.

Discussion and Future Work

Metatheory.jl demonstrates that equational reasoning—where users define what is equal rather than how to transform code—is a powerful paradigm for domain-specific optimization. However, several challenges remain:

  • Scalability: Pattern matching in e-graphs (e-matching) is NP-hard. Future iterations may move toward "Relational E-Matching," treating e-graphs as relational databases to improve throughput.
  • Explainability: While the system finds the optimal expression, it does not currently provide a human-readable "proof" of the steps taken. Developing proof-production capabilities is a priority for future research.
  • Search Guidance: To prevent combinatorial explosions in complex graphs, the author proposes using machine learning (such as reinforcement learning) to guide rule application schedules.

By decoupling the rewriting engine from specific expression types, this work transforms a previously monolithic symbolic ecosystem into a modular, high-performance framework, empowering scientists to write their own compiler optimizations without becoming compiler experts.

Summary updated

BibTeX
@misc{cheli2021automated,
  author = {Alessandro Cheli and Christopher Rackauckas},
  title = {{Automated Code Optimization with E-Graphs}},
  year = {2021},
  eprint = {2112.14714},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2112.14714},
}
Figure