Skip to content

Console CPU instructions: no VirtualQuery at every emulated instruction - #306

Merged
ivall merged 1 commit into
ivall:mainfrom
imnicho:console-insn-no-query
Nov 23, 2026
Merged

ivall merged 1 commit into
ivall:mainfrom
imnicho:console-insn-no-query

Conversation

@imnicho

@imnicho imnicho commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

On an Intel CPU the newer retail title of the README shows about half a frame a second on main. Its main thread zeroes memory with CLZERO, and every emulated instruction asks Windows about memory: readable twice about the code at rip, and is_writable about the target of every CLZERO loop. VirtualQuery walks a region to its end to size it – the title's code section for rip, and for the title's memset a committed window of unified memory (gpu/uma.rs notes ~40 ms a call on a 4 GiB one). This takes those queries out. Part of #184.

What the emulation does is unchanged: the loop's semantics are #290 / #295 and the is_writable guards are planned in #278; none of them changes what the queries cost, and the hunks here are other lines.

What is in it

  • crates/xcl/src/eh.rs, code_bytes_at: the page rip is on is taken as there, since the instruction that raised the exception was fetched from it; only code bytes on the next page are asked about. Test code_bytes_on_the_instructions_page_are_not_asked_about (also: a CLZERO in the last bytes of a page with nothing mapped after it is still emulated).
  • crates/xcl/src/eh.rs, is_writable: a committed window of unified memory is not asked about (uma::in_committed_window), as in gpu::shader::readable, which the single CLZERO used before perf(insn): CLZERO loop fast-forwarding and CLWB NOP hot-patching #217.

Tested

  • hardware: Intel Core i7-7700K (4 cores / 8 threads; no CLZERO, CLWB or MWAITX), 16 GB RAM, NVIDIA GeForce RTX 5080 (driver 610.47), Windows 10 22H2 (19045), rustc 1.100.0 stable-x86_64-pc-windows-msvc; debug for the tests, release for the title.

  • cargo build --workspace && cargo test --workspace -- --test-threads=4 on a99f64c: 652 passed, 0 failed, 40 ignored (SKIPPED GPU tests: gpu_wave32_program_in_wave64_hardware, gpu_geometry_wave32_prefix_count_over_a_mask – no wave64).

  • game checked: the newer retail title of the README, scripts/smoke.ps1 for 60 s, the two builds alternating on the same warm shader cache. Frames shown:

    run main this PR
    1 (9ab7d76 / 2953c11, this commit before its rebase) 27 943
    2 (the same two builds) 28 871
    3 (the same two builds) 28 906
    4 (0e946cf / a99f64c, the blocks below) 26 861

    Every run ended by itself with exit 0, no crash and no device removal.

  • On main (60565c4) three thread dumps of three runs had the title's main thread in VirtualQuery under XCL's illegal-instruction handler.

Regression run

main:

xcl-smoke v1
commit: 0e946cfc68244edee1fba6e0425371f687a05ccd
gpu: NVIDIA GeForce RTX 5080
title: retail-title
seconds: 60
exit: 0x0
frames shown: 26 (PresentX 26)
presents per 2 s (last 3): 1 1 2
device removals: 0
crashes: 0
peak commit: 14140 MB

PR:

xcl-smoke v1
commit: a99f64cba2f85f2cc0e0c039b6179d3a5f25cd6f
gpu: NVIDIA GeForce RTX 5080
title: retail-title
seconds: 60
exit: 0x0
frames shown: 861 (PresentX 861)
presents per 2 s (last 3): 41 42 44
device removals: 0
crashes: 0
peak commit: 14416 MB

Not tested

  • A CPU that has CLZERO (AMD): this code does not run there. AMD and Intel GPUs.
  • How much of the gain is the query about the target and how much the two about rip: not measured separately.
  • The committed-window case of is_writable has no unit test (it needs the process's unified memory); the title runs go through it.
  • A title whose CLZERO targets lie outside a committed window still pays one VirtualQuery a loop.

AI use

  • Claude Code with Claude Fable 5.2: found the cause from thread dumps, wrote the change, the test and this description, and ran the test suite and the title runs above on the machine named.

Duplicate check before opening (2026-11-23 22:49 UTC, main 0e946cf): read #217, #227, #234, #253, #290, #295 and issue #278.

🤖 Generated with Claude Code

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 ivall#217.

What the emulation does is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@requiemsnkrp

Copy link
Copy Markdown

Tested a99f64c on NVIDIA GeForce RTX 3060 12 GB (driver 32.0.16.1714 / 617.14, 32-lane waves), Ryzen 5 5600X, Windows 11 Pro 25H2 (26200.9974), Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17 (Windows SDK), debug: pass.

Tested

  • cargo test --workspace -j 4 --no-fail-fast -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip test_title_draws_with_a_root_signature_under_the_debug_layer: 650 passed, 0 failed, 40 ignored
  • SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware
  • PR's test: eh::tests::code_bytes_on_the_instructions_page_are_not_asked_about ... ok
  • main 4017f8f on the same machine, same command: 650 passed, 0 failed, 40 ignored, same 2 SKIPPED. The PR's base is 0e946cf (before the DirectStorage: a destination page array entry that is 0 at enqueue is read at the request's turn #289 merge), so the same count includes this PR's new test.
  • Tested the head itself; it merges cleanly into main 4017f8f (git merge-tree), the merge result was not tested.
  • Not run here: the two e2e tests that run the test title (test_title_runs_and_game_folder_is_untouched, test_title_draws_with_a_root_signature_under_the_debug_layer): they open the XCL game window and this PC was in use (the 2 "filtered out").

Not tested

  • Retail title regression run (no game copy here), so the frame-rate effect on a title is not checked.
  • The Intel case the PR is about: this is a Zen 3 CPU, which has CLZERO itself, so only the unit test reaches the emulation here.

AI use: Claude Code with Claude Opus 5.5 (ran the tests and wrote this report).

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed a99f64c merged onto main 4017f8f (clean with git merge-tree), looking at what the two skipped queries could miss. Nothing blocks it; three notes, the last one optional.

The saving, measured. Scratch tests calling the same functions on main and on the merge (debug):

per call main merge
CLZERO at the start of a touched 256 MB code region 857 µs 53 µs
the same, 1024 MB 16.4 ms 66 µs
is_writable of 64 KiB in the committed window, 256 MB of it touched 659 µs 0.1 µs
the same, a 1024 MB window all touched 7.3 ms 0.1 µs

Checked for a stale answer

  1. The window: no unmap can go stale. in_committed_window is bounded by the committed size (gpu/uma.rs:473), and a title's VirtualFree of window memory returns the block to the allocator or zeroes it, never decommits (hle/kernelx.rs:344-352). What can differ is protection: the title's VirtualProtect goes straight to Windows (kernelx.rs:376). For a window page made PAGE_READONLY or PAGE_NOACCESS, is_writable says false on main and true on the merge (ran both), so an emulated CLZERO there would fault inside the handler instead of being passed on. gpu::shader::readable already assumes the same on main.
  2. The page rip is on: with readable(rip, 1) gone, a rip on a page that is not there kills the handler. Scratch test, a registered module with that page decommitted: main returns 0, the merge dies with an access violation. A CPU-raised exception cannot get there (the page was just executed, and the loader commits whole images, loader.rs:72), only a record raised by software with its own context, or another thread decommitting the code in between.
  3. An instruction in the last 15 bytes of its page still asks once: 8 bytes before the page end in the 1024 MB region, 12.6 ms on main, 8.1 ms on the merge.

Optional, covers 2 and 3: ReadProcessMemory on the own process read the 16 bytes in 0.17 µs in that region and returned 0 for the decommitted page. It is all or nothing (0 bytes when the next page was missing), so the part on rip's page and the part on the next need a call each.

The PR's test passes on the merge. It also passes on main with main's expression under the name code_bytes_at: it holds the behaviour (the byte counts, the CLZERO at a page end), not the saving. That seems right for this change; the table is the other half.

Tested

  • RTX 5070 Ti (NVIDIA 610.74), Ryzen 9 9950X, Windows 11 26200, Rust 1.99, debug. PR head a99f64c merged onto 4017f8f as 13c7501; main 4017f8f as the control.
  • cargo test -p xcl --lib -- eh::tests::: 4 passed, 0 failed on the merge.
  • The scratch tests above (not in the PR), each run on both builds. They call emulate_console_only and is_writable directly: this CPU has CLZERO.

Not tested

  • The whole suite on the merge: not run here (requiemsnkrp ran it on the head).
  • Any title, and a CPU without CLZERO. Release-build timings.
  • Whether any title changes the protection of window pages, or re-raises an illegal-instruction record of its own.

🤖 Claude Fable 5.2

@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Merged (2b4bb43). Checked on an RX 9070 (RDNA4) on main 2b4bb43 together with #306, #288 and #294: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 630 unit tests passed, 0 failed; the retail game reaches its menus and starts loading the world (stopped at ~150 s by the machine's commit limit, not by the game). The merge conflicted with #290's cmp_eflags and its tests in eh.rs; both are kept.

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.

4 participants