Every optimizing compiler faces an awkward dilemma: which rewrites to apply, and in what order? Replacing with (a left bit-shift) might unlock a later simplification — or might block one. Apply too few rewrites and leave performance on the table; chase the wrong sequence and miss the global optimum.
The traditional answer is to pick a fixed order, accept the losses, and move on. But since 2005, and especially since the egg library (Willsey et al., POPL 2021) made it practical, a richer answer has emerged: equality saturation.
The key ingredient is the e-graph (equality graph). An e-graph groups expressions into e-classes — sets of provably equivalent terms — and represents the whole group at once rather than committing to a single form. When you apply a rewrite rule the graph simply grows: both the old and new form exist, linked in the same equivalence class. Apply all your rules exhaustively and the e-graph reaches saturation: every expression equivalent to the original lives inside it. Then a single extraction pass walks the graph and picks out the cheapest variant — and the order in which rewrites fired never mattered.
The result is a clean separation: equality (what is true) and cost (what is cheap) are handled in two completely independent phases.
Comments
Loading comments...