Shader: a load through a built CODE descriptor starts the constant table at the byte it reads #3153

Open
pablogonz12 wants to merge 1 commit from upstream-pr/3155 into main
pablogonz12 commented 2026-11-26 10:27:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pablogonz12 on 2026-11-26T10:27:34Z.


What

code_table_start picks the first byte of the program's constant table from the tracked CODE addresses: the scalar s_load_* through s_getpc_b64, and every built descriptor's base.

A buffer load through a built CODE descriptor (s_buffer_load_dword on a descriptor the code builds around s_getpc_b64) reads its base plus the load's own byte offset, and that offset can be negative (s_sub_u32 against s_getpc_b64), so the byte the load reads can lie below the base. Only the base was recorded, the table started there, and the load's index was then rebased against a start the load never reaches: it read a dword of the table shifted by the difference, or past its end.

Now resource::track records the effective byte of every fixed s_buffer_* load whose descriptor is built around the code (Resources::code_reads: base plus the load's offset, wrapping_add, so a load below the base wraps low as the instruction does), and code_dword lets those bytes take part in choosing the table's start. Loads whose offset is not a constant, and descriptors that are not built around the code, are untouched.

Why

This is a correctness fix, not a size or speed one: the wrong dword ends up in the emitted module wherever the code does this, and the console would have read the right one.

How tested

  • New test lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads: a program whose instructions end at 0xa0, a descriptor built around s_getpc_b64 with base 0x80, a buffer load reading byte 0x40 through it. It asserts the table starts at 0x40. Without the change it fails ("the table starts at the load's byte": start 0x80), so the case is real and not accepted silently.
  • cargo xwin test -p victory-shader --lib (x86_64-pc-windows-msvc, under Wine on Linux): 215 passed, 88 failed, 3 ignored. The 88 are the tests that need an SM 6.6 D3D12 device ("no SM 6.6 device"); main on the same host gives 214 passed, 88 failed, 3 ignored - same 88, one more pass from the new test.
  • cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings: clean.
  • cargo xwin check -p victory-shader --all-targets --target x86_64-pc-windows-msvc: clean.

The translated DXIL is unchanged for every program that has no fixed load through a built CODE descriptor: the address list code_table_start sees only gains entries in that case, and code_reads is empty otherwise. This tree has no offline dump example, so I did not put a per-program DXIL count next to it.

Other hardware

The change is in the shader translator, ahead of any adapter: it adds tracked addresses and can only move the table's start down to a byte a load actually reads, which is what the console reads. Nothing is gated on vendor or wave width, and the emitted module's layout is unchanged (same code constant array and indices; only its first byte can differ).

  • x86_64-pc-windows-msvc build and tests as above; not tested on NVIDIA hardware, and not run in a game.

Written with an AI assistant.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5508288026 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3155). Originally posted by [pablogonz12](https://github.com/pablogonz12) on 2026-11-26T10:27:34Z. --- ## What `code_table_start` picks the first byte of the program's constant table from the tracked `CODE` addresses: the scalar `s_load_*` through `s_getpc_b64`, and every *built* descriptor's base. A buffer load through a built `CODE` descriptor (`s_buffer_load_dword` on a descriptor the code builds around `s_getpc_b64`) reads its base **plus the load's own byte offset**, and that offset can be negative (`s_sub_u32` against `s_getpc_b64`), so the byte the load reads can lie below the base. Only the base was recorded, the table started there, and the load's index was then rebased against a start the load never reaches: it read a dword of the table shifted by the difference, or past its end. Now `resource::track` records the effective byte of every *fixed* `s_buffer_*` load whose descriptor is built around the code (`Resources::code_reads`: base plus the load's offset, `wrapping_add`, so a load below the base wraps low as the instruction does), and `code_dword` lets those bytes take part in choosing the table's start. Loads whose offset is not a constant, and descriptors that are not built around the code, are untouched. ## Why This is a correctness fix, not a size or speed one: the wrong dword ends up in the emitted module wherever the code does this, and the console would have read the right one. ## How tested - New test `lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads`: a program whose instructions end at `0xa0`, a descriptor built around `s_getpc_b64` with base `0x80`, a buffer load reading byte `0x40` through it. It asserts the table starts at `0x40`. **Without the change it fails** ("the table starts at the load's byte": start `0x80`), so the case is real and not accepted silently. - `cargo xwin test -p victory-shader --lib` (x86_64-pc-windows-msvc, under Wine on Linux): **215 passed, 88 failed, 3 ignored**. The 88 are the tests that need an SM 6.6 D3D12 device ("no SM 6.6 device"); `main` on the same host gives **214 passed, 88 failed, 3 ignored** - same 88, one more pass from the new test. - `cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings`: clean. - `cargo xwin check -p victory-shader --all-targets --target x86_64-pc-windows-msvc`: clean. The translated DXIL is unchanged for every program that has no fixed load through a built `CODE` descriptor: the address list `code_table_start` sees only gains entries in that case, and `code_reads` is empty otherwise. This tree has no offline dump example, so I did not put a per-program DXIL count next to it. ## Other hardware The change is in the shader translator, ahead of any adapter: it adds tracked addresses and can only move the table's start **down** to a byte a load actually reads, which is what the console reads. Nothing is gated on vendor or wave width, and the emitted module's layout is unchanged (same `code` constant array and indices; only its first byte can differ). - `x86_64-pc-windows-msvc` build and tests as above; not tested on NVIDIA hardware, and not run in a game. Written with an AI assistant.
cloph-dsp commented 2026-11-26 11:04:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T10:40:09Z.


Independent local verification of head e86940c on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-shader --lib emit::lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

I used a worktree-local CARGO_TARGET_DIR because the shared target dir served a stale test binary from a different worktree of the same crate. Synthetic shader inputs only; no game assets, extracted shaders or DRM material used. AI-assisted (Hy4 flash).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7189672707 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3155#issuecomment-7189672707). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T10:40:09Z. --- Independent local verification of head `e86940c` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-shader --lib emit::lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads` — 1 passed, 0 failed. - `git diff --check origin/main HEAD` — clean. I used a worktree-local `CARGO_TARGET_DIR` because the shared target dir served a stale test binary from a different worktree of the same crate. Synthetic shader inputs only; no game assets, extracted shaders or DRM material used. AI-assisted (Hy4 flash).
Dukestar69 commented 2026-11-26 11:11:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T11:11:16Z.

Review: APPROVED


Verified against origin/main (63bcca4):

  • cargo test -p victory-shader --lib emit::lower::tests:: -- --test-threads=1: 34/34 passed (including code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

Cleanly starts the constant table at the lowest byte read through a built CODE descriptor.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6073114222 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3155#pullrequestreview-6073114222). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T11:11:16Z. **Review:** APPROVED --- Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib emit::lower::tests:: -- --test-threads=1`: 34/34 passed (including `code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 192 files ok. Cleanly starts the constant table at the lowest byte read through a built `CODE` descriptor. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
futurepro62 commented 2026-11-26 21:43:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T21:43:57Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head e86940c (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: the_code_table_starts_at_the_lowest_byte_a_load_reads.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7204006568 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3155#issuecomment-7204006568). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-26T21:43:57Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, cloph-dsp, since none of them names a GPU run. Tested exact head `e86940c` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `the_code_table_starts_at_the_lowest_byte_a_load_reads`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
fuzeofficlal commented 2026-11-26 22:20:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T22:20:25Z.

Review: APPROVED


Verified on e86940c2:

  • cargo xtask policy: 192 files ok; crates/victory-shader/src/emit/lower.rs size is 521,519 bytes.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • cargo test --locked -p victory-shader --lib -- --test-threads=1 resource::tests:: emit::lower:: emit::stride_tests::: 77 passed, 0 failed under VICTORY_WARP=1 (including emit::lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads); 75 passed, 2 failed under VICTORY_WARP=0 (only the two pre-existing a_heap_buffer_read_* tests at stride_tests.rs:300, 350).

Recording effective Source::Built CODE load addresses (address.offset.wrapping_add(*offset as u32)) into Resources::code_reads cleanly fixes constant-table truncation when an s_buffer_load_* uses a negative immediate offset below the descriptor base. Three minor non-blocking notes:

  1. resource.rs:252 only pushes code_reads when soffset is NULL, so a nonzero/constant-SGPR soffset still falls back to the descriptor base alone.
  2. In crates/victory-shader/src/emit/lower/tests/code_table.rs:20, 0x7E02_030C encodes v_mov_b32 v1, v12 rather than v_mov_b32 v1, s12 (0x7E02_020C), though the assertions on l.code_table still exercise the lowering fix.
  3. rustfmt --edition 2024 --check flags line 37 of code_table.rs (wrapping the assert_eq! call).

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6081076040 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3155#pullrequestreview-6081076040). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T22:20:25Z. **Review:** APPROVED --- Verified on `e86940c2`: - `cargo xtask policy`: `192 files ok`; `crates/victory-shader/src/emit/lower.rs` size is `521,519` bytes. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`): clean. - `cargo test --locked -p victory-shader --lib -- --test-threads=1 resource::tests:: emit::lower:: emit::stride_tests::`: **77 passed, 0 failed** under `VICTORY_WARP=1` (including `emit::lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads`); **75 passed, 2 failed** under `VICTORY_WARP=0` (only the two pre-existing `a_heap_buffer_read_*` tests at `stride_tests.rs:300, 350`). Recording effective `Source::Built` `CODE` load addresses (`address.offset.wrapping_add(*offset as u32)`) into `Resources::code_reads` cleanly fixes constant-table truncation when an `s_buffer_load_*` uses a negative immediate offset below the descriptor base. Three minor non-blocking notes: 1. `resource.rs:252` only pushes `code_reads` when `soffset` is `NULL`, so a nonzero/constant-SGPR `soffset` still falls back to the descriptor base alone. 2. In `crates/victory-shader/src/emit/lower/tests/code_table.rs:20`, `0x7E02_030C` encodes `v_mov_b32 v1, v12` rather than `v_mov_b32 v1, s12` (`0x7E02_020C`), though the assertions on `l.code_table` still exercise the lowering fix. 3. `rustfmt --edition 2024 --check` flags line 37 of `code_table.rs` (wrapping the `assert_eq!` call). LGTM.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3155:upstream-pr/3155'
git switch 'upstream-pr/3155'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3155'
git switch 'upstream-pr/3155'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3155'
git switch 'upstream-pr/3155'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3155'
git switch 'main'
git merge --squash 'upstream-pr/3155'
git switch 'main'
git merge --ff-only 'upstream-pr/3155'
git switch 'main'
git merge 'upstream-pr/3155'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!3153
No description provided.