D3D12.X: GPU timestamps into the title's own memory land after the GPU #311

Closed
github-bot wants to merge 1 commit from gh-pr/311/head into gh-pr/311/base
Owner

@⁠emirduran707 on GitHub · 2026-11-23 23:07 UTC · original
emirduran707:timestamps-outside-uma → main · +181 −41 · 3 files · 1 commits · closed without merging

Write64BitValueBottomOfPipeX / Write64BitValueTopOfPipeX with a timestamp flag were dropped when the address was outside the unified memory windows, although the same calls with a value already reach such memory through cpu_writes. The warning said 0x…698 is not 8-byte aligned in unified memory for 8-byte-aligned addresses, because one message covered both cases.

A retail title asks for such timestamps from its first frames. It hits this on a first run, while the coherent window is 512 MB (#284); with a learned window the addresses are inside it. XCL_COHERENT_MB=512 reproduces it on any run.

What is in it

  • hle/d3d12x/cpu_writes.rs: a deferred write is either dwords (as before) or the 8 bytes the GPU resolved into a mapped readback buffer; the worker copies those to the address once the queue's fence is there, as one 8-byte store (a title polling it never sees half a value).
  • hle/d3d12x/list_x.rs: timestamp() resolves a timestamp for the title's own writable memory into a per-device readback buffer (one 8-byte slot per query of the existing timestamp heap) and queues the copy; unified memory goes as before. Unaligned addresses and memory that is neither unified nor writable get their own warnings.
  • Tests: cpu_writes::resolved_values_land_after_the_queue; the test title's console list check writes a timestamp into its heap (it failed on main with no GPU timestamp in the title's own memory). Both cpu_writes tests wait for the whole value (review below).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.31044.16; Ryzen 5 5600, 32 GB; Windows 11 Pro 26300; rustc 1.100.0 x86_64-pc-windows-msvc; DXC 1.9.2602.17; debug for the suite, release for the runs.
  • cargo test --workspace -- --test-threads=4 at e39bbfb: 667 passed, 0 failed, 41 ignored; no SKIPPED lines.
  • Retail title, scripts/smoke.ps1 (60 s) with XCL_COHERENT_MB=512 set, main db1ddb2 vs this PR: main logs Write64BitValueBottomOfPipeX: … not 8-byte aligned in unified memory – not done 4 times (the warning's cap); this PR logs none and bottom-of-pipe writes outside unified memory are done by the CPU after the GPU (2 in the first submission). Frames 561 vs 694, peak commit 15 458 vs 15 483 MB, no crash or device removal in either. I did not see a visible change from the timestamps.
  • The race from the review: each cpu_writes test run alone, 10 times, passes on this machine before and after; with a 20 ms sleep after each dword store (local experiment, not committed) both fail 5 of 5 before the fix and 0 of 5 after.

Regression run

The gpu: line names a virtual display adapter of this machine (what #281 / #282 / #285 fix); XCL's log names backend 'real' – AMD Radeon RX 9060 XT (16188 MiB VRAM) in both runs.

main:

xcl-smoke v1
commit: db1ddb25f3ff0c1686eed8200d238ad35f895926
gpu: Meta Virtual Monitor
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1438 (PresentX 1438)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14862 MB

PR:

xcl-smoke v1
commit: e39bbfb040acdfa5a5290948a8b75b0d6b98802b
gpu: Meta Virtual Monitor
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1439 (PresentX 1439)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14873 MB

Not tested

  • NVIDIA / wave32-only GPUs and RDNA2/3.
  • Whether the title's behaviour depends on these timestamps (nothing visible changed in the runs above; the title stayed on its first menu, no input given).
  • Write64BitValueTopOfPipeX timestamps into the title's own memory on a retail title (same code path; the test title covers the bottom-of-pipe one).

AI use

  • Claude Code with Claude Opus 5.5: found the dropped timestamps in a run's log, wrote the code, the tests and this description; the runs above were made on my machine.
**@⁠emirduran707** on GitHub · 2026-11-23 23:07 UTC · [original](https://github.com/xcl-dev/xcl/pull/311) `emirduran707:timestamps-outside-uma` → `main` · +181 −41 · 3 files · 1 commits · closed without merging `Write64BitValueBottomOfPipeX` / `Write64BitValueTopOfPipeX` with a timestamp flag were dropped when the address was outside the unified memory windows, although the same calls with a value already reach such memory through `cpu_writes`. The warning said `0x…698 is not 8-byte aligned in unified memory` for 8-byte-aligned addresses, because one message covered both cases. A retail title asks for such timestamps from its first frames. It hits this on a first run, while the coherent window is 512 MB (#284); with a learned window the addresses are inside it. `XCL_COHERENT_MB=512` reproduces it on any run. **What is in it** - `hle/d3d12x/cpu_writes.rs`: a deferred write is either dwords (as before) or the 8 bytes the GPU resolved into a mapped readback buffer; the worker copies those to the address once the queue's fence is there, as one 8-byte store (a title polling it never sees half a value). - `hle/d3d12x/list_x.rs`: `timestamp()` resolves a timestamp for the title's own writable memory into a per-device readback buffer (one 8-byte slot per query of the existing timestamp heap) and queues the copy; unified memory goes as before. Unaligned addresses and memory that is neither unified nor writable get their own warnings. - Tests: `cpu_writes::resolved_values_land_after_the_queue`; the test title's console list check writes a timestamp into its heap (it failed on main with `no GPU timestamp in the title's own memory`). Both `cpu_writes` tests wait for the whole value (review below). **Tested** - AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.31044.16; Ryzen 5 5600, 32 GB; Windows 11 Pro 26300; rustc 1.100.0 `x86_64-pc-windows-msvc`; DXC 1.9.2602.17; debug for the suite, release for the runs. - `cargo test --workspace -- --test-threads=4` at e39bbfb: 667 passed, 0 failed, 41 ignored; no `SKIPPED` lines. - Retail title, `scripts/smoke.ps1` (60 s) with `XCL_COHERENT_MB=512` set, main db1ddb2 vs this PR: main logs `Write64BitValueBottomOfPipeX: … not 8-byte aligned in unified memory – not done` 4 times (the warning's cap); this PR logs none and `bottom-of-pipe writes outside unified memory are done by the CPU after the GPU (2 in the first submission)`. Frames 561 vs 694, peak commit 15 458 vs 15 483 MB, no crash or device removal in either. I did not see a visible change from the timestamps. - The race from the review: each `cpu_writes` test run alone, 10 times, passes on this machine before and after; with a 20 ms sleep after each dword store (local experiment, not committed) both fail 5 of 5 before the fix and 0 of 5 after. **Regression run** The `gpu:` line names a virtual display adapter of this machine (what #281 / #282 / #285 fix); XCL's log names `backend 'real' – AMD Radeon RX 9060 XT (16188 MiB VRAM)` in both runs. main: ``` xcl-smoke v1 commit: db1ddb25f3ff0c1686eed8200d238ad35f895926 gpu: Meta Virtual Monitor title: retail-title seconds: 60 exit: 0x0 frames shown: 1438 (PresentX 1438) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14862 MB ``` PR: ``` xcl-smoke v1 commit: e39bbfb040acdfa5a5290948a8b75b0d6b98802b gpu: Meta Virtual Monitor title: retail-title seconds: 60 exit: 0x0 frames shown: 1439 (PresentX 1439) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14873 MB ``` **Not tested** - NVIDIA / wave32-only GPUs and RDNA2/3. - Whether the title's behaviour depends on these timestamps (nothing visible changed in the runs above; the title stayed on its first menu, no input given). - `Write64BitValueTopOfPipeX` timestamps into the title's own memory on a retail title (same code path; the test title covers the bottom-of-pipe one). **AI use** - Claude Code with Claude Opus 5.5: found the dropped timestamps in a run's log, wrote the code, the tests and this description; the runs above were made on my machine.
Write64BitValueBottomOfPipeX / Write64BitValueTopOfPipeX with a timestamp
flag were dropped when the address was outside the unified memory windows,
although the same calls with a value already reach such memory through
cpu_writes. A retail title asks for them in plain memory from its first
frames; the log said "is not 8-byte aligned in unified memory" for
8-byte-aligned addresses, because one message covered both cases.

Now such a timestamp is resolved into a per-device readback buffer (one
8-byte slot per query of the timestamp heap) and the cpu_writes worker
copies it to the address once the queue's fence is there. Unaligned
addresses and memory that is neither unified nor writable each get their
own warning.

Tests: cpu_writes::resolved_values_land_after_the_queue, and the test
title's console list check writes a timestamp into its heap (failed
before with "no GPU timestamp in the title's own memory").

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:19:43 +00:00
github-bot closed this pull request 2026-11-26 04:19:43 +00:00
Author
Owner

@⁠regiumlepidi on GitHub · 2026-11-23 23:31 UTC · original

A race in the cpu_writes tests that this PR makes easy to hit, and that touches the timestamps themselves.

write_now stores the dwords one at a time (cpu_writes.rs, for (i, &w) in words.iter().enumerate() { … store(w, Release) }), so a Value::Resolved timestamp reaches the title as two 32-bit stores, low half first. Both tests wait only for the first store: writes_land_after_the_queue for word 0 == 7, resolved_values_land_after_the_queue for cell != 0, i.e. the low half, and then compare the whole value.

Tested on 600f092, RTX 5070 Ti (driver 617.42), Ryzen 7 9800X3D, Windows 11 Pro 25H2, rustc 1.100.0 MSVC, debug:

Run writes_land_after_the_queue resolved_values_land_after_the_queue
cargo test -p xcl --lib -- --exact <test>, this PR 20 of 20 failed 20 of 20 failed
the same, main 4017f8f 0 of 20 failed –
this PR + a 20 ms sleep after each store in write_now (local experiment only) 5 of 5 failed: left: [7, 0] 5 of 5 failed: left: 3642888684, right: 252750991852
.\scripts\test.ps1, this PR pass (653 passed, 0 failed, 40 ignored) pass

Alone, the resolved test fails with only the low half, e.g. left: 1473621646, right: 254876692110 (the difference is 59 × 2³²). So with the suite's timing both pass, as in your run; run alone they fail every time on this machine. (writes_land_after_the_queue printing left: [7, 9] right: [7, 9] is the same race: the comparison saw [7, 0], and the second store landed before the message was formatted.)

Outside the tests this means a title polling a 64-bit timestamp can read the new low half with the old high half.

Suggestions (either is enough for the tests; the second also covers the title):

  • the tests wait for the whole value, read atomically, e.g. while words() != [7, 9] with words = || [0, 1].map(|i| (*((va + 4 * i) as *const AtomicU32)).load(Acquire)), and while cell.load(Acquire) != std::ptr::read_volatile(p as *const u64) for the resolved one. On main with the 20 ms experiment, that change to writes_land_after_the_queue passes 5 of 5 (and 300 of 300 without it);
  • a resolved timestamp written as one 8-byte store when the address is 8-byte aligned (one AtomicU64::store), which is what #317 does for its Write::U64 / OsTimestamp. The two PRs reshape Write differently and will conflict in this file.

Not tested: a retail title; AMD for the failure (these tests use the default adapter, so the RX 9060 XT / Ryzen 5 5600 timing may simply never lose the race).

AI use: Claude Code with Claude Opus 5.5 found the race, ran these tests and wrote this comment; the numbers come from those runs.

**@⁠regiumlepidi** on GitHub · 2026-11-23 23:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/311#issuecomment-7117060832) A race in the `cpu_writes` tests that this PR makes easy to hit, and that touches the timestamps themselves. `write_now` stores the dwords one at a time (`cpu_writes.rs`, `for (i, &w) in words.iter().enumerate() { … store(w, Release) }`), so a `Value::Resolved` timestamp reaches the title as two 32-bit stores, low half first. Both tests wait only for the first store: `writes_land_after_the_queue` for word 0 == 7, `resolved_values_land_after_the_queue` for `cell != 0`, i.e. the low half, and then compare the whole value. **Tested** on `600f092`, RTX 5070 Ti (driver 617.42), Ryzen 7 9800X3D, Windows 11 Pro 25H2, rustc 1.100.0 MSVC, debug: | Run | `writes_land_after_the_queue` | `resolved_values_land_after_the_queue` | |---|---|---| | `cargo test -p xcl --lib -- --exact <test>`, this PR | **20 of 20 failed** | **20 of 20 failed** | | the same, `main` `4017f8f` | 0 of 20 failed | – | | this PR + a 20 ms sleep after each store in `write_now` (local experiment only) | 5 of 5 failed: `left: [7, 0]` | 5 of 5 failed: `left: 3642888684, right: 252750991852` | | `.\scripts\test.ps1`, this PR | pass (653 passed, 0 failed, 40 ignored) | pass | Alone, the resolved test fails with only the low half, e.g. `left: 1473621646, right: 254876692110` (the difference is 59 × 2³²). So with the suite's timing both pass, as in your run; run alone they fail every time on this machine. (`writes_land_after_the_queue` printing `left: [7, 9] right: [7, 9]` is the same race: the comparison saw `[7, 0]`, and the second store landed before the message was formatted.) Outside the tests this means a title polling a 64-bit timestamp can read the new low half with the old high half. **Suggestions** (either is enough for the tests; the second also covers the title): - the tests wait for the whole value, read atomically, e.g. `while words() != [7, 9]` with `words = || [0, 1].map(|i| (*((va + 4 * i) as *const AtomicU32)).load(Acquire))`, and `while cell.load(Acquire) != std::ptr::read_volatile(p as *const u64)` for the resolved one. On `main` with the 20 ms experiment, that change to `writes_land_after_the_queue` passes 5 of 5 (and 300 of 300 without it); - a resolved timestamp written as one 8-byte store when the address is 8-byte aligned (one `AtomicU64::store`), which is what #317 does for its `Write::U64` / `OsTimestamp`. The two PRs reshape `Write` differently and will conflict in this file. **Not tested:** a retail title; AMD for the failure (these tests use the default adapter, so the RX 9060 XT / Ryzen 5 5600 timing may simply never lose the race). AI use: Claude Code with Claude Opus 5.5 found the race, ran these tests and wrote this comment; the numbers come from those runs.
Author
Owner

@⁠emirduran707 on GitHub · 2026-11-23 23:49 UTC · original

Thanks, you were right on both counts. Fixed in e39bbfb (squashed, rebased on db1ddb2):

  • A resolved timestamp now goes to the title as one 8-byte AtomicU64 store when the address is 8-byte aligned (it always is: timestamp() refuses unaligned ones); the two-dword path is only the fallback.
  • Both tests wait for the whole value: writes_land_after_the_queue for [7, 9] read as two atomic loads, resolved_values_land_after_the_queue for the resolved value itself.
  • writable() and the worker share one can_write_now check instead of two copies of it.

On the RX 9060 XT the race never shows on its own (each test alone, 10 runs: 0 failures before and after, as you expected for AMD). With your experiment (a 20 ms sleep after each dword store, local only): 5 of 5 failures for each test on 135195f, 0 of 5 on e39bbfb. Suite at e39bbfb: 667 passed, 0 failed, 41 ignored, no SKIPPED; new smoke blocks are in the description.

On #317: it reshapes Write too, so whichever lands second will need a rebase; this PR keeps the GPU-timestamp flags as they are on main and only adds the readback path, so the OS-QPC split there can sit on top of it.

AI use: Claude Code with Claude Opus 5.5 made the fix, ran these tests and wrote this reply.

**@⁠emirduran707** on GitHub · 2026-11-23 23:49 UTC · [original](https://github.com/xcl-dev/xcl/pull/311#issuecomment-7117410582) Thanks, you were right on both counts. Fixed in e39bbfb (squashed, rebased on db1ddb2): - A resolved timestamp now goes to the title as one 8-byte `AtomicU64` store when the address is 8-byte aligned (it always is: `timestamp()` refuses unaligned ones); the two-dword path is only the fallback. - Both tests wait for the whole value: `writes_land_after_the_queue` for `[7, 9]` read as two atomic loads, `resolved_values_land_after_the_queue` for the resolved value itself. - `writable()` and the worker share one `can_write_now` check instead of two copies of it. On the RX 9060 XT the race never shows on its own (each test alone, 10 runs: 0 failures before and after, as you expected for AMD). With your experiment (a 20 ms sleep after each dword store, local only): 5 of 5 failures for each test on 135195f, 0 of 5 on e39bbfb. Suite at e39bbfb: 667 passed, 0 failed, 41 ignored, no `SKIPPED`; new smoke blocks are in the description. On #317: it reshapes `Write` too, so whichever lands second will need a rebase; this PR keeps the GPU-timestamp flags as they are on `main` and only adds the readback path, so the OS-QPC split there can sit on top of it. AI use: Claude Code with Claude Opus 5.5 made the fix, ran these tests and wrote this reply.
Author
Owner

@⁠ivall on GitHub · 2026-11-24 00:04 UTC · original

Closing as superseded by #330 (merged in a6f5f7a): the same timestamps outside unified memory, with the resolved 64-bit value written as one atomic store (the two 32-bit stores here were the race regiumlepidi measured). Thanks!

**@⁠ivall** on GitHub · 2026-11-24 00:04 UTC · [original](https://github.com/xcl-dev/xcl/pull/311#issuecomment-7117666634) Closing as superseded by #330 (merged in a6f5f7a): the same timestamps outside unified memory, with the resolved 64-bit value written as one atomic store (the two 32-bit stores here were the race regiumlepidi measured). Thanks!
Author
Owner

@⁠PHeitor1 on GitHub · 2026-11-24 00:13 UTC · original

Tested e39bbfb merged onto main 3516080.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 671 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Build clean.

Run and written with Claude Code (Claude Opus 5.5).

**@⁠PHeitor1** on GitHub · 2026-11-24 00:13 UTC · [original](https://github.com/xcl-dev/xcl/pull/311#issuecomment-7117846115) Tested `e39bbfb` merged onto `main` `3516080`. RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, `cargo test --workspace --no-fail-fast -- --test-threads=4`, no `XCL_SAMPLE_*`. **Pass:** 671 passed, 0 failed, 41 ignored. SKIPPED: only the 2 `no wave64` tests (`gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`). Build clean. _Run and written with Claude Code (Claude Opus 5.5)._

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.