Shaders: accept loaded buffer descriptors rewritten with base intact (#815) #2388

Closed
github-bot wants to merge 1 commit from gh-pr/2388/head into gh-pr/2388/base
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 12:30 UTC · original
fuzeofficlal:fix/shader-rewritten-buffer-descriptors-815 → main · +540 −11 · 2 files · 1 commits · closed without merging

Summary

Allows non-format 4-dword V# buffer descriptors (Fields::Buffer and Fields::Smem) whose stride, record-count, or flag fields were rewritten in place via pure SCC-free scalar ALU after s_load_* to resolve to Source::Loaded, provided the 48-bit virtual base address (w0 and w1[15:0]) is provably preserved (and w1 is untouched when idxen = true).

In the native compute pipeline measurements on main reported in #815 (#issuecomment-7160878577 world-run breakdown), "a descriptor changed after loading" is the single largest refusal reason (316 distinct compute programs), where the shader compiler converts structured buffer descriptors to raw views or adjusts record counts/flags with scalar ALU sequences outside made_raw's single 6-instruction FidelityFX pattern.

Soundness Guards

  • Non-format 4-dword buffer accesses only: Scoped strictly to Fields::Smem and Fields::Buffer with !inst.name().contains("format") && dwords == 4 (threaded through classify, phi, select, per_lane, lane_select, and lane_phi). 8-dword T# image descriptors, 4-dword S# samplers, and formatted tbuffer_*_format_* / buffer_*_format_* accesses remain rejected if modified.
  • 48-bit Base Address & Indexed Stride Preservation:
    • w0 (BASE_ADDRESS[31:0]) must be the unmodified Value inst.writes[first].1 of the s_load_*.
    • For unindexed accesses (Fields::Smem or Fields::Buffer { idxen: false, .. }), preserves_low16 proves w1 retains bits [15:0] (BASE_ADDRESS[47:32]) of inst.writes[first + 1].1 (e.g. s_and_b32 with mask & 0xFFFF == 0xFFFF, s_or_b32/s_xor_b32/s_andn2_b32 with low16_is_zero, s_bitset0/1_b32 on bit >= 16).
    • For stride-indexed accesses (Fields::Buffer { idxen: true, .. }), w1 must equal inst.writes[first + 1].1 unchanged so STRIDE (w1[29:16]) and SWIZZLE_ENABLE match the host view.
  • Pure Single-Descriptor Derivation: w1, w2, and w3 must derive via pure SCC-free scalar ALU exclusively from constants and inst.writes[first..first + 4] of that descriptor's load (rejecting cross-descriptor dwords from wider s_load_dwordx8/x16 loads or external SGPRs).

Verification

  • resource::tests::rewritten_loaded_buffer_descriptors_keep_base_and_reject_unsafe_edits: covers valid MUBUF, SMEM, idxen w2/w3, and phi-joined rewrites, and verifies refusals for modified w0, modified w1[15:0], modified w1 stride with idxen = true, tbuffer_load_format_x, cross-descriptor s_load_dwordx8 mixing, and rewritten 4-dword samplers.
  • emit::valu_tests::rewritten_loaded_buffer_descriptor_executes_on_gpu_and_warp: validates DXIL with dxil.dll and executes on both hardware GPU and WARP.
  • cargo clippy -p victory-shader --lib --tests --all-features --locked -- -D warnings & cargo xtask policy pass.

Refs #815

**@⁠fuzeofficlal** on GitHub · 2026-11-25 12:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2388) `fuzeofficlal:fix/shader-rewritten-buffer-descriptors-815` → `main` · +540 −11 · 2 files · 1 commits · closed without merging ### Summary Allows non-format 4-dword `V#` buffer descriptors (`Fields::Buffer` and `Fields::Smem`) whose stride, record-count, or flag fields were rewritten in place via pure SCC-free scalar ALU after `s_load_*` to resolve to `Source::Loaded`, provided the 48-bit virtual base address (`w0` and `w1[15:0]`) is provably preserved (and `w1` is untouched when `idxen = true`). In the native compute pipeline measurements on `main` reported in #815 (`#issuecomment-7160878577` world-run breakdown), **`"a descriptor changed after loading"` is the single largest refusal reason (316 distinct compute programs)**, where the shader compiler converts structured buffer descriptors to raw views or adjusts record counts/flags with scalar ALU sequences outside `made_raw`'s single 6-instruction FidelityFX pattern. ### Soundness Guards - **Non-format 4-dword buffer accesses only**: Scoped strictly to `Fields::Smem` and `Fields::Buffer` with `!inst.name().contains("format") && dwords == 4` (threaded through `classify`, `phi`, `select`, `per_lane`, `lane_select`, and `lane_phi`). 8-dword `T#` image descriptors, 4-dword `S#` samplers, and formatted `tbuffer_*_format_*` / `buffer_*_format_*` accesses remain rejected if modified. - **48-bit Base Address & Indexed Stride Preservation**: - `w0` (`BASE_ADDRESS[31:0]`) must be the unmodified `Value` `inst.writes[first].1` of the `s_load_*`. - For unindexed accesses (`Fields::Smem` or `Fields::Buffer { idxen: false, .. }`), `preserves_low16` proves `w1` retains bits `[15:0]` (`BASE_ADDRESS[47:32]`) of `inst.writes[first + 1].1` (e.g. `s_and_b32` with `mask & 0xFFFF == 0xFFFF`, `s_or_b32`/`s_xor_b32`/`s_andn2_b32` with `low16_is_zero`, `s_bitset0/1_b32` on bit `>= 16`). - For stride-indexed accesses (`Fields::Buffer { idxen: true, .. }`), `w1` must equal `inst.writes[first + 1].1` unchanged so `STRIDE` (`w1[29:16]`) and `SWIZZLE_ENABLE` match the host view. - **Pure Single-Descriptor Derivation**: `w1`, `w2`, and `w3` must derive via pure SCC-free scalar ALU exclusively from constants and `inst.writes[first..first + 4]` of that descriptor's load (rejecting cross-descriptor dwords from wider `s_load_dwordx8`/`x16` loads or external SGPRs). ### Verification - `resource::tests::rewritten_loaded_buffer_descriptors_keep_base_and_reject_unsafe_edits`: covers valid MUBUF, SMEM, `idxen` `w2`/`w3`, and `phi`-joined rewrites, and verifies refusals for modified `w0`, modified `w1[15:0]`, modified `w1` stride with `idxen = true`, `tbuffer_load_format_x`, cross-descriptor `s_load_dwordx8` mixing, and rewritten 4-dword samplers. - `emit::valu_tests::rewritten_loaded_buffer_descriptor_executes_on_gpu_and_warp`: validates DXIL with `dxil.dll` and executes on both hardware GPU and `WARP`. - `cargo clippy -p victory-shader --lib --tests --all-features --locked -- -D warnings` & `cargo xtask policy` pass. Refs #815
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:04:21 +00:00
github-bot closed this pull request 2026-11-26 05:04:21 +00:00
Author
Owner

@⁠welisonzika14-jpg on GitHub · 2026-11-25 13:08 UTC · original

First run on NVIDIA hardware here, as far as I can see. RTX 5070 (Blackwell, driver 621.24), Ryzen 7 5700X, Windows 11, Rust 1.98.1 MSVC, no game files. Head a276e8b merged onto main 940d32f, cargo test -p victory-gpu -p victory-shader --lib --no-fail-fast.

The PR's new test(s) pass on this card: emit::valu_tests::rewritten_loaded_buffer_descriptor_executes_on_gpu_and_warp, resource::tests::rewritten_loaded_buffer_descriptors_keep_base_and_reject_unsafe_edits. The rest is the same as main on this card: the 5 known NVIDIA failures from #1744 (2 view heap cap tests in victory-gpu, 3 in victory-shader), nothing new.

One run, unit tests only, title not run.

Written with AI help (Claude/Kiro); the numbers are from runs on my machine.

**@⁠welisonzika14-jpg** on GitHub · 2026-11-25 13:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2388#issuecomment-7163339572) First run on NVIDIA hardware here, as far as I can see. RTX 5070 (Blackwell, driver 621.24), Ryzen 7 5700X, Windows 11, Rust 1.98.1 MSVC, no game files. Head `a276e8b` merged onto `main` `940d32f`, `cargo test -p victory-gpu -p victory-shader --lib --no-fail-fast`. The PR's new test(s) pass on this card: `emit::valu_tests::rewritten_loaded_buffer_descriptor_executes_on_gpu_and_warp`, `resource::tests::rewritten_loaded_buffer_descriptors_keep_base_and_reject_unsafe_edits`. The rest is the same as `main` on this card: the 5 known NVIDIA failures from #1744 (2 view heap cap tests in `victory-gpu`, 3 in `victory-shader`), nothing new. One run, unit tests only, title not run. Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 15:56 UTC · original

Thanks @⁠RomainKH — great catch on the MUBUF addressing bits! Rebased onto da82e444 and updated in 4e642d2e:

  • Generalized preserves_low16 to preserves_bits(mask) / bits_zero(mask).
  • For Mubuf { idxen: false }, w1 now requires 0x8000_FFFF (SWIZZLE_ENABLE bit 31 plus base bits 15:0) and w3 requires 0x00E0_0000 (ADD_TID_ENABLE bit 23 and INDEX_STRIDE bits 22:21) unchanged from the load; Mubuf { idxen: true } keeps all of w1 unchanged plus 0x00E0_0000 on w3; Smem keeps 0x0000_FFFF on w1.
  • Added a // shortcut: note on w2 (NUM_RECORDS) and included rewrites_that_change_addressing_are_refused in resource::tests (with Co-authored-by credit).
**@⁠fuzeofficlal** on GitHub · 2026-11-25 15:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2388#issuecomment-7167442367) Thanks @⁠RomainKH — great catch on the MUBUF addressing bits! Rebased onto `da82e444` and updated in `4e642d2e`: - Generalized `preserves_low16` to `preserves_bits(mask)` / `bits_zero(mask)`. - For `Mubuf { idxen: false }`, `w1` now requires `0x8000_FFFF` (`SWIZZLE_ENABLE` bit 31 plus base bits `15:0`) and `w3` requires `0x00E0_0000` (`ADD_TID_ENABLE` bit 23 and `INDEX_STRIDE` bits `22:21`) unchanged from the load; `Mubuf { idxen: true }` keeps all of `w1` unchanged plus `0x00E0_0000` on `w3`; `Smem` keeps `0x0000_FFFF` on `w1`. - Added a `// shortcut:` note on `w2` (`NUM_RECORDS`) and included `rewrites_that_change_addressing_are_refused` in `resource::tests` (with `Co-authored-by` credit).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:03 UTC · original · edited 2026-11-25 18:45 UTC

Automated triage for the maintainers.

Needs work. Tracer::rewritten_buffer in crates/victory-shader/src/resource.rs lets a loaded 4-dword non-format buffer descriptor resolve to Source::Loaded after scalar-ALU rewrites of w1 to w3. The conditions are that the base (w0, w1[15:0]) and the addressing bits (SWIZZLE_ENABLE, ADD_TID_ENABLE/INDEX_STRIDE) are provably kept (preserves_bits/bits_zero), and that every word comes only from that load's four dwords (derived_from_load). The rebase onto 67e8ae1 (d135178) has dropped the unrelated rustfmt hunks, so the diff now shows only the change and its tests. CI (check): passing.

What still blocks it is evidence for the semantics. By the PR's own // shortcut: note, a rewritten NUM_RECORDS (w2) is ignored: the host view keeps the bound of the descriptor in memory. For unindexed access, a rewritten stride is ignored too. That is safe only if the title's rewrites never shrink the range or rely on the new bound. Otherwise reads the console would clamp to zero go past the intended range on the host.

On your reply: understood that you have no shader corpus. Either of these would move it forward:

  • A tester with the #815 shader set posts an offline translate --validate count before and after this PR, with which rewrite patterns occur (opcode sequences or counts only, no shader data).
  • Or narrow the change so a rewritten w2, or a rewritten stride on unindexed access, is still refused. Then only the rewrites whose effect the host view really reproduces are accepted, and the shortcut goes away.

Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 16:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/2388#issuecomment-7167550725) · edited 2026-11-25 18:45 UTC <!-- victory-triage v1 head=d135178f50db81912af923125bcef6d698e34027 verdict=needs-work at=2026-11-25T18:44:57Z --> Automated triage for the maintainers. **Needs work.** `Tracer::rewritten_buffer` in `crates/victory-shader/src/resource.rs` lets a loaded 4-dword non-format buffer descriptor resolve to `Source::Loaded` after scalar-ALU rewrites of `w1` to `w3`. The conditions are that the base (`w0`, `w1[15:0]`) and the addressing bits (`SWIZZLE_ENABLE`, `ADD_TID_ENABLE`/`INDEX_STRIDE`) are provably kept (`preserves_bits`/`bits_zero`), and that every word comes only from that load's four dwords (`derived_from_load`). The rebase onto `67e8ae1` (`d135178`) has dropped the unrelated rustfmt hunks, so the diff now shows only the change and its tests. CI (`check`): passing. What still blocks it is evidence for the semantics. By the PR's own `// shortcut:` note, a rewritten `NUM_RECORDS` (`w2`) is ignored: the host view keeps the bound of the descriptor in memory. For unindexed access, a rewritten stride is ignored too. That is safe only if the title's rewrites never shrink the range or rely on the new bound. Otherwise reads the console would clamp to zero go past the intended range on the host. On your reply: understood that you have no shader corpus. Either of these would move it forward: - A tester with the #815 shader set posts an offline `translate --validate` count before and after this PR, with which rewrite patterns occur (opcode sequences or counts only, no shader data). - Or narrow the change so a rewritten `w2`, or a rewritten stride on unindexed access, is still refused. Then only the rewrites whose effect the host view really reproduces are accepted, and the shortcut goes away. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 16:51 UTC · original

Rebased onto 67e8ae14 (d135178f) and stripped the unrelated rustfmt hunks from crates/victory-shader/src/resource.rs (track, phi, etc.) and crates/victory-shader/src/emit/valu_tests.rs so the diff contains only the descriptor rewrite change and its tests.

Regarding the offline translate --validate count and opcode breakdown on the 316 programs from #815: I do not have the title shader packs locally (all testing here uses synthetic shaders only), so I cannot run that corpus measurement myself — if a tester with the #815 shader set can run before/after translate --validate and share the rewrite counts/patterns, I am happy to tighten or adjust the guard accordingly.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 16:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2388#issuecomment-7168638484) Rebased onto `67e8ae14` (`d135178f`) and stripped the unrelated `rustfmt` hunks from `crates/victory-shader/src/resource.rs` (`track`, `phi`, etc.) and `crates/victory-shader/src/emit/valu_tests.rs` so the diff contains only the descriptor rewrite change and its tests. Regarding the offline `translate --validate` count and opcode breakdown on the 316 programs from #815: I do not have the title shader packs locally (all testing here uses synthetic shaders only), so I cannot run that corpus measurement myself — if a tester with the #815 shader set can run before/after `translate --validate` and share the rewrite counts/patterns, I am happy to tighten or adjust the guard accordingly.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 03:06 UTC · original

Thanks for the detailed review. I agree that keeping the 48-bit base intact is not sufficient when lowering through the original loaded host view: changes to NUM_RECORDS (w2), STRIDE (w1[29:16]), or OOB_SELECT (w3[29:28]) can alter guest bounds or addressing semantics relative to the unmodified host descriptor. Since all of the positive rewrite cases in this PR rely on those fields and there is no demonstrated hint-only shader case here, I'm closing this PR and will revisit if we have a specific title rewrite with proven bounds equivalence or explicit lowered bounds checks.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 03:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/2388#issuecomment-7181394798) Thanks for the detailed review. I agree that keeping the 48-bit base intact is not sufficient when lowering through the original loaded host view: changes to `NUM_RECORDS` (`w2`), `STRIDE` (`w1[29:16]`), or `OOB_SELECT` (`w3[29:28]`) can alter guest bounds or addressing semantics relative to the unmodified host descriptor. Since all of the positive rewrite cases in this PR rely on those fields and there is no demonstrated hint-only shader case here, I'm closing this PR and will revisit if we have a specific title rewrite with proven bounds equivalence or explicit lowered bounds checks.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2388
No description provided.