code wiki / _hdl_build / _xperf003c1_gate.nx

_xperf003c1_gate.nx source

↩ module page · 210 lines · 9340 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" 19import "nx_gate_verdict.nx" 20 21const G_MODE_RWX: i64 = 0x1a4 // 0644 22const G_BUFSZ: i64 = 8192 23 24func 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 } 25func 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 } 26func 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 } 27func 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 } 28 29func g_run(path: *u8, argv: *i64, envp: *i64, redir_out: i64) -> i64 { 30 let pid: i64 = sys_fork() 31 if pid == 0 { 32 if redir_out >= 0 { sys_dup3(redir_out, 1, 0) } 33 sys_execve(path, argv, envp) 34 sys_exit(127) 35 } 36 let st: *i64 = sys_mmap(16) as *i64 37 sys_wait4(pid, st, 0) 38 let sig: i64 = st[0] & 0x7f 39 if sig != 0 { return 128 + sig } 40 return (st[0] >> 8) & 0xff 41} 42 43func g_slurp(path: *u8, buf: *u8) -> i64 { 44 let fd: i64 = sys_openat_rd(path) 45 if fd < 0 { return 0 } 46 var total: i64 = 0 47 var n: i64 = sys_read(fd, (buf + total) as *u8, G_BUFSZ - 1 - total) 48 while n > 0 { 49 total = total + n 50 n = sys_read(fd, (buf + total) as *u8, G_BUFSZ - 1 - total) 51 } 52 sys_close(fd) 53 buf[total] = 0 as u8 54 return total 55} 56 57func g_match_at(buf: *u8, i: i64, key: *u8) -> i64 { 58 var j: i64 = 0 59 while key[j] != (0 as u8) { 60 if buf[i + j] != key[j] { return 0 } 61 j = j + 1 62 } 63 return 1 64} 65 66func g_parse_after(buf: *u8, blen: i64, key: *u8, notfound: i64) -> i64 { 67 var i: i64 = 0 68 var hit: i64 = 0 - 1 69 while i < blen { 70 if hit < 0 { 71 if g_match_at(buf, i, key) == 1 { hit = i } 72 } 73 i = i + 1 74 } 75 if hit < 0 { return notfound } 76 var p: i64 = hit 77 while key[p - hit] != (0 as u8) { p = p + 1 } 78 var neg: i64 = 0 79 if buf[p] == (45 as u8) { neg = 1; p = p + 1 } 80 var val: i64 = 0 81 var any: i64 = 0 82 var go: i64 = 1 83 while go == 1 { 84 let ch: i64 = buf[p] as i64 85 if ch < 48 { go = 0 } 86 if ch > 57 { go = 0 } 87 if go == 1 { 88 val = val * 10 + (ch - 48) 89 any = 1 90 p = p + 1 91 } 92 } 93 if any == 0 { return notfound } 94 if neg == 1 { return 0 - val } 95 return val 96} 97 98func main() -> i64 { 99 g_puts("=== X-PERF-003c1 gate (codegen redundant-defect census; SOVEREIGN read-only) ===\n" as *u8) 100 101 let envp: *i64 = sys_mmap(8*4) as *i64 102 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0 103 104 // -------- (1) run the SOVEREIGN census, capture its stdout -------- 105 let cen_out: *u8 = "/tmp/_xperf003c1_census.txt" as *u8 106 let cen_bin: *u8 = "/tmp/nx_codegen_defect_census.sov.elf" as *u8 107 let cfd: i64 = sys_openat_wr(cen_out, G_MODE_RWX) 108 if cfd < 0 { 109 g_puts("GATE verdict=RED reason=cannot-open-census-out\n" as *u8) 110 sys_exit(1); return 1 111 } 112 let cargv: *i64 = sys_mmap(8*4) as *i64 113 cargv[0] = cen_bin as i64; cargv[1] = 0 114 let crc: i64 = g_run(cen_bin, cargv, envp, cfd) 115 sys_close(cfd) 116 if crc != 0 { 117 g_puts("GATE verdict=RED reason=census-run-nonzero rc=" as *u8); g_putn(crc); g_puts("\n" as *u8) 118 sys_exit(1); return 1 119 } 120 121 let cbuf: *u8 = sys_mmap(G_BUFSZ) 122 let clen: i64 = g_slurp(cen_out, cbuf) 123 124 // -------- (2) parse the CODEGENDEFECT line -------- 125 let total_redundant: i64 = g_parse_after(cbuf, clen, "total_redundant=" as *u8, 0 - 1) 126 let store_reload: i64 = g_parse_after(cbuf, clen, "store_reload=" as *u8, 0 - 1) 127 let dead_spill: i64 = g_parse_after(cbuf, clen, "dead_spill=" as *u8, 0 - 1) 128 let const_remat: i64 = g_parse_after(cbuf, clen, "const_remat=" as *u8, 0 - 1) 129 let dom_count: i64 = g_parse_after(cbuf, clen, "count=" as *u8, 0 - 1) 130 let region_lines: i64 = g_parse_after(cbuf, clen, "region_lines=" as *u8, 0 - 1) 131 132 g_puts(" parsed: total_redundant=" as *u8); g_putn(total_redundant) 133 g_puts(" store_reload=" as *u8); g_putn(store_reload) 134 g_puts(" dead_spill=" as *u8); g_putn(dead_spill) 135 g_puts(" const_remat=" as *u8); g_putn(const_remat) 136 g_puts(" dominant_count=" as *u8); g_putn(dom_count) 137 g_puts(" region_lines=" as *u8); g_putn(region_lines); g_puts("\n" as *u8) 138 139 // -------- (3) assertions: a real, addressable ROI target exists -------- 140 // presence of the MEASURED verdict token (no trailing digit -> matched explicitly). 141 var saw_measured: i64 = 0 142 var z: i64 = 0 143 while z < clen { 144 if saw_measured == 0 { 145 if g_match_at(cbuf, z, "verdict=MEASURED" as *u8) == 1 { saw_measured = 1 } 146 } 147 z = z + 1 148 } 149 if saw_measured == 0 { 150 g_puts("GATE verdict=RED reason=census-emitted-no-MEASURED-line\n" as *u8) 151 sys_exit(1); return 1 152 } 153 if total_redundant <= 0 { 154 g_puts("GATE verdict=RED reason=total_redundant-not-positive\n" as *u8) 155 sys_exit(1); return 1 156 } 157 if dom_count <= 0 { 158 g_puts("GATE verdict=RED reason=dominant-count-not-positive (no addressable ROI target)\n" as *u8) 159 sys_exit(1); return 1 160 } 161 if region_lines <= 0 { 162 g_puts("GATE verdict=RED reason=hot-region-empty\n" as *u8) 163 sys_exit(1); return 1 164 } 165 166 // determine dominant class name for the evidence line (recompute from parts to be 167 // robust: the census prints dominant=<name> but we re-derive to assert agreement). 168 var dom_sel: i64 = 0 169 var dmax: i64 = store_reload 170 if dead_spill > dmax { dmax = dead_spill; dom_sel = 1 } 171 if const_remat > dmax { dmax = const_remat; dom_sel = 2 } 172 if dmax != dom_count { 173 g_puts("GATE verdict=RED reason=dominant-count-disagrees-with-class-parts\n" as *u8) 174 sys_exit(1); return 1 175 } 176 177 // -------- (4) append the MEASURED evidence line -------- 178 let lfd: i64 = sys_openat_append("knowledge/status/codegen_defect.log" as *u8, G_MODE_RWX) 179 if lfd >= 0 { 180 g_fputs(lfd, "CODEGENDEFECT target=fannkuch_hot total_redundant=" as *u8); g_fputn(lfd, total_redundant) 181 g_fputs(lfd, " store_reload=" as *u8); g_fputn(lfd, store_reload) 182 g_fputs(lfd, " dead_spill=" as *u8); g_fputn(lfd, dead_spill) 183 g_fputs(lfd, " const_remat=" as *u8); g_fputn(lfd, const_remat) 184 g_fputs(lfd, " dominant=" as *u8) 185 if dom_sel == 0 { g_fputs(lfd, "store_reload" as *u8) } 186 if dom_sel == 1 { g_fputs(lfd, "dead_spill" as *u8) } 187 if dom_sel == 2 { g_fputs(lfd, "const_remat" as *u8) } 188 g_fputs(lfd, " count=" as *u8); g_fputn(lfd, dom_count) 189 g_fputs(lfd, " origin=" as *u8) 190 if dom_sel == 0 { g_fputs(lfd, "regalloc-home-everything" as *u8) } 191 if dom_sel == 1 { g_fputs(lfd, "regalloc-home-everything" as *u8) } 192 if dom_sel == 2 { g_fputs(lfd, "peephole-missing-in-emitter" as *u8) } 193 g_fputs(lfd, " region_lines=" as *u8); g_fputn(lfd, region_lines) 194 g_fputs(lfd, " verdict=MEASURED\n" as *u8) 195 sys_close(lfd) 196 } 197 198 g_puts(" dominant defect = " as *u8) 199 if dom_sel == 0 { g_puts("store_reload" as *u8) } 200 if dom_sel == 1 { g_puts("dead_spill" as *u8) } 201 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 202 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 203 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 204 let ctr__dry: *i64 = gv_ctr() 205 ctr__dry[0] = dom_sel 206 ctr__dry[1] = 2 207 let rc__dry: i64 = gv_verdict("XPERF003C1-GATE" as *u8, ctr__dry, "census ran on REAL sovereign asm; total_redundant>0; dominant count>0; MEASURED line written)" as *u8) 208 sys_exit(rc__dry) 209 return rc__dry 210}