code wiki / _hdl_build / nx_superopt.nx
nx_superopt.nx
buildroot/runtime/_hdl_build/nx_superopt.nx
about
nx_superopt.nx -- the AUTHOR leg of the Sovereign Invention Engine: a
MECHANIZED SUPEROPTIMIZER. With NO LLM in the loop it runs one full invention
tick over an arithmetic expression:
PROPOSE -- nx_eqsat saturation generates the equivalence-class closure of
the seed under the wired sound rewrite rules (the proposal space
is materialized deterministically; no search policy, no LLM).
SCORE -- TWO tiers, both honest. Tier-1 = the e-graph's bottom-up cost
extractor (nx_eqsat_recompute_best / nx_eqsat_best_cost, the
RV64IM op-cost model). Tier-2 = an INDEPENDENT width-weighted
gate-level critical-path (nx_lat_honest over a lowered NxGsim
netlist) so "cheaper" cannot be the unit-metric self-deception.
VERIFY -- nx_triangulate equivalence-by-agreement: the candidate must
compute the SAME value as the seed over a dense+random+edge
battery (exhaustive at the small example width). Crucially the
legs are GENUINELY INDEPENDENT realizations (see below), so a
collusion among same-instruction legs cannot rubber-stamp an
unsound rewrite -- and we prove the check CATCHES a planted
non-equivalent candidate.
EMIT -- only after VERIFY passes AND the cost strictly decreased on the
honest metric do we accept the extracted cheapest DAG.
This is mechanized superoptimization = the first rung of mechanized INVENTION
(imitate -> match -> superopt/exceed). Honesty: equivalence is high-confidence
AGREEMENT over a battery (exhaustive at small width), NOT a symbolic proof --
stated, per the racing doctrine.
LEG INDEPENDENCE (the keystone defense). A naive triangulation of (mul x 2^k)
-> (shl x k) would have every leg evaluate `x << k` on the same i64 ALU and
collude: native MUL and SHL wrap identically on i64, so a shared-substrate
battery passes by echoing one computation three times -- worthless. So our
three legs do NOT share the candidate's arithmetic path:
LEG 0 = interpret the CANDIDATE emit-DAG (native ops; the lowered form).
LEG 1 = run the lowered CANDIDATE NxGsim netlist (independent gate-sim path).
LEG 2 = the OPERAND-RECONSTRUCTING witness: recompute the SEED's value WITHOUT
any shift fast-path -- a behavioral shift-add multiply that adds
shifted partial products bit-by-bit. An off-by-one shift count, or a
rule firing outside its proven width domain, makes LEG 2 disagree.
The ORACLE is the SEED emit-DAG interpreted directly. (LEG 0 vs ORACLE already
pits candidate-form against seed-form; LEG 2 is the cross-substrate check.)
dependencies 3 imports · 3 importers
imports: nx_eqsat.nxnx_triangulate.nxnx_latency_metric.nx
imported by: nx_optimize_suite_test.nxnx_superopt_governed_test.nxnx_superopt_test.nx
structs
| 55 | struct NxSuperoptResult |
| 186 | struct NxSoCtx |
consts
| 47 | const NX_MAGIC_999999: i64 = 999999 |
| 49 | const NX_SUPEROPT_OK: i64 = 0 |
| 50 | const NX_SUPEROPT_BAD_ARGS: i64 = 1 |
| 51 | const NX_SUPEROPT_OVERFLOW: i64 = 2 |
| 52 | const NX_SUPEROPT_BAD_KIND: i64 = 3 |
functions
| 76 | func nx_superopt_eval_emit(out: *NxEmitNode, n: i64, root: i64, env: *i64, val: *i64) -> i64 |
| 107 | func nx_superopt_mul_shiftadd(a: i64, b: i64) -> i64 |
| 133 | func nx_superopt_eqop_to_gate(op: i64) -> i64 called by 1: nx_superopt_lower_to_gsim |
| 147 | func nx_superopt_lower_to_gsim(out: *NxEmitNode, n: i64, |
| 214 | func nx_superopt_gsim_eval(gs: *NxGsim, var_net: *i64, n_var_slots: i64, |
| 229 | func nx_superopt_judge(ctx: *NxSoCtx, x: i64, vec: i64) -> i64 |