perf(insn): CLZERO loop fast-forwarding and CLWB NOP hot-patching #217
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!217
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/217/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCFollow-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::decodenow recognizes this 12-byte pattern (ConsoleOnly::ClzeroLoop { end_reg }), clears[rax..end)in one burst viawrite_bytes, and steps past the loop (+12 bytes).• CLWB NOP hot-patching: hot-patches the instruction bytes with NOPs (
0x90) viaVirtualProtect. Host memory caches are coherent, eliminating repeated exception overhead across subsequent frames and update loops.• Memory writability check:
is_writablechecksMEM_COMMITandPAGE_READWRITE/PAGE_EXECUTE_READWRITEviaVirtualQuerybeforewrite_bytesto prevent taking an unhandled AV inside the VEH.• MWAITX: added
std::hint::spin_loop()hint.• Crash diagnostics: include volatile scratch registers
r10andr11inunhandled_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.@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 mainaf20b4b: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).