Console CPU instructions: monitorx / mwaitx are rewritten in place at their first fault #2608

Closed
lukastaro wants to merge 1 commit from upstream-pr/2618 into main
lukastaro commented 2026-11-26 02:27:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by lukastaro on 2026-11-26T02:27:07Z.


A title that paces its frames with monitorx / mwaitx spins on them. On a CPU without them (Intel) every one raises an illegal-instruction exception that eh emulates, the same instruction again and again. clwb is already rewritten to nops at its first fault; this does the same for these two:

  • monitorx → nops, mwaitx → pause + nops (a wait that ended at once; the title checks its own condition again, as it must after any mwaitx).
  • The bytes are written with one atomic 8-byte compare-exchange when the instruction lies inside an aligned 8-byte word, so a thread at the same instruction runs either the old one (and is emulated) or the new one, never a mix. Otherwise the instruction stays emulated as before.

Tested

  • NVIDIA GeForce RTX 5060 Ti (driver 621.40), Intel Core i7-14700K, Windows 10 22H2 (19045), release builds.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 19aa5c2: 1426 passed, 11 failed, 47 ignored. On main b49be18 the same run: 1425 passed, 10 failed, 47 ignored, the same 10 failures (fuzz_validate_and_gpu_0–_3, the two link_tests, vram::tests::d32s8_targets_take_their_own_size_of_video_memory and the three test_title_* integration tests). The 11th, list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list, passed 3 of 3 times alone.
  • New tests: monitorx_and_mwaitx_are_rewritten_at_their_first_fault (both rewritten, rip stepped over them), code_across_an_aligned_word_is_not_rewritten.
  • A retail title on the machine above (a lab tree with this change; same build and settings with and without it, one run each): two sites rewritten at 3.9 s; in its world 1.24 → 2–3.5 presents per second.

Regression run

main:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5060 Ti
title: eccf25ce
seconds: 60
exit: 0x0
frames shown: 1489 (PresentX 1489)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15106 MB

PR:

xcl-smoke v1
commit: 19aa5c28b3ae4ed912c36c7a2fd18dae59d46c13
gpu: NVIDIA GeForce RTX 5060 Ti
title: eccf25ce
seconds: 60
exit: 0x0
frames shown: 1588 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15252 MB

The 60 s stay in the title's menus; the two sites are rewritten in the first seconds.

Not tested

  • AMD CPUs: they run monitorx / mwaitx natively and never reach this path.
  • An instruction that crosses an aligned 8-byte word in a real title: it stays emulated, as before (covered by the second test only).
  • Other titles than the one above.

AI use

  • Claude Code (Claude Opus 5.5) wrote the change, the tests and this description; the runs above were made on the hardware named.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5502891165 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618). Originally posted by [lukastaro](https://github.com/lukastaro) on 2026-11-26T02:27:07Z. --- A title that paces its frames with `monitorx` / `mwaitx` spins on them. On a CPU without them (Intel) every one raises an illegal-instruction exception that `eh` emulates, the same instruction again and again. `clwb` is already rewritten to nops at its first fault; this does the same for these two: - `monitorx` → nops, `mwaitx` → `pause` + nops (a wait that ended at once; the title checks its own condition again, as it must after any `mwaitx`). - The bytes are written with one atomic 8-byte compare-exchange when the instruction lies inside an aligned 8-byte word, so a thread at the same instruction runs either the old one (and is emulated) or the new one, never a mix. Otherwise the instruction stays emulated as before. **Tested** - NVIDIA GeForce RTX 5060 Ti (driver 621.40), Intel Core i7-14700K, Windows 10 22H2 (19045), release builds. - `cargo test --workspace --no-fail-fast -- --test-threads=4` on `19aa5c2`: 1426 passed, 11 failed, 47 ignored. On `main` `b49be18` the same run: 1425 passed, 10 failed, 47 ignored, the same 10 failures (`fuzz_validate_and_gpu_0`–`_3`, the two `link_tests`, `vram::tests::d32s8_targets_take_their_own_size_of_video_memory` and the three `test_title_*` integration tests). The 11th, `list_x::tests::a_reset_while_the_last_submit_runs_records_on_another_pc_list`, passed 3 of 3 times alone. - New tests: `monitorx_and_mwaitx_are_rewritten_at_their_first_fault` (both rewritten, rip stepped over them), `code_across_an_aligned_word_is_not_rewritten`. - A retail title on the machine above (a lab tree with this change; same build and settings with and without it, one run each): two sites rewritten at 3.9 s; in its world 1.24 → 2–3.5 presents per second. **Regression run** main: ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 5060 Ti title: eccf25ce seconds: 60 exit: 0x0 frames shown: 1489 (PresentX 1489) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15106 MB ``` PR: ``` xcl-smoke v1 commit: 19aa5c28b3ae4ed912c36c7a2fd18dae59d46c13 gpu: NVIDIA GeForce RTX 5060 Ti title: eccf25ce seconds: 60 exit: 0x0 frames shown: 1588 (PresentX 1588) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15252 MB ``` The 60 s stay in the title's menus; the two sites are rewritten in the first seconds. **Not tested** - AMD CPUs: they run `monitorx` / `mwaitx` natively and never reach this path. - An instruction that crosses an aligned 8-byte word in a real title: it stays emulated, as before (covered by the second test only). - Other titles than the one above. **AI use** - Claude Code (Claude Opus 5.5) wrote the change, the tests and this description; the runs above were made on the hardware named. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DavidM1911 commented 2026-11-26 03:06:33 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T02:35:12Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 26 ignored.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7180938707 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7180938707). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T02:35:12Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 26 ignored. Thanks! — Devin
luckyteagirl commented 2026-11-26 03:15:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T03:15:35Z.

Review: COMMENTED


Two source-derived concurrency points on the current head. No local build or title run. AI-assisted source review: OpenAI Codex (GPT-6; exact serving variant unavailable) and a Luna subagent.

<!-- greyfinch-sync:ivall/xcl:review:6068075238 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#pullrequestreview-6068075238). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T03:15:35Z. **Review:** COMMENTED --- Two source-derived concurrency points on the current head. No local build or title run. AI-assisted source review: OpenAI Codex (GPT-6; exact serving variant unavailable) and a Luna subagent.
luckyteagirl commented 2026-11-26 04:07:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T03:15:35Z.

Code review location: crates/xcl/src/eh.rs; line 274

@@ -245,6 +245,39 @@ unsafe fn bounded_cstr(p: usize, max: usize) -> String {
     String::from_utf8_lossy(&bytes).into_owned()
 }
 
+/// Rewrites the code bytes at `at` with `new` when they lie inside one aligned 8-byte word: one
+/// atomic store, so another thread at the same instruction runs either the old one (and comes
+/// back here) or the new one, never a mix. Otherwise the code is left as it is and the
+/// instruction stays emulated. Whether it was rewritten.
+fn rewrite_code(at: usize, new: &[u8]) -> bool {
+    use std::sync::atomic::AtomicU64;
+    use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache;
+    use windows_sys::Win32::System::Memory::{VirtualProtect, PAGE_EXECUTE_READWRITE};
+    use windows_sys::Win32::System::Threading::GetCurrentProcess;
+    let word = at & !7;
+    let off = at - word;
+    if new.is_empty() || off + new.len() > 8 {
+        return false;
+    }
+    let mut old = 0u32;
+    // SAFETY: `word` is the aligned word around code the processor just fetched; it is made
+    // writable for the one store and given its protection back
+    unsafe {
+        if VirtualProtect(word as *const c_void, 8, PAGE_EXECUTE_READWRITE, &mut old) == 0 {
+            return false;
+        }
+        let cell = &*(word as *const AtomicU64);
+        let was = cell.load(Ordering::Acquire);
+        let mut bytes = was.to_le_bytes();
+        bytes[off..off + new.len()].copy_from_slice(new);
+        // another thread may have rewritten it first: then it holds the same bytes already
+        let _ = cell.compare_exchange(was, u64::from_le_bytes(bytes), Ordering::AcqRel, Ordering::Acquire);

A failed compare_exchange is currently ignored, but another thread may have rewritten a different monitorx/mwaitx site in this same aligned 8-byte word. If both load the original word, one CAS wins and the other loses; the losing site remains unchanged even though rewrite_code returns true, so later visits fault again. Could this retry after a lost CAS by reloading and merging only the requested bytes? A deterministic test with two adjacent sites would cover it. This is a source-derived interleaving, not a measured title failure.

<!-- greyfinch-sync:ivall/xcl:review-comment:4703493224 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#discussion_r4703493224). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T03:15:35Z. **Code review location:** `crates/xcl/src/eh.rs`; line 274 ~~~~diff @@ -245,6 +245,39 @@ unsafe fn bounded_cstr(p: usize, max: usize) -> String { String::from_utf8_lossy(&bytes).into_owned() } +/// Rewrites the code bytes at `at` with `new` when they lie inside one aligned 8-byte word: one +/// atomic store, so another thread at the same instruction runs either the old one (and comes +/// back here) or the new one, never a mix. Otherwise the code is left as it is and the +/// instruction stays emulated. Whether it was rewritten. +fn rewrite_code(at: usize, new: &[u8]) -> bool { + use std::sync::atomic::AtomicU64; + use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache; + use windows_sys::Win32::System::Memory::{VirtualProtect, PAGE_EXECUTE_READWRITE}; + use windows_sys::Win32::System::Threading::GetCurrentProcess; + let word = at & !7; + let off = at - word; + if new.is_empty() || off + new.len() > 8 { + return false; + } + let mut old = 0u32; + // SAFETY: `word` is the aligned word around code the processor just fetched; it is made + // writable for the one store and given its protection back + unsafe { + if VirtualProtect(word as *const c_void, 8, PAGE_EXECUTE_READWRITE, &mut old) == 0 { + return false; + } + let cell = &*(word as *const AtomicU64); + let was = cell.load(Ordering::Acquire); + let mut bytes = was.to_le_bytes(); + bytes[off..off + new.len()].copy_from_slice(new); + // another thread may have rewritten it first: then it holds the same bytes already + let _ = cell.compare_exchange(was, u64::from_le_bytes(bytes), Ordering::AcqRel, Ordering::Acquire); ~~~~ --- A failed `compare_exchange` is currently ignored, but another thread may have rewritten a *different* `monitorx`/`mwaitx` site in this same aligned 8-byte word. If both load the original word, one CAS wins and the other loses; the losing site remains unchanged even though `rewrite_code` returns `true`, so later visits fault again. Could this retry after a lost CAS by reloading and merging only the requested bytes? A deterministic test with two adjacent sites would cover it. This is a source-derived interleaving, not a measured title failure.
luckyteagirl commented 2026-11-26 04:07:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T03:15:35Z.

Code review location: crates/xcl/src/eh.rs; line 266

@@ -245,6 +245,39 @@ unsafe fn bounded_cstr(p: usize, max: usize) -> String {
     String::from_utf8_lossy(&bytes).into_owned()
 }
 
+/// Rewrites the code bytes at `at` with `new` when they lie inside one aligned 8-byte word: one
+/// atomic store, so another thread at the same instruction runs either the old one (and comes
+/// back here) or the new one, never a mix. Otherwise the code is left as it is and the
+/// instruction stays emulated. Whether it was rewritten.
+fn rewrite_code(at: usize, new: &[u8]) -> bool {
+    use std::sync::atomic::AtomicU64;
+    use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache;
+    use windows_sys::Win32::System::Memory::{VirtualProtect, PAGE_EXECUTE_READWRITE};
+    use windows_sys::Win32::System::Threading::GetCurrentProcess;
+    let word = at & !7;
+    let off = at - word;
+    if new.is_empty() || off + new.len() > 8 {
+        return false;
+    }
+    let mut old = 0u32;
+    // SAFETY: `word` is the aligned word around code the processor just fetched; it is made
+    // writable for the one store and given its protection back
+    unsafe {
+        if VirtualProtect(word as *const c_void, 8, PAGE_EXECUTE_READWRITE, &mut old) == 0 {

VirtualProtect(word, 8, ...) changes the whole containing page. Two concurrent calls on that page can save RX/RWX respectively; if the first restores RX before the second restores its saved RWX, the page stays writable and executable. SEEN is unlocked before this span. Could the protect/CAS/restore span be serialized for calls touching the same page, with a concurrent-patch test asserting the original protection afterwards? This is a source-derived interleaving, not a measured title failure.

<!-- greyfinch-sync:ivall/xcl:review-comment:4703493236 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#discussion_r4703493236). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T03:15:35Z. **Code review location:** `crates/xcl/src/eh.rs`; line 266 ~~~~diff @@ -245,6 +245,39 @@ unsafe fn bounded_cstr(p: usize, max: usize) -> String { String::from_utf8_lossy(&bytes).into_owned() } +/// Rewrites the code bytes at `at` with `new` when they lie inside one aligned 8-byte word: one +/// atomic store, so another thread at the same instruction runs either the old one (and comes +/// back here) or the new one, never a mix. Otherwise the code is left as it is and the +/// instruction stays emulated. Whether it was rewritten. +fn rewrite_code(at: usize, new: &[u8]) -> bool { + use std::sync::atomic::AtomicU64; + use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache; + use windows_sys::Win32::System::Memory::{VirtualProtect, PAGE_EXECUTE_READWRITE}; + use windows_sys::Win32::System::Threading::GetCurrentProcess; + let word = at & !7; + let off = at - word; + if new.is_empty() || off + new.len() > 8 { + return false; + } + let mut old = 0u32; + // SAFETY: `word` is the aligned word around code the processor just fetched; it is made + // writable for the one store and given its protection back + unsafe { + if VirtualProtect(word as *const c_void, 8, PAGE_EXECUTE_READWRITE, &mut old) == 0 { ~~~~ --- `VirtualProtect(word, 8, ...)` changes the [whole containing page](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualprotect). Two concurrent calls on that page can save RX/RWX respectively; if the first restores RX before the second restores its saved RWX, the page stays writable and executable. `SEEN` is unlocked before this span. Could the protect/CAS/restore span be serialized for calls touching the same page, with a concurrent-patch test asserting the original protection afterwards? This is a source-derived interleaving, not a measured title failure.
DavidM1911 commented 2026-11-26 09:07:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T08:29:21Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7186654799 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7186654799). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T08:29:21Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
kole07139 commented 2026-11-26 16:31:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by kole07139 on 2026-11-26T16:31:40Z.


The case in your "Not tested" list (an instruction across an aligned 8-byte word in a real title) happens on another build of the retail title (title: 16b5e5d4), and it is the monitorx site: only mwaitx is rewritten, so monitorx stays emulated every time it runs. Two runs per arm show no change in the world's present rate, but that does not test the PR's idea while half of it is inactive.

What the log shows

I added a local log line (not part of the PR) in the two new match arms, printing what rewrite_code returned:

lab: monitorx in <title exe>+0x4fb0bd7 rewritten in place: false
lab: mwaitx   in <title exe>+0x4fb0c00 rewritten in place: true
lab: monitorx in <title exe>+0x4fb0bd7 rewritten in place: false   (the same line at each later fault of that site, up to the log's cap)

In the title's .text, +0x…bd7 holds 0F 01 FA: monitorx, 3 bytes, no prefix, at bd7–bd9. So off = 7 and off + len > 8. rewrite_code refuses it by design, which is the case code_across_an_aligned_word_is_not_rewritten tests. The image base is 64 KiB-aligned, so this happens on every run of this build. Your runs used title: eccf25ce, where both sites were rewritten, so that build's monitorx must lie inside one word.

A possible way to cover it (not tried): bd7–bd9 lie inside the aligned 16-byte block bd0–bdf. A lock cmpxchg16b on that block would give the same guarantee as the 8-byte exchange: a thread runs either the old instruction or the new one.

Numbers

Same tree, same cache snapshot copied for each run, 300 s each, runs alternated (3d, 3e, 3d, 3e). Presents are counted in fixed windows after the title's first ListX #101, which is where it enters the world:

run this PR first #101 presents 0–30 s after 30–95 s after removal / crash
Y2 no 82.4 s 104 14 none
Y4 no 81.9 s 159 15 none (presents stop at 181 s on a 28 s cold compute pipeline build)
Y3 yes 87.9 s 136 15 none
Y5 yes 101.6 s 25 14 none

From 30 s after entry, all four runs present about 0.23 times a second with or without the PR. At that rate this tree is bound by something other than the title's CPU pacing loop. So these runs do not show whether the rewrite helps: monitorx is still emulated, and the frame rate is held down elsewhere.

Tested

  • RTX 3070 8 GB (driver 617.42), Intel Core i9-9900K (no monitorx / mwaitx), 32 GB RAM, Windows 11 26200, release builds.
  • Tree: main 6d12f58 + #2404, #2097, #684, #751, #1803, #2229, #2384, #1975, then + #2590 + #2753. With and without this PR at 19aa5c2, which applies cleanly.
  • XCL_DIRECT_DXIL=1 XCL_HEAP_WINDOWS=1 XCL_TEXTURE_LEVEL_PAGES=1 XCL_PSO_WAIT_MS=50, A pressed every 5 s, warm DXIL and driver-blob cache. The NVIDIA shader heap was 1 GiB in all four runs (xcl nvprofile install --mb 1024, #2590).
  • cargo test -p xcl --lib eh:: on the tree with the PR: 8 passed, 0 failed, 0 ignored.

Not tested

  • The whole suite; title: eccf25ce; a version that also rewrites a monitorx across a word; more than two runs per arm; AMD (not relevant: AMD CPUs run these natively).

AI use

  • Claude Code (Claude Opus 5.5) built the trees, added the local log line, ran the series, read the logs and wrote this report.
<!-- greyfinch-sync:ivall/xcl:comment:7197762045 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7197762045). Originally posted by [kole07139](https://github.com/kole07139) on 2026-11-26T16:31:40Z. --- **The case in your "Not tested" list (an instruction across an aligned 8-byte word in a real title) happens on another build of the retail title (`title: 16b5e5d4`), and it is the `monitorx` site: only `mwaitx` is rewritten, so `monitorx` stays emulated every time it runs.** Two runs per arm show no change in the world's present rate, but that does not test the PR's idea while half of it is inactive. **What the log shows** I added a local log line (not part of the PR) in the two new match arms, printing what `rewrite_code` returned: ``` lab: monitorx in <title exe>+0x4fb0bd7 rewritten in place: false lab: mwaitx in <title exe>+0x4fb0c00 rewritten in place: true lab: monitorx in <title exe>+0x4fb0bd7 rewritten in place: false (the same line at each later fault of that site, up to the log's cap) ``` In the title's `.text`, `+0x…bd7` holds `0F 01 FA`: `monitorx`, 3 bytes, no prefix, at `bd7`–`bd9`. So `off = 7` and `off + len > 8`. `rewrite_code` refuses it by design, which is the case `code_across_an_aligned_word_is_not_rewritten` tests. The image base is 64 KiB-aligned, so this happens on every run of this build. Your runs used `title: eccf25ce`, where both sites were rewritten, so that build's `monitorx` must lie inside one word. A possible way to cover it (not tried): `bd7`–`bd9` lie inside the aligned 16-byte block `bd0`–`bdf`. A `lock cmpxchg16b` on that block would give the same guarantee as the 8-byte exchange: a thread runs either the old instruction or the new one. **Numbers** Same tree, same cache snapshot copied for each run, 300 s each, runs alternated (3d, 3e, 3d, 3e). Presents are counted in fixed windows after the title's first `ListX #101`, which is where it enters the world: | run | this PR | first `#101` | presents 0–30 s after | 30–95 s after | removal / crash | |---|---|---|---|---|---| | Y2 | no | 82.4 s | 104 | 14 | none | | Y4 | no | 81.9 s | 159 | 15 | none (presents stop at 181 s on a 28 s cold compute pipeline build) | | Y3 | yes | 87.9 s | 136 | 15 | none | | Y5 | yes | 101.6 s | 25 | 14 | none | From 30 s after entry, all four runs present about 0.23 times a second with or without the PR. At that rate this tree is bound by something other than the title's CPU pacing loop. So these runs do not show whether the rewrite helps: `monitorx` is still emulated, and the frame rate is held down elsewhere. **Tested** - RTX 3070 8 GB (driver 617.42), Intel Core i9-9900K (no `monitorx` / `mwaitx`), 32 GB RAM, Windows 11 26200, release builds. - Tree: `main` `6d12f58` + #2404, #2097, #684, #751, #1803, #2229, #2384, #1975, then + #2590 + #2753. With and without this PR at `19aa5c2`, which applies cleanly. - `XCL_DIRECT_DXIL=1 XCL_HEAP_WINDOWS=1 XCL_TEXTURE_LEVEL_PAGES=1 XCL_PSO_WAIT_MS=50`, A pressed every 5 s, warm DXIL and driver-blob cache. The NVIDIA shader heap was 1 GiB in all four runs (`xcl nvprofile install --mb 1024`, #2590). - `cargo test -p xcl --lib eh::` on the tree with the PR: 8 passed, 0 failed, 0 ignored. **Not tested** - The whole suite; `title: eccf25ce`; a version that also rewrites a `monitorx` across a word; more than two runs per arm; AMD (not relevant: AMD CPUs run these natively). **AI use** - Claude Code (Claude Opus 5.5) built the trees, added the local log line, ran the series, read the logs and wrote this report.
throny commented 2026-11-26 18:28:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-11-26T18:28:32Z.


Test report: in the world on an Intel CPU with an RTX 4080 16 GB. No gain here, because this machine waits for the GPU; one figure worth a look in the intro.

Tested

  • i9-12900K (no monitorx / mwaitx), RTX 4080 16 GB (driver 621.40), 64 GB RAM, Windows 11 26300, release.
  • This PR's diff at its head (git apply, as it is) on nvidia-dev af2cecc4 + twelve other open PRs (#684, #2697, #2750, #2749, #2729, #2794, #2817 stack, #2768, #2782, #2725, #2581, #2422), XCL_WATCH_HOT=0. The retail title of #505, Series X profile. Two world runs with it, three of the same tree without it just before.
  • The log has the two "emulated (logged once)" lines for monitorx and mwaitx in every run, with and without.
without (3 runs) with (2 runs)
presents per second in the world 0.18, 0.21, 0.20 0.21, 0.22
presents per second, 40–120 s (intro and loading) 30.1, 30.1, 30.1 27.3, 28.3
end device removed in 3 of 3 device removed in 2 of 2
  • In the world this card is limited by the GPU (93 % of the submitting thread's time is inside PresentX, see #2729), so the CPU time this PR gives back cannot show as frames here. That fits your numbers coming from a setup where the CPU was the limit.
  • The intro figure is the part to check: with the PR the 30 fps stretch reads 27–28 in both runs. Two runs against three, and the window may simply start a little later (I did not line up the first frame), so I would not call it a regression yet; but a pause loop where the title expected to sleep could show up exactly like that.
  • No crash or hang from the rewrite.

Not tested

  • CPU use of the title's waiting threads before and after (that would show the PR's effect directly on this machine), the PR's test, the suite.

AI use: Claude Code (Claude Fable 5.2) ran this on the owner's machine and wrote the report.

<!-- greyfinch-sync:ivall/xcl:comment:7200504647 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7200504647). Originally posted by [throny](https://github.com/throny) on 2026-11-26T18:28:32Z. --- **Test report: in the world on an Intel CPU with an RTX 4080 16 GB. No gain here, because this machine waits for the GPU; one figure worth a look in the intro.** **Tested** - i9-12900K (no monitorx / mwaitx), RTX 4080 16 GB (driver 621.40), 64 GB RAM, Windows 11 26300, release. - This PR's diff at its head (`git apply`, as it is) on `nvidia-dev` `af2cecc4` + twelve other open PRs (#684, #2697, #2750, #2749, #2729, #2794, #2817 stack, #2768, #2782, #2725, #2581, #2422), `XCL_WATCH_HOT=0`. The retail title of #505, Series X profile. Two world runs with it, three of the same tree without it just before. - The log has the two "emulated (logged once)" lines for monitorx and mwaitx in every run, with and without. | | without (3 runs) | with (2 runs) | |---|---|---| | presents per second in the world | 0.18, 0.21, 0.20 | 0.21, 0.22 | | presents per second, 40–120 s (intro and loading) | 30.1, 30.1, 30.1 | **27.3, 28.3** | | end | device removed in 3 of 3 | device removed in 2 of 2 | - In the world this card is limited by the GPU (93 % of the submitting thread's time is inside `PresentX`, see #2729), so the CPU time this PR gives back cannot show as frames here. That fits your numbers coming from a setup where the CPU was the limit. - The intro figure is the part to check: with the PR the 30 fps stretch reads 27–28 in both runs. Two runs against three, and the window may simply start a little later (I did not line up the first frame), so I would not call it a regression yet; but a `pause` loop where the title expected to sleep could show up exactly like that. - No crash or hang from the rewrite. **Not tested** - CPU use of the title's waiting threads before and after (that would show the PR's effect directly on this machine), the PR's test, the suite. AI use: Claude Code (Claude Fable 5.2) ran this on the owner's machine and wrote the report.
voiceovarian1-max commented 2026-11-27 11:39:14 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by voiceovarian1-max on 2026-11-27T11:39:14Z.


A second build with the case @kole07139 found, and what we use for it.

The site: in the retail title's 1.0.2.1 build (game_scarlett_master.exe), monitorx at +0x4fb0bd7 crosses the aligned word at +0x4fb0bd8; mwaitx at +0x4fb0c00 does not (checked in the executable). So with this PR only the mwaitx is rewritten there, and the monitorx keeps faulting at every pass of the wait loop. Under Wine the pair faulted ~6,000 times a second before any rewrite: a core's worth of exception handling.

A fallback for that case: change only the byte after 0F: 0F 01 FA/FB becomes 0F 1F FA/FB, a multi-byte NOP of the same length. A one-byte store is atomic at any alignment, so another thread still sees either the old instruction or the new one. A thread whose fault is handled after the rewrite finds 0F 1F FA/FB and is resumed as is. On our fork it runs when rewrite_code returns false, on top of this PR: e9f4c457, with a test for a site across a word. An earlier version of ours used only this one-byte form for every site, and the title's world ran with it through several 200 s runs.

Tested: Linux, Wine 11 (Proton Experimental), i7-14700K (no monitorx / mwaitx), this PR's head 19aa5c28 carried on nvidia-dev plus the fallback. cargo test -p xcl --lib eh:: under Wine: 13 passed, 0 failed (this PR's two tests among them). Not tested: Windows, and a retail run with this exact combination.

Written with Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:ivall/xcl:comment:7217322135 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7217322135). Originally posted by [voiceovarian1-max](https://github.com/voiceovarian1-max) on 2026-11-27T11:39:14Z. --- A second build with the case @kole07139 found, and what we use for it. **The site:** in the retail title's 1.0.2.1 build (`game_scarlett_master.exe`), `monitorx` at +0x4fb0bd7 crosses the aligned word at +0x4fb0bd8; `mwaitx` at +0x4fb0c00 does not (checked in the executable). So with this PR only the `mwaitx` is rewritten there, and the `monitorx` keeps faulting at every pass of the wait loop. Under Wine the pair faulted ~6,000 times a second before any rewrite: a core's worth of exception handling. **A fallback for that case:** change only the byte after `0F`: `0F 01 FA/FB` becomes `0F 1F FA/FB`, a multi-byte NOP of the same length. A one-byte store is atomic at any alignment, so another thread still sees either the old instruction or the new one. A thread whose fault is handled after the rewrite finds `0F 1F FA/FB` and is resumed as is. On our fork it runs when `rewrite_code` returns false, on top of this PR: [e9f4c457](https://github.com/voiceovarian1-max/xcl/commit/e9f4c457), with a test for a site across a word. An earlier version of ours used only this one-byte form for every site, and the title's world ran with it through several 200 s runs. **Tested:** Linux, Wine 11 (Proton Experimental), i7-14700K (no `monitorx` / `mwaitx`), this PR's head `19aa5c28` carried on nvidia-dev plus the fallback. `cargo test -p xcl --lib eh::` under Wine: 13 passed, 0 failed (this PR's two tests among them). Not tested: Windows, and a retail run with this exact combination. Written with Claude Code (Claude Opus 5.5).
VolenNakov commented 2026-11-27 12:11:01 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by VolenNakov on 2026-11-27T12:11:01Z.


@voiceovarian1-max, a Windows retail run with your fallback (e9f4c457) on top of this PR, the combination your "Not tested" names. On an i7-8700 the world's present rate went from 3.30 to 4.60 presents/s, and the GPU went from 75 % to 88 % busy. One run per arm, so take it as a first data point.

Setup

  • RTX 4060 Ti 16 GB (driver 610.88); i7-8700 (no monitorx / mwaitx; 6 cores / 12 threads); 32 GB RAM; Windows 11 26200; release. Retail title ea3b5e4d of #505, Series X profile. Its monitorx is at +0x4fb0bd7 and its mwaitx at +0x4fb0c00 in the log, the same sites you name.
  • Tree: main 08db802 + #2526, #2097, #2404, #2590, #2615, #751, #2753, #2759, #759 / #1826, #2844 (fc007a9), #2934 (7692dd87, c230aa1b), and this PR at its head (19aa5c28, identical by range-diff). The "with" arm adds e9f4c457 (git am, clean).
  • Env: XCL_NETWORK=0 XCL_DIRECT_DXIL=all XCL_TEXTURE_LEVEL_PAGES=1 XCL_SPLIT_INDIRECT_ROUNDS=2 XCL_DIRECT_SPLIT_INSTRUCTIONS=0 XCL_WATCH_TRAP=1, 2 GiB shader heap (#2590), warm caches, 900 s.
this PR this PR + e9f4c457
world presents/s, ~310 s to the end 3.30 4.60
300-frame lines in the world 3.7, 3.8, 4.5, 1.9, 4.1 5.0, 5.0, 4.9, 4.4, 4.4, 4.1, 4.9, 4.3
GPU busy in the world (nvidia-smi every 5 s, 200–880 s) 75 % mean, ≥ 95 % in 64 of 97 88 % mean, ≥ 95 % in 81 of 106
sparse windows timing: write-watch share of the run ~23 % ~24 %
peak commit / video memory 34.2 / 12.1 GB 32.7 / 12.3 GB
errors / removals / GPU wait timeouts / crashes 0 / 0 / 0 / 0 0 / 0 / 0 / 0
  • This machine is CPU-bound once the write-watch cost is down (#2934), so CPU time given back to the title can show up here, unlike on @throny's GPU-bound 4080.
  • The "with" arm held 4–5 fps throughout, without the "without" arm's dip to 1.9.
  • I have no log line for the rewrite itself: the two "emulated (logged once)" lines appear in both arms. So the gain fits the fallback stopping the monitorx faults, but I have not shown the fault count directly.
  • The runs were not played identically (a person had the controller part of the time), so some of the difference may be what was on screen.
  • The world is grey on this tree (as on #2938's runs), so the frames say nothing about correctness. There was no hang or crash in either run.

Not tested: more than one run per arm; a fault count with and without; cargo test -p xcl --lib eh:: on this tree, whose test build fails on an unrelated conflict between two of the other PRs; the intro's 30 fps figure that @throny asked about.

AI use: Claude Code (Claude Opus 5.5) ran the runs, read the logs and wrote this.

<!-- greyfinch-sync:ivall/xcl:comment:7217936807 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7217936807). Originally posted by [VolenNakov](https://github.com/VolenNakov) on 2026-11-27T12:11:01Z. --- @voiceovarian1-max, a Windows retail run with your fallback (`e9f4c457`) on top of this PR, the combination your "Not tested" names. **On an i7-8700 the world's present rate went from 3.30 to 4.60 presents/s, and the GPU went from 75 % to 88 % busy.** One run per arm, so take it as a first data point. **Setup** - RTX 4060 Ti 16 GB (driver 610.88); i7-8700 (no `monitorx` / `mwaitx`; 6 cores / 12 threads); 32 GB RAM; Windows 11 26200; release. Retail title `ea3b5e4d` of #505, Series X profile. Its `monitorx` is at `+0x4fb0bd7` and its `mwaitx` at `+0x4fb0c00` in the log, the same sites you name. - Tree: `main` `08db802` + #2526, #2097, #2404, #2590, #2615, #751, #2753, #2759, #759 / #1826, #2844 (`fc007a9`), #2934 (`7692dd87`, `c230aa1b`), and this PR at its head (`19aa5c28`, identical by `range-diff`). The "with" arm adds `e9f4c457` (`git am`, clean). - Env: `XCL_NETWORK=0 XCL_DIRECT_DXIL=all XCL_TEXTURE_LEVEL_PAGES=1 XCL_SPLIT_INDIRECT_ROUNDS=2 XCL_DIRECT_SPLIT_INSTRUCTIONS=0 XCL_WATCH_TRAP=1`, 2 GiB shader heap (#2590), warm caches, 900 s. | | this PR | this PR + `e9f4c457` | |---|---|---| | world presents/s, ~310 s to the end | 3.30 | **4.60** | | 300-frame lines in the world | 3.7, 3.8, 4.5, 1.9, 4.1 | 5.0, 5.0, 4.9, 4.4, 4.4, 4.1, 4.9, 4.3 | | GPU busy in the world (`nvidia-smi` every 5 s, 200–880 s) | 75 % mean, ≥ 95 % in 64 of 97 | **88 % mean, ≥ 95 % in 81 of 106** | | `sparse windows timing: write-watch` share of the run | ~23 % | ~24 % | | peak commit / video memory | 34.2 / 12.1 GB | 32.7 / 12.3 GB | | errors / removals / GPU wait timeouts / crashes | 0 / 0 / 0 / 0 | 0 / 0 / 0 / 0 | - This machine is CPU-bound once the write-watch cost is down (#2934), so CPU time given back to the title can show up here, unlike on @throny's GPU-bound 4080. - The "with" arm held 4–5 fps throughout, without the "without" arm's dip to 1.9. - I have no log line for the rewrite itself: the two "emulated (logged once)" lines appear in both arms. So the gain fits the fallback stopping the `monitorx` faults, but I have not shown the fault count directly. - The runs were not played identically (a person had the controller part of the time), so some of the difference may be what was on screen. - The world is grey on this tree (as on #2938's runs), so the frames say nothing about correctness. There was no hang or crash in either run. **Not tested:** more than one run per arm; a fault count with and without; `cargo test -p xcl --lib eh::` on this tree, whose test build fails on an unrelated conflict between two of the other PRs; the intro's 30 fps figure that @throny asked about. AI use: Claude Code (Claude Opus 5.5) ran the runs, read the logs and wrote this.
Foniass commented 2026-12-01 08:29:17 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:29:17Z.


Triage (#1607) – head 19aa5c2

Main commit d634138 (merged with #2959) is adapted from this PR. In eh.rs, rewrite_code(at, expected, new) turns monitorx into nops and mwaitx into pause + nops at the first fault, with an atomic aligned-8-byte replacement, as here.
Main also checks the expected bytes, serializes the rewrite under CODE_REWRITE, and records rewritten sites so a fault already in progress at a just-rewritten site is recognized. #1829 only adds tests for the emulation.
Recommendation: close (superseded by main commit d634138).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7305382829 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7305382829). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T08:29:17Z. --- **Triage (#1607)** – head 19aa5c2 Main commit d634138 (merged with #2959) is adapted from this PR. In `eh.rs`, `rewrite_code(at, expected, new)` turns monitorx into nops and mwaitx into `pause` + nops at the first fault, with an atomic aligned-8-byte replacement, as here. Main also checks the expected bytes, serializes the rewrite under `CODE_REWRITE`, and records rewritten sites so a fault already in progress at a just-rewritten site is recognized. #1829 only adds tests for the emulation. Recommendation: close (superseded by main commit d634138). AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
throny commented 2026-12-01 12:55:32 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-01T12:55:32Z.


Closing as superseded: this change is already on main (d634138c). main eh.rs rewrite_code (atomic aligned-word replace, serialized, with expected bytes) rewrites monitorx to nops and mwaitx to pause+nops at first fault; superset of PR with tests

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

<!-- greyfinch-sync:ivall/xcl:comment:7311847481 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2618#issuecomment-7311847481). Originally posted by [throny](https://github.com/throny) on 2026-12-01T12:55:32Z. --- Closing as superseded: this change is already on `main` (d634138c). main eh.rs rewrite_code (atomic aligned-word replace, serialized, with expected bytes) rewrites monitorx to nops and mwaitx to pause+nops at first fault; superset of PR with tests Two independent checks found this: Foniass's close list on #1607 and ours against current `main`. If something here is still missing on `main`, say so and reopen it, or ask and I will. AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

Pull request closed

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!2608
No description provided.