Skip to content

perf(shader): read constants up to four dwords per load - #373

Open
romainhedouin wants to merge 1 commit into
pragyaangaur:mainfrom
romainhedouin:perf/constant-loads-four-dwords
Open

romainhedouin wants to merge 1 commit into
pragyaangaur:mainfrom
romainhedouin:perf/constant-loads-four-dwords

Conversation

@romainhedouin

@romainhedouin romainhedouin commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Constant and root-view reads through raw views (heap and table constant buffers, root SRVs, title memory) issued one raw buffer load per dword. They now load up to four consecutive dwords at once (mask (1 << n) - 1, 4-byte alignment), as buffer() already does. Constants at a computed root-CBV offset load each row they touch once, instead of a row and three selects per dword.

static, from the emitted DXIL main this PR
s_buffer_load_dwordx16 through a table 16 loads, 98 instructions 4, 73
global_load_dwordx4 through a root view 4 loads, 39 instructions 1, 32
x16 at a computed root-CBV offset 16 rows, 271 instructions 5, 117
s_load_dwordx16 from title memory 16 data loads 4, plus 16 one-dword loads behind a branch taken only by runs that wrap

A load that runs partly past its view relies on D3D12 bounding raw loads per component, as the existing multi-dword buffer() loads do. In title memory, a run that starts before its copy (on the copy's first page) has an offset just under 2^32 that wraps into the copy between dwords. Lanes whose run wraps read it a dword at a time, as on main.

Tested: native victory-shader suite (207 passed), Windows clippy with and without --all-features, xtask policy. On a 68,064-program synthetic corpus, the 25,897 accepted programs that don't reach the changed loads emit byte-identical DXIL to main, and no program went between accepted and refused. The GPU tests constants_read_four_dwords_a_load_read_zeros_past_the_view and runs_from_just_before_a_copy_read_its_first_dwords haven't run on D3DMetal yet.

🤖 Generated with Claude Code

@brainbudt

Copy link
Copy Markdown
Contributor

Note

🤖 GPT-6.2 Sol responding on behalf of brainbudt

Two findings from reviewing this PR and its CI failure:

  1. Partial-copy wraparound risk in wide raw loads (crates/victory-shader/src/emit/lower.rs:5461): For a title-memory read starting 4 bytes before a partial copy (crates/victory-formats/src/pagetable.rs:83), the page table can select that copy with byte offset 0xffff_fffc. The previous second scalar load added 4 in i32 (wrapping to offset 0 and reading the valid dword), whereas a wide load reads consecutive components from the starting byte address, leaving both out of bounds under D3D raw-load addressing rules. Retaining scalar loads for chunks whose offsets can wrap (plus a test covering the start of a partial copy alongside the end-of-view test) would avoid dropping the first valid dword.
  2. Unrelated CI failure (descriptor/host.rs:2536): The failing check job hit a_slot_rewritten_while_its_view_is_prepared_views_the_new_record, which uses a process-wide one-shot test hook (crates/victory-gpu/src/descriptor/host.rs:2433) that can be consumed by another concurrent test's preparation. That looks unrelated to this PR's shader changes.

@pragyaangaur

Copy link
Copy Markdown
Owner

Thank you. CI is green, but this now conflicts with #368 and #371 (both just merged) in bitcode.rs, lower.rs and lower/tests.rs, in the same load and page table code. They overlap in more than a line or two, so I would rather not merge them by hand. Could you rebase on main and re-run your identical-DXIL comparison? I will merge it as soon as CI passes.

Scalar and per-lane constant reads through raw views (heap and table
constant buffers, root SRVs, title memory) issued one raw buffer load per
dword; they now load up to four consecutive dwords at once (mask
(1 << n) - 1, 4-byte alignment), as D3D12 bounds raw loads per component.
Constants at a computed offset of a root CBV load each row the run touches
once, instead of a row and three selects per dword.

In title memory a run that starts before its copy (on the copy's first
page) has an offset just under 2^32 that wraps into the copy from one
dword to the next; lanes whose run wraps read it a dword at a time, as
before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@romainhedouin
romainhedouin force-pushed the perf/constant-loads-four-dwords branch from e20c8ae to d1b4717 Compare November 26, 2026 18:28
@romainhedouin

Copy link
Copy Markdown
Contributor Author

Rebased on bf99e72 (after #371 and #368). It's one commit, and it replaces the two "update branch" merges.

  • Wraparound (thanks, brainbudt): fixed. A title-memory run whose offset wraps past 2^32 (it starts before a copy that begins mid-page) now reads one dword at a time in a branch only those lanes take, as on main. Root SRVs read as title memory take the same path. New GPU test: runs_from_just_before_a_copy_read_its_first_dwords (scalar and global x4 from 4 bytes before a copy expect [0, c0, c1, c2]).
  • DXIL comparison on the rebase (68,064 synthetic programs, main bf99e72 against this branch):
programs
refused on both, same reason 41,929
accepted, don't reach the changed loads: byte-identical 25,897 / 25,897
accepted, reach them: changed / unchanged 159 / 79
moved between accepted and refused 0

The per-pattern counts in the description were re-measured on the rebase. The non-title-memory rows match the earlier numbers exactly.

  • Native victory-shader suite 207 passed, Windows clippy (both configurations) and xtask policy clean.

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.

3 participants