code wiki / _hdl_build / nx_codegen_improve_loop.nx
nx_codegen_improve_loop.nx source
↩ module page · 150 lines · 6950 B
1// nx_codegen_improve_loop.nx -- the TEAM's autonomous codegen-improvement loop:
2// the one machine that drives a codegen change from source to a governed,
3// MEASURED promotion decision, with no human at a shell. This is what "get the
4// team there in partnership till they can do it themselves" means concretely.
5//
6// 1. SELF-HOST the candidate compiler from the compiler's own source.
7// 2. VERIFY differentially vs known-good on a corpus (no miscompile).
8// 3. MEASURE race the SAME kernel compiled by candidate vs known-good:
9// checksum must stay byte-identical (correctness) and we read
10// cycles for both (the win, or a regression).
11// 4. GOVERN the crew council ACTs only if verified AND not slower; a
12// miscompile or a regression is ESCALATEd (rolled back).
13//
14// Run here on the IDENTITY candidate (current source) it proves the loop end to
15// end + measures the baseline; a real regalloc change plugs into step 1's source
16// and the SAME loop decides. Non-destructive (candidate in /tmp; known-good
17// pinned). Known answer: loop runs, candidate verified + not slower -> council
18// ACT, exit 0.
19
20import "nx_codegen_exec.nx"
21import "nx_crew_council.nx"
22
23func cil_is_digit(c: i64) -> i64 { if c < 48 { return 0 } if c > 57 { return 0 } return 1 }
24
25// read a file into buf (cap), return length (or -1).
26func cil_read_file(path: *u8, buf: *u8, cap: i64) -> i64 {
27 let fd: i64 = sys_openat_rd(path)
28 if fd < 0 { return 0 - 1 }
29 var total: i64 = 0
30 var go: i64 = 1
31 while go == 1 {
32 let got: i64 = sys_read(fd, (buf as i64 + total) as *u8, cap - total)
33 if got <= 0 { go = 0 } else { total = total + got; if total >= cap { go = 0 } }
34 }
35 sys_close(fd)
36 return total
37}
38
39// parse two leading decimals ("<checksum> <cycles>") into out[0],out[1].
40func cil_parse_two(buf: *u8, len: i64, out: *i64) -> i64 {
41 var i: i64 = 0
42 var which: i64 = 0
43 while which < 2 {
44 var adv: i64 = 1
45 while adv == 1 {
46 if i >= len { adv = 0 }
47 else { if cil_is_digit(buf[i] as i64) == 1 { adv = 0 } else { i = i + 1 } }
48 }
49 if i >= len { return 0 - 1 }
50 var v: i64 = 0
51 var rd: i64 = 1
52 while rd == 1 {
53 if i >= len { rd = 0 }
54 else { let c: i64 = buf[i] as i64
55 if cil_is_digit(c) == 1 { v = v * 10 + (c - 48); i = i + 1 } else { rd = 0 } }
56 }
57 out[which] = v
58 which = which + 1
59 }
60 return 0
61}
62
63// build the race kernel with <compiler>, run it, parse "<checksum> <cycles>" into
64// out[0],out[1]. Returns 0 on success, negative on a pipeline/parse failure.
65func cil_race(compiler: *u8, sout: *u8, eout: *u8, capf: *u8, out: *i64) -> i64 {
66 if gd_build(compiler, "runtime/_hdl_build/nx_race_kernel.nx" as *u8, sout, eout) != 0 { return 0 - 1 }
67 let av: *i64 = sys_mmap(16) as *i64
68 av[0] = eout as i64; av[1] = 0
69 gd_exec(av, capf, 1)
70 let buf: *u8 = sys_mmap(256)
71 let n: i64 = cil_read_file(capf, buf, 250)
72 if n <= 0 { return 0 - 2 }
73 return cil_parse_two(buf, n, out)
74}
75
76func main() -> i64 {
77 gd_puts("=== AUTONOMOUS codegen-improvement loop (self-host->verify->measure->govern) ===\n" as *u8)
78 let kg: *u8 = "_offc/nx_cc_known_good.elf" as *u8
79 let cc_src: *u8 = "runtime/nx_compile_x86.nx" as *u8
80
81 // 1. SELF-HOST the candidate compiler.
82 gd_puts(" [1] self-host candidate compiler ...\n" as *u8)
83 if gd_build(kg, cc_src, "/tmp/cil_cc.s" as *u8, "/tmp/cil_cc.elf" as *u8) != 0 {
84 gd_puts(" candidate build FAILED -> reject\n" as *u8); sys_exit(1); return 1
85 }
86 let cand: *u8 = "/tmp/cil_cc.elf" as *u8
87
88 // 2. VERIFY differentially on a corpus.
89 gd_puts(" [2] differential verify vs known-good ...\n" as *u8)
90 let corpus: *i64 = sys_mmap(8 * 4) as *i64
91 var ncp: i64 = 0
92 corpus[ncp] = ("runtime/_hdl_build/nx_u128_test.nx" as *u8) as i64; ncp = ncp + 1
93 corpus[ncp] = ("runtime/_hdl_build/nx_regalloc_linscan_test.nx" as *u8) as i64; ncp = ncp + 1
94 var diverge: i64 = 0
95 var ci: i64 = 0
96 while ci < ncp {
97 let src: *u8 = corpus[ci] as *u8
98 let ka: i64 = gd_build_run(kg, src, "/tmp/cil_k.s" as *u8, "/tmp/cil_k.elf" as *u8)
99 let ca: i64 = gd_build_run(cand, src, "/tmp/cil_c.s" as *u8, "/tmp/cil_c.elf" as *u8)
100 if ka < 0 { diverge = diverge + 1 }
101 if ca < 0 { diverge = diverge + 1 }
102 if ka != ca { diverge = diverge + 1 }
103 ci = ci + 1
104 }
105 gd_emit(" divergences (0=clean) : " as *u8, diverge)
106
107 // 3. MEASURE: race the same kernel through both compilers.
108 gd_puts(" [3] measure: race kernel, candidate vs known-good ...\n" as *u8)
109 let kr: *i64 = sys_mmap(16) as *i64
110 let cr: *i64 = sys_mmap(16) as *i64
111 let rk: i64 = cil_race(kg, "/tmp/cil_rk.s" as *u8, "/tmp/cil_rk.elf" as *u8, "/tmp/cil_rk.out" as *u8, kr)
112 let rc: i64 = cil_race(cand, "/tmp/cil_rc.s" as *u8, "/tmp/cil_rc.elf" as *u8, "/tmp/cil_rc.out" as *u8, cr)
113 var measured_ok: i64 = 0
114 var same_result: i64 = 0
115 var not_slower: i64 = 0
116 if rk == 0 { if rc == 0 {
117 measured_ok = 1
118 gd_emit(" known-good checksum : " as *u8, kr[0])
119 gd_emit(" candidate checksum : " as *u8, cr[0])
120 gd_emit(" known-good cycles : " as *u8, kr[1])
121 gd_emit(" candidate cycles : " as *u8, cr[1])
122 if kr[0] == cr[0] { same_result = 1 }
123 // allow 3% slack so noise alone is not a 'regression'
124 if cr[1] <= kr[1] + (kr[1] / 32) { not_slower = 1 }
125 } }
126
127 // 4. GOVERN: council ACT iff verified (no divergence) AND correct race AND not slower.
128 gd_puts(" [4] govern (crew council 3->2->1) ...\n" as *u8)
129 let a: *CrewAction = sys_mmap(64) as *CrewAction
130 let why: *i64 = sys_mmap(8) as *i64
131 var verifies: i64 = 0
132 if diverge == 0 { if measured_ok == 1 { if same_result == 1 { verifies = 1 } } }
133 cc_set(a, "promote candidate compiler (codegen change)" as *u8, verifies, 1, not_slower, 1, 1)
134 let vd: i64 = cc_council(a, why)
135 gd_puts(" verdict : " as *u8); gd_puts(cc_verdict_name(vd)); gd_puts("\n" as *u8)
136
137 gd_puts("----------------------------------------------------------------\n" as *u8)
138 gd_puts(" the team ran the whole codegen-improvement cycle itself: self-host a\n" as *u8)
139 gd_puts(" candidate, verify it, MEASURE it vs known-good, and let the council decide.\n" as *u8)
140 gd_puts(" A real regalloc change plugs into step 1's source -- same loop, measured win.\n" as *u8)
141
142 // GATE: the loop ran end to end; identity candidate is verified, correct, and
143 // not slower -> council ACT.
144 if diverge != 0 { sys_exit(2); return 2 }
145 if measured_ok != 1 { sys_exit(3); return 3 }
146 if same_result != 1 { sys_exit(4); return 4 }
147 if vd != CC_ACT { sys_exit(5); return 5 }
148 sys_exit(0)
149 return 0
150}