Shaders: lower v_msad_u8 to Msad and a wrapping add #3564
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3564
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3564"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Ingenioeren on GitHub · 2026-11-27 17:56 UTC · original
Ingenioeren:perf/v_msad_u8-dxil-msad→main· +130 −8 · 4 files · 1 commits · openWhat
Lower
v_msad_u8to DXILMsad(src1, src0, 0)followed by an integer add ofsrc2. 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
src1is 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_U8DXIL permits saturation or wrapping when its accumulator overflows. Starting
Msadat zero bounds its sum to 1,020; a separate ordinaryi32add guarantees the ISA's wrapping accumulation for everysrc2, including0xffffffff. Passingsrc2directly toMsadwould not preserve that guarantee. DXIL specificationThe 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_instructionshelper is the same one used in #3525, #2954 and #2893; whichever PR lands later can drop its duplicate helper.origin/main(63bcca4)v_msad_u8The instruction's own lowering goes from 45 DXIL instructions to two (95.6% fewer).
Fetched
originbefore surveying. Searches weregh search prs --repo Marvisak/VIctory --state open v_msad_u8(two matches), the same command withMsad(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 rangh pr list --repo Marvisak/VIctory --state open --limit 1000 --json number,titleand 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 usinggit 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.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 areemit::rect::tests::rectangles_get_their_fourth_cornerandemit::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 --checkpassed.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
@andrewpozdnakov7-jpg on GitHub · 2026-11-27 18:07 UTC · original
I tested the graphics stage on
d8b7c0b, one test commit on top of89caff6. 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
63bcca4and 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.
@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; withvector_masked_sad_instruction_count, 0/10 failures over 10 runs. So the RDNA3 driver'sMsadgives the ISA result, including the masked-byte and wrapping-accumulator cases.cargo test -p victory-shaderon the 780M: 304 passed, 0 failed.AI-assisted (Claude).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.