Shaders: refuse per-lane reads of mesh v5 in v5 writes and after joins #659

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

@⁠coderanonymus on GitHub · 2026-11-24 06:24 UTC · original
coderanonymus:mesh-group-input-guard → main · +96 −8 · 2 files · 1 commits · open

What

In a mesh program, v5 holds the group input. Only the first lane's value is right, so io::raise refuses any per-lane read of it other than v_readfirstlane_b32 ("the mesh group input read per lane").

The guard skipped every instruction that writes v5. That exemption exists because the IR adds a VGPR's old value to the reads of every write to it (lanes outside EXEC keep it). But reads is deduplicated, so the exemption also covered instructions that read v5 as a real operand. The guard also compared against the input value only, so a read of a phi that merges it was missed. On main, all of these mesh programs translate without an error:

  • v_add_nc_u32 v5, v5, v1
  • v_cvt_f32_u32 v5, v5
  • global_load_dword v5, v5, s[0:1]
  • v_swap_b32 v5, v1
  • s_cmp_eq_u32 s1, 0 / s_cbranch_scc1 / v_mov_b32 v5, 0 / then, after the join, v_mov_b32 v1, v5

Lowering (Input::MeshGroup) then gives every lane groupId * mesh_stride, the first lane's value. Each lane after lane 0 computes a wrong value, with no diagnostic.

Change:

  • ir.rs: build adds the old-value read of written VGPRs, instead of effects. The resulting reads are the same as before. The new ir::operand_reads returns what effects gives, so it covers VALU sources, v_swap_b32, memory addresses and data, and exports.
  • io.rs: the guard now tracks the group input plus every phi that may carry it (to a fixpoint). It does not follow a phi argument that arrives over the EXEC-zero edge of s_cbranch_execz (taken) or s_cbranch_execnz (not taken): no lane is active there, so it is the same case as a partial-EXEC write. It exempts a write to v5 only when v5 is not one of its operand reads.

Still not refused, noted as a shortcut: comment: writes that may keep the old v5 in active lanes (v_fmac_f32's accumulator, a 16-bit half or d16 load, v_writelane_b32, v_movreld_b32, DPP that leaves lanes unwritten) and writes to v5 under a partial EXEC. I chose not to add per-opcode or EXEC checks for these to keep the guard small. Refusing every write under a possibly partial EXEC would also refuse the normal case where v5 is reused after v_readfirstlane_b32, inside a divergent if.

Verification

  • Regression cases are added to io::tests::mesh_inputs_and_primitive_export: the five programs above must be refused. Two must still be accepted: v_mov_b32 v5, 0; v_mov_b32 v1, v5 (v5 overwritten, then read), and a wave64 program that reuses v5 after v_readfirstlane_b32 inside a divergent if (s_and_saveexec_b64 + s_cbranch_execz, v_mov_b32 v5, 0, s_or_b64 exec) and reads it under the same mask in a second if. Main accepts that one too. Without the EXEC-zero edge rule it is refused at offset 32. The words were checked with llvm-mc -mcpu=gfx1030.
  • Before the fix (main d2a4bae plus the test): CARGO_TARGET_DIR=/home/eric/src/VIctory/target cargo test -p victory-shader --lib mesh_inputs_and_primitive_export fails with accepted: [[4A0A0305, BF810000], [7E0A0D05, BF810000], [DC308000, 05000005, BF810000], [7E0ACB01, BF810000], [BF068001, BF850001, 7E0A0280, 7E020305, BF810000]].
  • After the fix: cargo test -p victory-shader gives 56 passed, 0 failed. The binary was built from this branch (its test list matches this branch's tests).
  • cargo clippy -p victory-shader --tests --target x86_64-pc-windows-msvc, with and without --features host,diagnostics: no warnings.
  • rustfmt --edition 2024 --check on io.rs and ir.rs: clean.
  • Not run: the Windows-only crates' tests (victory-gpu, victory-hle, apps/victory), and no real mesh shaders. I don't know how often shipped console shaders read v5 per lane.

No game data used.

🤖 Generated with Claude Code

**@⁠coderanonymus** on GitHub · 2026-11-24 06:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/659) `coderanonymus:mesh-group-input-guard` → `main` · +96 −8 · 2 files · 1 commits · open ## What In a mesh program, v5 holds the group input. Only the first lane's value is right, so `io::raise` refuses any per-lane read of it other than `v_readfirstlane_b32` ("the mesh group input read per lane"). The guard skipped every instruction that writes v5. That exemption exists because the IR adds a VGPR's old value to the reads of every write to it (lanes outside EXEC keep it). But `reads` is deduplicated, so the exemption also covered instructions that read v5 as a real operand. The guard also compared against the input value only, so a read of a phi that merges it was missed. On main, all of these mesh programs translate without an error: - `v_add_nc_u32 v5, v5, v1` - `v_cvt_f32_u32 v5, v5` - `global_load_dword v5, v5, s[0:1]` - `v_swap_b32 v5, v1` - `s_cmp_eq_u32 s1, 0` / `s_cbranch_scc1` / `v_mov_b32 v5, 0` / then, after the join, `v_mov_b32 v1, v5` Lowering (`Input::MeshGroup`) then gives every lane `groupId * mesh_stride`, the first lane's value. Each lane after lane 0 computes a wrong value, with no diagnostic. Change: - `ir.rs`: `build` adds the old-value read of written VGPRs, instead of `effects`. The resulting `reads` are the same as before. The new `ir::operand_reads` returns what `effects` gives, so it covers VALU sources, `v_swap_b32`, memory addresses and data, and exports. - `io.rs`: the guard now tracks the group input plus every phi that may carry it (to a fixpoint). It does not follow a phi argument that arrives over the EXEC-zero edge of `s_cbranch_execz` (taken) or `s_cbranch_execnz` (not taken): no lane is active there, so it is the same case as a partial-EXEC write. It exempts a write to v5 only when v5 is not one of its operand reads. Still not refused, noted as a `shortcut:` comment: writes that may keep the old v5 in active lanes (`v_fmac_f32`'s accumulator, a 16-bit half or d16 load, `v_writelane_b32`, `v_movreld_b32`, DPP that leaves lanes unwritten) and writes to v5 under a partial EXEC. I chose not to add per-opcode or EXEC checks for these to keep the guard small. Refusing every write under a possibly partial EXEC would also refuse the normal case where v5 is reused after `v_readfirstlane_b32`, inside a divergent if. ## Verification - Regression cases are added to `io::tests::mesh_inputs_and_primitive_export`: the five programs above must be refused. Two must still be accepted: `v_mov_b32 v5, 0; v_mov_b32 v1, v5` (v5 overwritten, then read), and a wave64 program that reuses v5 after `v_readfirstlane_b32` inside a divergent if (`s_and_saveexec_b64` + `s_cbranch_execz`, `v_mov_b32 v5, 0`, `s_or_b64 exec`) and reads it under the same mask in a second if. Main accepts that one too. Without the EXEC-zero edge rule it is refused at offset 32. The words were checked with `llvm-mc -mcpu=gfx1030`. - Before the fix (main d2a4bae plus the test): `CARGO_TARGET_DIR=/home/eric/src/VIctory/target cargo test -p victory-shader --lib mesh_inputs_and_primitive_export` fails with `accepted: [[4A0A0305, BF810000], [7E0A0D05, BF810000], [DC308000, 05000005, BF810000], [7E0ACB01, BF810000], [BF068001, BF850001, 7E0A0280, 7E020305, BF810000]]`. - After the fix: `cargo test -p victory-shader` gives 56 passed, 0 failed. The binary was built from this branch (its test list matches this branch's tests). - `cargo clippy -p victory-shader --tests --target x86_64-pc-windows-msvc`, with and without `--features host,diagnostics`: no warnings. - `rustfmt --edition 2024 --check` on `io.rs` and `ir.rs`: clean. - Not run: the Windows-only crates' tests (`victory-gpu`, `victory-hle`, `apps/victory`), and no real mesh shaders. I don't know how often shipped console shaders read v5 per lane. No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The mesh guard let any instruction that writes v5 read the group input,
because a VGPR write also reads its old value. That also let through
instructions that take v5 as a real operand, such as
v_add_nc_u32 v5, v5, v1 or global_load_dword v5, v5, s[0:1]. It also
only checked the input value itself, so v_mov_b32 v1, v5 after a join
where one path kept v5 passed. Lowering then gave every lane the first
lane's value.

The IR now adds the old-value read in build, so ir::operand_reads can
return an instruction's operand reads. The guard exempts a write to v5
only when v5 is not one of its operands, and follows the group input
through phis. It skips phi arguments from the EXEC-zero edge of
s_cbranch_execz or s_cbranch_execnz. No lane runs there, so that is the
same case as a partial-EXEC write, and a divergent if that reuses v5
after v_readfirstlane_b32 stays accepted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:11:50 +00:00
Author
Owner

@⁠Parakeet-Wheat on GitHub · 2026-11-24 10:51 UTC · original

Retail shader check for 98fe8fe, merged locally onto main ffe310e (clean):

  • Setup: Windows 11 26300, NVIDIA GeForce RTX 3070 (8 GB, driver 610.88), Ryzen 5 5600X, Rust 1.98.1 (x86_64-pc-windows-msvc), dxil.dll 1.8.2502, title 1.0.2.1.
  • victory translate <exe> shaders.rpf --list --validate, main vs this PR: no program changes outcome (21,736 validated, 4,003 failed, 162 rejected on both).
  • None of the title's 8,770 mesh programs is refused with "the mesh group input read per lane", before or after. So the stricter guard costs nothing here, and it finds no program that was silently getting the first lane's value.
  • cargo test -p victory-shader --locked: 120 passed, 0 failed, 1 ignored, including mesh_inputs_and_primitive_export.

Offline translation and validation only; no shader contents or extracted data included. AI-assisted (Claude Code); run on my machine.

**@⁠Parakeet-Wheat** on GitHub · 2026-11-24 10:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/659#issuecomment-7129333207) Retail shader check for `98fe8fe`, merged locally onto `main` `ffe310e` (clean): - **Setup:** Windows 11 26300, NVIDIA GeForce RTX 3070 (8 GB, driver `610.88`), Ryzen 5 5600X, Rust 1.98.1 (`x86_64-pc-windows-msvc`), `dxil.dll` 1.8.2502, title 1.0.2.1. - `victory translate <exe> shaders.rpf --list --validate`, `main` vs this PR: **no program changes outcome** (21,736 validated, 4,003 failed, 162 rejected on both). - None of the title's 8,770 mesh programs is refused with "the mesh group input read per lane", before or after. So the stricter guard costs nothing here, and it finds no program that was silently getting the first lane's value. - `cargo test -p victory-shader --locked`: **120 passed, 0 failed, 1 ignored**, including `mesh_inputs_and_primitive_export`. *Offline translation and validation only; no shader contents or extracted data included. AI-assisted (Claude Code); run on my machine.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:33 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/ir.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/659#issuecomment-7148624645) <!-- victory-triage v1 head=98fe8fe840bab7ad086c8126ea9d5dd856fb2e89 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-shader/src/ir.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/ir.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/659/head:github-bot/gh-659'
git switch 'github-bot/gh-659'

Merge

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