AVX-512 Ivory Instruction Stream Decoding Notes
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):
- 8-wide classify (branchless):
- Load 8 words (
vmovdqu64), extractcdr = (w >> 38) & 3anddtp = (w >> 32) & 0x3F. - Build 8-bit opmask registers (
k_fenceforcdr == 1,k_packedfor(dtp & 0x30) == 0x30,k_jumpforcdr == 3).
- Load 8 words (
- 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.
- Two ways to emit the threaded stream:
- Option A — Compacted trace via
vpcompressq/vpcompressd(your exact idea):- Build a 16-bit mask
k_emitwhere every Even slot is1and an Odd slot is1iffpacked && !cdr_jump && !fence. vpcompressstrips skipped Odd slots (fromCDR-JUMPand fullwords) into a dense array where every step isip++, and compresses a parallel[vma_off, phase]lane vector with the samek_emitmask 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).
- Build a 16-bit mask
- Option B — Fixed 512-slot strided page mirror (usually faster for Lisp!):
- Instead of compacting, emit all
512halfword 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 setsnext_stride = 2onCDR-JUMPor fullword Even slots and1otherwise (novpcompressneeded). - Sequential execution is just
ip += ip->stride; goto *ip->handler;— zeroCDR/Even-Odd checks — and every branch, call, or return lands in O(1) directly atpage_base[((vma & 0xFF) << 1) | is_odd]with no address-translation table.
- Instead of compacting, emit all
- Option A — Compacted trace via
2. Where the Real Speedup Comes From (and Bonus AVX-512 Wins)
| Component | Role & Expected Impact |
|---|---|
Direct-threaded loop (goto *ip->handler) + register-pinned ip/SP/TOS + page-end sentinel | The 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 decoder | Makes 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 ops | OP_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
- Coherence with Genera GC / self-modifying code: Track a
cached_code_ppnbitmap (32,768pages = 4 KB); any RAM write to a cached PPN invalidates that page's decoded slot (almost never triggers on normal data writes). libivory_core.sohot-reload safety:- Computed-goto handler tables (
static void * const handlers[]) land in.data.rel.ro(still0bytes.data/.bssunderreadelf -WS). - Never persist raw handler pointers across
dlclose/dlopen— either store relativeint32_toffsets / 16-bit handler IDs, or keep the decoded-page cache ephemeral (re-decoded lazily whenevercore_genchanges or on stack/scratch arena invalidated on reload).
- Computed-goto handler tables (
- ABI stability: Keep
sizeof(IvoryCpu) == 4096Uandsizeof(IvoryMem) == 32768Ufrozen (if you want a persistent decoded-page cache pointer, it fits insideIvoryMem._reserved— or keep a small cache owned insideivory_cpu_run/ allocated on first run and torn down onivory_mem_destroy). - Debugging/watchpoints: Fall back cleanly to the scalar
cpu_step_internalloop whenevertrace_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.