Skip to content

runtime: preserve CPU fence signal rewinds (replacement for #3279) - #3350

Open
romn4rch wants to merge 4 commits into
R0Y00:mainfrom
romn4rch:copilot/pr-3279-review-58fd6315
Open

romn4rch wants to merge 4 commits into
R0Y00:mainfrom
romn4rch:copilot/pr-3279-review-58fd6315

Conversation

@romn4rch

@romn4rch romn4rch commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Change

Replacement for #3279: serialize each fence's native CPU Signal, desktop completion observation, and cached-word publication with a per-fence SRW lock. The source-built fixture covers the 9 → 2 → 0 → 7 rewind sequence and 128 barrier-started two-caller signal pairs. The PR smoke-test filter includes startup_fence_values_warp. Current upstream main at 24fcd0b5742abb2fcaba8066093d70829df956a3 was merged with normal history (merge commit 95613c4f5); the original PR remains open and unchanged.

Verification

  • & .\tools\dev.ps1; cmake --preset dev; cmake --build --preset dev --target startup_fence_values startup_queue_wait --parallel 4; ctest --preset dev --no-tests=error -V -R '^startup_(fence_values|queue_wait)_warp$' — passed 2/2 on Microsoft Basic Render Driver (WARP), D3D12 debug layer enabled.
  • ctest --preset pr --no-tests=error -N -R '^startup_fence_values_warp$' — selected the fence WARP test (1 test).
  • python -B tests/docs_links_check.py — passed; checked 156 Markdown files and 674 links.

The full PR test suite and a post-merge hardware run were not run. CMake reported that Python-driven CTest registrations were disabled because out/pe-env/Scripts/python.exe is absent.

Evidence and follow-up

The fixture is source-built and compares the shim's completion method and address with its desktop fence. See the experiment note. It does not establish full-title startup. Queued rewinds and mixed CPU/queue producer ordering remain separate, unresolved behavior; no claim is made for those cases.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

wish131400 and others added 4 commits November 28, 2026 13:04
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>
@romn4rch romn4rch changed the title runtime: CPU fence rewind review follow-up runtime: preserve CPU fence signal rewinds (replacement for #3279) Nov 28, 2026

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — correct fix for CPU fence signal rewinds.

Patches fence_signal to read GetCompletedValue() after a successful desktop Signal and publish the actual desktop observation atomically via InterlockedExchange64 on the fence_lock, instead of blindly setting the cached word to the requested value. fence_completed_value also acquires the fence_lock for a consistent read. Concurrent signal test (128 pairs) validates the fix.

The documented queued-rewind limitation is useful context and correctly excluded from passing tests.

Ready to land.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — correct fix for CPU fence signal rewinds.

Patches fence_signal to read GetCompletedValue() after a successful desktop Signal and publish the actual desktop observation atomically via InterlockedExchange64 on the fence_lock, instead of blindly setting the cached word to the requested value. fence_completed_value also acquires the fence_lock for a consistent read. Concurrent signal test (128 pairs) validates the fix.

The documented queued-rewind limitation is useful context and correctly excluded from passing tests.

Ready to land.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Diff review of ac685174, by reading only; nothing built or run. Against the two requests in the #3279 review: the serialization is done, and the two-caller test is not yet a controlled regression of the interleaving described there.

Lock (src/startup_stubs/d3d12_xs_device.cpp):

  • After wrap(), both writers of completed hold fence_lock exclusively: fence_reached (:234-241) and fence_signal (:4682-4688). fence_completed_value reads under the shared lock (:4694-4696). wrap() memsets the wrapper, so the lock starts zeroed, which is SRWLOCK_INIT.
  • Under fence_lock, fence_signal only calls the desktop Signal and GetCompletedValue, and fence_reached calls nothing. fence_signal no longer calls fence_reached, so the SRW lock is not re-entered. The fence_reached call sites (:1978, :1987, :4758) take no other lock around the call, and the DirectStorage worker releases its queue mutex before run_entry reaches signal_fence (xcrdapi.cpp:1586). I found no lock-order problem.

Test (tests/startup_fence_values.cpp:107-131):

  • The barrier only lines up the two thread starts. Nothing holds caller A between the desktop Signal/GetCompletedValue and its InterlockedExchange64.
  • Without the lock (the a5a92ea1 code), a mismatch needs A's exchange to land after B's whole Signal, read and publish. A's window is a few instructions, so the loop would likely pass there too.
  • The loop should fail on main's atomic maximum: values 1 and 2 alternate, so once the word is 2, any pair that ends with native 1 mismatches.
  • A controlled version needs a test-only hook that pauses fence_signal after the desktop calls, in the style of the existing x2w_d3d12_fail_wraps export. The test can then run A (value 1) to the pause, start B (value 2), give B a bounded time, release A, join both, and check word == method == native == 2. Without the lock B finishes during the pause and A then publishes 1 over it; with the lock B waits for A, so the test must not wait for B before releasing A.

Other:

  • CMakePresets.json: only startup_fence_values added to the pr build targets and startup_fence_values_warp to the pr test filter.
  • No test reaches the failed-Signal side of the SUCCEEDED(hr) check; the doc says a failed desktop signal was not induced.
  • git merge-tree --write-tree origin/main ac685174 on 212d4b5f: clean, rc 0.

@lukaslw-lab lukaslw-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed ac685174 by reading the diff; not built or run. Adding two points to the review above.

  • No longer merges into current main: git merge-tree --write-tree against 46226004 conflicts in CMakePresets.json (the pr build targets and test filter); d3d12_xs_device.cpp merges cleanly. GitHub shows the PR as not mergeable.
  • fence_completed_value (d3d12_xs_device.cpp:4692-4697) takes fence_lock shared only to read completed. That word is written only by InterlockedExchange64 / InterlockedCompareExchange64, an aligned 64-bit read is atomic on x64, and the title reads the same word without the lock through the slot 12 address. The lock does not change which values slot 8 can return; a plain read (or a comment on why the lock is needed) would keep slot 8 lock-free.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

I reviewed ac685174 by reading the diff against main af1cab5c; I built and ran nothing. git merge-base --is-ancestor a5a92ea1 ac685174 is true, so this branch carries #3279 unchanged and supersedes it. CI at this head ran the new test: the Windows job shows startup_fence_values_warp ... Passed and 100% tests passed out of 74. On an export of the head, tests/docs_links_check.py (Checked 156 Markdown files and 674 links.) and tests/bidi_controls_check.py pass. Leyukaka's and lukaslw-lab's points still stand, and I don't repeat them here.

Please rebase. git merge-tree --write-tree origin/main ac685174 still conflicts in CMakePresets.json only, because main has added more entries to the pr targets and filter since.

MUST

  1. The two-caller loop still doesn't pin the lock, as Leyukaka worked through. A controlled two-caller test was the second half of R0Y00's change request on runtime: preserve CPU fence signal rewinds #3279, so I'd treat this as blocking until a pause-and-release test, or an equivalent, is in.

SHOULD
2. The change applies to every fence on every path, with no opt-in. A CPU Signal below the published word now lowers the word the title polls through slot 12; main kept the maximum. That matches the D3D12 contract, but nothing shows whether a loading run ever rewinds a fence. fence_signal logs nothing, so a run can't show how often its result now differs from main's. A count of successful CPU signals whose published value is below the previous word would answer that, either in the heartbeat or as a first-calls log line.
3. The "Measured result" section doesn't say whether its table was measured with the lock and the two-caller loop. The date, base 240e0fb9, compiler and both rows (WARP and RTX 4070 Ti SUPER) are unchanged from #3279. Yet the section now says "These results cover the CPU signal contract and a two-caller barrier regression". The body reports only a WARP run at this head and says no post-merge hardware run was done. Please name the commit each row was measured at, or re-measure the table at this head.

NIT
4. "A later or equal callback retains the existing value through the same publication path" is hard to follow. fence_reached keeps the larger of the word and the callback's value, under the same lock. Saying that directly would be clearer.

AI-assisted.

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.

6 participants