Skip to content

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

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
Jefter5549:perf/insn-clzero-loop-and-clwb-patch
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
Jefter5549:perf/insn-clzero-loop-and-clwb-patch

Conversation

@Jefter5549

Copy link
Copy Markdown
Contributor

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.
@Jefter5549
Jefter5549 force-pushed the perf/insn-clzero-loop-and-clwb-patch branch from e7be544 to b25582a Compare November 23, 2026 21:17
@ivall
ivall merged commit 31def9e into xcl-dev:main Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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 pushed a commit that referenced this pull request Nov 23, 2026
emulate_console_only asked `readable` about the code at rip twice at every
exception, and is_writable asked about the target of every CLZERO and
CLZERO loop. VirtualQuery walks a region to its end to size it: the
title's code section for rip, and for a title's memset a committed window
of unified memory (gpu/uma.rs notes ~40 ms a call on a 4 GiB one). On an
Intel CPU a retail title's main thread was in that query in every thread
dump taken, and the title showed half a frame a second.

- The page rip is on is taken as there - the instruction that raised the
  exception was fetched from it - and only code bytes on the next page are
  asked about (code_bytes_at).
- A committed window of unified memory is not asked about, as in
  gpu::shader::readable, which the single CLZERO used before #217.

What the emulation does is unchanged.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@Jefter5549
Jefter5549 deleted the perf/insn-clzero-loop-and-clwb-patch branch November 25, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants