Skip to content

victory-shader: lazily lower non-active console lane-mask SOPs bitwise - #1423

Closed
bartikele wants to merge 1 commit into
Marvisak:mainfrom
bartikele:shader-non-active-console-mask
Closed

bartikele wants to merge 1 commit into
Marvisak:mainfrom
bartikele:shader-non-active-console-mask

Conversation

@bartikele

Copy link
Copy Markdown
Contributor

In Lower::mask_dword, when a compute shader (MaskData::Console) reads a lane mask as data that is not in self.x.st.active (e.g. a bitwise combination of outer-EXEC masks computed under a narrower EXEC), lazily evaluate the mask's defining bitwise scalar operation (s_mov, s_not, s_and, s_or, s_xor, s_andn2, s_orn2, s_nand, s_nor, s_xnor in _b32/_b64) from its operands instead of failing with a lane mask holding lanes elsewhere read as data.

Evaluating lazily inside mask_dword (rather than eagerly in Lower::sop) ensures control-flow-only masks in waterfall loops never trigger waveActiveBallot or flags::WAVE_OPS.

Adds unit test emit::lower::tests::non_active_console_mask_sop_lowers_bitwise.

Tested with:

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings
  • cargo test -p victory-shader --locked
  • cargo xtask policy

@dpalacios69

Copy link
Copy Markdown
Contributor

Independent check on Linux of head 55f0dc4, merged onto current main (ffe310e; a fast-forward): cargo test -p victory-shader --locked passes (97 passed, 0 failed, 1 ignored). The 1 test(s) it adds run on Linux and pass. cargo clippy -p victory-shader --all-targets -- -D warnings shows nothing of its own, only the two unused-import errors in emit/mod.rs that main already has on Linux. No GPU run. AI-assisted; no game data.

@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.

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 97. GPU/valu execution tests are Windows-gated and were not run here.

Non-active console lane masks (SOPs whose value is read as data after EXEC narrowed) are now lowered lazily and bitwise from their defining scalar op (mov/not/and/or/xor/andn2/orn2/nand/nor/xnor) instead of forcing a waveActiveBallot in the narrower block. Self-dependency is rejected via the balloting set, constants and special operands handled, and the macOS unit test lowers the v_cmpx-narrowed s_and_b32→s_bcnt1 case (97 passed, +1). Bitwise semantics match the hardware definition; please still run the shader suite on Windows since the interplay with control-flow masks is subtle.

@rezioliver-collab

Copy link
Copy Markdown

I ran an offline A/B comparison of ffe310e and 55f0dc4 on Windows 11 with Rust 1.98.1 (MSVC) and dxil.dll 1.8.2502.11. I reused two baseline runs after verifying the archive and validator SHA-256 hashes, then ran victory translate --validate --list --jobs 1 twice on the PR against the same shaders.rpf.

The a lane mask holding lanes elsewhere read as data error dropped from 272 to 7. Of the 265 compute shaders that progressed past it, 260 hit another translation error and five emitted DXIL that the validator rejected.

Next reported result Compute shaders
a descriptor read only as data 219
a structured buffer of unknown stride 25
a select on an untracked mask 5
a descriptor's address or format read as data 5
a scalar memory read as data 4
a lane-mask join of an untracked mask 1
s_getpc_b64 1
DXIL validator rejection after emission 5
Total 265

Compute DXIL emission rose from 1,441 to 1,446. All five newly emitted shaders received the validator diagnostic about an operation incompatible with the shader stage or another entry property. Both revisions validated 21,724 shaders overall, including 1,417 compute shaders. Comparing all 25,889 listed shaders by archive offset and stage showed zero validation gains or losses. Both repeats agreed within each revision.

For the synthetic check, I copied only non_active_console_mask_sop_lowers_bitwise into a separate baseline checkout. The test builds two masks before narrowing EXEC, combines them with s_and_b32, and reads the result as data with s_bcnt1_i32_b32. It reproduced the targeted error on the baseline and passed on the PR. cargo xtask policy passed on both revisions.

AI use : Assisted by GPT 6.2 Sol.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 55f0dc4 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

Another 219 programs move from "a lane mask holding lanes elsewhere read as data" on to "a descriptor read only as data", the reason #1067 handles. So this PR may gain much more stacked on #1402, and I'm measuring that.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 121, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@sinan-cmyk

Copy link
Copy Markdown

Stacked on #1402: +214 programs, 0 lost

Result: On main alone this PR gained nothing (earlier comment). It moved 219 programs from "a lane mask holding lanes elsewhere read as data" to "a descriptor read only as data", which #1067 handles. On top of #1402, which contains #1067, those programs now validate.

The merge. This PR conflicts with #1402 in one hunk of emit/lower.rs: both add a different test at the same place in the test module. I kept both tests. No functional code conflicts.

Build Validated Change Lost
#1402 head 2310409 23,412
#1402 + this PR (head 55f0dc4) 23,626 +214 0

Interaction with #1221. With #1221 also merged on #1402, the result is +213, one fewer than this PR alone. One program goes back to "a lane-mask join of an untracked mask". #1221 alone adds nothing on top of #1402 (details on #1221).

Method: victory translate --validate --list on shaders.rpf (1.0.2.1), dxil.dll 1.8.2502.11, compared program by program (25,889 programs). No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@sinan-cmyk

Copy link
Copy Markdown

Static review of #1423 (head 55f0dc4)

Summary: I read the diff against main ffe310e and found no likely bug. The lowering looks right by construction. What is missing is a test that checks the computed values, and wave64 coverage. Line numbers below refer to crates/victory-shader/src/emit/lower.rs at this head. Context from my earlier measurements: stacked on #1402 this PR gains 214 programs offline, 0 lost, and its tests pass on an RTX 4070 SUPER. Offline counts show only that the DXIL validates.

What looks right

  • Where it applies (mask_dword, ~1191): only a mask read as data that structure did not mark active, and only in MaskData::Console (compute). Masks used as branch conditions (s_cbranch_vccz/execz), as v_cndmask selectors or as EXEC go through structure's per-lane booleans, not through mask_dword. So no use that already translated changes. Only reads that were refused before now lower. That fits the 0 programs lost.
  • The semantics: SALU bitwise ops ignore EXEC, so computing the result dword from the operands' dwords is exactly what the console computes, including the bits of lanes outside EXEC.
    • An operand that is itself a mask goes through i32_of → get → mask_dword. If it is active, it gets its ballot at its defining block, and that ballot is exact. If it is a non-active SOP mask, it recurses.
    • balloting guards the recursion (1255, 1313). A phi mask has no defining SOP and returns None, so it keeps the old refusal. Loops cannot recurse forever.
  • Where it is emitted: the ops are placed at the read (self.cur), not at the SOP. Operands are SSA values that dominate the SOP, which dominates the read, and the ops are pure. So the result is the same.
  • Wave64 high half: it reads Reg::S(code + 1) of each operand, and that maps to the operand's highs entry, the ballot's second dword. The inline-constant sign extension and the zero-extended literal match the existing 64-bit SOP helper (~2449).

Findings, ranked

  1. Risk: no test checks a value. non_active_console_mask_sop_lowers_bitwise (~7298) only asserts that lowers() returns Ok. A wrong operand, a wrong op mapping or a wrong half would still pass. This path now produces numbers where the translator used to refuse. Suggestion: one run_cases hardware/WARP test in valu_tests. Two compares, then v_cmpx to narrow EXEC, then s_and/s_andn2/s_not of the outer masks, then s_bcnt1 or v_mov of the result. Assert the console bit count, including lanes outside the narrowed EXEC.
  2. Risk: the wave64 path is untested. lowers() builds wave64: false, so the high-half code (h = 1, the Reg::S(code + 1) reads, the const sign extension) never runs in a test. In Console mode on a 32-lane host, the ballot's second dword also depends on how wave64 is emulated. That is not new in this PR, but this path now relies on it too.
  3. Nit: guard high in wave32. In a wave32 program, highs should be empty. If high were ever true, the code would read the next SGPR as a "high half" without any error. if high && !self.x.native.wave64 { return Ok(None) } makes that impossible.
  4. Nit: duplicated helper. The half closure (1259) is a copy of the 64-bit operand helper in the SOP lowering (~2449). A shared method would keep both from drifting apart, for example in literal handling.
  5. Nit: the result is not cached. Each read re-emits the bitwise ops; the operand ballots are cached. That is harmless, but caching like ballots would keep the DXIL smaller when a mask is read several times.
  6. Coverage note (safe): s_cselect_b64/_b32 of two masks, and the saveexec forms, return None and keep the old refusal. They are possible follow-ups, not bugs.

AI-assisted (Claude Code); static review only, nothing run.

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Lane masks holding lanes elsewhere computed bitwise from their defining scalar op in compute: main has exactly this as Lower::mask_number (lower.rs 1763, commit 8af0472).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants