nx_opt_eqsat_pass.nx
buildroot/runtime/nx_opt_eqsat_pass.nx
about
nx_opt_eqsat_pass.nx -- EQUALITY SATURATION AS AN ACTUAL IR REWRITE PASS.
This is the first thing in the estate that makes nx_eqsat CHANGE A PROGRAM rather than merely judge
one. Everything before it -- seven gates, 93 teeth, and nx_opt_eqsat_oracle_gate -- proved the engine
sound and used it to CHECK the shipping optimizer. This consumes it.
WHAT IT DOES: for each two-operand integer instruction, build a small e-graph of that expression
(constants as constants, non-constant operands as e-graph VARIABLES KEYED ON THEIR IR VALUE ID, so
the same SSA value becomes the same e-class), saturate it under the data-driven rule table plus the
const-fold analysis, extract the cost-minimal representative, and if that representative is a
CONSTANT, fold the instruction's result to it. The rewrite mechanism is the incumbent one, copied
deliberately from opt_const_fold: set the result Value's kind to CONST and its const_int. No new way
to mutate IR is introduced by this file.
WHY IT IS NOT A DUPLICATE OF opt_const_fold: that pass requires BOTH operands to be constants. This
one folds through ALGEBRAIC IDENTITIES, so it reaches expressions the shipping pipeline cannot --
`x ^ x` and `x - x` with x a runtime value fold to 0 here and are left untouched there. That gap is
real and currently unmitigated, because opt_simplify (the peephole that would catch some of these)
is BISECT-DISABLED in opt_run and has been since a self-host miscompile.
DELIBERATELY NOT IMPORTED BY nx_opt.nx YET, and the lang_opt_eqsat_pass watch contract stays OPEN
because of it. Adding `import nx_eqsat.nx` to nx_opt pulls the whole engine into every nx_cc build,
changing the compiler's closure and its bytes. That is a compiler change and it belongs behind the
equivalence net (nx_cc_equiv_gate 10/10 + selfhost) as a deliberate act, not as a side effect of
landing a pass. Shipping the pass first, wired to nothing, is the sequencing that keeps the
shipping compiler byte-identical while the capability becomes real and provable.
TYPE NARROWNESS, STATED: i32-typed instructions are SKIPPED. opt_const_fold wraps folded constants
mod 2^32 to match the backend's masking behaviour, and a fold that ignored that would produce a
silently wrong value -- exactly the class this estate tracks hardest. Rather than duplicate that
wrapping logic (a second ruler), this pass declines i32 and says so.
license_tier: ORIGINAL expect_exit: 0
dependencies 4 imports · 2 importers
imports: nx_syscalls.nxnx_types.nxnx_ir.nxnx_eqsat.nx
imported by: nx_opt.nxnx_opt_eqsat_pass_gate.nx
structs
| none |
consts
| 39 | const OEP_CAP_NODES: i64 = 64 |
| 40 | const OEP_CAP_CLS: i64 = 64 |
| 41 | const OEP_SAT_ITERS: i64 = 8 |
| 42 | const OEP_DSL_ROWS: i64 = 16 |
functions
| 48 | func oep_ir_op_to_eq(op: i64) -> i64 called by 1: opt_eqsat_pass |
| 59 | func opt_eqsat_pass(f: *Function) -> i64 |