code wiki / _hdl_build / nx_superopt_strength.nx

nx_superopt_strength.nx

buildroot/runtime/_hdl_build/nx_superopt_strength.nx

3775 B98 linesdepth 2pulls 2 transitivereach 2 importersview sourcekind librarytopic superopt
docsdependenciesstructsconstsfunctions

about

nx_superopt_strength.nx -- a GENERATOR: it AUTHORS strength-reduction lowerings for `x * C` by SEARCH, not by me hand-writing each rule. Given a constant C, it enumerates candidate implementations -- a single shift (x<<a), or a shift-op pair ((x<<a) + (x<<b) / (x<<a) - (x<<b)), or the fallback multiply -- VERIFIES each is 1:1 equal to x*C over many inputs, scores each by the measured cost model (mul > shift+add, per nx_measure_cost), and RETURNS the cheapest verified one. THIS is a building capability: the team discovers `x*7 = (x<<3)-x`, `x*9 = (x<<3)+x`, `x*1024 = x<<10` ITSELF -- I wrote the searcher; the search wrote the answers. It is the Generator leg the codegen loop consumes (each authored lowering is a verified, cheaper codegen rewrite). Refs: Warren, Hacker's Delight 2e ch.8 (strength reduction); the e-graph superopt idea (egg, Willsey POPL 2021).

dependencies 1 imports · 2 importers

nx_syscalls.nx nx_superopt_strength.nx nx_superopt_govern_test.nx nx_superopt_strength_test.nx

imports: nx_syscalls.nx

imported by: nx_superopt_govern_test.nxnx_superopt_strength_test.nx

structs

none

consts

15const SO_MAGIC_19088743: i64 = 19088743
16const SO_MAGIC_6364136223846793005: i64 = 6364136223846793005
17const SO_MAGIC_1442695040888963407: i64 = 1442695040888963407
19const SO_PLAN_MUL: i64 = 0
20const SO_PLAN_SHL: i64 = 1
21const SO_PLAN_SHIFTOP: i64 = 2
22const SO_ADD: i64 = 0
23const SO_SUB: i64 = 1
26const SO_MUL_COST: i64 = 4
27const SO_SHL_COST: i64 = 1
28const SO_OP_COST: i64 = 1
29const SO_SHIFT_MAX: i64 = 62

functions

32func so_eval(kind: i64, a: i64, op: i64, b: i64, C: i64, x: i64) -> i64
called by 1: so_verify
42func so_cost(kind: i64, a: i64, b: i64) -> i64
called by 1: so_best
52func so_verify(kind: i64, a: i64, op: i64, b: i64, C: i64) -> i64
called by 3: mainso_bestmain calls 1: so_eval
68func so_best(C: i64, out: *i64) -> i64
called by 2: mainmain calls 2: so_verifyso_cost