Skip to content

fix(shader): accept overwritten VGPR inputs in counted indexed writes - #457

Open
jeremygold02 wants to merge 2 commits into
Marvisak:mainfrom
jeremygold02:codex/counted-movreld-phi
Open

jeremygold02 wants to merge 2 commits into
Marvisak:mainfrom
jeremygold02:codex/counted-movreld-phi

Conversation

@jeremygold02

@jeremygold02 jeremygold02 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Shader lowering

A pixel shader can use v_movreld_b32 to initialize a range of VGPRs that held barycentric inputs at entry. The IR tracks the old value of every possible destination. On main, fill_phis can reject a later color export with barycentrics read directly even when the indexed writes cover that register before the first observable read.

Allow an undefined incoming value when the translator can prove that an unguarded indexed move overwrites it on that edge or that a counted loop initializes the slot before its only exit. The loop case follows the SSA values for M0, the unit increment, the SCC comparison and the branch. It preserves the value carried on the backedge and requires that the old value has no other consumer.

This runs on the default translation path and requires no raw barycentric input in the emitted shader. Shaders that actually consume incoming barycentrics still receive the existing refusal. The raw-input paths proposed in #281, #442 and #453 address that separate use case.

Verification

  • Compared a two-iteration native pixel shader with a direct-write reference on a physical RTX 5070. Iteration one writes 0.0 to the first slot, and iteration two writes 1.0 to the second. After fence completion, all 64 RGBA32F texels from each shader matched the expected [1.0, 0.0, 0.5, 0.5] bit for bit. Both emitted shaders passed Windows SDK DXIL validation. Main rejected the indexed shader. Ending the loop after one iteration still produces the expected refusal.
  • The in-tree regression fixture covers two accepted cases and thirteen refusals, including partial EXEC, an early exit, a clobbered SCC, a wrapping counter and a value consumed on the backedge.
  • Replayed eleven previously refused shaders against main and this branch using separate builds. One newly passed the Windows SDK validator. The other ten retained their original refusal reasons and offsets.
  • Compared 25,694 distinct supported shader inputs across four stages in separate builds of main and this commit. SDK-valid outputs increased from 21,971 to 21,973. All 21,971 previously valid DXIL outputs were byte-identical. Both new successes had previously failed emission, and the SDK rejection count stayed at 185.
  • Repeated a bounded Windows 11 title run on the same RTX 5070 and starting pipeline cache as the previous run. VIctory translated the pixel shader that failed in two earlier runs, wrote it to the shader cache, and created a host graphics PSO for the same vertex and pixel shader pair. The tester still saw a black screen with audio. Execution of a draw using that PSO remains unverified.
  • All 58 CPU-side shader library tests passed. The separate RTX draw above exercised the changed path. Package formatting and Clippy with warnings denied also passed.

Refs #97.

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested efcd753 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. None of the 12 embedded programs uses this pattern.

  • This only changes behaviour where fill_phis previously stopped with "barycentrics read directly". Every shader that translated before goes through the same code. That fits your 21,971 byte-identical outputs. The risk is limited to shaders that were refused before.

  • The proof is deliberately narrow:

    • The incoming value has exactly one consumer, an unguarded v_movreld_b32 in the same block, with no DPP or SDWA, writing that register.
    • M0 traces back through s_mov_b32 to a scalar phi whose start value on the edge is a constant.
    • For the loop case, a unit increment, an SCC compare and a branch exit are checked.

    I followed the direct case in full. For the counted-loop case I read the checks, and the 13 refusal fixtures (partial EXEC, early exit, clobbered SCC, wrap, a backedge consumer) cover the ways it could be wrong.

  • Returning undef for the overwritten value is sound only if the proof holds. A bug in it would show up as wrong output in a shader that was refused until now, not as a regression in one that already worked.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…inputs in counted indexed writes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sebastianspicker

Copy link
Copy Markdown

Tested efcd753 against current main ffe310e (macOS, Rust 1.98.1).

The branch merges without conflicts, but the new test no longer compiles there. #566 changed resource::track to take the structured program as a fourth argument:

error[E0061]: this function takes 4 arguments but 3 arguments were supplied
   --> crates/victory-shader/src/emit/mod.rs:661:23

Building st first fixes it, the same way main now does in lowers:

let st = structure::structure(&p).unwrap();
let res = resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap();
let io = io::raise(&p, &native).unwrap();

With that change, on the merge with main:

  • cargo test -p victory-shader: 97 passed, 1 ignored. indexed_move_initializes_an_input_before_its_first_use fails with main's lower.rs and passes with this branch's.
  • Windows-target clippy (x86_64-pc-windows-msvc, --all-targets -D warnings) is clean.
  • A 48-program synthetic corpus (scalar, VOP1/2/3, VOPC and carry ops) still translates.
  • 4.8 million libFuzzer inputs through Container::parse plus emit::translate in 90 s, no panic.

I haven't checked the hardware result or reviewed each refusal case in the fixture; this is only the build, test and regression side.

AI-assisted.

@towiice towiice left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested:

  • Hardware: NVIDIA GeForce GTX 1650 SUPER (Turing TU116, 4 GB VRAM, 32-wide subgroups / wave32, driver 610.88), Windows 11 x64 (26H2 / 10.0.26300), toolchain stable-x86_64-pc-windows-gnullvm (llvm-mingw 23.1.6).
  • Test command: cargo +stable-x86_64-pc-windows-gnullvm test -p victory-shader
  • Result: 70 passed, 0 failed (including emit::tests::indexed_move_initializes_an_input_before_its_first_use).
    • Correctly verifies that an indexed move loop overwriting VGPR inputs prior to observable reads accepts incoming undefined states without triggering false-positive barycentric read refusals.
      Clean implementation and passes all unit tests.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Textually clean merge with current main (ffe310e), but it fails to compile after merge — a semantic conflict: main's resource::track now takes a 4th argument (Option<&structure::Structured>), and this PR's test call in crates/victory-shader/src/emit/mod.rs:661 still passes 3:

error[E0061]: this function takes 4 arguments but 3 arguments were supplied
   --> crates/victory-shader/src/emit/mod.rs:661:23
    = note: argument #4 of type `Option<&structure::Structured>` is missing

Needs a rebase onto current main plus updating that call site (pass None or the structured data, whichever the new signature expects). I'll re-review once it's green.

@alexdeliu

Copy link
Copy Markdown
Contributor

Merged onto main @ ffe310eb, this PR (efcd753a) no longer compiles its tests. crates/victory-shader/src/emit/mod.rs:661 calls resource::track with 3 arguments. Since 6638098 it takes a 4th, lanes: Option<&Structured>; main's tests pass Some(&st) with let st = structure::structure(&p).unwrap();.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

@jeremygold02

jeremygold02 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@alexdeliu Thanks for catching this. I merged main at ffe310eb into this PR and fixed the fixture in 08b681c2. It now computes st before calling resource::track and passes Some(&st), matching the other tests on main.

The focused regression test and both CI Clippy commands passed locally on Windows 11. cargo test --workspace --locked also passed with 343 tests passed, 0 failed, and 6 ignored.

@alexdeliu

Copy link
Copy Markdown
Contributor

@jeremygold02 rechecked 08b681c2 merged onto main @ ffe310eb on the RX 9070: both CI clippy runs are clean and cargo test --workspace passes (343 tests).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Proposed close: already in main. Your proof was ported onto the current lowering in f342dcf ("Shaders: inputs a counted v_movreld loop overwrites first need no value", credited to this PR): Lower::overwritten_movreld_edge and Lower::counted_movreld_index in crates/victory-shader/src/emit/lower.rs, used from the fill-phis arm, with your 15-case test as indexed_move_initializes_an_input_before_its_first_use in crates/victory-shader/src/emit/mod.rs. The latest push (d00ddae) is a merge of main that leaves no difference from main. Thank you for the work and the hardware verification; it made the bloom downsample shader translate.

This PR will be closed in 48 hours unless you push a change that adds something main lacks or a maintainer removes the label.


Generated by Claude Code

Allow an incoming raw barycentric value to remain undefined only when an unguarded indexed move is proven to initialize the register before any use. Preserve the loop-carried register and reject incomplete or ambiguous initialization.

Refs: Marvisak#97
@jeremygold02
jeremygold02 force-pushed the codex/counted-movreld-phi branch from 08b681c to 2cc715e Compare November 25, 2026 02:55
@jeremygold02

jeremygold02 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

@Marvisak I rebased this branch onto main at 940d32f6 and resolved the conflict in emit/lower.rs. The new head is 2cc715e1. All 15 focused regression cases passed locally, including Windows SDK validation for both positive cases. GitHub now reports the branch merges cleanly. This is ready for the content review requested in triage. The CI run passed both Clippy configurations and the workspace tests.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
Marvisak pushed a commit that referenced this pull request Nov 25, 2026
`PS_DownSampleBloomFilter(Karis)_557ae3d7` (the bloom downsample, 3 draws
a frame in the intro cutscene) fills a register array v0.. with
`v_movreld` in a uniform loop counted from a constant start to an `eq`
bound. v0/v1 hold the raw barycentrics on entry, which have no host form
("barycentrics read directly"), so the loop header's phi refused the
program although no lane ever reads those values: the loop's first passes
overwrite them.

PR #457's proof, ported onto the current lowering: the loop-carried
register's only use is the unguarded `v_movreld` at index M0, M0 a copy
of a scalar phi starting at a constant and stepping by 1 to an `s_cmp_eq`
bound that exits through the one exit block; the register's slot lies in
[start, bound), so the move writes it before anything can read it, and
the entry edge's value is undefined. Every other shape keeps the refusal
(the PR's 15-case test: wrong start, skipped slot, decrement, wrap,
unknown start, short loop, clobbered SCC, early or divergent exit, masked
write, old value as source, reads before the write or on the back edge).

Shader list against f6cd0144: 0 newly failing; `PS_DownSampleBloomFilter
Karis_557ae3d7` and `PS_DownSampleBloomFilter_557ae3d7` emit, validate and
make pipelines (1-2 s, under 0.1 GB).

Ported from PR #457 (efcd753, 08b681c, Jeremy Goldberg): the fill-phis
arm now takes the edge's liveness from `live_edges`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants