Skip to content

Shaders: buffer loads of a table in the program's code - #2084

Open
rminzao wants to merge 3 commits into
Marvisak:mainfrom
rminzao:shaders-buffer-loads-through-code-tables
Open

rminzao wants to merge 3 commits into
Marvisak:mainfrom
rminzao:shaders-buffer-loads-through-code-tables

Conversation

@rminzao

@rminzao rminzao commented Nov 25, 2026

Copy link
Copy Markdown

Claimed in #1633 (claim).

Problem

A buffer_load_* through a V# whose base is built around s_getpc_b64 (a constant table after the program's code) is refused: a descriptor built around a table in the code is not supported. #815 lists 20 upscaler compute programs failing on it. Scalar loads (s_buffer_load_dword*) and global_load_* of the same kind of table already read it through code_dword; only the VMEM buffer form falls through to handle().

Change

In Lower::buffer, a load whose source is a Source::Built descriptor with address.param == CODE reads its dwords with code_dword, at base + offset + offen VGPR + soffset + (idxen ? index × stride : 0), and extracts bytes/halfwords as flat() does. Out of range, it reads 0, per the V#'s OOB_SELECT (dword 3 bits 28-29):

  • 1: index >= num_records
  • 2: num_records == 0
  • 3: offset + soffset + size > num_records, each dword of a multi-dword load checked on its own; index * stride moves the address but is not checked

These are the rules measured on RDNA2 (gfx1036) and RDNA1 (gfx1011) hardware by the Leonida project (its docs/buffer_bounds.md, 732 cases; they match Mesa's GFX11 column of ac_descriptors.c, with soffset counted), which this tree already cites for other measurements. code_dword alone clamps to the table, which is not what the hardware returns.

Still refused, each with its own reason: stores, atomics and typed loads of a code table; swizzled V#s or ADD_TID_ENABLE; OOB_SELECT 0 (whether the instruction offset counts in its stride check is unmeasured).

Tested

Intel UHD Graphics (driver 31.0.101.4146), RTX 4050 Laptop (595.97) and WARP, Windows 11.

  • emit::memory_tests::code_table_buffer_loads_lower: the hand-assembled program decodes to the expected text, lowers and validates; an OOB_SELECT 0 V# is refused. On main 940d32f this test fails with the refusal above.
  • emit::memory_tests::code_table_buffer_loads_read_zeros_out_of_range: 32 lanes read a 64-dword table after s_endpgm, compared per lane with a CPU model: buffer_load_dword offen (lanes 29+ read 0), buffer_load_ubyte offen offset:1 (lanes 30+, so the size counts), buffer_load_dwordx2 with soffset 4 (lane 27 gets one dword and a 0), and buffer_load_dword idxen offset:4 with stride 8 and 21 records (lanes 21+). Passes on all three adapters (the RTX 4050 through a local, uncommitted switch of the test host to the high-performance adapter). With the zeroing turned off it fails.
  • cargo test -p victory-shader --lib on the Intel UHD: 240 passed, 9 failed; main gives 238 passed and the same 9 failures (all covered by other open PRs; see Test report: Intel UHD (Raptor Lake Xe-LP), 70564ae: victory-shader 201 passed, 8 failed (3 missing-feature skips, 5 different results) #1829).
  • cargo clippy -p victory-shader --all-targets -- -D warnings, with and without --all-features: clean. rustfmt --check: no diffs in the new hunks. cargo xtask policy: ok.

Not run: the title or the #815 programs themselves, and AMD hardware. The console part (gfx1013) is unmeasured; the rules hold on gfx1011 and gfx1036.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib emit::memory_tests:: — 5 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1 (including the new code-table buffer load tests)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean lowering for buffer loads reading from a constant table embedded in the program's code, verified end-to-end on hardware and WARP. Looks good.

AI-assisted (Gemini 4 Neon).

@jeremygold02

Copy link
Copy Markdown
Contributor

Triage verdict — Keep the VMEM code-table path, cover unaligned subword reads (0077404)

Evidence

  • @rminzao fills a gap separate from the scalar and global code-table paths already on main. Untyped VMEM loads can now read through the same code-table helper. The submitted fixtures cover raw and structured bounds, including a partially out-of-bounds multi-dword read. New lowering · Bounds fixture
  • The subword arm reads one aligned dword and shifts it. The byte fixture exercises an in-word offset. It does not establish what happens for a halfword starting at the last byte of a dword, where this implementation cannot read the following byte. Subword path

Checks and limits

  • Compared the VMEM, scalar, and global paths and the synthetic fixtures. The existing review reports successful hardware and WARP execution with DXIL validation. I did not independently dispatch this head or verify the reported affected corpus.

Next step

  • Keep the focused VMEM support. Check the halfword boundary against the intended console semantics and either handle it explicitly or refuse it until supported. Keep the existing raw and structured out-of-bounds controls.

This verification was AI-assisted.

@catminou4 catminou4 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.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. Buffer loads of a table in the program's code — program-code table loads; relates to #2068's table-constant SRV fallback.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 0077404 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. 36d0a30 is rebased and merges cleanly. In Lower::buffer (crates/victory-shader/src/emit/lower.rs), an untyped load through a Source::Built descriptor at CODE now goes to code_table_buffer_load (new emit/lower/code_table.rs), which reads the dwords with the existing code_dword and applies OOB_SELECT 1/2/3 as the PR body describes (the offset checked for mode 3 already includes offset, offen and soffset, as built earlier in buffer). Stores, atomics, typed loads, swizzled/ADD_TID descriptors and OOB_SELECT 0 stay refused with their own reasons. The halfword at byte 3 now takes its high byte from the next dword; I checked the shift ((next << 1) << (31 - sh) is 0 at sh = 0 and next << 8 at sh = 24), and the fixture covers it. The bounds rules cite hardware measurements and Mesa. CI check passes, including the policy check now that the code moved out of lower.rs.

On your replies: the boundary fix and the file-size correction match the diff; thanks for the follow-up.


Generated by Claude Code

rminzao and others added 2 commits November 25, 2026 22:29
A buffer load through a descriptor built around s_getpc_b64 was refused
("a descriptor built around a table in the code"), though scalar and
global loads of the same tables already read them. Read the dwords from
the code too, at base + offsets + index * stride, and give 0 where the
descriptor's range check (OOB_SELECT 1-3) puts the access out of range,
per dword for multi-dword loads. Stores, atomics, typed loads, swizzled
descriptors and OOB_SELECT 0 stay refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… next dword

A halfword load at the last byte of a dword took the shifted aligned
dword alone, so its high byte came out 0. It now ORs in the next
dword's low byte. The fixture adds a signed halfword at byte 3, which
fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rminzao
rminzao force-pushed the shaders-buffer-loads-through-code-tables branch from 0077404 to b6d6be6 Compare November 26, 2026 01:34
@rminzao

rminzao commented Nov 26, 2026

Copy link
Copy Markdown
Author

Rebased onto main (63bcca4) and pushed b6d6be6, which handles the halfword boundary @jeremygold02 raised.

A halfword at byte 3 of a dword now reads both bytes it spans: its high byte comes from the next dword. It ORs in (next << 1) << (31 - shift), which is next << (32 - shift) and 0 at shift 0, with no shift by 32. A byte can't cross, so it is unchanged. The bounds check was already per field (offset + 2 <= records), so a halfword whose second byte is past the records reads 0.

The fixture adds buffer_load_sshort v8, v1, s[12:15], 0 offen offset:3 to every lane. That is a signed halfword across the dword boundary, and lane 29 is past RAW_RECORDS. code_table_buffer_loads_read_zeros_out_of_range matches the model on the Intel UHD, the RTX 4050 Laptop and WARP. With the new arm disabled it fails.

Also dropped the comment's attribution of the OOB_SELECT values. The memory_tests.rs conflict was only the two branches' new tests both appended at the end.

cargo test -p victory-shader --lib on the Intel UHD: 293 passed, 11 failed, the same 11 as main. Clippy and cargo xtask policy are clean.

AI-assisted (Claude Code).

With current main, lower.rs went past the 512 KiB file limit
`cargo xtask policy` enforces. The code-table load moves unchanged into
its own module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rminzao

rminzao commented Nov 26, 2026

Copy link
Copy Markdown
Author

Correction to the comment above: cargo xtask policy was not clean. On current main this PR put lower.rs at 525,127 bytes, past the 512 KiB limit, and CI failed on repository_passes_policy. 36d0a30 moves the code-table load unchanged to emit/lower/code_table.rs, so lower.rs is now 521,884 bytes. Policy, the xtask tests and clippy are clean, and the code-table tests pass on the Intel UHD and WARP.

AI-assisted (Claude Code).

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…the program's code

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/memory_tests.rs
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…the program's code

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/memory_tests.rs

@fuzeofficlal fuzeofficlal 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 36d0a303dd670928f9c82e09136a92bbc32fbd36 (merged cleanly onto 63bcca43) and verified on Windows 11 with an RTX 5090, WARP, dxil.dll, and VICTORY_D3D12_DEBUG=1.

Moving the VMEM code-table load helper into emit/lower/code_table.rs keeps emit/lower.rs growth small (521,884 B, 2,404 B under the 512 KiB cap), cleanly separates descriptor base (base + extra) from instruction offsets (off + index * stride), and handles unaligned split 16-bit halfword shifts ((next << 1) << (31 - sh)) without shifting by 32.

Non-blocking follow-up notes:

  • In OOB_SELECT = 3, lower.rs:9438-9463 folds *offset, OFFEN, and SOFFSET with 32-bit Bin::Add before code_table_buffer_load, so a wrapping combination like VGPR offset 0xffff_fffc + SOFFSET = 4 arrives as 0 and bypasses the unsigned IULE check; preserving carry or checking components separately would close that wraparound edge case.
  • Adding tests for OOB_SELECT = 2 (empty and non-empty NUM_RECORDS), dynamic descriptor base, and an explicit error when a CODE leaf appears inside a Phi/Select would broaden coverage further.

Verification

  • xtask policy (192 files ok), cargo clippy (default & --all-features) clean, code_table.rs rustfmt-clean.
  • RTX 5090 + WARP + dxil.dll + VICTORY_D3D12_DEBUG=1: code_table_buffer_loads_lower and code_table_buffer_loads_read_zeros_out_of_range (2/2 passed), emit::census_tests (1/1 passed), and the remaining emit::memory_tests (6/6 passed, skipping the pre-existing main #2571 crash in heap_direct_texture_reads_mark_their_heap_index).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants