Algorithm Search⌘K

Techniqueresearch

Equality saturation

Also called e-graph rewriting.

Applies all rewrite rules to an e-graph without choosing, until no rule adds anything, and then extracts the cheapest equivalent program.

Equality saturation is a way to optimize a program with rewrite rules without choosing which rule to apply first. A normal rewriter replaces a term by another, so each step loses the old term, and the order of the steps decides the result. This is the phase ordering problem. Equality saturation keeps every version. It puts the program in an e-graph, finds every match of every rule by E-matching, and adds each right-hand side to the class of the term it matched, so the class now holds both. It repeats this until no rule adds anything, which is saturation, and then extracts the cheapest term of the root class under a cost model. Tate, Stepp, Tatlock and Lerner introduced it in 2009, and the egg library of Willsey and others made it fast and common. When it stops, the e-graph holds every program that the rules can reach, so the result does not depend on rule order. The common mistake is to think that it always stops. Many natural rules make the e-graph grow without end, and real tools stop at a limit on iterations, e-nodes or time.

In plain words

Think of a rewriter that may only keep one version of a formula. Each change throws away the old version, and an early change can block a better one later. Equality saturation keeps a shared record of all versions that are known to be equal. Each rule only adds a new version beside the old one. It never removes anything.

We apply every rule everywhere, again and again. If a round adds nothing new, the record holds every version the rules can reach, and we pick the cheapest one. Because nothing was thrown away, the order of the rules does not matter.

The record is compact: a part that two versions share is stored once. Still, some rule sets make it grow forever, so in practice we also stop after a fixed number of rounds or a size limit, and pick the cheapest version found so far.

When it applies

  • The program, or a part of it, is a term or a DAG of pure operations. A block of three-address code without side effects becomes a DAG by naming each value once. Loops and effects need an extended form, such as the program expression graphs of Tate and others.
  • The optimizations are equations: rules l→rl \to r where lθl\theta and rθr\theta have the same value for every substitution θ\theta, possibly under a side condition.
  • The order of the rules matters for a normal rewriter, or the rules include ones that make a term worse before it gets better, such as reassociation.
  • A cost model gives a cost to each operation, and the best program is the one of least cost.

The template

Rules. A rule l→rl \to r is a pair of patterns, terms with variables, where every variable of rr occurs in ll. A step s→Rs′s \to_R s' replaces one subterm lθl\theta of ss by rθr\theta. Let =R=_R be the least congruence that contains every pair (lθ,rθ)(l\theta, r\theta); equivalently, s=Rs′s =_R s' when a chain of steps, each one forward or backward, leads from ss to s′s'. A rule is sound if lθl\theta and rθr\theta have the same value for every θ\theta. If every rule is sound, s=Rs′s =_R s' implies that ss and s′s' have the same value, since equality of values is a congruence.

Represented terms. A class represents a term as on the e-graph page: f(t1,…,tk)f(t_1, \ldots, t_k) is represented by xx if xx has an e-node f(c1,…,ck)f(c_1, \ldots, c_k) and each tit_i is represented by cic_i. In a clean state, a term is represented by at most one class, by the congruence invariant.

The loop.

G ← an e-graph with the input term t; root ← the class of t
repeat
    M ← every (l → r, x, σ) such that class x matches l with σ    ▷ on a clean G
    for each (l → r, x, σ) in M: y ← ADD(r σ); MERGE(x, y)
    REBUILD
until no step in this round added an e-node or merged two classes,
      or a limit on rounds, e-nodes or time is reached
return EXTRACT(root)

Here σ\sigma maps each variable of ll to a class, and rσr\sigma is added bottom up. All matches of a round are found before any is applied, so the round does not depend on the order of the rules. EXTRACT is the least tree cost fixpoint of the e-graph page.

Legality. Theorem 1. At every point of the loop, two terms represented by the same class are equal under =R=_R. So if the rules are sound, every term that the root class represents, and in particular the extracted one, has the value of the input. Proof. In a clean state, the relation "represented by the same class" is contained in the congruence closure of the pairs that were merged, by the invariants on the e-graph page. Before any merge, each class represents one term. So it is enough that each merged pair is made of =R=_R-equal terms, by induction on the merges, since =R=_R is a congruence and contains the closure of equal pairs. ADD merges nothing. A merge of the loop joins xx and yy, where xx matches ll with σ\sigma and yy is the class of rσr\sigma. Choose for each variable vv a term θ(v)\theta(v) that σ(v)\sigma(v) represents; every class represents at least one term, since its first e-node was built from existing classes. Then xx represents lθl\theta, yy represents rθr\theta, and lθ=Rrθl\theta =_R r\theta. By induction, every term of xx is =R=_R-equal to lθl\theta, and every term of yy to rθr\theta, so all are =R=_R-equal. □\square

Profit. The input term stays represented by the root class, since the loop never removes an e-node. So the extracted term has a tree cost at most that of the input. With an exact extractor it has the least cost of all the terms the e-graph represents.

Saturation. Theorem 2. Suppose a round adds no e-node and merges no classes. If a class xx represents ss and s→Rs′s \to_R s', then xx represents s′s'. So the root class represents every term that forward steps reach from the input. If RR has each rule in both directions, the root class represents exactly the terms s′s' with s′=Rts' =_R t.

Proof. (a) Matching finds the step. If xx represents lθl\theta, then xx matches ll with some σ\sigma such that σ(v)\sigma(v) represents θ(v)\theta(v) for each variable vv. By induction on ll: a variable binds to xx; for l=f(l1,…,lk)l = f(l_1, \ldots, l_k), the class xx has an e-node f(c1,…,ck)f(c_1, \ldots, c_k) with cic_i representing liθl_i\theta, and E-matching tries this e-node. A variable that occurs twice gets the same class both times, because a term is represented by only one class. (b) Instances. If σ(v)\sigma(v) represents θ(v)\theta(v) for each vv and the e-graph has every e-node of rσr\sigma, then the class of rσr\sigma represents rθr\theta, by induction on rr. (c) One step at the root. Let s=lθs = l\theta. By (a), the round found (l→r,x,σ)(l \to r, x, \sigma). It added nothing, so the e-graph had every e-node of rσr\sigma, and MERGE(x,y)(x, y) joined nothing, so y=xy = x. By (b), xx represents rθ=s′r\theta = s'. (d) A step below the root. Let s=f(s1,…,sk)s = f(s_1, \ldots, s_k) with the step in sis_i. The class xx has an e-node f(c1,…,ck)f(c_1, \ldots, c_k) with cjc_j representing sjs_j. By induction on the depth of the step, cic_i represents si′s'_i, so xx represents s′s'. (e) By induction on the number of steps, xx represents every term that forward steps reach. With both directions of each rule, any chain of steps is a chain of forward steps, so the root class contains the whole =R=_R-class of tt; Theorem 1 gives the other inclusion. □\square

So "saturated" means that the e-graph is closed under the rules, and the choice of the result is left to extraction. The result is the cheapest term reachable by the rules, up to the congruence that the e-graph keeps, and it does not depend on any order of rule application. Saturation can fail to happen; Example 3 below gives a rule set that never saturates.

Extraction with sharing. The tree cost counts a shared subterm once for each use. The DAG cost counts each chosen e-node once. An extraction chooses one e-node for each class that it needs, starting at the root and going through the children of the chosen e-nodes, with no cycle. Its DAG cost is the sum of the costs of the chosen e-nodes.

Theorem 3. To decide whether an e-graph has an extraction of DAG cost at most KK is NP-complete. Proof. It is in NP: an extraction is a certificate, and its cost and the absence of a cycle are checked in polynomial time. For hardness, reduce set cover: a universe u1,…,unu_1, \ldots, u_n, sets S1,…,SmS_1, \ldots, S_m and a bound kk. Build an e-graph with:

  • a class XjX_j for each set, with one leaf e-node sjs_j of cost 1;
  • a class EiE_i for each element, with one e-node gi(Xj)g_i(X_j) of cost 0 for each jj such that ui∈Sju_i \in S_j;
  • a root class with one e-node root(E1,…,En)\mathit{root}(E_1, \ldots, E_n) of cost 0.

The symbols gig_i differ for different ii, so no two e-nodes are congruent, and the e-graph is clean. It has O(nm)O(nm) e-nodes. An extraction chooses for each EiE_i one gi(Xj)g_i(X_j) with ui∈Sju_i \in S_j, and then it must choose sjs_j in each class XjX_j that it reaches. Its DAG cost is the number of distinct jj chosen, and those sets cover every uiu_i. Conversely, a cover by Sj1,…,SjcS_{j_1}, \ldots, S_{j_c} gives an extraction of cost cc: for each uiu_i, choose gi(Xj)g_i(X_j) for some jj in the cover with ui∈Sju_i \in S_j. So an extraction of DAG cost at most kk exists if and only if a cover of size at most kk exists. Set cover is NP-complete. □\square

So exact DAG extraction is done by 0-1 integer programming or a pseudo-Boolean solver, as in Tate and others, or by a heuristic. The tree cost fixpoint is exact for tree cost and runs in polynomial time.

Classic examples

The numbers come from a short Python script with an e-graph, E-matching by search over all classes, and the loop above. Each count is the number of classes and of e-nodes after the round.

Division by a power of two. The input is (a⋅2)/2(a \cdot 2) / 2, read as exact arithmetic on rationals, with the rules

name rule
shl x⋅2→x≪1x \cdot 2 \to x \ll 1
reassoc (x⋅y)/z→x⋅(y/z)(x \cdot y) / z \to x \cdot (y / z)
div-self x/x→1x / x \to 1
mul-one x⋅1→xx \cdot 1 \to x

A rewriter that applies shl first, as a strength reduction pass would, gets (a≪1)/2(a \ll 1) / 2, where no rule applies any more. Equality saturation gives:

round matches classes e-nodes what happened
start 4 4 aa, 22, a⋅2a \cdot 2, (a⋅2)/2(a \cdot 2) / 2
1 2 6 8 shl adds a≪1a \ll 1; reassoc adds 2/22 / 2 and a⋅(2/2)a \cdot (2 / 2)
2 3 5 8 div-self merges 2/22 / 2 with 11
3 4 4 8 mul-one matches a⋅(2/2)a \cdot (2 / 2) and merges the root with aa
4 4 4 8 nothing new: saturated

At the end the four classes are {a,(a⋅2)/2,a⋅1}\{a, (a \cdot 2)/2, a \cdot 1\}, {2}\{2\}, {1,2/2}\{1, 2/2\} and {a⋅2,a≪1}\{a \cdot 2, a \ll 1\}. The root class has e-nodes that point back to it, so it represents infinitely many terms, such as a⋅1⋅1a \cdot 1 \cdot 1. With costs 8 for //, 4 for ⋅\cdot, 1 for ≪\ll and 1 for a leaf, the input costs 15, and extraction returns aa with cost 1. The rule div-self needs x≠0x \ne 0 to be sound; here it matches only 2/22 / 2. The rule reassoc is sound for rationals but not for integer division: (3⋅2)/4=1(3 \cdot 2) / 4 = 1 but 3⋅(2/4)=03 \cdot (2 / 4) = 0.

Associativity and commutativity. The rules x+y→y+xx + y \to y + x, x+(y+z)→(x+y)+zx + (y + z) \to (x + y) + z and (x+y)+z→x+(y+z)(x + y) + z \to x + (y + z), from a1+(a2+(⋯+an))a_1 + (a_2 + (\cdots + a_n)):

nn rounds classes e-nodes
2 2 3 4
3 4 7 15
4 5 15 54
5 6 31 185
6 6 63 608

The rounds count the last round, which adds nothing. Each class is the sum of a nonempty subset of the aia_i, so there are 2n−12^n - 1 classes. A class of a subset SS with ∣S∣≥2|S| \ge 2 has one e-node X+YX + Y for each ordered split of SS into two nonempty parts, 2∣S∣−22^{|S|} - 2 of them, and the sum over all subsets, with the nn leaves, is 3n−2n+1+1+n3^n - 2^{n+1} + 1 + n. The e-graph saturates, but its size is exponential in nn. It is still much smaller than the set of terms it represents, which holds every bracketing and every order of the nn operands.

A rule set that never saturates. The rule x/y→(x⋅2)/(y⋅2)x / y \to (x \cdot 2) / (y \cdot 2) from a/ba / b: after round kk there are 4+2k4 + 2k classes and 4+3k4 + 3k e-nodes, for k=1k = 1 to 6, the limit in the script. Each round adds the class of b⋅2kb \cdot 2^{k}. These classes never merge: a term without // has no subterm that the rule matches, so it is =R=_R-equal only to itself, and by Theorem 1 two different such terms are in different classes. The loop runs until a limit stops it. In contrast, x→x+0x \to x + 0 from aa saturates after 3 rounds with 2 classes and 4 e-nodes: a+0a + 0 goes into the class of aa, and the next match of xx to that class adds the same e-node again.

Sharing in extraction. The set cover instance with U={1,2,3,4}U = \{1, 2, 3, 4\}, S1={1,2}S_1 = \{1, 2\}, S2={3,4}S_2 = \{3, 4\}, S3={1,3}S_3 = \{1, 3\}, S4={2,4}S_4 = \{2, 4\}, S5={1}S_5 = \{1\}, built as in Theorem 3, has 3⋅2⋅2⋅2=243 \cdot 2 \cdot 2 \cdot 2 = 24 extractions. Every one has tree cost 4, so the tree cost fixpoint cannot tell them apart. The least DAG cost is 2, with S1S_1 and S2S_2, which is the least cover.

Variations

  • E-class analysis. egg attaches a value from a semilattice to each class, such as a known constant, and joins the values at each merge. A class with a known constant gets the constant as an e-node, which does constant folding inside the e-graph.
  • Program expression graphs. Tate and others turn a whole function, with loops, into a graph of pure operators with special nodes for loop values, saturate it, and turn the extracted graph back into a program. Their Peggy optimizer works this way on Java bytecode.
  • Schedules and limits. egg stops on a limit of rounds, e-nodes or time, and can apply a rule less often when it matches too much, so that rules such as associativity do not fill the e-graph alone.
  • Extraction. Exact DAG extraction by integer programming or a pseudo-Boolean solver, as in Tate and others; the tree cost fixpoint of the e-graph page; or a greedy heuristic.
  • Superoptimization. Denali saturates an e-graph with axioms and asks a SAT solver for the fastest machine program that the e-graph holds, a goal-directed form of superoptimization.
  • Accuracy and validation. Herbie rewrites real-number expressions in an e-graph and extracts the most accurate floating point program. The same saturation can show that two programs are equal, which is translation validation by equality.

Pitfalls

  • It may not stop. As in the example above, a rule can make new classes in every round. Run with a limit on rounds, e-nodes and time, and accept that a stop by limit gives the best term found, not the best term the rules can reach.
  • Exponential growth. Associativity and commutativity saturate, but with 2n−12^n - 1 classes and about 3n3^n e-nodes for nn operands. Apply such rules with a schedule, or handle them with a special form of the e-graph.
  • Unsound rules. Theorem 1 only says that the result is =R=_R-equal to the input. A rule that is false, such as reassoc for integer division, or div-self without x≠0x \ne 0, makes the optimizer return a wrong program. Each rule needs its side condition and a proof, or an e-class analysis that checks it.
  • Tree cost and shared terms. The fixpoint counts a shared term once for each use. For (a+b)⋅(a+b)(a + b) \cdot (a + b) it pays for a+ba + b twice. If the code generator shares the value, the costs differ, and the exact DAG problem is NP-hard by Theorem 3.
  • A dirty e-graph. Matching between MERGE and REBUILD can miss matches. Find all matches of a round on a clean e-graph, as in the loop above.
  • The cost model. Extraction is as good as its costs. Costs per e-node cannot see the latency of a chain or the pressure on registers.

Exercises

  1. Run the loop by hand on (a⋅2)/2(a \cdot 2) / 2 with only shl, div-self and mul-one. Does it saturate, and what does it extract?
  2. Prove that the tree cost of the extracted term never increases from one round to the next, if extraction is run after every round.
  3. With the rules x+y→y+xx + y \to y + x only, how many classes and e-nodes does a1+(a2+a3)a_1 + (a_2 + a_3) have at saturation?
  4. Give a sound rule that adds a new class in every round, other than the one above, and prove that the classes never merge.
  5. In the reduction of Theorem 3, show that the e-graph has no cycle, and that the tree cost of every extraction is nn.

References

  1. Ross Tate, Michael Stepp, Zachary Tatlock and Sorin Lerner, Equality saturation: a new approach to optimization, Proceedings of the 36th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL 2009), 264-276, the phase ordering problem, the saturation of an e-graph by axioms, program expression graphs for loops, and the extraction of the best program by a pseudo-Boolean solver
  2. Ross Tate, Michael Stepp, Zachary Tatlock and Sorin Lerner, Equality saturation: a new approach to optimization, Logical Methods in Computer Science 7(1), 2011, the journal version, with the full translation of programs to and from program expression graphs and the evaluation in the Peggy optimizer
  3. Max Willsey, Chandrakana Nandi, Yisu Remy Wang, Oliver Flatt, Zachary Tatlock and Pavel Panchekha, egg: fast and extensible equality saturation, Proceedings of the ACM on Programming Languages 5(POPL), 2021, Section 2, the equality saturation loop with search, apply and rebuild, the limits on iterations, e-nodes and time, the example of (a * 2) / 2, and e-class analyses
  4. Greg Nelson and Derek C. Oppen, Fast decision procedures based on congruence closure, Journal of the ACM 27(2), 1980, 356-364, the graph of terms with merged classes and the congruence closure that the e-graph keeps
  5. Rajeev Joshi, Greg Nelson and Keith Randall, Denali: a goal-directed superoptimizer, Proceedings of the ACM SIGPLAN 2002 Conference on Programming Language Design and Implementation (PLDI 2002), 304-314, the use of an e-graph saturated by axioms as the space of programs for a superoptimizer
  6. Pavel Panchekha, Alex Sanchez-Stern, James R. Wilcox and Zachary Tatlock, Automatically improving accuracy for floating point expressions, Proceedings of the 36th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2015), 1-11, the Herbie tool, which rewrites real-number expressions in an e-graph and extracts the most accurate floating point program
  7. Michael Stepp, Ross Tate and Sorin Lerner, Equality-based translation validator for LLVM, Computer Aided Verification (CAV 2011), LNCS 6806, 737-742, the use of equality saturation to show that a program and its optimized version are equal

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples