perf(insn): CLZERO loop fast-forwarding and CLWB NOP hot-patching #217

Manually merged
github merged 1 commit from gh-pr/217/head into gh-pr/217/base 2026-11-26 04:15:50 +00:00
Owner

@⁠Jefter5549 on GitHub · 2026-11-23 21:12 UTC · original
Jefter5549:perf/insn-clzero-loop-and-clwb-patch → main · +75 −4 · 2 files · 1 commits · merged 2026-11-23 21:24 UTC

Follow-up to #151 with performance and safety improvements for non-Zen / older host CPUs:

• CLZERO loop fast-path: titles emit 12-byte unrolled loops (clzero; add rax, 0x40; cmp rax, <reg>; jb -12) during heap allocations. Emulating 64 bytes per exception generates an exception storm (18,000+ VEH round-trips/s). insn::decode now recognizes this 12-byte pattern (ConsoleOnly::ClzeroLoop { end_reg }), clears [rax..end) in one burst via write_bytes, and steps past the loop (+12 bytes).
• CLWB NOP hot-patching: hot-patches the instruction bytes with NOPs (0x90) via VirtualProtect. Host memory caches are coherent, eliminating repeated exception overhead across subsequent frames and update loops.
• Memory writability check: is_writable checks MEM_COMMIT and PAGE_READWRITE / PAGE_EXECUTE_READWRITE via VirtualQuery before write_bytes to prevent taking an unhandled AV inside the VEH.
• MWAITX: added std::hint::spin_loop() hint.
• Crash diagnostics: include volatile scratch registers r10 and r11 in unhandled_filter.

**@⁠Jefter5549** on GitHub · 2026-11-23 21:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/217) `Jefter5549:perf/insn-clzero-loop-and-clwb-patch` → `main` · +75 −4 · 2 files · 1 commits · merged 2026-11-23 21:24 UTC Follow-up to #151 with performance and safety improvements for non-Zen / older host CPUs: • CLZERO loop fast-path: titles emit 12-byte unrolled loops (`clzero; add rax, 0x40; cmp rax, <reg>; jb -12`) during heap allocations. Emulating 64 bytes per exception generates an exception storm (18,000+ VEH round-trips/s). `insn::decode` now recognizes this 12-byte pattern (`ConsoleOnly::ClzeroLoop { end_reg }`), clears `[rax..end)` in one burst via `write_bytes`, and steps past the loop (+12 bytes). • CLWB NOP hot-patching: hot-patches the instruction bytes with NOPs (`0x90`) via `VirtualProtect`. Host memory caches are coherent, eliminating repeated exception overhead across subsequent frames and update loops. • Memory writability check: `is_writable` checks `MEM_COMMIT` and `PAGE_READWRITE` / `PAGE_EXECUTE_READWRITE` via `VirtualQuery` before `write_bytes` to prevent taking an unhandled AV inside the VEH. • MWAITX: added `std::hint::spin_loop()` hint. • Crash diagnostics: include volatile scratch registers `r10` and `r11` in `unhandled_filter`.
- insn: recognize 12-byte unrolled CLZERO loops (clzero; add rax, 0x40; cmp rax, <reg>; jb -12) emitted across title memory allocators, returning ConsoleOnly::ClzeroLoop { end_reg }.
- eh: emulate ClzeroLoop in one burst via std::ptr::write_bytes at full memory bandwidth, advancing Rax to end and stepping over the 12-byte loop body.
- eh: replace CLWB opcode bytes with NOPs (0x90) via VirtualProtect; PC host cache is coherent, avoiding repeated VEH exception overhead in tight update loops.
- eh: verify destination writability (MEM_COMMIT + PAGE_READWRITE) before write_bytes to prevent unhandled access violations in the handler.
- eh: include r10 and r11 in unhandled exception register dump.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:15:49 +00:00
github manually merged commit b25582a392 into gh-pr/217/base 2026-11-26 04:15:50 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:24 UTC · original

Merged (main d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on main af20b4b: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 605 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed; and the retail game still reaches its first menu at 30 fps (720 draws per 2 s).

**@⁠ivall** on GitHub · 2026-11-23 21:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/217#issuecomment-7114416854) Merged (main d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on main af20b4b: `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 605 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed; and the retail game still reaches its first menu at 30 fps (720 draws per 2 s).
This discussion has been locked. Commenting is limited to contributors.
No description provided.