Skip to content

Shaders: zero-extend unsigned 24-bit operands with one mask - #3632

Open
Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:perf/u24-zero-extend-mask
Open

Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:perf/u24-zero-extend-mask

Conversation

@Ingenioeren

Copy link
Copy Markdown
Contributor

The unsigned 24-bit multiply forms zero-extend each operand with a left shift followed by a logical right shift. Masking with 0x00FF_FFFF does the same work in one instruction. Share the operand extension between the multiply, high-product and multiply-add arms; signed forms keep their arithmetic shifts, and the MAD addend remains a full dword.

Synthetic shader Before After
v_mul_u32_u24 (VOP2 and VOP3) 21 19
v_mul_hi_u32_u24 (VOP2 and VOP3) 25 23
v_mad_u32_u24 24 22

These are emitted DXIL instruction counts for a shader with runtime inputs and a UAV result, including setup and terminators. The corresponding signed forms remain at 21, 25 and 24. GPU execution time was not benchmarked.

Validation on Windows, RX 6750 XT and WARP, based on 63bcca4:

  • cargo test --locked --offline -p victory-shader unsigned_24_bit_operands_use_one_mask_each -- --nocapture: failed with tests alone on the old lowering (21/25/24 against 19/23/22 budgets); passes with the mask lowering.
  • cargo test --locked --offline -p victory-shader multiplies_of_24_bits_ignore_the_upper_byte -- --nocapture: passes before and after. Ten synthetic signed/unsigned instruction encodings each run 4,580 inputs per adapter, with zero CPU-reference mismatches or hardware/WARP disagreements. Cases cover 24-bit sign boundaries, nonzero upper bytes, maximum products and full-width wrapping addends.
  • cargo test --locked --offline -p victory-shader 24_bit -- --nocapture: 3 passed.
  • cargo test --locked --offline -p victory-shader: baseline 300 passed / 2 failed / 3 ignored; final 302 passed / the same 2 failed / 3 ignored. Both failures (emit::rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order) report the same local dxil.dll Pipeline State Validation mismatch on both revisions.
  • cargo test --locked --offline -p victory-shader --test resource_tracking: 3 passed / 1 ignored. cargo test --locked --offline -p victory-shader --doc: no doc tests, passed.
  • cargo clippy --locked --offline -p victory-shader --all-targets -- -D warnings: passes before and after the production change.
  • cargo fmt --check: fails on existing formatting differences on main; comparing the changed files with main found identical rustfmt deviations and none in the new test file.
  • cargo run --locked --offline -p xtask -- policy --staged: passes, 4 files. lower.rs shrinks from 520,563 to 520,384 bytes.

The test-only instruction counter matches #3600 exactly and is also shared by the other open instruction-count PRs. #3007 changes the high-product implementation; this change only shortens its operand extension, and the count budgets allow that further reduction. #2926 separately fixes clamping in the low-product/MAD arms; whichever lands second will need a small rebase in those arms. No duplicate optimization was found in the open/merged PR search.

No game files, game shaders or game run were used. Other GPUs and Linux/Wine were not tested.

AI assistance: prepared with OpenAI Codex, reviewed by Ingenioeren.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I resolved the overlap between e34a9af and #2926 3b60f1b on 63bcca4. The merge keeps #2926's MUL/MAD route and reuses your extend24 helper there and in MUL_HI. Unsigned operands use the mask. Signed operands keep their sign extension, and the MAD addend remains a full dword. Integration branch 0e22c83

With #2926's test alone on e34a9af, the first clamped shader was refused with integer output modifiers. The merged code matched the wide-integer reference in all 96 signed/unsigned MUL/MAD cases, with clamp on and off. The 10 arithmetic encodings in the upper-byte test also matched the reference for all 4,580 inputs each. I forced both harness passes to app-local WARP 1.0.22 with Agility 1.620.2 and DXIL validation.

The unsigned unclamped DXIL counts still match 19 for MUL, 23 for MUL_HI and 22 for MAD. The signed counts still match 21, 25 and 24. I got 36 VALU, 35 semantics and 34 lowerer tests passing, with one semantics test ignored. Strict all-feature shader Clippy passed. The branch preserves both PR commits. AI-assisted with Codex.

@oni-swr

oni-swr commented Nov 28, 2026

Copy link
Copy Markdown

WARP-only independent verification of #3632 (no GPU used)

Claim: #1633 (comment)

Revisions: PR head e34a9af93fe616a7a019d744c0d525ff6a80de7d (still that SHA) on base 63bcca43c6a00a758510398fa43096ce7e4f7df0. The merge-base is 63bcca43, so it merges cleanly (fast-forward).

Setup: Windows 11, VICTORY_WARP=1 and VICTORY_D3D12_WARP=1, so no hardware adapter. VICTORY_DXIL pointed at the Windows SDK 10.0.26100 dxil.dll (1.8.2502.11). The WARP used was the OS d3d10warp.dll 10.0.26100.9818. I could not find a separate "1.0.22" WARP package on this machine, so that exact version was not used. Separate target dir, -j 4 on every cargo command.

Base 63bcca43: cargo test --locked -p victory-shader gives lib 298 passed / 4 failed / 3 ignored. The failures are synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out}. They report "no pipeline for a wave of 32" and a gather mismatch, which are WARP limits on this machine. They are unrelated to 24-bit ops. lower.rs is 520,563 bytes.

Control on base (PR test changes only, no extend24 change):

  • unsigned_24_bit_operands_use_one_mask_each FAILS. Counts are 21/21, 25/25, 24 against budgets 19, 23, 22 (signed forms stay at 21, 25, 24).
  • multiplies_of_24_bits_ignore_the_upper_byte PASSES. Behaviour is unchanged on base, as expected for a pure optimisation.

Merged 63bcca43 + e34a9af9:

  • Full crate with --no-fail-fast: lib 300 passed / 4 failed / 3 ignored. The failures are the same 4 as base. The two new tests account for the +2. The other targets passed: resource_tracking 3 passed / 1 ignored, doc tests 0.
  • unsigned_24_bit_operands_use_one_mask_each passes. Counts are [(19,19),(19,19),(23,23),(23,23),(21,21),(21,21),(25,25),(25,25),(22,22),(24,24)].
  • multiplies_of_24_bits_ignore_the_upper_byte passes under WARP + dxil.dll validation.
  • cargo test --locked -p victory-shader --test resource_tracking: 3 passed / 1 ignored.
  • cargo test --locked -p victory-shader --doc: ok, 0 tests.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings: clean.
  • cargo run --locked -p xtask -- policy --staged (merged diff staged): "asset policy: 4 files ok".
  • lower.rs: 520,563 -> 520,384 bytes (-179), as expected.

Diff review (all hunks in bitcode.rs, lower.rs, u24_tests.rs, semantics_tests.rs): no defects found.

  • extend24 signed is shl 8; ashr 8, which is identical to the old signed path. Unsigned is and 0x00FFFFFF, which equals the old shl 8; lshr 8 zero-extension.
  • Signed forms (v_mul_i32_i24, v_mul_hi_i32_i24, v_mad_i32_i24) keep the arithmetic extension. Unsigned forms (v_mul_u32_u24, v_mul_hi_u32_u24, v_mad_u32_u24) get the mask.
  • The v_mad_* addend d is still added as a full dword, unextended. The test uses full-width addends (0x80000000, u32::MAX).
  • mul_hi still receives the extended values with the correct signedness.
  • written_instructions is #[cfg(test)]. It counts live phis and body instructions in the layout plus one terminator per block, which matches what write emits. The test comments say the same.
  • The semantics_tests oracle masks to 24 bits, sign-extends only for the signed forms, and uses i64 products. That is correct for the mul, mul_hi and mad cases. It covers VOP2 and VOP3 encodings and edge values.
  • lower_valu was made pub(super) for the new test module. That is a test-only change.
  • Overlap: Shaders: saturate clamped 24-bit multiply and multiply-add #2926 (mul24.rs, lower.rs) and Shaders: lower the 32-bit mul_hi forms to one UMul/IMul #3007 (bitcode.rs, lower.rs) are both open and touch the same arms or the same file. A small rebase is likely for whichever lands second. No logical conflict.

AI-assisted: Claude Sonnet 5.5. No game files, dumps, install paths, shader names or retail hashes were used or are included.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I updated the combined branch for your exact e34a9af and #2926 3b60f1b with #3731 7c6f908 in integration commit 3b693c3. It resolves the overlap in the existing 24-bit lowering module. Unsigned operands still use one mask, and the clamped forms still use the wide calculation. The unclamped MAD forms now emit UMad or IMad.

The same minimal fixtures now emit 21 unsigned and 23 signed MAD instructions, down from 22 and 24 in the earlier combined tree. Each multiply and high-multiply count stays at its previous budget. WARP with Agility 1.620.2 passed 34 lowerer, 36 VALU and 36 semantics tests, with one existing ignored test. The clamped regression and all 726 generated full-width addend cases per MAD opcode passed. Strict all-feature shader Clippy and the asset policy passed.

@Ingenioeren, this branch is ready for merge coordination with #2926 and #3731. Please retain the original Git authors when rebasing or cherry-picking. If folding my changes into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>.

AI-assisted with OpenAI Codex.

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.

3 participants