Shaders: lower v_msad_u8 to Msad and a wrapping add #3564

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

@⁠Ingenioeren on GitHub · 2026-11-27 17:56 UTC · original
Ingenioeren:perf/v_msad_u8-dxil-msad → main · +130 −8 · 4 files · 1 commits · open

What

Lower v_msad_u8 to DXIL Msad(src1, src0, 0) followed by an integer add of src2. Replace the four separate byte extractions, comparisons, selections and accumulations. Add an instruction-count regression test and a synthetic GPU execution test.

Declare the required MSAD metadata flag and SFI0 feature bit from the surviving calls. Extend the existing feature-usage test to cover unread calls, unreachable calls, live MSAD calls, and another opcode sharing the same DXIL declaration.

Why

The ISA masks byte positions whose reference byte in src1 is zero. DXIL masks its first operand, so the source operands must be reversed. Each included byte contributes an unsigned absolute difference. AMD RDNA 2 ISA, V_MSAD_U8

DXIL permits saturation or wrapping when its accumulator overflows. Starting Msad at zero bounds its sum to 1,020; a separate ordinary i32 add guarantees the ISA's wrapping accumulation for every src2, including 0xffffffff. Passing src2 directly to Msad would not preserve that guarantee. DXIL specification

The synthetic shader takes three runtime inputs and stores one result to a UAV. The count includes emitted setup, live instructions, and terminators. The test-only written_instructions helper is the same one used in #3525, #2954 and #2893; whichever PR lands later can drop its duplicate helper.

Synthetic shader origin/main (63bcca4) Branch Reduction
v_msad_u8 63 20 68.3%

The instruction's own lowering goes from 45 DXIL instructions to two (95.6% fewer).

Fetched origin before surveying. Searches were gh search prs --repo Marvisak/VIctory --state open v_msad_u8 (two matches), the same command with Msad (four matches), and with "masked sad" (no matches). Inspected the relevant results: #1492 contains the existing per-byte lowering, #2015 explicitly excludes MSAD from its behavior tests, #1662 lowers different quad-byte SAD instructions, and #1608 concerns half FMA. None proposes this optimization. Also ran gh pr list --repo Marvisak/VIctory --state open --limit 1000 --json number,title and searched the returned titles for the opcode/MSAD: no matches. The list reached its 1,000-item limit; the opcode searches also found older PRs. Scalar masks were excluded because #3528 covers them. This change does not modify the optimizations in #3525, #3027, #3007, #2954, #2943, #2924 or #2893.

How tested

  • cargo test --locked --offline -p victory-shader vector_masked_sad -- --nocapture: with the lowering stashed using git stash push --keep-index, the count regression failed as intended (63 emitted instructions versus the asserted 20), while the behavior test passed. With the final lowering, both tests passed and the count was 20.
  • The behavior test validates the synthetic container with the local dxil.dll, then executes 2,704 cases on each of the RX 6750 XT and WARP, both before and after. Both revisions match the independent byte-array CPU reference. Cases cover all 16 zero/nonzero reference-byte patterns, every byte value in each position, asymmetric source/reference values, maximum differences, equality, signed/unsigned boundaries, and accumulator wraparound. No backend was skipped in these runs.
  • cargo test --locked --offline -p victory-shader: clean main had 300 passed, 2 failed, 3 ignored; branch had 302 passed, 2 failed, 3 ignored. Both pre-existing failures are emit::rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order; the local validator rejects their Pipeline State Validation container parts. No tests were weakened or newly ignored.
  • cargo test --locked --offline -p victory-shader --test resource_tracking: 3 passed, 0 failed, 1 ignored. Run separately because the full command stops after the existing library failures.
  • cargo clippy --locked --offline -p victory-shader --all-targets -- -D warnings: passed.
  • cargo fmt --check -p victory-shader: fails on clean main and the branch with the same 127 pre-existing formatting blocks. The proposed formatting additions/deletions, grouped by filename, are identical; only line numbers and surrounding context differ. All four changed files were included (63 existing blocks across those files); this change introduces no formatting edits requested by rustfmt. git diff --cached --check passed.
  • cargo run --locked --offline -p xtask -- policy --staged: passed, asset policy: 4 files ok.

What was not tested

No game files, game-derived data, game launch, or game performance benchmark. Execution checks run translated DXIL in a synthetic one-thread Wave32 compute shader against the public-ISA CPU reference; they do not execute the original GFX10 instruction natively. Other adapters and optimized-opcode execution in graphics stages were not separately tested. The measured improvement is emitted DXIL instruction count, with no runtime speedup claim.

AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.

🤖 Generated with Claude Code

**@⁠Ingenioeren** on GitHub · 2026-11-27 17:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/3564) `Ingenioeren:perf/v_msad_u8-dxil-msad` → `main` · +130 −8 · 4 files · 1 commits · open ## What Lower `v_msad_u8` to DXIL `Msad(src1, src0, 0)` followed by an integer add of `src2`. Replace the four separate byte extractions, comparisons, selections and accumulations. Add an instruction-count regression test and a synthetic GPU execution test. Declare the required MSAD metadata flag and SFI0 feature bit from the surviving calls. Extend the existing feature-usage test to cover unread calls, unreachable calls, live MSAD calls, and another opcode sharing the same DXIL declaration. ## Why The ISA masks byte positions whose reference byte in `src1` is zero. DXIL masks its first operand, so the source operands must be reversed. Each included byte contributes an unsigned absolute difference. [AMD RDNA 2 ISA, V_MSAD_U8](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) DXIL permits saturation or wrapping when its accumulator overflows. Starting `Msad` at zero bounds its sum to 1,020; a separate ordinary `i32` add guarantees the ISA's wrapping accumulation for every `src2`, including `0xffffffff`. Passing `src2` directly to `Msad` would not preserve that guarantee. [DXIL specification](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#msad) The synthetic shader takes three runtime inputs and stores one result to a UAV. The count includes emitted setup, live instructions, and terminators. The test-only `written_instructions` helper is the same one used in #3525, #2954 and #2893; whichever PR lands later can drop its duplicate helper. | Synthetic shader | `origin/main` (`63bcca4`) | Branch | Reduction | | --- | ---: | ---: | ---: | | `v_msad_u8` | 63 | 20 | 68.3% | The instruction's own lowering goes from 45 DXIL instructions to two (95.6% fewer). Fetched `origin` before surveying. Searches were `gh search prs --repo Marvisak/VIctory --state open v_msad_u8` (two matches), the same command with `Msad` (four matches), and with `"masked sad"` (no matches). Inspected the relevant results: #1492 contains the existing per-byte lowering, #2015 explicitly excludes MSAD from its behavior tests, #1662 lowers different quad-byte SAD instructions, and #1608 concerns half FMA. None proposes this optimization. Also ran `gh pr list --repo Marvisak/VIctory --state open --limit 1000 --json number,title` and searched the returned titles for the opcode/MSAD: no matches. The list reached its 1,000-item limit; the opcode searches also found older PRs. Scalar masks were excluded because #3528 covers them. This change does not modify the optimizations in #3525, #3027, #3007, #2954, #2943, #2924 or #2893. ## How tested - `cargo test --locked --offline -p victory-shader vector_masked_sad -- --nocapture`: with the lowering stashed using `git stash push --keep-index`, the count regression failed as intended (63 emitted instructions versus the asserted 20), while the behavior test passed. With the final lowering, both tests passed and the count was 20. - The behavior test validates the synthetic container with the local `dxil.dll`, then executes 2,704 cases on each of the RX 6750 XT and WARP, both before and after. Both revisions match the independent byte-array CPU reference. Cases cover all 16 zero/nonzero reference-byte patterns, every byte value in each position, asymmetric source/reference values, maximum differences, equality, signed/unsigned boundaries, and accumulator wraparound. No backend was skipped in these runs. - `cargo test --locked --offline -p victory-shader`: clean main had 300 passed, 2 failed, 3 ignored; branch had 302 passed, 2 failed, 3 ignored. Both pre-existing failures are `emit::rect::tests::rectangles_get_their_fourth_corner` and `emit::tests::elements_sharing_a_row_keep_their_order`; the local validator rejects their Pipeline State Validation container parts. No tests were weakened or newly ignored. - `cargo test --locked --offline -p victory-shader --test resource_tracking`: 3 passed, 0 failed, 1 ignored. Run separately because the full command stops after the existing library failures. - `cargo clippy --locked --offline -p victory-shader --all-targets -- -D warnings`: passed. - `cargo fmt --check -p victory-shader`: fails on clean main and the branch with the same 127 pre-existing formatting blocks. The proposed formatting additions/deletions, grouped by filename, are identical; only line numbers and surrounding context differ. All four changed files were included (63 existing blocks across those files); this change introduces no formatting edits requested by rustfmt. `git diff --cached --check` passed. - `cargo run --locked --offline -p xtask -- policy --staged`: passed, `asset policy: 4 files ok`. ## What was not tested No game files, game-derived data, game launch, or game performance benchmark. Execution checks run translated DXIL in a synthetic one-thread Wave32 compute shader against the public-ISA CPU reference; they do not execute the original GFX10 instruction natively. Other adapters and optimized-opcode execution in graphics stages were not separately tested. The measured improvement is emitted DXIL instruction count, with no runtime speedup claim. AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 18:03:49 +00:00
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-27 18:07 UTC · original

I tested the graphics stage on d8b7c0b, one test commit on top of 89caff6. The pixel shader reads its source from the pixel position, covers all 16 reference-byte masks with two accumulators, and exports both halves of the exact 32-bit result.

On app-local WARP 1.0.22 with Agility 1.620.2 and DXIL validation, all 512 pixels matched the CPU byte-array reference bit for bit on 63bcca4 and on the PR. The same test on main fails only at the optimization assertion because the per-byte lowering uses no MSAD flag. With the PR, SFI0 advertises MSAD in all 30 nonzero-mask variants. Both original compute tests also passed on WARP, including the 20-instruction check. Strict shader Clippy and the asset policy passed.

The branch is available if you want the pixel test. Please retain the Git author when cherry-picking the test, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding it into another commit.

Synthetic inputs, WARP only. AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-27 18:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3564#issuecomment-7225142280) I tested the graphics stage on [`d8b7c0b`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/review/msad-pixel-warp), one test commit on top of `89caff6`. The pixel shader reads its source from the pixel position, covers all 16 reference-byte masks with two accumulators, and exports both halves of the exact 32-bit result. On app-local WARP 1.0.22 with Agility 1.620.2 and DXIL validation, all 512 pixels matched the CPU byte-array reference bit for bit on `63bcca4` and on the PR. The same test on main fails only at the optimization assertion because the per-byte lowering uses no MSAD flag. With the PR, SFI0 advertises MSAD in all 30 nonzero-mask variants. Both original compute tests also passed on WARP, including the 20-instruction check. Strict shader Clippy and the asset policy passed. The branch is available if you want the pixel test. Please retain the Git author when cherry-picking the test, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding it into another commit. Synthetic inputs, WARP only. AI-assisted with Codex.
Author
Owner

@⁠kilolimaVI on GitHub · 2026-11-27 18:26 UTC · original

Verified on AMD hardware, head 89caff6: Windows 11, Radeon 780M (RDNA3 iGPU), toolchain 1.98.1.

  • vector_masked_sad_matches_rdna2: all 2,704 cases match on the 780M and on WARP; with vector_masked_sad_instruction_count, 0/10 failures over 10 runs. So the RDNA3 driver's Msad gives the ISA result, including the masked-byte and wrapping-accumulator cases.
  • cargo test -p victory-shader on the 780M: 304 passed, 0 failed.

AI-assisted (Claude).

**@⁠kilolimaVI** on GitHub · 2026-11-27 18:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3564#issuecomment-7225498422) Verified on AMD hardware, head 89caff6: Windows 11, Radeon 780M (RDNA3 iGPU), toolchain 1.98.1. - `vector_masked_sad_matches_rdna2`: all 2,704 cases match on the 780M and on WARP; with `vector_masked_sad_instruction_count`, 0/10 failures over 10 runs. So the RDNA3 driver's `Msad` gives the ISA result, including the masked-byte and wrapping-accumulator cases. - `cargo test -p victory-shader` on the 780M: 304 passed, 0 failed. AI-assisted (Claude).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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