code wiki / _hdl_build / _xperf003c1_gate.nx
_xperf003c1_gate.nx source
↩ module page · 207 lines · 9065 B
1// _xperf003c1_gate.nx -- SOVEREIGN gate for rung X-PERF-003c1 (codegen redundant-defect
2// census; the honest BEFORE defect-count the codegen-win rung drives toward zero).
3//
4// (1) Runs the SOVEREIGN-compiled census /tmp/nx_codegen_defect_census.sov.elf, which
5// itself re-invokes the REAL sovereign compiler on runtime/_hdl_build/nx_clbg_fannkuch.nx
6// to regenerate /tmp/fk.s and mechanically tallies the three redundant-codegen
7// defect classes over the fannkuch_redux hot region (NOT a fixture).
8// (2) Parses the census's CODEGENDEFECT line: total_redundant, store_reload, dead_spill,
9// const_remat, count (dominant), region_lines.
10// (3) ASSERTS total_redundant>0 AND dominant count>0 -> a real, addressable ROI target
11// exists (PASS condition). Census run rc must be 0 (it ran on real asm: compiled,
12// region found, non-empty).
13// (4) Appends the MEASURED line to knowledge/status/codegen_defect.log with the
14// CODEGENDEFECT anchor, then writes verdict=GREEN.
15//
16// NO compiler change in this rung -> self-host and golden-diff are trivially preserved
17// (read-only analysis). spec: knowledge/specs/2026-06-13-codegen-defect-census.md
18import "nx_syscalls.nx"
19
20const G_MODE_RWX: i64 = 0x1a4 // 0644
21const G_BUFSZ: i64 = 8192
22
23func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
24func g_fputs(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
25func g_putn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
26func g_fputn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
27
28func g_run(path: *u8, argv: *i64, envp: *i64, redir_out: i64) -> i64 {
29 let pid: i64 = sys_fork()
30 if pid == 0 {
31 if redir_out >= 0 { sys_dup3(redir_out, 1, 0) }
32 sys_execve(path, argv, envp)
33 sys_exit(127)
34 }
35 let st: *i64 = sys_mmap(16) as *i64
36 sys_wait4(pid, st, 0)
37 let sig: i64 = st[0] & 0x7f
38 if sig != 0 { return 128 + sig }
39 return (st[0] >> 8) & 0xff
40}
41
42func g_slurp(path: *u8, buf: *u8) -> i64 {
43 let fd: i64 = sys_openat_rd(path)
44 if fd < 0 { return 0 }
45 var total: i64 = 0
46 var n: i64 = sys_read(fd, (buf + total) as *u8, G_BUFSZ - 1 - total)
47 while n > 0 {
48 total = total + n
49 n = sys_read(fd, (buf + total) as *u8, G_BUFSZ - 1 - total)
50 }
51 sys_close(fd)
52 buf[total] = 0 as u8
53 return total
54}
55
56func g_match_at(buf: *u8, i: i64, key: *u8) -> i64 {
57 var j: i64 = 0
58 while key[j] != (0 as u8) {
59 if buf[i + j] != key[j] { return 0 }
60 j = j + 1
61 }
62 return 1
63}
64
65func g_parse_after(buf: *u8, blen: i64, key: *u8, notfound: i64) -> i64 {
66 var i: i64 = 0
67 var hit: i64 = 0 - 1
68 while i < blen {
69 if hit < 0 {
70 if g_match_at(buf, i, key) == 1 { hit = i }
71 }
72 i = i + 1
73 }
74 if hit < 0 { return notfound }
75 var p: i64 = hit
76 while key[p - hit] != (0 as u8) { p = p + 1 }
77 var neg: i64 = 0
78 if buf[p] == (45 as u8) { neg = 1; p = p + 1 }
79 var val: i64 = 0
80 var any: i64 = 0
81 var go: i64 = 1
82 while go == 1 {
83 let ch: i64 = buf[p] as i64
84 if ch < 48 { go = 0 }
85 if ch > 57 { go = 0 }
86 if go == 1 {
87 val = val * 10 + (ch - 48)
88 any = 1
89 p = p + 1
90 }
91 }
92 if any == 0 { return notfound }
93 if neg == 1 { return 0 - val }
94 return val
95}
96
97func main() -> i64 {
98 g_puts("=== X-PERF-003c1 gate (codegen redundant-defect census; SOVEREIGN read-only) ===\n" as *u8)
99
100 let envp: *i64 = sys_mmap(8*4) as *i64
101 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
102
103 // -------- (1) run the SOVEREIGN census, capture its stdout --------
104 let cen_out: *u8 = "/tmp/_xperf003c1_census.txt" as *u8
105 let cen_bin: *u8 = "/tmp/nx_codegen_defect_census.sov.elf" as *u8
106 let cfd: i64 = sys_openat_wr(cen_out, G_MODE_RWX)
107 if cfd < 0 {
108 g_puts("GATE verdict=RED reason=cannot-open-census-out\n" as *u8)
109 sys_exit(1); return 1
110 }
111 let cargv: *i64 = sys_mmap(8*4) as *i64
112 cargv[0] = cen_bin as i64; cargv[1] = 0
113 let crc: i64 = g_run(cen_bin, cargv, envp, cfd)
114 sys_close(cfd)
115 if crc != 0 {
116 g_puts("GATE verdict=RED reason=census-run-nonzero rc=" as *u8); g_putn(crc); g_puts("\n" as *u8)
117 sys_exit(1); return 1
118 }
119
120 let cbuf: *u8 = sys_mmap(G_BUFSZ)
121 let clen: i64 = g_slurp(cen_out, cbuf)
122
123 // -------- (2) parse the CODEGENDEFECT line --------
124 let total_redundant: i64 = g_parse_after(cbuf, clen, "total_redundant=" as *u8, 0 - 1)
125 let store_reload: i64 = g_parse_after(cbuf, clen, "store_reload=" as *u8, 0 - 1)
126 let dead_spill: i64 = g_parse_after(cbuf, clen, "dead_spill=" as *u8, 0 - 1)
127 let const_remat: i64 = g_parse_after(cbuf, clen, "const_remat=" as *u8, 0 - 1)
128 let dom_count: i64 = g_parse_after(cbuf, clen, "count=" as *u8, 0 - 1)
129 let region_lines: i64 = g_parse_after(cbuf, clen, "region_lines=" as *u8, 0 - 1)
130
131 g_puts(" parsed: total_redundant=" as *u8); g_putn(total_redundant)
132 g_puts(" store_reload=" as *u8); g_putn(store_reload)
133 g_puts(" dead_spill=" as *u8); g_putn(dead_spill)
134 g_puts(" const_remat=" as *u8); g_putn(const_remat)
135 g_puts(" dominant_count=" as *u8); g_putn(dom_count)
136 g_puts(" region_lines=" as *u8); g_putn(region_lines); g_puts("\n" as *u8)
137
138 // -------- (3) assertions: a real, addressable ROI target exists --------
139 // presence of the MEASURED verdict token (no trailing digit -> matched explicitly).
140 var saw_measured: i64 = 0
141 var z: i64 = 0
142 while z < clen {
143 if saw_measured == 0 {
144 if g_match_at(cbuf, z, "verdict=MEASURED" as *u8) == 1 { saw_measured = 1 }
145 }
146 z = z + 1
147 }
148 if saw_measured == 0 {
149 g_puts("GATE verdict=RED reason=census-emitted-no-MEASURED-line\n" as *u8)
150 sys_exit(1); return 1
151 }
152 if total_redundant <= 0 {
153 g_puts("GATE verdict=RED reason=total_redundant-not-positive\n" as *u8)
154 sys_exit(1); return 1
155 }
156 if dom_count <= 0 {
157 g_puts("GATE verdict=RED reason=dominant-count-not-positive (no addressable ROI target)\n" as *u8)
158 sys_exit(1); return 1
159 }
160 if region_lines <= 0 {
161 g_puts("GATE verdict=RED reason=hot-region-empty\n" as *u8)
162 sys_exit(1); return 1
163 }
164
165 // determine dominant class name for the evidence line (recompute from parts to be
166 // robust: the census prints dominant=<name> but we re-derive to assert agreement).
167 var dom_sel: i64 = 0
168 var dmax: i64 = store_reload
169 if dead_spill > dmax { dmax = dead_spill; dom_sel = 1 }
170 if const_remat > dmax { dmax = const_remat; dom_sel = 2 }
171 if dmax != dom_count {
172 g_puts("GATE verdict=RED reason=dominant-count-disagrees-with-class-parts\n" as *u8)
173 sys_exit(1); return 1
174 }
175
176 // -------- (4) append the MEASURED evidence line --------
177 let lfd: i64 = sys_openat_append("knowledge/status/codegen_defect.log" as *u8, G_MODE_RWX)
178 if lfd >= 0 {
179 g_fputs(lfd, "CODEGENDEFECT target=fannkuch_hot total_redundant=" as *u8); g_fputn(lfd, total_redundant)
180 g_fputs(lfd, " store_reload=" as *u8); g_fputn(lfd, store_reload)
181 g_fputs(lfd, " dead_spill=" as *u8); g_fputn(lfd, dead_spill)
182 g_fputs(lfd, " const_remat=" as *u8); g_fputn(lfd, const_remat)
183 g_fputs(lfd, " dominant=" as *u8)
184 if dom_sel == 0 { g_fputs(lfd, "store_reload" as *u8) }
185 if dom_sel == 1 { g_fputs(lfd, "dead_spill" as *u8) }
186 if dom_sel == 2 { g_fputs(lfd, "const_remat" as *u8) }
187 g_fputs(lfd, " count=" as *u8); g_fputn(lfd, dom_count)
188 g_fputs(lfd, " origin=" as *u8)
189 if dom_sel == 0 { g_fputs(lfd, "regalloc-home-everything" as *u8) }
190 if dom_sel == 1 { g_fputs(lfd, "regalloc-home-everything" as *u8) }
191 if dom_sel == 2 { g_fputs(lfd, "peephole-missing-in-emitter" as *u8) }
192 g_fputs(lfd, " region_lines=" as *u8); g_fputn(lfd, region_lines)
193 g_fputs(lfd, " verdict=MEASURED\n" as *u8)
194 sys_close(lfd)
195 }
196
197 g_puts(" dominant defect = " as *u8)
198 if dom_sel == 0 { g_puts("store_reload" as *u8) }
199 if dom_sel == 1 { g_puts("dead_spill" as *u8) }
200 if dom_sel == 2 { g_puts("const_remat" as *u8) }
201 g_puts(" count=" as *u8); g_putn(dom_count)
202 g_puts(" -> X-PERF-003c2 highest-ROI fix target\n" as *u8)
203
204 g_puts("GATE verdict=GREEN (census ran on REAL sovereign asm; total_redundant>0; dominant count>0; MEASURED line written)\n" as *u8)
205 sys_exit(0)
206 return 0
207}