Shaders: an SGPR no instruction writes stays refused above the initialized ones #1261

Open
Proudnoob4 wants to merge 1 commit from upstream-pr/1261 into main
Proudnoob4 commented 2026-11-24 16:53:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Proudnoob4 on 2026-11-24T16:53:06Z.


Follow-up to #432, from @upipibi's check after the merge.

Problem

#432 lets a compute program's read of an SGPR above the ones the hardware initializes read zero. That rule exists for a read the paths only seem to allow, where the register is written on another path (the s37/s38 case in #432). But it also zeroes a register that no instruction writes. A read like that more likely means an input the translator does not know about, such as a system SGPR in a position we have wrong. Before #432 it was a refusal, and census tables counted it.

Fix

In Lower::inputs, a live-in SGPR above initialized_sgprs now reads zero only if some instruction in the program writes it. Otherwise it is refused as an SGPR no instruction writes, above the initialized ones. That keeps it apart from a hardware SGPR input in tables.

Measured on the title

I ran victory translate --list on main ffe310e over the title's shaders: shaders.rpf plus the executable, 26,503 containers and 26,084 programs, 2,582 of them compute. The comparison rows used a local-only switch in inputs(), which is not part of this PR.

Compute emitted (of 2,582) Programs whose outcome differs from main
main 1,441 n/a (baseline)
this PR (zero only if written somewhere) 1,441 0
refuse every such read (behaviour before #432) 1,441 4
  • I also logged every live-in SGPR read above the initialized ones, whether or not the value is used. 12 distinct programs (by code) have one:

    Register Initialized SGPRs Programs
    s37, s38 9 4 meshlet-culling programs
    s46 14 7 programs
    s83, s84 14 1 program

    Each of these registers is written elsewhere in its program; none is written nowhere. On this title the narrower rule changes no program and brings back the refusal for the case above.

  • The 4 programs in the last row are the meshlet_culling_CS_BatchCullMeshlets* variants from #432's description. On main they now get as far as image_gather4_l.

Tests

  • New an_sgpr_written_on_another_path_reads_zero: s20 is written only on the path a branch skips. The program translates, and it is validated when dxil.dll is present.
  • New an_sgpr_no_instruction_writes_is_refused. With main's lower.rs this test fails because the program translates. Forcing the zero arm to refuse makes the first test fail, so that test does reach the rule.
  • Both tests are CPU-only. try_build_valu_cs is build_valu_cs without the validation, and it returns the refusal instead of panicking.
  • cargo test -p victory-shader: 107 passed. The same 15 GPU tests fail on unchanged main on this machine (Windows 10 without an SM 6.6 test device: neither hardware nor WARP SM 6.6 device available; see #84).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean. cargo xtask policy: ok. rustfmt: clean for the changed lines.

Interplay with #1176

#1176 (open) gives pixel programs an initialized_sgprs too, so with both merged this narrowing also applies to pixel shaders. The two touch different lines (#1176: io.rs/native.rs; this PR: lower.rs/valu_tests.rs). I have not measured the pixel side yet. I'll run the corpus on main + #1176 with and without this change and post the counts here.

Counts and hashes only; no game data attached. AI-assisted (Claude Code); I ran the measurements and tests on my machine.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586969335 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1261). Originally posted by [Proudnoob4](https://github.com/Proudnoob4) on 2026-11-24T16:53:06Z. --- Follow-up to #432, from @upipibi's [check after the merge](https://github.com/Marvisak/VIctory/pull/432#issuecomment-7128622386). ### Problem #432 lets a compute program's read of an SGPR above the ones the hardware initializes read zero. That rule exists for a read the paths only seem to allow, where the register is written on another path (the `s37`/`s38` case in #432). But it also zeroes a register that **no instruction writes**. A read like that more likely means an input the translator does not know about, such as a system SGPR in a position we have wrong. Before #432 it was a refusal, and census tables counted it. ### Fix In `Lower::inputs`, a live-in SGPR above `initialized_sgprs` now reads zero only if some instruction in the program writes it. Otherwise it is refused as `an SGPR no instruction writes, above the initialized ones`. That keeps it apart from `a hardware SGPR input` in tables. ### Measured on the title I ran `victory translate --list` on `main` `ffe310e` over the title's shaders: `shaders.rpf` plus the executable, 26,503 containers and 26,084 programs, 2,582 of them compute. The comparison rows used a local-only switch in `inputs()`, which is not part of this PR. | | Compute emitted (of 2,582) | Programs whose outcome differs from `main` | |---|---:|---:| | `main` | 1,441 | n/a (baseline) | | this PR (zero only if written somewhere) | 1,441 | **0** | | refuse every such read (behaviour before #432) | 1,441 | 4 | - I also logged every live-in SGPR read above the initialized ones, whether or not the value is used. 12 distinct programs (by code) have one: | Register | Initialized SGPRs | Programs | |---|---:|---| | `s37`, `s38` | 9 | 4 meshlet-culling programs | | `s46` | 14 | 7 programs | | `s83`, `s84` | 14 | 1 program | **Each of these registers is written elsewhere in its program; none is written nowhere.** On this title the narrower rule changes no program and brings back the refusal for the case above. - The 4 programs in the last row are the `meshlet_culling_CS_BatchCullMeshlets*` variants from #432's description. On `main` they now get as far as `image_gather4_l`. ### Tests - New `an_sgpr_written_on_another_path_reads_zero`: `s20` is written only on the path a branch skips. The program translates, and it is validated when `dxil.dll` is present. - New `an_sgpr_no_instruction_writes_is_refused`. With `main`'s `lower.rs` this test fails because the program translates. Forcing the zero arm to refuse makes the first test fail, so that test does reach the rule. - Both tests are CPU-only. `try_build_valu_cs` is `build_valu_cs` without the validation, and it returns the refusal instead of panicking. - `cargo test -p victory-shader`: 107 passed. The same 15 GPU tests fail on unchanged `main` on this machine (Windows 10 without an SM 6.6 test device: `neither hardware nor WARP SM 6.6 device available`; see #84). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. `cargo xtask policy`: ok. rustfmt: clean for the changed lines. ### Interplay with #1176 #1176 (open) gives pixel programs an `initialized_sgprs` too, so with both merged this narrowing also applies to pixel shaders. The two touch different lines (#1176: `io.rs`/`native.rs`; this PR: `lower.rs`/`valu_tests.rs`). I have not measured the pixel side yet. I'll run the corpus on `main` + #1176 with and without this change and post the counts here. Counts and hashes only; no game data attached. AI-assisted (Claude Code); I ran the measurements and tests on my machine.
DenRakEiw commented 2026-11-24 17:04:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T16:56:51Z.

Review: APPROVED


Tested merged cleanly onto main (ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC):

  • cargo test -p victory-shader ir::: 6 passed, 0 failed
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean fix keeping unwritten SGPRs above the initialized set refused. Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6043698282 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1261#pullrequestreview-6043698282). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T16:56:51Z. **Review:** APPROVED --- Tested merged cleanly onto `main` (`ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC): - `cargo test -p victory-shader ir::`: **6 passed, 0 failed** - `cargo clippy -p victory-shader --all-targets`: **0 warnings** - `cargo xtask policy`: ok Clean fix keeping unwritten SGPRs above the initialized set refused. Approving. AI-assisted (Gemini 4 Neon).
pablogonz12 commented 2026-11-24 18:05:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pablogonz12 on 2026-11-24T17:16:19Z.


Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main ffe310e plus #1017 (main's host build fails without it: E0425 host_resource), then the four-stage offline check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped).

translate valid SPIR-V RADV pipeline
base 666 / 768 656 656
your head 666 / 768 656 656

No program gained or lost, and the failure table is identical to the base, reason for reason — nothing in this capture reaches the class you lower. A no-regression result only.

Not tested: in-game.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7138944164 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7138944164). Originally posted by [pablogonz12](https://github.com/pablogonz12) on 2026-11-24T17:16:19Z. --- Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main `ffe310e` plus #1017 (main's host build fails without it: `E0425 host_resource`), then the four-stage offline check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped). | | translate | valid SPIR-V | RADV pipeline | |---|---|---|---| | base | 666 / 768 | 656 | 656 | | your head | 666 / 768 | 656 | 656 | No program gained or lost, and the failure table is identical to the base, reason for reason — nothing in this capture reaches the class you lower. A no-regression result only. Not tested: in-game.
adamdotpy commented 2026-11-24 18:05:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T17:18:19Z.


Reviewed at head d493a13 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The distinction is correct and the implementation matches it. In inputs() (lower.rs:929), an SGPR read with no declared input and n in [initialized_sgprs, 105] used to unconditionally read 0, justified as a path artifact ("a branch past the write"). The PR adds the one check that separates the two cases: if no instruction in the whole program has the register in writes, a path artifact is impossible — there is nothing to be on a path — so it is an unknown input (a system SGPR in another position) and stays refused (Val::Missing("an SGPR no instruction writes, above the initialized ones")). If any instruction writes it, the old read-zero behavior is kept. written() scans i.writes over all blocks, which is exactly the right over-approximation: a write anywhere means a path could produce it. Borrow-wise it copies the self.x reference before the mutable loop, compiles clean.

2. Red/green proven here. With the PR's valu_tests.rs against main's lower.rs, an_sgpr_no_instruction_writes_is_refused fails (the read of an unwritten s20 translates instead of refusing — panic "translated"), while an_sgpr_written_on_another_path_reads_zero passes. On the PR head both pass: cargo test -p victory-shader --lib an_sgpr 2 passed / 0 failed. So the branch changes exactly the intended case and nothing else.

3. Full checks on d493a13: cargo test -p victory-shader --lib 122 passed / 0 failed / 1 ignored (22 s, GPU tests included); cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds; rustfmt --edition 2024 --check on both changed files shows only the same 3 pre-existing hunks main has in lower.rs (shifted by the +16 lines), valu_tests.rs fully clean — the PR adds no new fmt diffs.

4. One judgment call to be aware of (not a defect): this turns a silent read-of-stale-garbage into a refusal for programs that read an SGPR nothing writes. On real hardware an uninitialized SGPR holds the previous wave's value, not zero, so any shader that genuinely depended on that was already running on garbage; refusing surfaces it as a named VSF2 reason (the same diagnostic currency #1094 introduced) instead of a wrong frame. If a real title shader ever hits this reason, that itself is evidence of a decoding gap — which is the PR's point. The n <= 105 bound and the initialized_sgprs gate are unchanged, so hardware SGPRs above 105 still refuse as before.

5. Test hygiene is good: build_valu_cs is now split into try_build_valu_cs (returns the refusal) + the panicking wrapper that still runs dxil.dll validation when available, and the new refusal test asserts on the reason text. No game data, no GPU needed for the new tests.

Nothing found that would block this.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7138993719 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7138993719). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T17:18:19Z. --- Reviewed at head `d493a13` (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked: **1. The distinction is correct and the implementation matches it.** In `inputs()` (`lower.rs:929`), an SGPR read with no declared input and `n` in `[initialized_sgprs, 105]` used to unconditionally read 0, justified as a path artifact ("a branch past the write"). The PR adds the one check that separates the two cases: if *no instruction in the whole program* has the register in `writes`, a path artifact is impossible — there is nothing to be on a path — so it is an unknown input (a system SGPR in another position) and stays refused (`Val::Missing("an SGPR no instruction writes, above the initialized ones")`). If any instruction writes it, the old read-zero behavior is kept. `written()` scans `i.writes` over all blocks, which is exactly the right over-approximation: a write anywhere means a path could produce it. Borrow-wise it copies the `self.x` reference before the mutable loop, compiles clean. **2. Red/green proven here.** With the PR's `valu_tests.rs` against **main's** `lower.rs`, `an_sgpr_no_instruction_writes_is_refused` fails (the read of an unwritten `s20` translates instead of refusing — panic "translated"), while `an_sgpr_written_on_another_path_reads_zero` passes. On the PR head both pass: `cargo test -p victory-shader --lib an_sgpr` 2 passed / 0 failed. So the branch changes exactly the intended case and nothing else. **3. Full checks on `d493a13`:** `cargo test -p victory-shader --lib` 122 passed / 0 failed / 1 ignored (22 s, GPU tests included); `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` clean; `cargo build --release -p victory` builds; `rustfmt --edition 2024 --check` on both changed files shows only the same 3 pre-existing hunks main has in `lower.rs` (shifted by the +16 lines), `valu_tests.rs` fully clean — the PR adds no new fmt diffs. **4. One judgment call to be aware of (not a defect):** this turns a silent read-of-stale-garbage into a refusal for programs that read an SGPR nothing writes. On real hardware an uninitialized SGPR holds the previous wave's value, not zero, so any shader that genuinely depended on that was already running on garbage; refusing surfaces it as a named `VSF2` reason (the same diagnostic currency #1094 introduced) instead of a wrong frame. If a real title shader ever hits this reason, that itself is evidence of a decoding gap — which is the PR's point. The `n <= 105` bound and the `initialized_sgprs` gate are unchanged, so hardware SGPRs above 105 still refuse as before. **5. Test hygiene is good:** `build_valu_cs` is now split into `try_build_valu_cs` (returns the refusal) + the panicking wrapper that still runs `dxil.dll` validation when available, and the new refusal test asserts on the reason text. No game data, no GPU needed for the new tests. Nothing found that would block this.
acetonahard-code commented 2026-11-24 19:04:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by acetonahard-code on 2026-11-24T18:13:33Z.


Setup: Windows 11 (26200), RTX 4070 SUPER, driver 621.14, Rust 1.98.1 (repo toolchain). Base main at ffe310e, PR head d493a13.

main this PR
cargo test -p victory-shader 120 passed, 0 failed, 1 ignored 122 passed, 0 failed, 1 ignored
  • Both new tests pass: an_sgpr_no_instruction_writes_is_refused, an_sgpr_written_on_another_path_reads_zero.
  • I re-ran with --nocapture, and run_cases printed no "no SM 6.6 ... device; skipped" lines, so the GPU tests ran on the hardware adapter and on WARP. The only skips were the 4 "no console / PC compiler" ones.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 154 files ok.
  • cargo fmt --check -p victory-shader reports the same 7 diffs on main and on this PR. In lower.rs they're only shifted 16 lines by the new code. This PR adds none of them. They're older test code in lower.rs, mod.rs, resource.rs and structure.rs.

Code read: written matches on exact Reg. I checked that this still works for wide writes. ir::effects expands every destination into one Reg per dword (Inst::writes: "Every register dword written"), so an SGPR filled by s_mov_b64 or s_load_dwordx* counts as written. Only DROPPED instructions are left out of insts, and none of them write an SGPR. Looks correct to me.

I didn't test with game data, so I can't confirm the "0 program outcomes changed" count.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140338840 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7140338840). Originally posted by [acetonahard-code](https://github.com/acetonahard-code) on 2026-11-24T18:13:33Z. --- **Setup:** Windows 11 (26200), RTX 4070 SUPER, driver 621.14, Rust 1.98.1 (repo toolchain). Base `main` at ffe310e, PR head d493a13. | | `main` | this PR | |---|---|---| | `cargo test -p victory-shader` | 120 passed, 0 failed, 1 ignored | 122 passed, 0 failed, 1 ignored | - Both new tests pass: `an_sgpr_no_instruction_writes_is_refused`, `an_sgpr_written_on_another_path_reads_zero`. - I re-ran with `--nocapture`, and `run_cases` printed no "no SM 6.6 ... device; skipped" lines, so the GPU tests ran on the hardware adapter and on WARP. The only skips were the 4 "no console / PC compiler" ones. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 154 files ok. - `cargo fmt --check -p victory-shader` reports the same 7 diffs on `main` and on this PR. In `lower.rs` they're only shifted 16 lines by the new code. This PR adds none of them. They're older test code in `lower.rs`, `mod.rs`, `resource.rs` and `structure.rs`. **Code read:** `written` matches on exact `Reg`. I checked that this still works for wide writes. `ir::effects` expands every destination into one `Reg` per dword (`Inst::writes`: "Every register dword written"), so an SGPR filled by `s_mov_b64` or `s_load_dwordx*` counts as written. Only `DROPPED` instructions are left out of `insts`, and none of them write an SGPR. Looks correct to me. I didn't test with game data, so I can't confirm the "0 program outcomes changed" count.
Marvisak commented 2026-11-25 02:05:53 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149444964 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7149444964). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:20:20Z. --- <!-- victory-triage v1 head=d493a131df47dc8519d5a766a955698c362c0459 verdict=needs-rebase at=2026-11-25T01:20:19Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/valu_tests.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
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/1261:upstream-pr/1261'
git switch 'upstream-pr/1261'

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/1261'
git switch 'upstream-pr/1261'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1261'
git switch 'upstream-pr/1261'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1261'
git switch 'main'
git merge --squash 'upstream-pr/1261'
git switch 'main'
git merge --ff-only 'upstream-pr/1261'
git switch 'main'
git merge 'upstream-pr/1261'
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!1261
No description provided.