Skip to content

Console CPU instructions: CLZERO into a title's own memory asks the working set - #647

Open
3L1A5-1 wants to merge 1 commit into
xcl-dev:mainfrom
3L1A5-1:fix/is-writable-working-set
Open

3L1A5-1 wants to merge 1 commit into
xcl-dev:mainfrom
3L1A5-1:fix/is-writable-working-set

Conversation

@3L1A5-1

@3L1A5-1 3L1A5-1 commented Nov 24, 2026 •

Copy link
Copy Markdown

On a CPU without CLZERO (Intel before the newest generations), every emulated CLZERO line or loop asks eh::is_writable before it writes. Outside the coherent window that was a VirtualQuery, which sizes the committed run around the address by walking it to its end. The retail title's CLZERO loops zero 0.9–15 KB at a time inside a 1.1 GB block of its own committed private memory, which is neither the coherent window (#306) nor a sparse window (#501). Each check there took 0.3–4.4 ms: 4,096 of them cost 10.3 s of the first minute on the thread that ran them, and the title presented ~5 frames a second with the GPU mostly idle.

is_writable now asks the working set first, as readable already does (resident_and_readable, #332): up to 16 pages that are resident with a read-write protection and no guard are writable, answered in one call that touches none of them. Anything else (not resident, guard, no access, read-only, more than 16 pages) takes the VirtualQuery path as before. Related to #278.

What is in it

  • crates/xcl/src/gpu/shader.rs: resident_and_writable, next to resident_and_readable; both use one working-set helper (resident_with). Unit test writable_pages_are_answered_from_the_working_set (written read-write pages; resident read-only pages, also as a range's tail; never-touched and guard pages, and the guard is left intact; empty, null and wrapping ranges; more than 16 pages).
  • crates/xcl/src/eh.rs: is_writable asks it after the coherent-window check.

Tested

  • NVIDIA GeForce RTX 2060 SUPER 8 GB (Turing, 32-lane waves), driver 617.14; Intel Core i5-9400F (no CLZERO, emulated); 16 GB RAM; Windows 11 Pro 26300; rustc 1.100.0 x86_64-pc-windows-msvc; tests in debug, the title in release.
  • cargo test --workspace -- --test-threads=4 on 83deb3c: 688 passed, 0 failed, 41 ignored. SKIPPED: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64).
  • Retail title (<package id>, 1.0.2.1), XCL_NETWORK=0, scripts\smoke.ps1 -Seconds 60 on the first screen: 266 frames on main, 534 with this change (presents per 2 s at the end 10–11 → 25–27); peak commit the same. Two earlier pairs of runs on cb868b2 (same change, before the rebase): 326 / 323 frames on main, 732 / 726 with it.
  • How it was found: 30 thread samples a second apart (XCL_THREAD_DUMP_AT) put the busiest title thread (56–67 % of a core) in VirtualQuery from is_writable in about half of them; a local, uncommitted counter around that VirtualQuery gave the region size and the per-call times above.

Regression run

xcl-smoke v1
commit: bdab19492aeeb2c9ce1e05f0a15ada518d15992f
gpu: NVIDIA GeForce RTX 2060 SUPER
title: 5c554abc
seconds: 60
exit: 0x0
frames shown: 266 (PresentX 266)
presents per 2 s (last 3): 10 10 11
device removals: 0
crashes: 0
peak commit: 15376 MB
xcl-smoke v1
commit: 83deb3c24b0e4139290e650f42b0734741325c10
gpu: NVIDIA GeForce RTX 2060 SUPER
title: 5c554abc
seconds: 60
exit: 0x0
frames shown: 534 (PresentX 534)
presents per 2 s (last 3): 27 27 25
device removals: 0
crashes: 0
peak commit: 15396 MB

Not tested

Not covered here, and the next hotspot in the same samples: list_x::gpu_va checks vertex buffers with readable, and the buffers longer than 16 pages still take its VirtualQuery walk.

AI use

  • Claude Code with Claude Opus 5.5: profiled the runs, wrote the change, the test and this description; I ran it on my machine and checked the results.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XM3GUHydjd5DSUVNT9WNSR

…orking set

On a CPU without CLZERO, `eh::is_writable` checks each emulated CLZERO line
or loop before it writes. Outside the coherent window it asked `VirtualQuery`,
which sizes the committed run around the address by walking it to its end. A
retail game's CLZERO loops zero 0.9-15 KB at a time inside its 1.1 GB block of
committed private memory (not in the coherent window, not a sparse window), and
each of those checks took 0.3-4.4 ms: 4,096 of them cost 10.3 s of the first
minute on the thread that ran them, and the title presented ~5 frames a second
with the GPU mostly idle.

`is_writable` now first asks the working set, as `readable` already does
(`resident_and_readable`, xcl-dev#332): up to 16 pages that are resident with a
read-write protection and no guard are writable, in one call that touches none
of them. Anything else (not resident, guard, no access, read-only, longer than
16 pages) takes the `VirtualQuery` path as before. The working-set check is
shared with `resident_and_readable` (`resident_with`).

Measured on an RTX 2060 SUPER / i5-9400F, retail title, `smoke.ps1 -Seconds 60`:
326 and 323 frames on main, 732 and 726 with this change.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XM3GUHydjd5DSUVNT9WNSR
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 83deb3c24b0e4139290e650f42b0734741325c10 on AMD RDNA4. I merged it onto current main 91f9a5e locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 693 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 653 passed, 0 failed, 21 ignored
  • 0 SKIPPED; writable_pages_are_answered_from_the_working_set passes.

Review: Resident pages are answered from the working set (RW / RWX and not a guard page), and a page that is not resident still goes to VirtualQuery, so no case loses its answer; the PAGE_* modifier bits (write-combine, no-cache) sit beside the RW bits and do not hide them. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@JoaoPauluu

Copy link
Copy Markdown

Test report: #647 under Wine on an Intel CPU without CLZERO (Linux, NVIDIA)

Same kind of CPU as the description (Intel without CLZERO, emulated), but under Wine, where VirtualQuery and QueryWorkingSetEx are Wine's. The new test passes, and the title runs the same on both builds. Here main doesn't have the slowdown, so the speed-up doesn't show.

Setup

  • CachyOS, kernel 7.3.3-1-cachyos, Intel i5-10400F (no clzero in /proc/cpuinfo), 16 GB RAM, NVIDIA GeForce RTX 3060 12 GB, driver 620.70.06
  • Proton Experimental experimental-11.0-20261112 (wine-11.0), vkd3d-proton v3.1 (4522cc1a) + the patch from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXVK from Proton, DXC 1.9.2609, DXVK_CONFIG="dxgi.hideNvidiaGpu = False"
  • Rust 1.100.0, x86_64-pc-windows-gnu (mingw-w64-gcc 16.2.0); debug for the test, release for the runs
  • Commits: this PR's head 83deb3c and its base main dd2ae9a

Unit test (lib test binary under Wine): gpu::shader::tests::writable_pages_are_answered_from_the_working_set ok, 2 of 2 runs.

GTA VI 1.0.2.1 (my own dump; 60 s runs with scripts/linux/smoke.sh from #257, XCL_NETWORK=0, alternating base / PR / PR / base). Every run logs ClzeroLoop { end_reg: 10 } in game_scarlett_master.exe+0x4e8aa70 – a console CPU instruction this CPU lacks: emulated (logged once).

build frames shown removals / crashes
dd2ae9a 998, 981 0 / 0
83deb3c 978, 982 0 / 0

On main this title already shows ~1000 frames in 60 s under Wine (the description has 266 on main and 534 with the PR on Windows), so the 0.3–4.4 ms VirtualQuery per check apparently doesn't happen with Wine's VirtualQuery. I didn't count or time the is_writable calls, so that is inferred from the frame counts, not measured.

xcl-smoke v1
commit: dd2ae9a70af5eb5aad60f02d86e386b72abe1e95
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 998 (PresentX 998)
presents per 2 s (last 3): 44 60 43
device removals: 0
crashes: 0
peak commit: 0 MB
xcl-smoke v1
commit: 83deb3c24b0e4139290e650f42b0734741325c10
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 978 (PresentX 978)
presents per 2 s (last 3): 56 38 0
device removals: 0
crashes: 0
peak commit: 0 MB

(The first pair. peak commit: 0 MB is XCL reading 0 MB committed under Wine, see #257.)

Not tested

  • Windows (the case the PR speeds up), AMD CPUs, the rest of the suite on this branch, the world-loading part of the title

AI use: Claude Code (Claude Opus 5.5) built both commits, ran everything above on the machine above and wrote this report. The blocks and counts are pasted from those runs. The account owner asked for test reports to be posted while they were away and has not reviewed this one.

@iggyvilla

Copy link
Copy Markdown
Contributor

Test report and review for 83deb3c24b0e4139290e650f42b0734741325c10 on AMD RDNA2 (merge base bdab19492, tested detached and clean, not merged onto current main).

Tested

xcl-smoke v1
commit: 83deb3c24b0e4139290e650f42b0734741325c10
gpu: AMD Radeon RX 6650 XT
title: GTA6.Xbox.v1.0.2.1-CYBERLEEK
seconds: 60
exit: 0x0
frames shown: 691 (PresentX 691)
presents per 2 s (last 3): 12 27 2
device removals: 0
crashes: 0
peak commit: 13583 MB

Review

  • The refactor of resident_and_readable into resident_with preserves the readable predicate (protect != 0 && protect & (PAGE_NOACCESS | PAGE_GUARD) == 0), which the smoke run exercises via readable.
  • is_writable fast-paths only resident Valid pages with PAGE_READWRITE/PAGE_EXECUTE_READWRITE and without PAGE_GUARD (<=16 pages, non-empty/non-wrapping), falling back to VirtualQuery for everything else (including PAGE_WRITECOPY), so it cannot classify a non-writable page as writable. Unit test coverage is solid. Nothing to change found.

Not tested

  • The CLZERO speedup itself. This machine's Ryzen 7 5700X executes CLZERO natively, so the title does not enter eh's emulation or is_writable during the smoke run; is_writable's working-set fast path is exercised by the unit test only, while the smoke run validates no regression and exercises resident_with via readable.
  • Base smoke on bdab194 or head merged onto current main; runs past 60 s; XCL_SAMPLE_* tests; Intel CPUs / NVIDIA GPUs.

AI use
opencode with DeepSeek V4.1 Flash (opencode-go/deepseek-v4.1-flash) ran the build, test suite, and smoke run and drafted this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants