code wiki / _hdl_build / nx_rv64_tier_gate.nx
nx_rv64_tier_gate.nx source
↩ module page · 70 lines · 6627 B
1// nx_rv64_tier_gate.nx -- GATE: the TIERED EXECUTION ENGINE (interpret cold + JIT hot loops) is (a) CORRECT with JIT
2// OFF (matches the golden fk_run -- proves the tier interpreter is faithful), (b) CORRECT with JIT ON (the hot inner
3// loop of a nested loop is compiled + run natively, same result), (c) FASTER than pure interpretation (real mixed-
4// program acceleration -- the point of a JIT), and (d) FAIL-SAFE: a hot loop the JIT can't handle stays interpreted
5// and still gives the right answer. This is the SOTA emulator architecture end-to-end. expect_exit:0
6import "nx_syscalls.nx"
7import "nx_rv64_asm.nx"
8import "nx_rv64_fast.nx"
9import "nx_rv64_jit.nx"
10import "nx_rv64_tier.nx"
11
12func 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 }
13func g_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x<0{b[0]=45;sys_write(1,b,1);x=0-x} if x==0{b[0]=48;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 }
14func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
15
16const TMEMSZ: i64 = 4096
17func predec(src: *u8, code: *u8, opk: *i64, rd: *i64, rs1: *i64, rs2: *i64, imm: *i64) -> i64 {
18 let nb: i64 = rvasm_assemble_str(src, code, 4096); if nb<0 { return 0-1 } return fk_predecode(code, nb, opk, rd, rs1, rs2, imm)
19}
20
21func main() -> i64 {
22 g_puts("nx_rv64_tier_gate (tiered engine: interpret cold + JIT hot loops -- correct vs golden, faster, fail-safe)\n" as *u8)
23 var pass: i64=0; var total: i64=0
24 let code: *u8=sys_mmap(4096)
25 let opk: *i64=sys_mmap(128*8) as *i64; let rd: *i64=sys_mmap(128*8) as *i64; let rs1: *i64=sys_mmap(128*8) as *i64; let rs2: *i64=sys_mmap(128*8) as *i64; let imm: *i64=sys_mmap(128*8) as *i64
26 let rf: *i64=sys_mmap(32*8) as *i64; let rn: *i64=sys_mmap(32*8) as *i64; let rj: *i64=sys_mmap(32*8) as *i64
27 let mf: *u8=sys_mmap(TMEMSZ); let mn: *u8=sys_mmap(TMEMSZ); let mj: *u8=sys_mmap(TMEMSZ)
28 let stn: *i64=sys_mmap(64) as *i64; let stj: *i64=sys_mmap(64) as *i64
29
30 // P1: NESTED loop. outer 100 x inner 1000 -> total (a0) = 100000. the inner loop is the hot block that gets JIT'd.
31 let nc1: i64 = predec(" li a0, 0\n li a1, 0\n li a2, 100\nouter:\n li t0, 0\n li t1, 0\n li t2, 1000\ninner:\n addi t0, t0, 1\n addi t1, t1, 1\n blt t1, t2, inner\n add a0, a0, t0\n addi a1, a1, 1\n blt a1, a2, outer\n" as *u8, code, opk, rd, rs1, rs2, imm)
32
33 var zz: i64=0; while zz<32 { rf[zz]=0; zz=zz+1 }
34 let ft0: i64=sys_now_us(); fk_run(opk, rd, rs1, rs2, imm, nc1, rf, mf, TMEMSZ, 200000000, (0 as i64) as *NxVirtioMmio); let ft1: i64=sys_now_us(); let fk_us: i64=ft1-ft0
35 while zz<32 { rn[zz]=0; zz=zz+1 } zz=0; while zz<32 { rn[zz]=0; zz=zz+1 }
36 tier_run(opk, rd, rs1, rs2, imm, nc1, rn, mn, TMEMSZ, 200000000, (0 as i64) as *NxVirtioMmio, 1000000000, stn) // JIT disabled (huge threshold)
37 zz=0; while zz<32 { rj[zz]=0; zz=zz+1 }
38 let jt0: i64=sys_now_us(); tier_run(opk, rd, rs1, rs2, imm, nc1, rj, mj, TMEMSZ, 200000000, (0 as i64) as *NxVirtioMmio, 5, stj); let jt1: i64=sys_now_us(); let tier_us: i64=jt1-jt0
39
40 g_puts(" P1 nested loop: fk_run a0="); g_pn(rf[10]); g_puts(" | tier(no-JIT) a0="); g_pn(rn[10]); g_puts(" | tier(JIT) a0="); g_pn(rj[10]); g_puts(" (expect 100000)\n" as *u8)
41 g_puts(" tier(JIT) stats: interpreted-steps="); g_pn(stj[0]); g_puts(" loops-compiled="); g_pn(stj[1]); g_puts(" native-loop-runs="); g_pn(stj[2]); g_puts("\n" as *u8)
42 g_puts(" time: fk_run="); g_pn(fk_us); g_puts("us | tier(JIT)="); g_pn(tier_us); g_puts("us\n" as *u8)
43
44 var t1: i64=0; if rn[10]==rf[10] { if rn[10]==100000 { t1=1 } }
45 pass=pass+ck("T1: tier engine with JIT OFF == golden fk_run (the tier interpreter is faithful)" as *u8, t1); total=total+1
46 var t2: i64=0; if rj[10]==rf[10] { if stj[1]>=1 { if stj[2]>=1 { t2=1 } } }
47 pass=pass+ck("T2: tier engine with JIT ON == golden fk_run AND actually compiled + ran the hot loop natively" as *u8, t2); total=total+1
48 var t3: i64=0; if tier_us < fk_us { t3=1 }
49 var sp: i64=0; if tier_us>0 { sp=(fk_us*100)/tier_us }
50 g_puts(" tiered SPEEDUP over pure interpretation = "); g_pn(sp/100); g_puts("."); if sp%100<10 { g_puts("0" as *u8) } g_pn(sp%100); g_puts("x (hot inner loop runs native, cold code interpreted)\n" as *u8)
51 pass=pass+ck("T3: the tiered engine is FASTER than pure interpretation (real mixed-program acceleration)" as *u8, t3); total=total+1
52
53 // P4 (fail-safe teeth): a hot loop using XOR (interpreter handles it, the JIT does NOT) -> stays interpreted, still
54 // correct. proves an un-JIT-able hot loop degrades safely to interpretation.
55 let nc4: i64 = predec(" li t0, 0\n li t1, 0\n li t2, 100\n li t3, 7\nloopx:\n xor t0, t0, t3\n addi t1, t1, 1\n blt t1, t2, loopx\n" as *u8, code, opk, rd, rs1, rs2, imm)
56 zz=0; while zz<32 { rf[zz]=0; zz=zz+1 } fk_run(opk, rd, rs1, rs2, imm, nc4, rf, mf, TMEMSZ, 200000000, (0 as i64) as *NxVirtioMmio)
57 zz=0; while zz<32 { rj[zz]=0; zz=zz+1 } tier_run(opk, rd, rs1, rs2, imm, nc4, rj, mj, TMEMSZ, 200000000, (0 as i64) as *NxVirtioMmio, 5, stj)
58 g_puts(" P4 XOR loop: fk_run t0="); g_pn(rf[5]); g_puts(" | tier t0="); g_pn(rj[5]); g_puts(" | loops-compiled="); g_pn(stj[1]); g_puts(" (0 = JIT declined XOR, stayed interpreted)\n" as *u8)
59 var t4: i64=0; if rj[5]==rf[5] { if stj[1]==0 { t4=1 } }
60 pass=pass+ck("T4 (fail-safe): an un-JIT-able hot loop (XOR) stays interpreted and is still correct vs fk_run" as *u8, t4); total=total+1
61
62 var okall: i64=0; if pass==total { okall=1 }
63 g_puts("---- nx_rv64_tier_gate: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8)
64 if okall==1 {
65 let logf: i64=sys_openat_append("knowledge/status/rv64_tier.log" as *u8, 420)
66 if logf>=0 { let z: i64=sys_write(logf,"NXRV64TIER GREEN: tiered execution engine -- interpret cold via shared fk_step + JIT hot loops (profile back-edges, compile+cache, run native on re-entry); correct vs golden with JIT on/off, faster on a nested loop, fail-safe for un-JIT-able loops. the SOTA emulator architecture end-to-end\n" as *u8,300); sys_close(logf) }
67 g_puts("verdict=GREEN (tiered execution engine: interpret cold + JIT hot loops, correct vs the golden interpreter + faster + fail-safe; the SOTA emulator architecture, end-to-end sovereign)\n" as *u8); sys_exit(0); return 0
68 }
69 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
70}