Skip to content

runtime: preserve CPU fence signal rewinds - #3279

Open
wish131400 wants to merge 7 commits into
R0Y00:mainfrom
wish131400:runtime/fence-observed-values
Open

wish131400 wants to merge 7 commits into
R0Y00:mainfrom
wish131400:runtime/fence-observed-values

Conversation

@wish131400

@wish131400 wish131400 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

CPU ID3D12Fence::Signal can rewind a fence. The previous atomic maximum left both the shim method and completion address at 9 after a CPU sequence 9 -> 2 -> 0 -> 7. Publishing the observed value without serialization then allowed two CPU callers to publish out of order: A captures 1, B publishes 2, A publishes its stale 1.

Serialize desktop Signal, GetCompletedValue and publication with a lock on each fence. Keep slot 8 and the slot 12 completion-address reads lock-free. fence_reached uses the same writer lock but retains its existing atomic-maximum queue semantics.

This branch incorporates the serialization commits from #3350, preserving their author history, and resolves the CMake preset conflict against main 44a3ad5aa9c679cc039b8cb8275738dd6e4405d2. The controlled regression goes beyond the 128-pair barrier stress check: a test-only hook pauses A after the desktop observation and before publication, acknowledges B immediately before the writer lock, allows B 250 ms, then releases A without requiring B to complete first. Fixed behavior must finish at desktop/method/address 2/2/2. The isolated test DLL can bypass the writer lock as a negative control, which succeeds only when the exact stale 2/1/1 outcome and all scheduling handshakes are observed. Each wait is bounded. The production DLL has no scheduling export or unlocked mode.

Both fence executables and all three WARP cases are included in the PR build/test preset. Historical sequential results in the experiment notes are now distinguished from the new concurrent measurements.

Validation on Windows 10 Pro build 19045, MSVC 19.43.34810, D3D12 debug layer enabled, tests run serially:

  • Code revision ed554820bed30737fba887ee991611b8fd761e92; final documentation revision 77b41d55. Debug was also rebuilt/rechecked at that final head.
  • Debug: 7/7 passed, zero skips. Release: 7/7 passed, zero skips. These cover the production, controlled and exact negative-control cases on WARP and RTX 4070 Ti SUPER (driver 32.0.15.7270), plus startup_queue_wait_warp.
  • Controlled fixed result on both adapters/configurations: B blocked during A's pause; desktop/method/address 2/2/2. Negative control on both: B completed during the pause; desktop/method/address 2/1/1.
  • Sequential 9, 2, 0, 7 and 128-pair stress checks passed in both production and controlled fixtures.
  • dumpbin /exports confirmed the scheduling hook is absent from the production DLL and present only in the isolated test DLL.
  • ctest --preset pr --show-only=json-v1 without an extra filter confirmed all three WARP cases are selected.
  • python -B tests/docs_links_check.py, python -B tests/bidi_controls_check.py and git diff --check passed.

Reproduction:

. .\tools\dev.ps1
cmake --preset dev -DINTAKE_PYTHON=C:/path/to/python.exe
cmake --build --preset dev --target startup_fence_values startup_fence_values_controlled startup_queue_wait --parallel 4
ctest --preset dev --no-tests=error -V -R '^startup_(fence_values(_controlled|_stale_control)?_(warp|hardware)|queue_wait_warp)$'
# Repeat configure/build/ctest with preset release for /O2 coverage.

Refs #3280. Queue rewinds and delayed higher-target callbacks still need their own completion-order mechanism. The optional --queue-rewind diagnostic was repeated on WARP Release: desktop 3, method/address 7, expected diagnostic failure (exit 1), excluded from the passing count. A failed native Signal was not induced; the HRESULT guard remains code-reviewed. These fixtures do not establish full startup or frame rate. All inputs are source-built; no game or SDK assets are included.

CI for 77b41d55 completed: run 46369026117.

  • Build and test on Windows: success. The PR preset selected 89 tests: 86 passed, 3 existing placeholder capability skips, zero failures. All three new/updated fence WARP cases passed.
  • Assemble and translate gfx1013 fixtures: success.
  • Platform-independent Python checks on Linux: failure in startup_triage_run_check.py, reporting the missing an output UAV interval exceeds the D3D12 buffer view element limit allowlist entry.

I reproduced the identical triage failure from the unmodified main base 44a3ad5a, using the test, tool and source inputs extracted directly from that commit. None of those files differ in this PR. Existing #3546 and #3559 already fix that exact upstream failure; #3542 also includes it. This PR does not duplicate those unrelated fixes. The Windows /O2 CI job is not selected for ordinary PR runs; Release coverage above was run locally.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Release check on a5a92ea1: ctest --preset release --no-tests=error -V -R '^startup_(fence_values_(warp|hardware)|queue_wait_warp)$' passed 3/3. The 9 → 2 → 0 → 7 sequence matched the desktop fence, method and completion address on inbox WARP and RTX 4060 Laptop with the debug layer enabled.

The new publication breaks ordering with two CPU callers, even with no queue and with increasing values. If A's Signal(1) succeeds and A reads 1, A can pause before InterlockedExchange64. B's Signal(2) can then complete and publish 2, after which A publishes its stale 1. The native value stays 2, while the method and completion word can stay at 1 indefinitely. The previous atomic max avoided this regression for increasing CPU signals. This is a source-derived interleaving, separate from the queued rewind tracked in #3280.

Please serialize the native CPU signal and publication on each fence, or otherwise prevent stale CPU observations from overwriting newer ones. Add a controlled two-caller regression alongside the existing sequential rewind fixture. The queue follow-up can remain separate.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the clear write-up and the sequential fixture. Could you address R0Y00's requested change before this merges? The head a5a92ea1 is the commit that review covered, so it isn't in yet. Here is what I measured on that head:

  1. Two CPU signallers can leave the completion word stale. fence_signal exchanges in GetCompletedValue() with no per-fence lock: awk '/^HRESULT __stdcall fence_signal/,/^}/' src/startup_stubs/d3d12_xs_device.cpp | grep -cE 'Lock|mutex|SRW' → 0, and WrapperOwn has no lock member either. Take this order: A signals 1 and reads 1, B signals 2 and publishes 2, then A's exchange writes 1. A step model of that schedule gives native=2 word=1 on this head and native=2 word=2 with main's fence_reached maximum.
  2. The title's own threads aren't needed to hit this. The DirectStorage stand-in signals through fence slot 10 from a worker thread per queue (signal_fence(entry.fence, entry.value), reached from queue_worker in src/startup_stubs/xcrdapi.cpp). Two queues sharing a fence, or one queue plus a CPU thread, take the same path. I found this in the source and did not trigger it.
  3. Suggested fix: serialize Signal, GetCompletedValue and the publish for each fence, or otherwise stop an older observation from replacing a newer one. Please also add the controlled two-caller regression next to the 9→2→0→7 loop, as requested.
  4. CI doesn't run the fixture yet. ctest --preset pr uses an include list, and the Windows job log has 0 lines matching startup_fence_values (100% tests passed out of 70). Adding startup_fence_values_warp to that list is the maintainers' call.
  5. Minor: no test reaches the SUCCEEDED(hr) guard. On the sequential fixture, publishing value instead of the desktop reading also gives the same results. After the fix, update the doc's sentence "this patch adds no live desktop read or lock" and the body's "does not establish concurrent producer ordering", because main's maximum handled the two-caller case.

The trial merge into current main is clean (git merge-tree --write-tree origin/main a5a92ea1 → rc 0), and tests/docs_links_check.py passes on the merged tree. I did not build or run anything on Windows.

This comment was AI-assisted.

Serialize native CPU Signal calls and cached completion publication per fence, and add a barrier-controlled two-caller regression. Register the WARP fixture in PR smoke tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

I compared this head with #3350. git merge-base --is-ancestor a5a92ea1 ac685174 is true: #3350 carries this commit unchanged and adds the per-fence lock that R0Y00's review asked for, plus the pr preset entry. So #3350 supersedes this PR, and review can continue there. Closing this one once #3350 lands is the maintainers' call.

This head still merges cleanly into main af1cab5c (git merge-tree --write-tree rc 0). On its own, though, it would bring in the two-caller stale-word case that #3350 fixes.

MUST
None new. The head is still a5a92ea1, so the two-caller point in my earlier comment still applies.

SHOULD / NIT
None new.

AI-assisted.

@wish131400

wish131400 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed the CPU publication ordering request at 77b41d55 (implementation ed554820). I incorporated #3350's per-fence writer serialization with its author history, resolved the current-main preset conflict, and added the controlled interleaving requested here and in Leyukaka's review. Slot 8 remains lock-free, as suggested in the review of #3350.

The test pauses A after capturing desktop value 1 and before its exchange, starts B at value 2, acknowledges B before its lock attempt, gives B a bounded interval, then releases A without waiting for B first. With the lock: B waits, final desktop/method/address = 2/2/2. The separate test DLL's unlocked negative control reproducibly gives 2/1/1 and only passes when that exact stale outcome and the scheduling handshakes are observed. Production exports no test hook.

Debug and Release each passed 7/7 checks, zero skips, serially on WARP and RTX 4070 Ti SUPER, with the debug layer enabled. This includes the controlled and negative-control cases on both adapters, the existing sequential/stress checks, and startup_queue_wait_warp. The PR preset selects all three WARP cases. Documentation links, bidi checks and git diff --check passed; the experiment notes now separate the old sequential run from these new measurements. The queued-rewind diagnostic still fails as expected and remains tracked by #3280.

The PR description contains the commands and environment. Please re-review this revision for the requested CPU ordering fix and controlled regression.

CI for 77b41d55 completed: run 46369026117.

  • Build and test on Windows: success. The PR preset selected 89 tests: 86 passed, 3 existing placeholder capability skips, zero failures. All three new/updated fence WARP cases passed.
  • Assemble and translate gfx1013 fixtures: success.
  • Platform-independent Python checks on Linux: failure in startup_triage_run_check.py, reporting the missing an output UAV interval exceeds the D3D12 buffer view element limit allowlist entry.

I reproduced the identical triage failure from the unmodified main base 44a3ad5a, using the test, tool and source inputs extracted directly from that commit. None of those files differ in this PR. Existing #3546 and #3559 already fix that exact upstream failure; #3542 also includes it. This PR does not duplicate those unrelated fixes. The Windows /O2 CI job is not selected for ordinary PR runs; Release coverage above was run locally.

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