Shaders: lower v_bfm_b32 to one Bfi #3525

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

@⁠Ingenioeren on GitHub · 2026-11-27 14:08 UTC · original
Ingenioeren:perf/v_bfm_b32-dxil-bfi → main · +69 −6 · 3 files · 1 commits · open

What

Lower v_bfm_b32 to one DXIL Bfi call instead of two masks, two shifts, and a subtraction. Add an instruction-count regression test and a synthetic GPU behavior test.

Why

DXIL Bfi(width, offset, ~0, 0) produces (((1 << (width & 31)) - 1) << (offset & 31)) & 0xffffffff, exactly the mask built by the previous lowering. Width zero produces zero; fields crossing bit 31 truncate to the low dword. All operands and the result remain 32-bit integers. DXIL specification

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

Synthetic shader origin/main (63bcca4) Branch Reduction
v_bfm_b32 21 17 19.0%

The instruction's own lowering goes from five DXIL instructions to one (80% fewer).

Fetched origin before surveying. Open-PR checks included gh pr list --repo Marvisak/VIctory --state open --search "v_bfm_b32" --limit 20 (no matches), the requested "Shaders: lower" search with limit 50, and broader bfm/Bfi searches. None proposes this optimization; #1395 concerns scalar 64-bit bitfields. Fresh origin/main still has the five-operation vector mask lowering. This does not change any of the instructions in #3027, #3007, #2954, #2943, #2924, or #2893.

How tested

  • cargo test --locked --offline -p victory-shader vector_bitfield_mask -- --nocapture: two tests passed on both revisions. On main, applied only the test changes and recorded 21 instructions before adding the 17-instruction assertion. The branch reports 17.
  • The behavior test validates its synthetic DXIL with the local dxil.dll and executes 2,078 cases on each of the hardware GPU and WARP on both revisions, with identical expected output bits. It covers all 1,024 effective width/offset pairs, the same pairs with ignored high bits set, zero and wrapped widths, signed/unsigned extremes, and fields ending at or crossing bit 31. The independent CPU reference uses 64-bit arithmetic followed by truncation.
  • cargo test --locked --offline -p victory-shader: main had 300 passed, 2 failed, 3 ignored; branch had 302 passed, 2 failed, 3 ignored. Both expected 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.
  • cargo test --locked --offline -p victory-shader --test resource_tracking: branch had 3 passed, 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: fails on clean main and the branch with the same 925 pre-existing diff blocks. The outputs are identical after removing line numbers; this change adds no formatting differences. git diff --cached --check passed.
  • cargo run --locked --offline -p xtask -- policy --staged: passed, asset policy: 3 files ok.

What was not tested

No hardware run on a real game, game launch, game files, or game performance benchmark. All shader inputs were synthesized in test code. The measured gain is translated DXIL instruction count, not a measured runtime speedup.

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

🤖 Generated with Claude Code

**@⁠Ingenioeren** on GitHub · 2026-11-27 14:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3525) `Ingenioeren:perf/v_bfm_b32-dxil-bfi` → `main` · +69 −6 · 3 files · 1 commits · open ## What Lower `v_bfm_b32` to one DXIL `Bfi` call instead of two masks, two shifts, and a subtraction. Add an instruction-count regression test and a synthetic GPU behavior test. ## Why DXIL `Bfi(width, offset, ~0, 0)` produces `(((1 << (width & 31)) - 1) << (offset & 31)) & 0xffffffff`, exactly the mask built by the previous lowering. Width zero produces zero; fields crossing bit 31 truncate to the low dword. All operands and the result remain 32-bit integers. [DXIL specification](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#bfi) The synthetic shader takes two runtime inputs and stores one result to a UAV. The count includes emitted setup, live instructions, and terminators. The test-only counter is the same helper used in #2954 and #2893, so whichever PR lands later can drop its copy. | Synthetic shader | `origin/main` (`63bcca4`) | Branch | Reduction | | --- | ---: | ---: | ---: | | `v_bfm_b32` | 21 | 17 | 19.0% | The instruction's own lowering goes from five DXIL instructions to one (80% fewer). Fetched `origin` before surveying. Open-PR checks included `gh pr list --repo Marvisak/VIctory --state open --search "v_bfm_b32" --limit 20` (no matches), the requested `"Shaders: lower"` search with limit 50, and broader `bfm`/`Bfi` searches. None proposes this optimization; #1395 concerns scalar 64-bit bitfields. Fresh `origin/main` still has the five-operation vector mask lowering. This does not change any of the instructions in #3027, #3007, #2954, #2943, #2924, or #2893. ## How tested - `cargo test --locked --offline -p victory-shader vector_bitfield_mask -- --nocapture`: two tests passed on both revisions. On main, applied only the test changes and recorded 21 instructions before adding the 17-instruction assertion. The branch reports 17. - The behavior test validates its synthetic DXIL with the local `dxil.dll` and executes 2,078 cases on each of the hardware GPU and WARP on both revisions, with identical expected output bits. It covers all 1,024 effective width/offset pairs, the same pairs with ignored high bits set, zero and wrapped widths, signed/unsigned extremes, and fields ending at or crossing bit 31. The independent CPU reference uses 64-bit arithmetic followed by truncation. - `cargo test --locked --offline -p victory-shader`: main had 300 passed, 2 failed, 3 ignored; branch had 302 passed, 2 failed, 3 ignored. Both expected 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. - `cargo test --locked --offline -p victory-shader --test resource_tracking`: branch had 3 passed, 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`: fails on clean main and the branch with the same 925 pre-existing diff blocks. The outputs are identical after removing line numbers; this change adds no formatting differences. `git diff --cached --check` passed. - `cargo run --locked --offline -p xtask -- policy --staged`: passed, `asset policy: 3 files ok`. ## What was not tested No hardware run on a real game, game launch, game files, or game performance benchmark. All shader inputs were synthesized in test code. The measured gain is translated DXIL instruction count, not a measured runtime speedup. AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Use DXIL Bfi to build the five-bit width/offset mask in one call instead of five operations. The complete synthetic shader drops from 21 to 17 DXIL instructions. Add an instruction-count budget and 2,078 behavior cases executed on the host GPU and WARP.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:14:12 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-27 14:33 UTC · original

Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC.

  • PR head: 4461e440c463989cfae59a318d57d589927fb7e8, merge base 63bcca43c6a00a758510398fa43096ce7e4f7df0.
  • Command: cargo test --locked -p victory-shader --lib -- emit::lower::tests::vector_bitfield_mask_instruction_count --exact --test-threads=1 --quiet
  • Result: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 306 filtered out, exit 0.
  • Negative control: git grep -F -c vector_bitfield_mask_instruction_count 63bcca43 -- . → exit 1 / 0 hits (test genuinely new); present on PR head.
  • git diff --check 63bcca43 HEAD → exit 0.
  • Caveat: only the synthetic instruction-count lowering test was replayed; the PR's hardware/WARP vector_bitfield_mask_matches_rdna2 test and the full crate suite were not run.
  • GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved.
  • AI-assisted: Hy4 flash.
**@⁠cloph-dsp** on GitHub · 2026-11-27 14:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3525#issuecomment-7220932955) Independent CPU-only verification on Windows / Rust 1.100.0 / MSVC. - PR head: `4461e440c463989cfae59a318d57d589927fb7e8`, merge base `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - Command: `cargo test --locked -p victory-shader --lib -- emit::lower::tests::vector_bitfield_mask_instruction_count --exact --test-threads=1 --quiet` - Result: `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 306 filtered out`, exit 0. - Negative control: `git grep -F -c vector_bitfield_mask_instruction_count 63bcca43 -- .` → exit 1 / 0 hits (test genuinely new); present on PR head. - `git diff --check 63bcca43 HEAD` → exit 0. - Caveat: only the synthetic instruction-count lowering test was replayed; the PR's hardware/WARP `vector_bitfield_mask_matches_rdna2` test and the full crate suite were not run. - GPU/driver: N/A (CPU-only test). No game data, assets, extracted shaders or DRM involved. - AI-assisted: Hy4 flash.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-27 14:38 UTC · original

The current heads 4461e44 (#3525) and e038572 (#3528) merge cleanly. I measured the two together and prepared integration follow-up 845f9dd, which shares the scalar mask lowering with the vector opcode and folds a mask when both operands are known.

For the synthetic vector mask with inline operands (12, 12), the combined baseline emits 13 instructions and 2064 bytes. The shared helper emits 12 instructions and 1988 bytes. The dynamic vector fixture still emits 17 instructions. Four new inline readbacks cover ordinary fields, a zero-width mask, and operands that wrap at 32. The original 2,078 vector cases and 4,098 scalar mask/SCC cases also pass on app-local WARP 1.0.22.

All 34 lowering, 36 VALU, and 35 semantics tests passed, with one existing ignored semantics test. Both strict shader Clippy configurations and the asset check passed. This is available for the authors when bringing the two branches together. Please retain Git authorship for the contributing commits, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding my follow-up in. AI-assisted. Synthetic shaders only.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-27 14:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/3525#issuecomment-7221041779) The current heads `4461e44` (#3525) and `e038572` (#3528) merge cleanly. I measured the two together and prepared [integration follow-up `845f9dd`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/845f9dde467adc3e3ce8a85b3f4cc3a9e973f1e0), which shares the scalar mask lowering with the vector opcode and folds a mask when both operands are known. For the synthetic vector mask with inline operands `(12, 12)`, the combined baseline emits 13 instructions and 2064 bytes. The shared helper emits 12 instructions and 1988 bytes. The dynamic vector fixture still emits 17 instructions. Four new inline readbacks cover ordinary fields, a zero-width mask, and operands that wrap at 32. The original 2,078 vector cases and 4,098 scalar mask/SCC cases also pass on app-local WARP 1.0.22. All 34 lowering, 36 VALU, and 35 semantics tests passed, with one existing ignored semantics test. Both strict shader Clippy configurations and the asset check passed. This is available for the authors when bringing the two branches together. Please retain Git authorship for the contributing commits, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding my follow-up in. AI-assisted. Synthetic shaders only.
Author
Owner

@⁠kilolimaVI on GitHub · 2026-11-27 17:42 UTC · original · reviewed

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

  • vector_bitfield_mask_matches_rdna2 (2,078 width/offset cases, hardware + WARP in one run) and vector_bitfield_mask_instruction_count: pass; 0/10 failures over 10 runs. So the RDNA3 driver's Bfi gives the shifted-mask result for every case, including zero width, wrapped counts and fields crossing bit 31.
  • cargo test -p victory-shader on the 780M: 304 passed, 0 failed.

AI-assisted (Claude).

**@⁠kilolimaVI** on GitHub · 2026-11-27 17:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/3525#pullrequestreview-6091674482) · **reviewed** Verified on AMD hardware, head 4461e44: Windows 11, Radeon 780M (RDNA3 iGPU), toolchain 1.98.1. - `vector_bitfield_mask_matches_rdna2` (2,078 width/offset cases, hardware + WARP in one run) and `vector_bitfield_mask_instruction_count`: pass; 0/10 failures over 10 runs. So the RDNA3 driver's `Bfi` gives the shifted-mask result for every case, including zero width, wrapped counts and fields crossing bit 31. - `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/3525/head:github-bot/gh-3525'
git switch 'github-bot/gh-3525'

Merge

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