code wiki / _hdl_build / nx_nrtl_opt_gate.nx
nx_nrtl_opt_gate.nx source
↩ module page · 87 lines · 5394 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_nrtl_opt_gate.nx -- GATE: the logic-OPTIMIZATION pass (closes the QoR-behind axis with a MEASURED gate-count cut).
4// Authors a deliberately-REDUNDANT RTL design (duplicate a&b thrice; a dead a|b), synthesizes it, then runs the
5// CSE+DCE optimizer, proving:
6// T1 EQUIVALENCE-PRESERVING: the optimized fabric == the ORIGINAL fabric == oracle over 100 random (a,b) -- opt
7// never changes behavior (a&b)+(a^b) == a|b.
8// T2 MEASURED REDUCTION: optimized gate count < original (CSE removed the duplicate a&b; DCE removed the dead a|b).
9// T3 NEVER-BRICK (#26): bounded, pure memory.
10// T4 LIAR-KILL: corrupt one optimized cell -> != oracle (the equivalence check has teeth).
11// Sovereign, no-float. expect_exit: 0 license_tier: ORIGINAL
12import "nx_nrtl.nx"
13import "nx_nrtl_opt.nx"
14import "nx_fpga_fabric.nx"
15import "nx_syscalls.nx"
16
17func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
18" as *u8); return ok }
19func cpstr(dst: *u8, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ dst[i]=s[i]; i=i+1 } dst[i]=0 as u8; return i }
20
21func run2(cn: i64, NPI: i64, ci: *i64, cs: *i64, cp: *i64, pi: *i64, co: *i64, W: i64, a: i64, b: i64) -> i64 {
22 var i: i64=0; while i<W { pi[i]=(a>>i)&1; pi[W+i]=(b>>i)&1; i=i+1 }
23 pi[2*W]=0
24 fab_eval(cn, NPI, ci, cs, pi, co)
25 var s: i64=0; i=0; while i<W { let v: i64=fab_resolve(cp[i], NPI, pi, co); s=s|(v<<i); i=i+1 }
26 return s
27}
28
29func main() -> i64 {
30 gw("=== nx_nrtl_opt_gate: logic-OPTIMIZATION (CSE+DCE) -- measured gate-count cut, equivalence-preserving ===\n" as *u8)
31 let W: i64=8; let MASK: i64=255
32 var pass: i64=0; var total: i64=0
33 let src: *u8=sys_mmap(4096)
34 let slen: i64=cpstr(src, ".nrtl 1\n.module red 8\n.in a b\n.out y\np = a & b\nq = a & b\nm = a & b\nr = a ^ b\nd = a | b\ny = p + r\n.end\n\x00" as *u8)
35 // synthesize (unoptimized)
36 let ci: *i64=sys_mmap(8*256) as *i64; let cs: *i64=sys_mmap(8*1024) as *i64; let ck: *i64=sys_mmap(8*256) as *i64; let cp: *i64=sys_mmap(8*32) as *i64
37 let on: *i64=sys_mmap(16) as *i64; let op: *i64=sys_mmap(16) as *i64; let oWp: *i64=sys_mmap(16) as *i64
38 let cn_before: i64=nrtl_synth(src, slen, ci, cs, ck, cp, on, op, oWp)
39 let NPI: i64=on[0]; let npo: i64=op[0]
40 // optimize (CSE + DCE)
41 let oi: *i64=sys_mmap(8*256) as *i64; let os: *i64=sys_mmap(8*1024) as *i64; let ok: *i64=sys_mmap(8*256) as *i64; let opo: *i64=sys_mmap(8*32) as *i64; let onpo: *i64=sys_mmap(16) as *i64
42 let cn_after: i64=nrtl_opt(cn_before, NPI, npo, ck, ci, cs, cp, ok, oi, os, opo, onpo)
43 let pi: *i64=sys_mmap(8*128) as *i64; let co: *i64=sys_mmap(8*256) as *i64
44
45 // ---- T1: equivalence-preserving (optimized == original == oracle) ----
46 var seed: i64=20260626; var mism: i64=0; var checks: i64=0
47 var t: i64=0
48 while t<100 {
49 seed=(seed*1103515245+12345)&2147483647; let a: i64=seed&MASK
50 seed=(seed*1103515245+12345)&2147483647; let b: i64=seed&MASK
51 let bef: i64=run2(cn_before, NPI, ci, cs, cp, pi, co, W, a, b)
52 let aft: i64=run2(cn_after, NPI, oi, os, opo, pi, co, W, a, b)
53 let exp: i64=((a&b)+(a^b))&MASK
54 if bef!=exp { mism=mism+1 }
55 if aft!=exp { mism=mism+1 }
56 if bef!=aft { mism=mism+1 }
57 checks=checks+1; t=t+1
58 }
59 total=total+1; if mism==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
60 gw("T1 EQUIVALENCE-PRESERVING: optimized == original == oracle ((a&b)+(a^b)=a|b), checks=" as *u8); gn(checks); gw(" mismatches=" as *u8); gn(mism); gw("\n" as *u8)
61
62 // ---- T2: measured gate-count reduction ----
63 let cut: i64=(cn_before-cn_after)*100/cn_before
64 total=total+1; if cn_after<cn_before { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
65 gw("T2 MEASURED REDUCTION: " as *u8); gn(cn_before); gw(" -> " as *u8); gn(cn_after); gw(" gates = " as *u8); gn(cut); gw("% cut (CSE merged 3x duplicate a&b; DCE removed dead a|b) -- the QoR axis Yosys wins on, now sovereign+measured\n" as *u8)
66
67 // ---- T3: never-brick ----
68 total=total+1; if cn_after>0 { if cn_after<=cn_before { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
69 gw("T3 NEVER-BRICK (#26): optimization bounded (cells only removed/merged, never added), pure memory\n" as *u8)
70
71 // ---- T4: liar-kill -- corrupt one optimized cell -> != oracle ----
72 let saved: i64=oi[0]; oi[0]=oi[0]^65535
73 var liar_wrong: i64=0; var lt: i64=0
74 while lt<10 {
75 seed=(seed*1103515245+12345)&2147483647; let a: i64=seed&MASK
76 seed=(seed*1103515245+12345)&2147483647; let b: i64=seed&MASK
77 if run2(cn_after, NPI, oi, os, opo, pi, co, W, a, b) != (((a&b)+(a^b))&MASK) { liar_wrong=liar_wrong+1 }
78 lt=lt+1
79 }
80 oi[0]=saved
81 total=total+1; if liar_wrong>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
82 gw("T4 LIAR-KILL: corrupted 1 optimized cell -> wrong on " as *u8); gn(liar_wrong); gw("/10 inputs\n" as *u8)
83
84 gw("OPT-GATE verdict=" as *u8)
85 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" END\n" as *u8); sys_exit(0); return 0 }
86 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" END\n" as *u8); sys_exit(1); return 1
87}