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

Open
coderanonymus wants to merge 1 commit from upstream-pr/659 into main
coderanonymus commented 2026-11-24 06:24:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by coderanonymus on 2026-11-24T06:24:25Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6575993532 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/659). Originally posted by [coderanonymus](https://github.com/coderanonymus) on 2026-11-24T06:24:25Z. --- ## 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)
LAKSHYA-cmd-tech commented 2026-11-24 06:54:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T06:26:41Z.

Review: APPROVED


LGTM! Tested on Windows 11 with MSVC Rust 1.98.1. All 69 tests in ictory-shader passed cleanly, including the new regression cases in mesh_inputs_and_primitive_export. The fixpoint phi tracking and ir::operand_reads differentiation cleanly protect mesh group inputs from illegal per-lane reads.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034652470 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/659#pullrequestreview-6034652470). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T06:26:41Z. **Review:** APPROVED --- LGTM! Tested on Windows 11 with MSVC Rust 1.98.1. All 69 tests in ictory-shader passed cleanly, including the new regression cases in mesh_inputs_and_primitive_export. The fixpoint phi tracking and ir::operand_reads differentiation cleanly protect mesh group inputs from illegal per-lane reads.
catminou4 commented 2026-11-24 10:03:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:37:36Z.

Review: APPROVED


Merges cleanly with main; macOS arm64, rustc 1.98.1, cargo test -p victory-shader --locked with the PR merged into main 8e79c80 → 70 passed / 0 failed / 1 ignored.

Mesh v5 (the group index) is uniform only as v_readfirstlane_b32 reads it; this PR closes the per-lane-read holes: the group value now propagates through phis across joins (with the s_cbranch_execz/_execnz no-lane edges carved out), writes to v5 count as reads of the old value via the new operand_reads, and the five refusal cases plus the two acceptance cases are tested with real encodings.

The ir.rs change is the subtle part and it's right: effects() no longer folds written VGPRs into reads (that's now done in build() for the IR's old-value semantics), while operand_reads() exposes the true operand reads for the mesh check — so v_add_nc_u32 v5, v5, v1 is refused (v5 is an operand) but v_mov_b32 v5, 0 followed by a read is accepted (only lanes outside EXEC keep the old value). The shortcut comment honestly lists the write forms still accepted (fmac accumulator, d16 loads, writelane, DPP) — acceptable as documented follow-up scope.

Note for CI: the interesting cases are structural/IR-level and run on macOS (they did); no GPU needed.

<!-- greyfinch-sync:Marvisak/VIctory:review:6036818872 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/659#pullrequestreview-6036818872). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T09:37:36Z. **Review:** APPROVED --- Merges cleanly with main; macOS arm64, rustc 1.98.1, `cargo test -p victory-shader --locked` with the PR merged into main `8e79c80` → **70 passed / 0 failed / 1 ignored**. Mesh v5 (the group index) is uniform only as `v_readfirstlane_b32` reads it; this PR closes the per-lane-read holes: the group value now propagates through phis across joins (with the `s_cbranch_execz`/`_execnz` no-lane edges carved out), writes to v5 count as reads of the old value via the new `operand_reads`, and the five refusal cases plus the two acceptance cases are tested with real encodings. The `ir.rs` change is the subtle part and it's right: `effects()` no longer folds written VGPRs into `reads` (that's now done in `build()` for the IR's old-value semantics), while `operand_reads()` exposes the true operand reads for the mesh check — so `v_add_nc_u32 v5, v5, v1` is refused (v5 is an operand) but `v_mov_b32 v5, 0` followed by a read is accepted (only lanes outside EXEC keep the old value). The shortcut comment honestly lists the write forms still accepted (fmac accumulator, d16 loads, writelane, DPP) — acceptable as documented follow-up scope. Note for CI: the interesting cases are structural/IR-level and run on macOS (they did); no GPU needed.
mercuryVM commented 2026-11-24 11:03:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T10:56:48Z.

Review: APPROVED


Tested 98fe8fe on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 69 passed, including the new per-lane cases. cargo clippy -p victory-shader --all-targets is clean. The local title executable has no mesh programs, so translate --pipelines can't exercise this.

  • Replacing "skip every write to v5" with "skip only when v5 isn't a real operand" (operand_reads) is the precise fix for the deduplicated-reads problem: v_add_nc_u32 v5, v5, v1 is now refused and v_mov_b32 v5, 0 still passes.
  • Growing group through phis until a fixed point catches the join case, and excluding the EXEC-zero edge of s_cbranch_execz/execnz avoids refusing the guarded-skip pattern that is actually safe.
  • The shortcut: comment lists the remaining partial writes that still pass (fmac accumulator, d16, writelane, movreld, DPP). Good that it's explicit.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6037952239 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/659#pullrequestreview-6037952239). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T10:56:48Z. **Review:** APPROVED --- Tested 98fe8fe on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` 69 passed, including the new per-lane cases. `cargo clippy -p victory-shader --all-targets` is clean. The local title executable has no mesh programs, so `translate --pipelines` can't exercise this. - Replacing "skip every write to v5" with "skip only when v5 isn't a real operand" (`operand_reads`) is the precise fix for the deduplicated-`reads` problem: `v_add_nc_u32 v5, v5, v1` is now refused and `v_mov_b32 v5, 0` still passes. - Growing `group` through phis until a fixed point catches the join case, and excluding the EXEC-zero edge of `s_cbranch_execz`/`execnz` avoids refusing the guarded-skip pattern that is actually safe. - The `shortcut:` comment lists the remaining partial writes that still pass (fmac accumulator, d16, writelane, movreld, DPP). Good that it's explicit. AI-assisted (Claude Code).
Parakeet-Wheat commented 2026-11-24 11:04:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Parakeet-Wheat on 2026-11-24T10:51:18Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129333207 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/659#issuecomment-7129333207). Originally posted by [Parakeet-Wheat](https://github.com/Parakeet-Wheat) on 2026-11-24T10:51:18Z. --- 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.*
Marvisak commented 2026-11-25 01:08:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:33:02Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148624645 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/659#issuecomment-7148624645). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:33:02Z. --- <!-- 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 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/659:upstream-pr/659'
git switch 'upstream-pr/659'

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