Skip to main content
QUICK REVIEW

[Paper Review] Sketch-Guided Equality Saturation: Scaling Equality Saturation to Complex Optimizations of Functional Programs

Thomas Kœhler, Phil Trinder|arXiv (Cornell University)|Nov 25, 2021
Parallel Computing and Optimization TechniquesComputer Science5 citations
TL;DR

This paper introduces sketch-guided equality saturation, a technique that combines efficient lambda calculus encoding with programmer-provided program sketches to guide equality saturation in functional program optimization. It enables discovery of complex matrix multiplication optimizations—such as loop blocking, vectorization, and multi-threading—in seconds with under 1GB RAM, whereas unguided equality saturation fails even after an hour and 60GB RAM.

ABSTRACT

Generating high-performance code for diverse hardware and application domains is challenging. Functional array programming languages with patterns like map and reduce have been successfully combined with term rewriting to define and explore optimization spaces. However, deciding what sequence of rewrites to apply is hard and has a huge impact on the performance of the rewritten program. Equality saturation avoids the issue by exploring many possible ways to apply rewrites, efficiently representing many equivalent programs in an e-graph data structure. Equality saturation has some limitations when rewriting functional language terms, as currently naive encodings of the lambda calculus are used. We present new techniques for encoding polymorphically typed lambda calculi, and show that the efficient encoding reduces the runtime and memory consumption of equality saturation by orders of magnitude. Moreover, equality saturation does not yet scale to complex compiler optimizations. These emerge from long rewrite sequences of thousands of rewrite steps, and may use pathological combinations of rewrite rules that cause the e-graph to quickly grow too large. This paper introduces \emph{sketch-guided equality saturation}, a semi-automatic technique that allows programmers to provide program sketches to guide rewriting. Sketch-guided equality saturation is evaluated for seven complex matrix multiplication optimizations, including loop blocking, vectorization, and multi-threading. Even with efficient lambda calculus encoding, unguided equality saturation can locate only the two simplest of these optimizations, the remaining five are undiscovered even with an hour of compilation time and 60GB of RAM. By specifying three or fewer sketch guides all seven optimizations are found in seconds of compilation time, using under 1GB of RAM, and generating high performance code.

Motivation & Objective

  • Address the scalability limitations of equality saturation in optimizing complex functional programs, particularly those requiring thousands of rewrite steps.
  • Overcome the exponential e-graph growth caused by pathological combinations of rewrite rules, such as associativity and commutativity.
  • Enable practical application of equality saturation to polymorphically typed functional languages like Rise, which are used in high-performance computing domains.
  • Reduce the need for manual phase ordering in rewriting strategies by introducing a semi-automatic, sketch-based guidance mechanism.
  • Demonstrate that efficient encoding of lambda calculus is essential for making equality saturation viable for real-world functional program optimizations.

Proposed method

  • Introduce sketch-guided equality saturation, where programmers provide a sequence of program sketches—partial program patterns with unspecified details—to guide the optimization process.
  • Decompose complex optimizations into a series of smaller, manageable equality saturation searches, each guided by a sketch to constrain the search space.
  • Develop an efficient encoding of polymorphically typed lambda calculus using De Bruijn indices and extraction-based substitution to minimize e-graph bloat and improve performance.
  • Integrate the new encoding into the Risegg equality saturation framework to support functional language term rewriting with name binding.
  • Use a cost model to extract the highest-performing program from the e-graph after saturation, ensuring semantic equivalence and performance optimization.
  • Apply the technique to seven matrix multiplication optimizations in the Rise functional language, evaluating performance and resource usage against unguided equality saturation and TVM.

Experimental results

Research questions

  • RQ1Can sketch-guided equality saturation effectively scale equality saturation to complex optimizations in functional programs that are infeasible with unguided approaches?
  • RQ2How does an efficient lambda calculus encoding reduce the memory and runtime overhead of equality saturation in functional program optimization?
  • RQ3To what extent can programmer-provided sketches guide the discovery of complex optimizations without requiring manual phase ordering of rewrite rules?
  • RQ4What performance and resource usage improvements does sketch-guided equality saturation achieve compared to unguided equality saturation and state-of-the-art compilers like TVM?
  • RQ5Can the combination of efficient encoding and sketch guidance enable equality saturation to discover optimizations that are otherwise unreachable due to e-graph explosion?

Key findings

  • Sketch-guided equality saturation discovered all seven complex matrix multiplication optimizations—including loop blocking, vectorization, and multi-threading—in under 1GB of RAM and seconds of compilation time.
  • Unguided equality saturation failed to discover five of the seven optimizations even after one hour of runtime and 60GB of RAM, highlighting the scalability limitations of naive equality saturation.
  • The new lambda calculus encoding reduced runtime and memory consumption by orders of magnitude compared to naive encodings, making equality saturation viable for functional programs.
  • The generated code from sketch-guided equality saturation achieved performance comparable to that of the state-of-the-art TVM compiler and to manually optimized code from rewriting strategies.
  • Using only three or fewer sketch guides, the technique successfully located all complex optimizations, demonstrating the effectiveness of sketch-based guidance in constraining the search space.
  • The approach enables a hybrid optimization strategy combining declarative sketching with equality saturation, offering a practical path toward interactive, high-performance code generation for functional languages.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.