code wiki / _hdl_build / nx_superopt.nx

nx_superopt.nx

buildroot/runtime/_hdl_build/nx_superopt.nx

12495 B238 linesdepth 7pulls 11 transitivereach 3 importersview sourcekind librarytopic superopt
docsdependenciesstructsconstsfunctions

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

nx_eqsat.nx nx_triangulate.nx nx_latency_metric.nx nx_superopt.nx nx_optimize_suite_test.nx nx_superopt_governed_test.nx nx_superopt_test.nx

imports: nx_eqsat.nxnx_triangulate.nxnx_latency_metric.nx

imported by: nx_optimize_suite_test.nxnx_superopt_governed_test.nxnx_superopt_test.nx

structs

55struct NxSuperoptResult
186struct NxSoCtx

consts

47const NX_MAGIC_999999: i64 = 999999
49const NX_SUPEROPT_OK: i64 = 0
50const NX_SUPEROPT_BAD_ARGS: i64 = 1
51const NX_SUPEROPT_OVERFLOW: i64 = 2
52const NX_SUPEROPT_BAD_KIND: i64 = 3

functions

76func nx_superopt_eval_emit(out: *NxEmitNode, n: i64, root: i64, env: *i64, val: *i64) -> i64
107func nx_superopt_mul_shiftadd(a: i64, b: i64) -> i64
133func nx_superopt_eqop_to_gate(op: i64) -> i64
147func nx_superopt_lower_to_gsim(out: *NxEmitNode, n: i64,
called by 1: main calls 1: nx_superopt_eqop_to_gate
214func nx_superopt_gsim_eval(gs: *NxGsim, var_net: *i64, n_var_slots: i64,
called by 2: nx_superopt_judgemain calls 1: nx_gsim_run
229func nx_superopt_judge(ctx: *NxSoCtx, x: i64, vec: i64) -> i64