Repository navigation
perf(insn): CLZERO loop fast-forwarding and CLWB NOP hot-patching - #217
Merged
ivall merged 1 commit intoNov 23, 2026
Merged
Conversation
- 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
force-pushed
the
perf/insn-clzero-loop-and-clwb-patch
branch
from
November 23, 2026 21:17
e7be544 to
b25582a
Compare
Collaborator
|
Merged (main d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on main af20b4b: |
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::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.