HEC: Equivalence Verification Checking for Code Transformation via Equality Saturation

Jiaqi Yin, Zhan Song, Nicolas Bohm Agostini, Antonino Tumeo, Cunxi Yu · DOI

Introduction

Optimizing compilers are essential to modern computing, performing complex source-to-source transformations to maximize hardware performance. However, as optimizations—such as loop tiling for parallelism or datapath restructuring for efficiency—become increasingly aggressive, ensuring the functional equivalence of the transformed code becomes a significant challenge. Traditional verification tools often struggle to bridge the gap between control-flow changes and datapath logic modifications.

HEC is a formal equivalence checking framework designed to address this gap. By utilizing e-graphs (equivalence graphs) and a novel hybrid rewriting approach, HEC provides a holistic method for verifying that optimized code remains faithful to its original specification. The framework has already demonstrated its utility by identifying critical, real-world bugs within the standard mlir-opt compiler toolchain.

The Challenge of Holistic Verification

In the "Post-Moore" era, High-Level Synthesis (HLS) and specialized compiler optimizations are vital for performance. These optimizations generally target two distinct areas:

  1. Control Flow: Altering loop structures through unrolling, tiling, or fusion.
  2. Datapath Logic: Applying mathematical identities, such as De Morgan’s laws or bitwise transformations.

Existing verification tools typically specialize in one domain or the other. When an optimization crosses these boundaries—for instance, hoisting a logic operation out of a tiled loop—traditional tools often fail to provide a unified proof of correctness. This "verification gap" allows subtle compiler bugs to persist, potentially leading to silent data corruption or system crashes.

The HEC Framework

HEC leverages the Multi-Level Intermediate Representation (MLIR) as its frontend and processes code through a three-stage pipeline designed for scalability and precision.

1. Specialized Graph Representation

HEC converts MLIR code into a specialized graph that unifies variable renaming and tracks operations. A key innovation is the decomposition of for loops into two distinct components: a loopvalue (containing parameters like start, end, and step) and a block (representing the operations performed within the loop body). This separation allows the framework to reason about loop structures and data operations independently yet within the same mathematical context.

2. Hybrid E-graph Rewriting

The core of HEC’s reasoning engine is its hybrid rewriting system:

  • Static Rules: HEC maintains a library of 62 pre-defined rules for datapath logic, such as bitwise identities (e.g., $a \oplus b \iff (a \land \neg b) \lor (\neg a \land b)$).
  • Dynamic Rules: Because loop parameters (like unrolling factors) are often dynamic, HEC generates custom rewriting rules on the fly. To ensure soundness, it utilizes the Z3 SMT solver to mathematically prove these dynamic rules before they are applied to the e-graph.

3. Equality Saturation

Using the egg e-graph framework, HEC performs equality saturation, exploring a massive space of equivalent program versions simultaneously. If the original and optimized versions of a program are found to reside within the same "equivalence class" in the e-graph, the transformation is formally proven correct. This approach is particularly effective because it naturally handles complex sequences of rules that must be applied in a specific order.

Empirical Evaluation

The researchers evaluated HEC using a diverse set of benchmarks, including PolyBenchC (for polyhedral optimizations), PolyBench-NN (for deep learning kernels), and a custom suite of over 150 datapath benchmarks ranging from 15,000 to 108,000 lines of code (LOC).

Key Results and Bug Discovery

HEC successfully identified two critical bugs in the mlir-opt optimizer:

  • Loop Boundary Check Error: A bug where unrolled loops would execute an extra iteration if the starting value exceeded the end value.
  • Memory RAW (Read-After-Write) Violation: A bug in loop fusion that altered data dependency sequences, leading to incorrect program output.

Beyond bug discovery, HEC demonstrated impressive scalability, processing over 100,000 lines of MLIR code in approximately 40 minutes. The complexity of the internal representation (e-nodes) grew linearly with the lines of code, suggesting the tool is viable for large-scale industrial applications.

Limitations and Future Directions

While powerful, HEC is subject to the inherent incompleteness of rewriting-based systems. If a compiler employs a transformation for which HEC lacks a corresponding static or dynamic rule, the tool may produce a false negative, flagging a correct program as unproven. Additionally, massive nested unrolling can lead to an exponential explosion in runtime, a known challenge in equality saturation.

Future work aims to expand HEC’s support for additional MLIR dialects and integrate the framework directly into compiler CI/CD pipelines. By catching optimization errors during the development phase, HEC provides a path toward more reliable and robust high-performance compilers.

Paper Citation: Jiaqi Yin, et al. "HEC: Equivalence Verification Checking for Code Transformation via Equality Saturation." arXiv:2506.02290v2 [cs.AR].

Summary updated

BibTeX
@inproceedings{yin2025hec,
  author = {Jiaqi Yin and Zhan Song and Nicolas Bohm Agostini and Antonino Tumeo and Cunxi Yu},
  title = {{HEC: Equivalence Verification Checking for Code Transformation via Equality Saturation}},
  booktitle = {USENIX ATC},
  year = {2025},
}
Figure