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

Open
github-bot wants to merge 1 commit from github-bot/gh-1261 into main AGit
Owner

@⁠Proudnoob4 on GitHub · 2026-11-24 16:53 UTC · original
Proudnoob4:fix/sgpr-zero-only-written → main · +62 −14 · 2 files · 1 commits · open

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.

**@⁠Proudnoob4** on GitHub · 2026-11-24 16:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/1261) `Proudnoob4:fix/sgpr-zero-only-written` → `main` · +62 −14 · 2 files · 1 commits · open Follow-up to #432, from @⁠upipibi's [check after the merge](https://git.gtahub.st/github/Marvisak--VIctory/pulls/432). ### 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.
Since #432 a compute program's read of an SGPR above the ones the hardware
initializes reads zero. That is meant for a read the paths only seem to
allow, of a register written on another path, but it also covered a
register no instruction writes at all. Such a read is more likely an input
the decoding does not know about, and before #432 it was a refusal that the
census counted. It is refused again, under its own reason.

Over the title's 2,582 compute programs, 12 distinct programs read an SGPR
above the initialized ones, and every one of them writes that register
elsewhere, so no program changes outcome.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:25:30 +00:00
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-24 17:16 UTC · original

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.

**@⁠pablogonz12** on GitHub · 2026-11-24 17:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7138944164) 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.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 17:18 UTC · original

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.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7138993719) 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.
Author
Owner

@⁠acetonahard-code on GitHub · 2026-11-24 18:13 UTC · original

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.

**@⁠acetonahard-code** on GitHub · 2026-11-24 18:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7140338840) **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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:20 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/1261#issuecomment-7149444964) <!-- 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 has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/valu_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1261/head:github-bot/gh-1261'
git switch 'github-bot/gh-1261'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1261'
git switch 'github-bot/gh-1261'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1261'
git switch 'github-bot/gh-1261'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1261'
git switch 'main'
git merge --squash 'github-bot/gh-1261'
git switch 'main'
git merge --ff-only 'github-bot/gh-1261'
git switch 'main'
git merge 'github-bot/gh-1261'
git push origin 'main'
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!1261
No description provided.