code wiki / _hdl_build / nx_js_feedback_gate.nx
nx_js_feedback_gate.nx source
↩ module page · 66 lines · 3300 B
1// nx_js_feedback_gate.nx -- P6 rung 1: TYPE FEEDBACK (Maglev's "runtime metadata collected during
2// unoptimized execution"). Proves the interpreter correctly classifies each BINOP site as INT-monomorphic
3// (safe for the optimizing tier to speculate unboxed i64) vs polymorphic. OPT-IN (js_fb_enable) = zero
4// baseline overhead. This is the substrate the CFG-SSA optimizing tier consumes. [[reference-js-optimizing-jit-sota-2026-07-08]]
5// expect_exit: 0 license_tier: ORIGINAL
6import "nx_js_vm.nx"
7func fw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
8func fn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var q: i64=k-1; var x: i64=0; while q>=0 { o[x]=t[q]; x=x+1; q=q-1 } sys_write(1,o,x); return 0 }
9
10func main(argc: i64, argv: *i64) -> i64 {
11 fw("=== nx_js_feedback_gate: P6 rung 1 -- type feedback (int-mono vs poly BINOP sites) ===\n" as *u8)
12 let out: *i64 = sys_mmap(16) as *i64
13 var pass: i64 = 0
14 var tot: i64 = 0
15 js_jit_disable(1) // force the INTERPRETER so feedback is collected (Maglev collects in the low tier)
16
17 // A) all-integer kernel: every BINOP site (+ - * <) sees int+int -> ALL should be INT-monomorphic, ZERO poly.
18 js_fb_enable(1)
19 compile_run("var s=0;var i=0;while(i<50){s=s+i*3-i;i=i+1;}s" as *u8, out)
20 let intA: i64 = js_fb_count(1)
21 let polyA: i64 = js_fb_count(2)
22 fw(" A all-int loop: int-mono sites=")
23 fn(intA)
24 fw(" poly sites=")
25 fn(polyA)
26 fw(" (result ")
27 fn(out[1])
28 fw(")\n")
29 tot = tot + 1
30 if intA > 0 { if polyA == 0 { pass = pass + 1; fw(" -> ok (all sites int-monomorphic, none poly)\n" as *u8) } else { fw(" -> FAIL (unexpected poly)\n" as *u8) } } else { fw(" -> FAIL (no int-mono sites recorded)\n" as *u8) }
31
32 // B) mixed int/float kernel: i*1.5 and s+float -> those sites MUST be classified polymorphic.
33 js_fb_enable(1)
34 compile_run("var s=0;var i=0;while(i<50){s=s+i*1.5;i=i+1;}s" as *u8, out)
35 let intB: i64 = js_fb_count(1)
36 let polyB: i64 = js_fb_count(2)
37 fw(" B mixed int/float loop: int-mono sites=")
38 fn(intB)
39 fw(" poly sites=")
40 fn(polyB)
41 fw("\n")
42 tot = tot + 1
43 if polyB > 0 { pass = pass + 1; fw(" -> ok (float-tainted sites correctly polymorphic)\n" as *u8) } else { fw(" -> FAIL (float site not flagged poly)\n" as *u8) }
44
45 // C) feedback OFF by default = zero recording (no overhead in production).
46 js_fb_enable(0)
47 js_jit_disable(0)
48 tot = tot + 1
49 // re-enable+reset, run nothing, expect all cold
50 js_fb_enable(1)
51 let coldC: i64 = js_fb_count(1) + js_fb_count(2)
52 js_fb_enable(0)
53 if coldC == 0 { pass = pass + 1; fw(" C reset clears the table (opt-in, zero baseline overhead): ok\n" as *u8) } else { fw(" C FAIL (stale feedback)\n" as *u8) }
54
55 fw(" --- ")
56 fn(pass)
57 fw("/")
58 fn(tot)
59 fw(" ---\n" as *u8)
60 if pass == tot {
61 fw("=== GREEN: type-feedback substrate works -- int-mono sites separable from poly (P6 rung 1) ===\n" as *u8)
62 return 0
63 }
64 fw("=== RED ===\n" as *u8)
65 return 1
66}