The acyclic e-graph: Cranelift's mid-end optimizer
4 days ago
- #Cranelift
- #Compiler Optimization
- #E-Graphs
- Aegraph (acyclic e-graph) is the core data structure in Cranelift's mid-end optimizer, enabling unified fine-grained optimization.
- It addresses the pass-ordering problem by integrating rewrites, canonicalization, and code motion into a single fixpoint loop.
- Key components: sea-of-nodes representation for pure operators, scoped elaboration for translating back to CFG, and eager rewriting to maintain acyclicity.
- Union nodes allow efficient multi-representation without traditional e-graph overhead, though benchmarks show minimal impact on current workloads.
- Extraction uses a simplified dynamic programming approach, ignoring shared substructure, which is efficient but suboptimal for NP-hard cases.
- Evaluation shows aegraph improves code performance by ~2% over classical pipelines, with compile-time overheads around 7-8%.
- Future directions include better extraction algorithms, control-flow rewrites, and integrating target-specific lowering rules.