Equality Saturation

Equality saturation retains many equivalent expressions in one e-graph. Rewriting adds choices, while extraction selects a representative for a specified cost model.

Source
Anbloq
Language
English
Created
Updated
Chapters
3
Reading time
1 min
In this article3 chapters
  1. A class stores interchangeable expressions
  2. A rewrite can be useful in either direction
  3. Extraction measures the whole candidate

A class stores interchangeable expressions

An e-graph groups e-nodes into equivalence classes. An e-node consists of an operator and a tuple of child class identifiers. Congruence closure enforces that equal children under the same operator produce equal parent expressions. If two classes merge, operators using either class may become congruent. Rebuilding repairs these consequences in batches. A rewrite adds a replacement and merges it with the matched expression’s class, preserving both choices.

Term rewriting specifies local equivalence steps. Congruence closure explains why equal arguments also make applications of the same constructor equal.

A rewrite can be useful in either direction

Over a fixed-width unsigned bit-vector domain,

\[ 2x \equiv x+x \pmod{2^w}. \]

The multiplicative form may be useful for finding other arithmetic rewrites. The additive form may share a subexpression with surrounding code. A one-way rewrite strategy commits to one local form early. An e-graph can keep both while applying other rules. An identity involving division or floating-point reassociation would need its own side conditions before merging. Once invalid expressions are equated, extraction cannot restore soundness.

Extraction measures the whole candidate

With additive tree cost, extraction can repeatedly minimize each class over the costs of its e-nodes and their children. Cyclic classes require care to find finite representatives. If the target cost depends on shared expressions in a DAG, optimizing each subtree independently can double-count or miss sharing. Saturation can also take more space than available, so a search budget matters. This separates rewrite quality from extraction quality. Related representations appear in SSA, semantic checks in superoptimization, and equality reasoning in automated proving.[1][2]

References

  1. Ross Tate et al., Equality Saturation: A New Approach to Optimization ↩
  2. Max Willsey et al., egg: Fast and Extensible Equality Saturation ↩