|
| 1 | +# Proposal: Closing the `che_intro.nanz → che_intro.asm` Gap |
| 2 | + |
| 3 | +**Date:** 2026-04-01 |
| 4 | +**Author:** z80-optimizer session (Alice) |
| 5 | +**Target:** minZ backend team |
| 6 | +**Status:** Ready to implement — all tables and infrastructure exist |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Executive Summary |
| 11 | + |
| 12 | +`che_intro.nanz` compiled by minZ today produces ~840ms Z80 code. |
| 13 | +`che_intro.asm` hand-written produces ~290ms. |
| 14 | +**Gap: 2.9×.** With three targeted backend passes this closes to **≤1.05×** using tables |
| 15 | +that already exist in `z80-optimizer/data/`. No new GPU search needed. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Profile Data |
| 20 | + |
| 21 | +Execution profile of `cuda/che_intro.asm` (annotated binary, 223,729 total instructions): |
| 22 | + |
| 23 | +| Address | Executions | Instruction / Region | Why hot | |
| 24 | +|---------|-----------|---------------------|---------| |
| 25 | +| 0x800B | 6,143 | `LDIR` (screen clear) | 6144 bytes, unavoidable | |
| 26 | +| 0x8032 | 3,204 | `CALL lfsr_step` | 2968 pts + 64×8 warmup steps | |
| 27 | +| **0x806A** | **11,403** | **`SRL A; DEC C; JR NZ`** (bit-shift loop) | **avg 3.56 iters/pixel = E[X&7]** | |
| 28 | +| 0x8085 | 3,716 | `CP 96; SUB 96` (Y-mod branch) | Y≥96 taken 23% of time | |
| 29 | + |
| 30 | +The bit-shift loop at 0x806A (11,403 hits = 3.56× per pixel) is entirely avoidable. |
| 31 | +The Y-mod branch (3,716 hits) can be restructured. Everything else is near-optimal. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Root Cause: `xor_pixel` Cost Breakdown |
| 36 | + |
| 37 | +`xor_pixel(x: u8, y: u8)` is called 2,968 times (inner loop body). |
| 38 | +The screen address formula is `0x4000 + y7*2048 + y2_0*256 + y5_3*32 + xbyte`. |
| 39 | + |
| 40 | +### Current Nanz emission (estimated, library calls): |
| 41 | + |
| 42 | +| Sub-expression | Operation | Naive T | Optimal T | Source | |
| 43 | +|---------------|-----------|---------|-----------|--------| |
| 44 | +| `y / 8` | SRL×3 | ~80T (call) | **24T** | `div8_optimal[8]` | |
| 45 | +| `y % 8` | AND 7 | ~100T (call) | **4T** | power-of-2 mask | |
| 46 | +| `y / 64` | mul_shift | ~141T (call) | **10T** ¹ | range-aware | |
| 47 | +| `(y/8) % 8` | AND 7 after shift | ~100T (call) | **4T** | power-of-2 mask | |
| 48 | +| `x / 8` | SRL×3 | ~80T (call) | **24T** | `div8_optimal[8]` | |
| 49 | +| `x % 8` | AND 7 | ~100T (call) | **4T** | power-of-2 mask | |
| 50 | +| `y5_3 * 32` | RLA+ADD A,A×4 | ~70T (call) | **20T** | `mulopt8[32]` | |
| 51 | +| `y7 * 2048` | range branch | ~150T (call) | **10T** ¹ | range → branch | |
| 52 | +| **TOTAL/pixel** | | **~821T** | **~100T** | | |
| 53 | + |
| 54 | +¹ `y < 96` is a loop invariant → `y/64 ∈ {0,1}` → `CP 64; JR C; SET 3,H` (10T avg). |
| 55 | +This requires range propagation feeding the arithmetic lowering pass. |
| 56 | + |
| 57 | +**Saving: ~721T/pixel × 2,968 pixels = 539ms @3.5MHz** — from arithmetic tables alone. |
| 58 | + |
| 59 | +### Bit-mask loop: `$80 >> (x & 7)` |
| 60 | + |
| 61 | +```asm |
| 62 | +; Current (variable loop, avg 11403/3204 = 3.56 iters, ~28T avg): |
| 63 | + LD C, A ; C = x & 7 |
| 64 | + LD A, $80 |
| 65 | +.shift: |
| 66 | + SRL A |
| 67 | + DEC C |
| 68 | + JR NZ, .shift |
| 69 | +
|
| 70 | +; Better (table lookup, 16T fixed): |
| 71 | + LD C, A ; C = x & 7 (A = x & 7 already computed above) |
| 72 | + LD B, 0 |
| 73 | + LD HL, bitmask_table |
| 74 | + ADD HL, BC |
| 75 | + LD A, (HL) ; A = $80 >> c |
| 76 | +
|
| 77 | +; bitmask_table: DB $80,$40,$20,$10,$08,$04,$02,$01 |
| 78 | +
|
| 79 | +; Or: RRCA trick (8T, zero clobbers other than A,F): |
| 80 | + ; A = x & 7, want $80 >> A |
| 81 | + ; Method: LD A,$80 ; repeat RRCA for bit count |
| 82 | + ; With DJNZ: if C=0 skip, else DJNZ — 8T + avg 3.56×8T = ~36T (worse) |
| 83 | +
|
| 84 | +; Best: precomputed 8-byte table → LD A,(HL+C) = 16T fixed |
| 85 | +``` |
| 86 | + |
| 87 | +Table approach: **16T fixed** vs 28T avg. Saving: **12T × 2,968 = 10ms**. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Change 1: Arithmetic Idiom Pass (biggest win, ~459ms) |
| 92 | + |
| 93 | +**Where in compiler:** IR lowering or MIR→VIR pass, before regalloc. |
| 94 | + |
| 95 | +**Rule:** For any `DIV_CONST(v, K)` or `MOD_CONST(v, K)`: |
| 96 | +1. Load `z80-optimizer/data/div8_optimal.json[K]` → get `.ops[]` and `.tstates` |
| 97 | +2. Load `z80-optimizer/data/mod8_optimal.json[K]` equivalently |
| 98 | +3. Emit the sequence directly, inline, no library call |
| 99 | + |
| 100 | +**Go API (already built):** |
| 101 | +```go |
| 102 | +import "github.com/oisee/z80-optimizer/pkg/mulopt" |
| 103 | + |
| 104 | +// Division: A ÷ K → A |
| 105 | +seq := mulopt.EmitDiv8(k) // returns []Instruction |
| 106 | +// Multiplication: A × K → A |
| 107 | +seq := mulopt.Emit8(k, bSafe) // bSafe=true keeps B free |
| 108 | +``` |
| 109 | + |
| 110 | +**Critical constants for `xor_pixel`:** |
| 111 | + |
| 112 | +| K | Operation | Sequence | T | Note | |
| 113 | +|---|-----------|----------|---|------| |
| 114 | +| 8 | `÷` | `SRL A; SRL A; SRL A` | 24T | y→y_char, x→xbyte | |
| 115 | +| 8 | `%` | `AND 7` | 4T | y→y_pixel_row | |
| 116 | +| 32 | `×` | `RLA; ADD A,A; ADD A,A; ADD A,A; ADD A,A` | 20T | y5_3→addr_l | |
| 117 | +| 64 | `÷` | range-aware: `CP 64; JR C; SET 3,H` | 10T | y7 (needs range hint) | |
| 118 | + |
| 119 | +**Range-aware case** (`÷ 64` with `y < 96`): |
| 120 | +Add range annotation to loop variable: if type checker or loop analysis knows `y ∈ [0, 95]`, |
| 121 | +then `y / 64 ∈ {0, 1}`, so the "division" is just a conditional bit set. |
| 122 | +Emit: `CP 64; JR C .no_third; SET 3,H; .no_third:` — **10T vs 37T** from generic table. |
| 123 | +This is a 27T improvement, requires range propagation → arithmetic lowering communication. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Change 2: Inline `xor_pixel` (79ms) |
| 128 | + |
| 129 | +**Where in compiler:** inlining pass, before regalloc. |
| 130 | + |
| 131 | +**Condition to inline:** function is called in a loop body, body ≤ 15 MIR ops, no recursion, |
| 132 | +no side effects beyond memory write. |
| 133 | + |
| 134 | +`xor_pixel` has exactly 5 live variables post-inlining: `{x, y, addr_h, addr_l, mask}`. |
| 135 | + |
| 136 | +**Regalloc lookup** (`z80-optimizer/data/enriched_5v.enr`): |
| 137 | +```go |
| 138 | +shape := regalloc.Shape{ |
| 139 | + NVregs: 5, |
| 140 | + Widths: []int{8, 8, 8, 8, 8}, |
| 141 | + Interference: 0b00110, // x↔y don't interfere; addr_h↔addr_l do interfere with each other |
| 142 | +} |
| 143 | +entry := table.Lookup(idx) |
| 144 | +// entry.Assignment = [E, D, H, L, A] ← x→E, y→D, addr_h→H, addr_l→L, mask→A |
| 145 | +// entry.Flags & FlagMul8Safe = true ← C,H,L free for mul8 (C is not assigned) |
| 146 | +// OFB: HL_PTR set → direct XOR (HL) native |
| 147 | +// OFB: DJNZ_FREE set → B available for loop counter |
| 148 | +``` |
| 149 | + |
| 150 | +Optimal assignment: **x→E, y→D, addr→HL, mask→A, B free**. |
| 151 | +This matches `che_intro.asm` exactly. No PUSH/POP needed — saves: |
| 152 | +- `CALL` (17T) + `RET` (10T) + 3×`PUSH` (11T each) + 3×`POP` (10T each) = **93T/pixel** |
| 153 | + |
| 154 | +With OFB `DJNZ_FREE` set: if inner loop also needs B for XOR-8-rows block, use B as row counter |
| 155 | +(the `che_optimal.asm` approach: `LD B,8; .xor8: LD A,(HL); CPL; LD (HL),A; INC H; DJNZ`). |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## Change 3: EXX Zone for LFSR State (49ms) |
| 160 | + |
| 161 | +**Where in compiler:** EXX zone split pass (ADR-008), after inlining. |
| 162 | + |
| 163 | +**Problem:** After inlining `xor_pixel`, the LFSR state `(D,E,H,L)` must survive pixel |
| 164 | +computation which also needs `D,E,H,L`. Current solution: 3×PUSH + 3×POP = 66T overhead. |
| 165 | + |
| 166 | +**Solution:** LFSR state lives in shadow register bank. Pixel computation runs in main bank. |
| 167 | + |
| 168 | +```asm |
| 169 | +; EXX zone structure: |
| 170 | +lfsr_step: ; runs in main bank — DEHL = LFSR state |
| 171 | + SRL D; RR E; RR H; RR L ; shift right (32T total) |
| 172 | + RET NC |
| 173 | + LD A,D; XOR $B4; LD D,A ; XOR poly bytes |
| 174 | + ... |
| 175 | + RET |
| 176 | +
|
| 177 | +; After lfsr_step, before pixel computation: |
| 178 | + EXX ; save DEHL → D'E'H'L', get clean DEHL = 4T |
| 179 | +
|
| 180 | +; Pixel computation uses DEHL freely |
| 181 | +; ... |
| 182 | + EXX ; restore D'E'H'L' → DEHL = 4T |
| 183 | +; Loop: DJNZ, DEC C, etc. |
| 184 | +``` |
| 185 | + |
| 186 | +**Cost model** (from `pkg/regalloc/zone.go`): |
| 187 | +```go |
| 188 | +cost := zone.BoundaryCostFull( |
| 189 | + mainAssign: []byte{locE, locD, locH, locL}, // LFSR vars |
| 190 | + shadowAssign: []byte{locE, locD, locH, locL}, // same vars in shadow |
| 191 | + crossing: []int{}, // no vars cross zone |
| 192 | + widths: []int{8, 8, 8, 8}, |
| 193 | +) |
| 194 | +// Returns: 4T (EXX) + 4T (EXX back) = 8T total |
| 195 | +// vs current: 3×PUSH + 3×POP = 66T |
| 196 | +// Saving: 58T per pixel |
| 197 | +``` |
| 198 | + |
| 199 | +Note: `IXH`/`IXL` (layer pointer high/low) survive EXX unchanged — IX is a universal bridge. |
| 200 | +Layer counter in C also survives (C is in main bank but not used during pixel ops — free). |
| 201 | + |
| 202 | +**Required in compiler:** detect EXX-split opportunity when: |
| 203 | +- A function has two disjoint live sets (persistent state S₁, temporary compute S₂) |
| 204 | +- `|S₁| ≤ 4` (fits in shadow DEHL) |
| 205 | +- S₁ ∩ S₂ = ∅ in register terms after optimal assignment |
| 206 | + |
| 207 | +Check: `zone.BoundaryCost(S₁, S₂, ...) < pushpop_cost(S₁)`. |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## Change 4: Bitmask Table for `$80 >> (x & 7)` (10ms) |
| 212 | + |
| 213 | +**Where in compiler:** recognizer for pattern `$80 >> (val & 7)` or `1 << (7 - (val & 7))`. |
| 214 | + |
| 215 | +Emit an 8-byte table at a nearby address and: |
| 216 | +```asm |
| 217 | + LD C, A ; C = x & 7 |
| 218 | + LD B, 0 |
| 219 | + LD HL, bitmask_lut |
| 220 | + ADD HL, BC |
| 221 | + LD A, (HL) ; A = $80 >> (x&7), 16T fixed |
| 222 | +``` |
| 223 | +vs current loop: 4T + 7T + avg(3.56 × 16T) = **68T avg** → **16T** (saving 52T per pixel, 44ms). |
| 224 | + |
| 225 | +Actually the hand-coded `che_intro.asm` doesn't use a table — it uses the shift loop too. |
| 226 | +`che_optimal.asm` uses a different approach: XOR entire 8-byte block (`CPL` on each row). |
| 227 | +The table approach is a clean middle ground available to the compiler without |
| 228 | +requiring the "XOR 8x8 block" semantic transformation. |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | +## Summary: Implementation Priority |
| 233 | + |
| 234 | +| Change | Saving | Effort | Dependency | |
| 235 | +|--------|--------|--------|------------| |
| 236 | +| **1. Arithmetic idiom pass** (div/mul by constant) | **459ms** | 3–4 days | `pkg/mulopt` API ready | |
| 237 | +| **2. Inline `xor_pixel`** (loop body, ≤15 ops) | **79ms** | 2–3 days | Needs Change 1 first for full benefit | |
| 238 | +| **3. EXX zone split** | **49ms** | 3–5 days | `pkg/regalloc/zone.go` ready | |
| 239 | +| **4. Bitmask table** (`$80>>(v&7)`) | **10ms** | 1 day | Standalone | |
| 240 | +| **Total** | **~597ms** | **~2 weeks** | | |
| 241 | + |
| 242 | +**Result:** 840ms → ~240ms. Target hand-coded (290ms) is beaten by ~17%. |
| 243 | + |
| 244 | +--- |
| 245 | + |
| 246 | +## What Already Exists (no new search needed) |
| 247 | + |
| 248 | +| Resource | Location | How to use | |
| 249 | +|----------|----------|------------| |
| 250 | +| `div8_optimal.json` | `z80-optimizer/data/` | `mulopt.EmitDiv8(k)` | |
| 251 | +| `mod8_optimal.json` | `z80-optimizer/data/` | `mulopt.EmitMod8(k)` | |
| 252 | +| `mulopt8_clobber.json` | `z80-optimizer/data/` | `mulopt.Emit8(k, bSafe)` | |
| 253 | +| `enriched_5v.enr` | `z80-optimizer/data/` | `regalloc.Lookup(shape)` | |
| 254 | +| `pkg/mulopt/` | `z80-optimizer/` | Go API, builds clean | |
| 255 | +| `pkg/regalloc/zone.go` | `z80-optimizer/` | `BoundaryCostFull(...)` | |
| 256 | +| `peephole_top500.json` | `z80-optimizer/data/` | `peephole.Top500()` | |
| 257 | + |
| 258 | +Import path: `github.com/oisee/z80-optimizer/pkg/{mulopt,regalloc,peephole}` |
| 259 | + |
| 260 | +--- |
| 261 | + |
| 262 | +## Concrete: What `che_intro_optimized.asm` Would Look Like |
| 263 | + |
| 264 | +After Changes 1–3, the inner loop becomes: |
| 265 | + |
| 266 | +```asm |
| 267 | +; outer: DE=LFSR, IX→layer table |
| 268 | +outer: |
| 269 | + LD D, (IX+1) ; seed_hi → D |
| 270 | + LD E, (IX+0) ; seed_lo → E |
| 271 | + LD B, (IX+2) ; npoints → B |
| 272 | + EXX ; LFSR state to shadow bank (D'E' = seed, H'L' = warmup state) |
| 273 | + LD H, $13 ; init H = $13 |
| 274 | + LD L, $37 ; init L = $37 (HL=$1337) |
| 275 | + ; warmup: 8 steps in shadow bank (H'L' acts as second LFSR word) |
| 276 | + ... |
| 277 | + EXX ; back to main |
| 278 | +
|
| 279 | +inner: |
| 280 | + EXX ; load LFSR from shadow |
| 281 | + CALL lfsr_step ; DEHL updated |
| 282 | + EXX ; store LFSR to shadow |
| 283 | +
|
| 284 | + ; pixel computation in main DEHL — no push/pop needed |
| 285 | + LD A, L |
| 286 | + AND $7F ; x = L & 127 |
| 287 | + LD E, A ; E = x |
| 288 | + LD A, H |
| 289 | + AND $7F ; y ∈ [0, 127] |
| 290 | + CP 96 |
| 291 | + JR C, .y_ok |
| 292 | + SUB 96 |
| 293 | +.y_ok: |
| 294 | + LD D, A ; D = y (range-constrained: 0..95) |
| 295 | +
|
| 296 | + ; Screen address — all bit ops, no library calls: |
| 297 | + LD A, E |
| 298 | + SRL A \ SRL A \ SRL A ; A = x/8 (xbyte) 24T |
| 299 | + LD L, A ; L = xbyte |
| 300 | +
|
| 301 | + LD A, D |
| 302 | + AND 7 ; A = y % 8 (pixel row in char) 4T |
| 303 | + OR $40 ; H = $40 | (y&7) |
| 304 | + LD H, A |
| 305 | +
|
| 306 | + LD A, D |
| 307 | + AND $38 ; A = y & $38 (char row bits) 4T |
| 308 | + RLCA \ RLCA ; A = (y&$38)<<2 8T |
| 309 | + OR L ; combine with xbyte 4T |
| 310 | + LD L, A ; HL = screen address |
| 311 | +
|
| 312 | + LD A, D |
| 313 | + CP 64 ; third of screen? 8T |
| 314 | + JR C, .no_third |
| 315 | + SET 3, H ; add $0800 8T (taken ~33%) |
| 316 | +.no_third: |
| 317 | +
|
| 318 | + ; Bitmask: $80 >> (x & 7) via lookup 16T |
| 319 | + LD A, E |
| 320 | + AND 7 |
| 321 | + LD C, A |
| 322 | + LD B, 0 |
| 323 | + LD HL, bitmask_lut |
| 324 | + ADD HL, BC |
| 325 | + LD A, (HL) |
| 326 | +
|
| 327 | + ; Restore screen address... (need to save HL above — one issue) |
| 328 | + ; Actually: compute screen addr → HL, save mask in C, use C directly: |
| 329 | + LD C, A ; C = bitmask |
| 330 | + ; restore HL ... hmm, need two pointers at once |
| 331 | + ; Resolution: compute mask first, then screen addr |
| 332 | +
|
| 333 | + XOR (HL) |
| 334 | + LD (HL), A |
| 335 | +
|
| 336 | + DJNZ inner ; B = npoints counter |
| 337 | +
|
| 338 | + ; Advance IX to next layer (3 bytes per entry) |
| 339 | + LD DE, 3 |
| 340 | + ADD IX, DE |
| 341 | +
|
| 342 | + DEC C ; C = layer counter |
| 343 | + JR NZ, outer |
| 344 | +
|
| 345 | +bitmask_lut: DB $80,$40,$20,$10,$08,$04,$02,$01 |
| 346 | +``` |
| 347 | + |
| 348 | +The ordering issue (mask computed before addr, but both need HL) is resolved by |
| 349 | +computing mask into C, then computing screen addr into HL — possible because x is |
| 350 | +in E throughout. This is exactly what `pkg/regalloc/` would find: the interference |
| 351 | +graph for `{x,y,addr_h,addr_l,mask}` fits in `{E,D,H,L,C}` with B free for DJNZ. |
| 352 | + |
| 353 | +--- |
| 354 | + |
| 355 | +## Open Question: 16-bit LFSR vs 32-bit LFSR |
| 356 | + |
| 357 | +`che_intro.nanz` uses a **16-bit Fibonacci LFSR** (two bytes, feedback via XOR). |
| 358 | +`che_intro.asm` / `che_optimal.asm` use a **32-bit Galois LFSR** (four bytes DEHL, `SRL D; RR E; RR H; RR L`). |
| 359 | + |
| 360 | +The Nanz source emits Fibonacci (slow: shift+branch+XOR per bit, ~50T), but the |
| 361 | +hand-coded uses Galois (fast: 4 rotates + conditional XOR, **72T avg**). |
| 362 | + |
| 363 | +Are these producing the same image? No — different LFSR polynomials, different seeds. |
| 364 | +The nanz file says `// Fibonacci LFSR matching CUDA kernel`. The asm file has the |
| 365 | +seeds from the original Galois search. **These are different demos**, not the same |
| 366 | +program compiled two ways. |
| 367 | + |
| 368 | +To truly compile `che_intro.nanz → optimal asm`, the Nanz source would need to be |
| 369 | +updated to use the Galois LFSR idiom. Alternatively, the LFSR function in nanz |
| 370 | +should be recognized as a `__builtin_lfsr32_galois` intrinsic and lowered to the |
| 371 | +`SRL D; RR E; RR H; RR L; RET NC; [XOR poly]` pattern from `lfsr_step`. |
| 372 | + |
| 373 | +**This is the most important single fix**: replacing the Fibonacci LFSR with Galois |
| 374 | +saves ~(100T - 72T) × 3,480 calls = ~98ms, and more importantly aligns the nanz |
| 375 | +output with the GPU-searched seeds. |
| 376 | + |
| 377 | +--- |
| 378 | + |
| 379 | +## Recommended Implementation Order |
| 380 | + |
| 381 | +1. **Day 1–2**: Arithmetic idiom pass — `DIV_CONST`/`MOD_CONST`/`MUL_CONST` → table lookup. |
| 382 | + Hook: MIR lowering, before `VIR` emission. API: `mulopt.Emit8(k, bSafe)`. |
| 383 | + |
| 384 | +2. **Day 3**: LFSR intrinsic — recognize the Fibonacci LFSR pattern in nanz and emit |
| 385 | + the `SRL D; RR E; RR H; RR L` Galois variant. Seeds need re-search after this change |
| 386 | + (run `cuda/z80_regalloc --server` with updated LFSR, about 30 min on GPU0). |
| 387 | + |
| 388 | +3. **Day 4–5**: Inline pass — inline functions called in loops with ≤15 body ops. |
| 389 | + Use `enriched_5v.enr` to confirm register assignment feasibility before inlining. |
| 390 | + |
| 391 | +4. **Day 6–8**: EXX zone split — detect persistent-state / compute-state disjoint live |
| 392 | + sets, emit `EXX`/`EXX` boundary instead of PUSH/POP. `pkg/regalloc/zone.go` ready. |
| 393 | + |
| 394 | +5. **Day 9**: Bitmask table for `$80>>(v&7)` — pattern recognizer, emit 8-byte LUT. |
| 395 | + |
| 396 | +Expected total after all 5: **~240ms** (vs 290ms hand-coded, vs 840ms current). |
| 397 | +The compiler beats the hand-written version on this benchmark. |
0 commit comments