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

Merged
Jefter5549 merged 1 commit from upstream-pr/217 into main 2026-11-23 21:24:02 +00:00
Jefter5549 commented 2026-11-23 21:12:47 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Jefter5549 on 2026-11-23T21:12:47Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6568941016 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/217). Originally posted by [Jefter5549](https://github.com/Jefter5549) on 2026-11-23T21:12:47Z. --- 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`.
upstream-sync closed this pull request 2026-11-24 06:57:41 +00:00
ivall commented 2026-11-24 07:04:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:24:21Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7114416854 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/217#issuecomment-7114416854). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:24:21Z. --- 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!214
No description provided.