code wiki / (root) / nx_probe_bchk_asm.nx

nx_probe_bchk_asm.nx source

↩ module page · 23 lines · 1258 B

1// nx_probe_bchk_asm.nx -- INSTRUMENT, not a witness: the smallest bounds-checked hot loop, so the 2// emitted assembly for ONE checked access can be read directly. It answers "what does the check 3// actually cost in instructions" before anyone decides how to make it cheaper -- and on 4// 2026-08-14 it re-ranked the whole perf lane by showing the answer is NOT the check. 5// 6// COMPILE IT AND READ main's bb2/bb4/bb6. Measured that day on the shipped compiler: ~17 7// instructions per iteration, of which the bounds check is 2 compares + 2 branches and SIX are 8// pure memory traffic. The loop counter lives in r12 and the accumulator in r13 for the entire 9// loop -- register allocation WORKED -- and the emitter still spills both to stack homes on 10// entry and reloads the counter TWICE more from a register that never changed. 11// => the dominant cost is the backend not consuming the allocation it already computed, not the 12// check and not missing phi nodes (the loop-carried values are already in registers here). 13// Debt: lang-perf-regalloc-unconsumed. Keep this file: it is the reproducer. 14func main(argc: i64, argv: *u8) -> i64 { 15 var a: [1024]i64 16 var i: i64 = 0 17 var s: i64 = 0 18 while i < 1024 { 19 s = s + a[i] 20 i = i + 1 21 } 22 return s 23}