Skip to content

fix(gpu): a pass reads the constants it was recorded with, and no reader joins a piece behind a shader's write - #735

Open
uncaney wants to merge 5 commits into
pragyaangaur:mainfrom
uncaney:fix/pass-constants-join-rule
Open

uncaney wants to merge 5 commits into
pragyaangaur:mainfrom
uncaney:fix/pass-constants-join-rule

Conversation

@uncaney

@uncaney uncaney commented Nov 28, 2026

Copy link
Copy Markdown

This series sits on main at 2738044.

What is wrong today

Two faults.

1. The join rule of the coalesced pump (hold.rs). Since #419, pump_coalesced and take_ready_with join ready segments of several queues into one host submission. All lists of a piece are prepared together: their steps run on the CPU before the GPU runs the first command of any of them. can_join is there to keep a list that reads title memory when its steps run (a copy, a move, a fill) out of a piece whose shaders write. But it is asked in only three places: when a segment of another queue joins, when a segment's wait is satisfied by the piece's own bottom-of-pipe writes, and when a fence wait on a deferred Signal is crossed. Even across queues, it is asked only of the first segment of a run. In three places where the per-queue pump ran the piece first, the next segment is taken with no question:

  • the queue's own Signal, deferred behind the piece (two submissions of one queue, ready together, become one piece);
  • a segment whose waits already hold, behind segments of its own queue;
  • the later segments of a queue's run joined across queues.

In each case a reader is prepared before the shaders ahead of it in the piece have run, and so it reads the bytes from before those shaders.

2. Constants a later title copy rewrites (draw.rs, host.rs). A list's title copies run on the CPU at submission, in recorded order, all before the GPU runs the submission. So a buffer's host copy has one content per submission, which is whatever the last copy left. Take a title that keeps one constant buffer and copies new values into it between two passes of one submission: the earlier pass reads the later values. One shape of this is a pass dispatched for a full-size target that reads a half size copied in for a later pass, and so draws only one quarter of its target.

The commits

  1. fix(gpu): ask the join of the whole run the per-queue pump kept together (hold.rs, six tests). Every place where the per-queue pump started a piece becomes a join boundary: a deferred Signal of the segment's own queue, and a segment with waits while the piece is not empty. At each boundary, can_join is asked of every segment of the run the per-queue pump kept together from there (run_from_head). The run joins whole, or the piece runs first and the run starts the next piece whole. No run that the per-queue pump kept together is split, and a piece never starts where that pump did not start one, so a frame never gets more pieces than the per-queue pump gave. Where can_join agrees, nothing changes. The rule also covers the lists that already copy, move or fill title memory on main today: this commit fixes the fault for them on its own, without the snapshots of commits 2 to 4.
  2. fix(gpu): learn which constant ranges a later copy of a submission rewrites (rewritten.rs, ranges.rs). The bookkeeping, plain std, with its tests and nothing using it yet: a pass notes the constant ranges it reads through tables, a title copy into one later in the submission makes the shared bytes known, and the next recording snapshots them. The first submission that shows a range is not helped (it is counted), and a range not seen for 64 to 128 frames is forgotten. A range a shader of the submission was declared to write before the pass is left in place.
  3. fix(gpu): tell tables made afresh apart by twin, cap and snapshot (fresh.rs). Why a table is made afresh, and the key that tells two such tables apart within a list, so that a table that needs a 16-bit unsigned twin and a snapshot is made once, with both.
  4. fix(gpu): give a pass a snapshot of constants a later copy of its submission rewrites (draw.rs, host.rs, descriptor/host.rs). The wiring: a constant view in a table reads a snapshot taken at its place among the list's steps, in a table made afresh in dynamic slots with the view on the list's scratch memory; the table's other slots follow the title's records at submission. A snapshot counts as a CPU read in batch_flags, so the rule of commit 1 keeps it out of a piece behind a shader's write. Commits 2 and 3 carry an #[allow(dead_code)] on their module until this commit uses them, and this commit removes it.
  5. docs: describe the snapshots of constants and the join rule in docs/macos.md.

On 2738044, the new list steps fit the list-steps line as main's own do: a snapshot counts with the title copies and fills, a constant read with the refreshes, and a remade table with the slots. VICTORY_SLOT_ONCE leaves a remade table out: it runs at every submission, as before.

Each of commits 1 to 4 builds, and passes the crate's native tests and the Windows clippy runs (with and without --all-features) on its own: 240, 252, 254 and 254 tests passed, none failed, 1 ignored. Commit 5 changes only the docs.

Defaults

Both changes are on by default, each with a way back:

  • The join rule of commit 1 is on whenever pieces are joined. VICTORY_COALESCE_PIECES=0 gives the per-queue pump, as before perf(gpu): coalesce cross-queue and bottom-of-pipe dependent held pieces #419, which does not have the fault.
  • The snapshots are on. VICTORY_SNAPSHOT_CONSTANTS=0 turns all of commits 2 to 4 off; with it off, the paths are those of main apart from an empty field or two and one range removal per title copy.

We kept them on because both fix wrong pictures, not speed. If you would rather have either behind a switch that is off by default, we will gladly change it.

Tests

The faults without the series. Measured on 5b57b11 (an earlier commit of main) with only the tests added. The failing run was not repeated on 2738044: hold.rs is the same file in both, but other files on the constants path (draw.rs, host.rs, descriptor/host.rs) changed between the two commits. On 2738044 all of them pass with the series (they are in the counts of the table).

  • The six new hold::tests (native) fail. Each one puts the writer and the reader in one piece with no join asked.
  • a_pass_reads_its_constants_as_they_were_before_a_later_copy_of_its_submission (GPU, under Wine with D3DMetal) runs three passes around two copies of one constant buffer in one submission. It checks the bytes once in a buffer with a host copy and once in title memory on a host buffer's pages. It fails: the first pass reads the values of the copy made after it (frame 1: 0x21xx where 0x11xx was copied before the pass).

Both test runs CONTRIBUTING asks for, on this branch and on plain main, both at 2738044:

this branch plain main
cargo test --workspace 878 passed, 0 failed, 23 ignored 858 passed, 0 failed, 23 ignored
Windows test binaries of the workspace under Wine 1804 passed, 5 failed, 55 ignored 1773 passed, 5 failed, 55 ignored

The Wine run: every test binary that cargo test --workspace --target x86_64-pc-windows-gnullvm builds (24), each run once under CrossOver's Wine with D3DMetal, with WINEDLLOVERRIDES="d3d12,d3d12core,dxgi=b" and the HLE probe fixture as tools/macos/test-wine.sh sets them up. We ran the binaries one by one rather than through the script itself.

  • The 31 more passes on this branch are its 31 new tests. In the crate's own binary: 741 passed, 5 failed, 33 ignored, against 710 passed, 5 failed, 33 ignored on plain main.
  • The same 5 fail on both trees: draw::tests::tracked_rows::block_compressed_rows_keep_their_block_alignment_across_pages and four indirect:: enhanced-barrier tests (enhanced_barriers_supported_and_command_list7_available and three narrow_clamp_sync_*).
  • On both trees the replay integration test binary aborts and gives no count; it is not in the numbers above.
  • The victory-hle test binary, which this series does not touch, was not steady on our machine, which other work kept busy during these runs. On plain main it ran past our five-minute limit in the full run (modules::xmem::tests::the_titles_virtual_free_is_ours failed and modules::xmem::tests::transfers_shrink_the_title_budget did not end); run alone four more times it passed whole each time (226 passed, 0 failed, 2 ignored), and that count is the one in the table. On this branch it passed whole in the full run and in two of four more runs; one of the other two stopped after modules::dstorage::tests::a_page_list_filled_after_its_request_starts failed, and one hung the same way as on plain main.
  • All snapshot and join tests pass, the 28 hold:: tests among them. With VICTORY_SNAPSHOT_CONSTANTS=0, the crate's snapshot, constant, twin, hold, rewritten and fresh tests pass too (113 passed, 0 failed, 4 ignored); the GPU test above then expects the old values.

Also on this branch, at 2738044:

  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings, with and without --all-features: clean. Host-target clippy fails on plain main too, with dead code (82 errors in victory-gpu), as CONTRIBUTING says. This branch has 84 there: the 2 more are run_from_head and run_joins, unused on the host like CanJoin and World.
  • cargo xtask policy: ok.
  • Only the changed code is formatted.

Tested on an Apple Silicon Mac, CrossOver 26.3, D3DMetal 3.0.

What was measured, and what was not

Measured in a real workload on earlier heads of the fork (before #419, and on two heads after it):

  • About 10 to 21 snapshots a frame (4 to 9 KB).
  • No pass left reading a later copy's bytes.
  • The quarter fault gone.
  • With joining on, as many pieces and hazard splits a frame as without the snapshots (median 11 snapshots a frame).

Not measured:

  • Frame time with the fix on and off, on a quiet machine. There is no such comparison, so no claim about cost or gain is made.
  • The series in a real workload on 2738044: the workload runs above were on earlier heads.
  • Pieces a frame with commit 1 in a workload. By construction it cannot exceed the per-queue pump's count, but it was not counted.
  • Whether the fault shows with D3DMetal 4.0 beta or in a release build.

Note

This work was AI-assisted: prepared with Claude Code, and reviewed and tested as described above.

🤖 Generated with Claude Code

uncaney and others added 5 commits November 28, 2026 19:52
The coalesced pump and `take_ready_with` join ready segments of several
queues into one host submission (pragyaangaur#419). The lists of a piece are prepared
together: all their steps run on the CPU before the GPU runs the first
command of any of them. `can_join` keeps a list that reads title memory
when its steps run (a copy, a move, a fill) out of a piece whose shaders
write or that has work to do after it ran. It was asked only across
queues, across a wait the piece's bottom-of-pipe writes hold, and across
a fence wait on a deferred Signal; across queues, of the first segment of
the queue's run only. Three places where the per-queue pump ran the
piece first took the next segment with no question:
- the queue's own Signal, deferred behind the piece: two submissions of
  one queue, ready together, became one piece;
- a segment whose waits already hold, behind segments of its own queue
  (a list that starts with a wait, or the second half of a list cut at
  a wait);
- the later segments of a queue's run joined across queues.
In each, a list that reads title memory was prepared before the shaders
of a segment ahead of it in the piece had run, and read the bytes of
before.

Now a segment of a queue whose Signal waits behind the piece, and a
segment with waits, are join boundaries while the piece is not empty,
and at every boundary `can_join` is asked of each segment of the run the
per-queue pump kept together from there (`run_from_head`: the segment,
then the segments after it with no waits, across fence waits that
already hold, up to the queue's next Signal, segment with waits, wait
that does not hold, or action). The run joins whole, or the piece runs
and the run starts the next piece whole. So no run that pump kept
together is split, and a piece never starts where that pump did not
start one: a frame gets at most the pieces the per-queue pump gave.
Where `can_join` agrees, nothing changes. A split is counted in
`hazard_splits`. VICTORY_COALESCE_PIECES=0 still gives the per-queue
pump.

Tests (native): six in hold::tests, each failing on the commit before
with the writer and the reader in one piece and no join asked, and
passing here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…writes

A list's title copies run on the CPU when it is submitted, in recorded
order with the refreshes of host copies, and all before the GPU runs the
submission's first command. A buffer's host copy so has one content per
submission: what the last copy left. A pass that reads constants through
a table, followed in the same submission by a title copy into them,
reads the later values.

This adds the bookkeeping that finds such ranges (`crate::rewritten`),
with nothing using it yet: `Reads` holds the constant ranges a
submission's passes read so far and the bytes its shaders were declared
to write; a title copy into a read range enters the shared bytes in
`Known`, from which the next recording takes a snapshot. The first
submission showing a range is counted, not helped; a range not shown for
64 to 128 frames is forgotten; a range a shader of the submission writes
before the pass is left in place. `VICTORY_SNAPSHOT_CONSTANTS=0` turns it
off. `Ranges` gains `new` (const, for a static) and `overlaps`.

The module is allowed to be unused until the commit that
wires it into draws and submission.

Tests (native): the learning rules in rewritten.rs and
`overlapping_is_sharing_a_byte` in ranges.rs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A shader reads the title's descriptor tables in place unless something
has to differ for it: the table reaches past a capped host heap, or the
shader moves 16-bit float or signed buffer views as integers and is shown
their unsigned twins. A constant view reading a snapshot (the commit
before) is a third reason. `crate::fresh` holds why a table is made
afresh (`Why`) and the key that tells two such tables apart within a list:
each slot's record, whether the shader moves halves there, the snapshot a
constant view reads there, and whether the slots are made again at
submission. A table needing a twin and a snapshot is one table holding
both, and no table with only one of them stands for it.

Plain std, used from the next commit (allowed to be unused until
then).

Tests (native): the twin-and-snapshot key tests in fresh.rs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…mission rewrites

A list's title copies run on the CPU when it is submitted, in recorded
order with the refreshes of host copies, and all before the GPU runs the
submission's first command. A buffer's host copy (or the title memory a
host buffer shares its pages with) so has one content per submission:
what the last copy left. A title that keeps one constant buffer and
copies new values into it between two passes of a submission had both
passes read the later values: a pass dispatched for a full-size target
read a half size copied in for a later pass, and drew one quarter of it.

A constant view in a table now reads a snapshot taken at its place among
the list's steps once a title copy has been seen to write its bytes
after a pass of the same submission read them (`crate::rewritten`, two
commits before). This wires that learning and the tables made afresh
(`crate::fresh`, the commit before) into the draws and the submission:
`Step::Snapshot`, `Step::Constant` and `Step::Table` in the list's steps,
`HostHeap::remake_at` for the slots made again at submission, and the
unused-code allowances of the two commits before removed.
- The table is made afresh in dynamic slots with that view on the list's
  scratch memory, as unaligned root constant views already read scratch.
  Its other slots follow the title's records at submission, as a table
  in place would; a table that also needs a 16-bit view as its unsigned
  twin is made afresh once with both (`crate::fresh`).
- Which ranges need it is learnt at submission: a pass notes the
  constant ranges it reads through tables, a title copy into one later
  in the submission makes the shared bytes known, and the next recording
  snapshots them. The first submission showing a range is not helped and
  is counted; a range not shown for 64 to 128 frames is forgotten.
- A snapshot cannot hold what a shader of the same submission wrote
  before its pass: where a shader write is declared over the constants
  before the pass, the pass reads them in place as before, and a
  snapshot already given is counted.
- A snapshot reads title memory when the list is prepared, so
  `batch_flags` counts it as a fill: a list holding one joins no piece
  after a shader's writes (with the commit before, at every place the
  per-queue pump ran the work before first).
- With the table memo (`VICTORY_TABLE_MEMO=1`) a remembered entry does
  not stand for a snapshot.

Cost: one note per constant range and list for buffers no copy rewrites
under a pass. One log line the first time, and a line with each
frame-rate report that has snapshots or late reads to count (snapshots,
tables made afresh with their host views and record bytes, late reads).
In a diagnostics build the operation list says `snapshot` beside the
view and describes the table made afresh.

VICTORY_SNAPSHOT_CONSTANTS=0 turns all of it off.

Tests: `a_pass_reads_its_constants_as_they_were_before_a_later_copy_of_its_submission`
(GPU, under Wine; fails without the snapshots: the first pass reads the
later copy's values), the twin, slot, memo, shader-write and count tests
in draw.rs and the join test in host.rs (native).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…acos.md

Says what a pass reading constants a later copy of its submission
rewrites looked like, what the layer does about it now, what it costs,
and the two switches: VICTORY_SNAPSHOT_CONSTANTS=0 and
VICTORY_COALESCE_PIECES=0.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

1 participant