XL1200-EMU Symbolics XL1200 Emulator
Architecture & GuidesDONE docs/avx512.md

AVX-512 Ivory Instruction Stream Decoding Notes

NOTE

Status: Implemented. Option B (fixed 512-slot strided decoded page mirror + AVX-512 VBMI page decoder + OS-independent Zen 4 x86-64 threaded assembly loop) is implemented in emu/src/cpu_decode.c, emu/src/cpu_zen4_avx512.c, and emu/src/cpu_zen4_loop.S. See docs/emulator/core-and-acceleration.md for the live implementation reference and benchmark results.

Prompt: /btw assuming we can go all out on inline assembly, would it be possible to use AVX512 to turn instruction stream into ordered (with already applied PC-stepping sequencing) list of decoded opcodes that can be just threading style executed off it?

Yes — completely feasible, and your host CPU (AMD Ryzen 9 7940HS, Zen 4) has full AVX-512 (avx512f, dq, bw, vl, vbmi, vbmi2, vpopcntdq) without Intel's older AVX-512 clock-throttling penalty.


1. How AVX-512 Decodes Ivory Words Into a Threaded Stream

Each IvoryWord in ivory_hw.h is a contiguous uint64_t, so 8 Ivory words fit in one 512-bit ZMM register (a full 256-word page is just 32 ZMM loads):

  1. 8-wide classify (branchless):
    • Load 8 words (vmovdqu64), extract cdr = (w >> 38) & 3 and dtp = (w >> 32) & 0x3F.
    • Build 8-bit opmask registers (k_fence for cdr == 1, k_packed for (dtp & 0x30) == 0x30, k_jump for cdr == 3).
  2. Split Even/Odd halfwords:
    • even = w & 0x3FFFF (<17:0>), odd = (w >> 18) & 0x3FFFF (<35:18>).
    • Extract 8-bit opcodes and 10-bit operands across all 16 candidate slots in parallel.
  3. Two ways to emit the threaded stream:
    • Option A — Compacted trace via vpcompressq/vpcompressd (your exact idea):
      • Build a 16-bit mask k_emit where every Even slot is 1 and an Odd slot is 1 iff packed && !cdr_jump && !fence.
      • vpcompress strips skipped Odd slots (from CDR-JUMP and fullwords) into a dense array where every step is ip++, and compresses a parallel [vma_off, phase] lane vector with the same k_emit mask so every entry knows its exact architectural PC.
      • Catch: Because skipped Odd slots are removed, branch/call targets inside the page need a small lookup map (vma_off, phase) -> compacted_idx (or you decode per basic block / trace).
    • Option B — Fixed 512-slot strided page mirror (usually faster for Lisp!):
      • Instead of compacting, emit all 512 halfword slots of a 256-word page (slot = ((vma & 0xFF) << 1) | is_odd), plus a slot 512 page-exit sentinel.
      • Each decoded entry stores { handler, operand_or_fullword, next_stride } where AVX-512 sets next_stride = 2 on CDR-JUMP or fullword Even slots and 1 otherwise (no vpcompress needed).
      • Sequential execution is just ip += ip->stride; goto *ip->handler; — zero CDR/Even-Odd checks — and every branch, call, or return lands in O(1) directly at page_base[((vma & 0xFF) << 1) | is_odd] with no address-translation table.

2. Where the Real Speedup Comes From (and Bonus AVX-512 Wins)

ComponentRole & Expected Impact
Direct-threaded loop (goto *ip->handler) + register-pinned ip/SP/TOS + page-end sentinelThe big win (~3–6× over switch interpreter, ~120–250+ MIPS): eliminates fetch, Even/Odd, CDR, double-switch, irq_active_cached per-insn checks (move to branches/calls/page-exit), and cpu->pc updates (reconstruct PC from ip - page_base only on traps/calls/branches).
AVX-512 page decoderMakes decoding a whole 256-word page take ~150–250 ns (~32 vector iterations), so cold code pages and GC-invalidated pages refill virtually for free.
Bonus AVX-512 in mem.c (PHT)Each Ivory PHT bucket has 4 entries (pht0 every 2 words). One vpcmpeqd checks all 4 VPNs in a bucket in a single cycle!
Bonus AVX-512 in stack/VRAM opsOP_STACK_BLT, stack-cache spill/fill, and VRAM BITBLT can move 8 IvoryWords (64 B) per instruction.

3. Caveats Specific to emu/ & Live Hot-Reload

  1. Coherence with Genera GC / self-modifying code: Track a cached_code_ppn bitmap (32,768 pages = 4 KB); any RAM write to a cached PPN invalidates that page's decoded slot (almost never triggers on normal data writes).
  2. libivory_core.so hot-reload safety:
    • Computed-goto handler tables (static void * const handlers[]) land in .data.rel.ro (still 0 bytes .data/.bss under readelf -WS).
    • Never persist raw handler pointers across dlclose/dlopen — either store relative int32_t offsets / 16-bit handler IDs, or keep the decoded-page cache ephemeral (re-decoded lazily whenever core_gen changes or on stack/scratch arena invalidated on reload).
  3. ABI stability: Keep sizeof(IvoryCpu) == 4096U and sizeof(IvoryMem) == 32768U frozen (if you want a persistent decoded-page cache pointer, it fits inside IvoryMem._reserved — or keep a small cache owned inside ivory_cpu_run / allocated on first run and torn down on ivory_mem_destroy).
  4. Debugging/watchpoints: Fall back cleanly to the scalar cpu_step_internal loop whenever trace_enabled, breakpoints, or watchpoints are active.

Bottom line: Combining a 512-slot strided direct-threaded page cache with an AVX-512 page decoder (and inline-asm/register-pinned threaded loop) is completely viable on your Zen 4 host and is the cleanest path to pushing the emulator past 150–250 MIPS without building a full x86-64 JIT.