Shaders: sign-extend the v_mad_i64_i32 literal addend #2259

Closed
Calvix2k wants to merge 2 commits from upstream-pr/2261 into main
Calvix2k commented 2026-11-25 09:17:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T09:17:24Z.


Fixes #2253.

Problem

Lower::src64 sign-extends a 32-bit literal only when the instruction's name ends in _i64 (the rule added in #447). v_mad_i64_i32 ends in _i32, so its 64-bit addend S2.i64 was zero-extended: 0 * 0 + (-100) produced 0x0000_0000_FFFF_FF9C instead of 0xFFFF_FFFF_FFFF_FF9C, and 10 * 10 + (-100) set the high dword to 1 instead of 0.

RDNA 2 expands a literal for a 64-bit signed operand by sign extension (ISA "Literal Expansion to 64 bits"; §12.12, V_MAD_I64_I32: {vcc_out, D.i64} = S0.i32 * S1.i32 + S2.i64). v_mad_u64_u32's S2.u64 is unsigned and keeps zero-extending.

Change

src64's literal arm takes v_mad_i64_i32's addend as signed as well (that instruction only passes its S2 through src64). v_mad_u64_u32, the _u32/bit forms and v_mad_i64_i32's i32 multiplicands are untouched.

Verification

  • mad_64_bit_addend_lowers_to_the_signed_literal (no GPU, dxc -dumpbin through the shared emit::tests::dump_dxil): the DXIL is add i64 %24, -100, i.e. the sign-extended literal. Made the shared helper pub(super) to use it instead of a copy.
  • mad_64_bit_addend_sign_extends_its_literal: GPU case reading the high dword — 0 * 0 + -100 gives all ones, 10 * 10 + -100 gives 0, and the same literal with v_mad_u64_u32 gives 0. Skips on this host (no SM 6.6 device, #1991 is not in this branch) and runs in CI.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean; cargo xtask policy 3 files ok.

The rest of the shader lib suite here still shows the known SM-6.6 environment failures (no device), unrelated.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6601154338 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2261). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T09:17:24Z. --- Fixes #2253. ## Problem `Lower::src64` sign-extends a 32-bit literal only when the instruction's name ends in `_i64` (the rule added in #447). `v_mad_i64_i32` ends in `_i32`, so its 64-bit addend `S2.i64` was zero-extended: `0 * 0 + (-100)` produced `0x0000_0000_FFFF_FF9C` instead of `0xFFFF_FFFF_FFFF_FF9C`, and `10 * 10 + (-100)` set the high dword to 1 instead of 0. RDNA 2 expands a literal for a 64-bit signed operand by sign extension (ISA "Literal Expansion to 64 bits"; §12.12, `V_MAD_I64_I32`: `{vcc_out, D.i64} = S0.i32 * S1.i32 + S2.i64`). `v_mad_u64_u32`'s `S2.u64` is unsigned and keeps zero-extending. ## Change `src64`'s literal arm takes `v_mad_i64_i32`'s addend as signed as well (that instruction only passes its `S2` through `src64`). `v_mad_u64_u32`, the `_u32`/bit forms and `v_mad_i64_i32`'s i32 multiplicands are untouched. ## Verification - `mad_64_bit_addend_lowers_to_the_signed_literal` (no GPU, `dxc -dumpbin` through the shared `emit::tests::dump_dxil`): the DXIL is `add i64 %24, -100`, i.e. the sign-extended literal. Made the shared helper `pub(super)` to use it instead of a copy. - `mad_64_bit_addend_sign_extends_its_literal`: GPU case reading the high dword — `0 * 0 + -100` gives all ones, `10 * 10 + -100` gives 0, and the same literal with `v_mad_u64_u32` gives 0. Skips on this host (no SM 6.6 device, #1991 is not in this branch) and runs in CI. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean; `cargo xtask policy` 3 files ok. The rest of the shader lib suite here still shows the known SM-6.6 environment failures (no device), unrelated.
upstream-sync closed this pull request 2026-11-25 10:07:12 +00:00
DenRakEiw commented 2026-11-25 10:07:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T09:23:32Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with dxil.dll validator):

  • cargo test -p victory-shader --lib mad_64_bit_addend — 2 passed, 0 failed on both RTX 5090 hardware and VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean sign-extension of v_mad_i64_i32 literal addends with both DXIL and end-to-end GPU+WARP test coverage. (Note for triage: overlaps #2254.)

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6054872340 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2261#pullrequestreview-6054872340). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T09:23:32Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC, with `dxil.dll` validator): - `cargo test -p victory-shader --lib mad_64_bit_addend` — **2 passed, 0 failed** on both RTX 5090 hardware and `VICTORY_D3D12_WARP=1` - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean sign-extension of `v_mad_i64_i32` literal addends with both DXIL and end-to-end GPU+WARP test coverage. (Note for triage: overlaps #2254.) AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 10:07:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T09:43:11Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — sign-extends the v_mad_i64_i32 literal addend (S2.i64 signed despite the _i32 name), plus the dump_dxil per-call temp file fix for parallel tests. Superset of #2254; see my note there.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055176403 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2261#pullrequestreview-6055176403). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T09:43:11Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — sign-extends the v_mad_i64_i32 literal addend (S2.i64 signed despite the _i32 name), plus the dump_dxil per-call temp file fix for parallel tests. Superset of #2254; see my note there.
Calvix2k commented 2026-11-25 10:07:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T09:40:14Z.


Closing in favour of #2254 (@BastienRodz), which has the same src64 change and is already verified on hardware (GTX 1050 via Agility, @kotekompotek) and integrated with #171 (@Dathinel). Two PRs for one arm only splits the review; nothing here is lost.

@BastienRodz if you want the extra coverage: this branch also checks the emitted DXIL (add i64 %x, -100, so the sign extension is visible without a GPU) and reads the high dword of 0 * 0 + -100 and 10 * 10 + -100 with the unsigned form as a control. Both are in codex/shader-mad-i64-literal-2253 if you would rather fold them in than have them disappear; otherwise your test plus the hardware runs are enough.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158643079 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2261#issuecomment-7158643079). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T09:40:14Z. --- Closing in favour of #2254 (@BastienRodz), which has the same `src64` change and is already verified on hardware (GTX 1050 via Agility, @kotekompotek) and integrated with #171 (@Dathinel). Two PRs for one arm only splits the review; nothing here is lost. @BastienRodz if you want the extra coverage: this branch also checks the emitted DXIL (`add i64 %x, -100`, so the sign extension is visible without a GPU) and reads the *high* dword of `0 * 0 + -100` and `10 * 10 + -100` with the unsigned form as a control. Both are in `codex/shader-mad-i64-literal-2253` if you would rather fold them in than have them disappear; otherwise your test plus the hardware runs are enough.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2259
No description provided.