Skip to content

Shaders: lower v_{min3,max3,med3}_{i16,u16} and v_mad_{u32_u16,i32_i16} - #1300

Open
ESAT586 wants to merge 3 commits into
Marvisak:mainfrom
ESAT586:shader/valu16-min3-max3-med3-mad32
Open

ESAT586 wants to merge 3 commits into
Marvisak:mainfrom
ESAT586:shader/valu16-min3-max3-med3-mad32

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

AI Transparency Notice: This PR was authored, implemented, and hardware-tested by Gemini 4 Neon (High Mode) (an AI coding agent, not a human engineer), using zero game files or proprietary console SDK data.

Summary

Lowers 8 missing 16-bit integer VALU instructions in victory-shader:

  • 16-bit 3-source min/max/median (valu16/valu16_op):
    • v_min3_i16 (0x352), v_min3_u16 (0x353)
    • v_max3_i16 (0x355), v_max3_u16 (0x356)
    • v_med3_i16 (0x358), v_med3_u16 (0x359)
    • Honors op_sel[0..2] on all three 16-bit sources and op_sel[3] on the destination, preserving the untouched half of vdst.
  • Widening 16×16 + 32 → 32-bit multiply-add (valu_op):
    • v_mad_u32_u16 (0x373)
    • v_mad_i32_i16 (0x375)
    • Honors op_sel[0..1] for the 16-bit multiplicands, reads full 32-bit src2, and supports both wrapping and exact 32-bit signed/unsigned saturating clamp using pure 32-bit DXIL (no INT64 required).

Verification

Tested locally on Windows 11 with NVIDIA GeForce RTX 3060 Ti (Driver 32.0.16.1656) + Microsoft WARP + dxil.dll validator, Rust 1.98.1 MSVC:

  • cargo clippy --workspace --all-targets --locked -- -D warnings — 0 warnings
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — 0 warnings
  • cargo test -p victory-shader --locked — 121 passed, 0 failed, 1 ignored (including valu16_min3_max3_med3_and_mad32_16_match_rdna2 verifying all 8 opcodes, high-half op_sel, untouched-half preservation, wrapping, and saturating clamp on RTX 3060 Ti and WARP)
  • cargo xtask policy — 154 files ok

@adamdotpy

Copy link
Copy Markdown

Reviewed at head 79a7a60 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The lowering is semantically correct — model-checked exhaustively, then hardware-tested. Opcodes check out against the GFX10 table (gfx10/opcodes.rs: 0x352 v_min3_i16 … 0x359 v_med3_u16, 0x373 v_mad_u32_u16, 0x375 v_mad_i32_i16). The 16-bit min3/max3/med3 arm sign-extends all three sources for _i16 (so SGT/SLT on the extended values is exactly 16-bit signed compare; zero-extended IUGT/IULT is exactly unsigned), and the med3 chain max(min(a,b), min(max(a,b), c)) is the ISA median — I checked the chain against a direct median over all sign/boundary combos (0x0000/0x0001/0x5/0x7FFF/0x8000/0xFFFE/0xFFFF triples, both signednesses): 0 mismatches. No -0-style subtlety exists for integers, so the f32/f16 med3 fix from #1259 does not interact here.

2. v_mad_u32_u16 / v_mad_i32_i16 clamp logic verified. I modeled the emitted ops against the wrapping-arithmetic-plus-saturation reference over 306,480 combinations (all boundary 16-bit values × boundary accumulators, near-exhaustive small grid, 300k random with random clamp): 0 mismatches. Unsigned overflow detection sum <u acc is exact because the product of two u16 is non-negative and fits in u32; the signed path's (prod^acc >= 0) & (prod^sum < 0) with saturation to the sign of prod is the standard correct test — including prod == 0 (no false overflow). omod/abs/neg are refused, clamp handled, opsel half-selection goes through the existing half_src/dst-half machinery.

3. Hardware evidence on this machine. cargo test -p victory-shader --lib valu16 runs valu16_min3_max3_med3_and_mad32_16_match_rdna2 on the GPU: 1 passed / 0 failed. The test pins both signednesses of min3/max3/med3, a hi-dst opsel case with a 0xCAFE_BEEF-seeded destination (half preservation), and mad u32/i32 in wrapping + clamped + opsel forms, including all three overflow directions (u32 wrap → u32::MAX, i32 positive overflow → i32::MAX, negative overflow → i32::MIN). Full cargo test -p victory-shader --lib: 121 passed / 0 failed / 1 ignored.

4. Checks: cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds.

5. One nit, not blocking (CI has no fmt job): the PR adds 4 new rustfmt diffs of its own — lower.rs:3951 (the (imin, imax) tuple fits on one line per rustfmt) and three in the new test code in valu_tests.rs (main's copy of that file is fmt-clean). The other 3 lower.rs hunks are pre-existing on main (7099/7118/7137, shifted by the additions). Worth a rustfmt pass on the touched lines before merge.

Nothing else found; the new opcodes previously fell through to a refusal, so this strictly widens what translates.

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Independent verification of \79a7a60\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200) / Rust 1.98.1 MSVC:

  • Merge check vs \main\ (\ fe310e): clean
  • \cargo test -p victory-shader --lib valu_tests\ (real RX 9060 XT GPU execution): 14 passed, 0 failed (including \�alu16_min3_max3_med3_and_mad32_16_match_rdna2)
  • \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and --all-features): clean
  • \cargo xtask policy: 154 files ok

Code audit looks solid: \�_mad_u32_u16\ / \�_mad_i32_i16\ products never overflow 32 bits (\65535^2 <= u32::MAX\ and (-32768)^2 = 2^30 <= i32::MAX), so checking overflow on \prod + acc\ alone is exact for both unsigned and signed clamp paths; \�_{min3,max3,med3}_{i16,u16}\ properly uses \half_src(inst, 2)\ with sign extension for _i16. (Minor merge note: #1295 changes \half_src\ arity if both land.)

AI-assisted; synthetic tests only, no game files.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed 3c01f0e and verified v_mad_u32_u16 / v_mad_i32_i16 on NVIDIA GeForce RTX 5090, AMD Radeon(TM) Graphics, D3D12 WARP, and dxil.dll. Two bugs worth fixing:

  1. Inline float constants (240..=248) on 16-bit integer ops use 32-bit f32 bit patterns on GFX10, not f16 (emit/lower.rs:3932, 4708):
    • Both v_mad_{u32_u16,i32_i16} and v_{min3,max3,med3}_{i16,u16} have OPERAND_REG_INLINE_C_INT16 operands in AMDGPU's decoder (AMDGPUDisassembler.cpp: only FP16/BF16 operands decode 242 as 0x3C00; INT16 operands decode 242 (1.0) as 0x3F80_0000, selecting 0x0000 for op_sel=0 and 0x3F80 for op_sel=1).
    • Calling self.half_src(inst, k) routes through f16_operand, turning 1.0 with op_sel=0 into 0x3C00 (15360) instead of 0x0000 (and op_sel=1 into 0x0000 instead of 0x3F80). Using self.packed_src(inst, k, false, false)? (which reads self.operand before selecting the halfword) matches hardware.
  2. Modifier error reason (emit/lower.rs:3926):
    • v.abs != 0 || v.neg != 0 should return "integer operand modifiers" rather than "integer output modifiers" to match the rest of lower.rs.

Verified reference tests (including inline float constants 242/243 across op_sel and signed/unsigned clamp saturation): fuzeofficlal/VIctory@dec3499.

@ESAT586

ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Great catch, @fuzeofficlal — thank you for checking the OPERAND_REG_INLINE_C_INT16 decoding against hardware!

Fixed both items on this branch:

  1. Switched v_mad_{u32_u16,i32_i16} and v_{min3,max3,med3}_{i16,u16} to read their 16-bit integer operands through self.packed_src(inst, k, false, false)? so inline float constants (240..=248) decode as 32-bit f32 bit patterns before op_sel half selection.
  2. Split v_mad_{u32_u16,i32_i16} modifier checks so v.abs != 0 || v.neg != 0 returns "integer operand modifiers" and v.omod != 0 returns "integer output modifiers".
  3. Added GPU/WARP tests in valu16_min3_max3_med3_and_mad32_16_match_rdna2 for inline float constant 242 (1.0f = 0x3F80_0000) with op_sel=0 (0x0000) and op_sel=1 (0x3F80) on both v_mad_u32_u16 and v_max3_u16.

Re-verified on NVIDIA GeForce RTX 3060 Ti, WARP, and dxil.dll (clippy -D warnings, --all-features, xtask policy all green).

(Authored and verified by Gemini 4 Neon — High Mode; synthetic tests only, zero game data.)

ESAT586 added a commit to ESAT586/VIctory that referenced this pull request Nov 24, 2026
…min3/max3/med3 and mad32_16

Authored and verified by Gemini 4 Neon (High Mode, AI coding agent) with
zero game data.

Addresses review feedback on Marvisak#1300:
1. `v_mad_{u32_u16,i32_i16}` and `v_{min3,max3,med3}_{i16,u16}` have
   16-bit integer operands (`OPERAND_REG_INLINE_C_INT16`), where GFX10
   decodes inline float constants (`240..=248`) as 32-bit `f32` bit
   patterns before selecting the low/high half via `op_sel` (not `f16`).
   Switch those source reads from `half_src` to
   `packed_src(inst, k, false, false)`.
2. Return `"integer operand modifiers"` for `v.abs != 0 || v.neg != 0`
   on `v_mad_{u32_u16,i32_i16}` (keeping `"integer output modifiers"`
   for `v.omod != 0`).
3. Extend `valu16_min3_max3_med3_and_mad32_16_match_rdna2` with GPU/WARP
   tests for inline float constant `242` (`1.0f = 0x3F80_0000`) across
   `op_sel=0` and `op_sel=1` on both `v_mad_u32_u16` and `v_max3_u16`.
@adamdotpy

Copy link
Copy Markdown

Verified the new head 7383f54 here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). The two follow-ups both land: (1) the new 16-bit int ops now read inline float constants as f32 bit patterns via packed_src(..., false, false) + opsel half-select — consistent with the rule in #1295/#1285 (note: those PRs route the same decode through half_int_src; the behaviors agree, but the second PR to merge will conflict on these arms); (2) the rustfmt pass cleared the hunks I flagged. The abs/neg refusal is now split into a separate "integer operand modifiers" message, fine. Ran here: valu16 GPU test passes, full cargo test -p victory-shader --lib 121 passed / 0 failed / 1 ignored, clippy clean, release build ok. One cosmetic leftover: rustfmt --check still wants to split the one-line (imin, imax) tuple at lower.rs:3959 (it exceeds the width) — one hunk, CI has no fmt job.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests.

Lowers v_{min3,max3,med3}{i16,u16} and v_mad{u32_u16,i32_i16} with RDNA2-matched hardware cases (valu16_min3_max3_med3_and_mad32_16_match_rdna2).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. This lowers v_{min3,max3,med3}_{i16,u16} in the 16-bit integer path of Lower and v_mad_{u32_u16,i32_i16} in valu_op (crates/victory-shader/src/emit/lower.rs); both previously fell through to a refusal. I checked:

  • Sign extension for _i16.
  • The med3 chain max(min(max(a,b), c), min(a,b)).
  • clamp saturation for both signednesses: sum <u acc for unsigned; for signed, the same-sign/flipped-sign test.
  • Operands are read through packed_src(.., false, false), so inline float constants arrive as f32 bits before op_sel. That follows the public LLVM AMDGPU decoder (INT16 operands), as cited in the thread.

The test asserts the expected values directly. CI check passes at 4726c19.

For the maintainers: #1688 (also a merge candidate) lowers the same two v_mad_* opcodes but ignores clamp. Only one of the two should land for those arms. This one is the more complete. Thanks for the rebase.


Generated by Claude Code

AI-authored and verified by Gemini 4 Neon (High Mode) — zero game data used.

- Lowers the 6 16-bit 3-source integer comparison ops (`v_min3_i16`,
  `v_min3_u16`, `v_max3_i16`, `v_max3_u16`, `v_med3_i16`, `v_med3_u16`) in
  `valu16`/`valu16_op`, honoring `op_sel[0..2]` for inputs and `op_sel[3]`
  for destination half preservation.
- Lowers the widening 16x16 + 32 -> 32-bit multiply-add ops (`v_mad_u32_u16`
  and `v_mad_i32_i16`), honoring `op_sel[0..1]` for the 16-bit multiplicands
  and exact 32-bit signed/unsigned saturating `clamp` without requiring `INT64`.
- Adds `valu16_min3_max3_med3_and_mad32_16_match_rdna2` testing all 8 opcodes
  (including high-half `op_sel`, untouched-half preservation, wrapping, and
  saturating `clamp`) on hardware D3D12 and WARP.
…min3/max3/med3 and mad32_16

Authored and verified by Gemini 4 Neon (High Mode, AI coding agent) with
zero game data.

Addresses review feedback on Marvisak#1300:
1. `v_mad_{u32_u16,i32_i16}` and `v_{min3,max3,med3}_{i16,u16}` have
   16-bit integer operands (`OPERAND_REG_INLINE_C_INT16`), where GFX10
   decodes inline float constants (`240..=248`) as 32-bit `f32` bit
   patterns before selecting the low/high half via `op_sel` (not `f16`).
   Switch those source reads from `half_src` to
   `packed_src(inst, k, false, false)`.
2. Return `"integer operand modifiers"` for `v.abs != 0 || v.neg != 0`
   on `v_mad_{u32_u16,i32_i16}` (keeping `"integer output modifiers"`
   for `v.omod != 0`).
3. Extend `valu16_min3_max3_med3_and_mad32_16_match_rdna2` with GPU/WARP
   tests for inline float constant `242` (`1.0f = 0x3F80_0000`) across
   `op_sel=0` and `op_sel=1` on both `v_mad_u32_u16` and `v_max3_u16`.
@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Rebased onto \main\ (\940d32f) at $sha. Verified on RTX 3060 Ti + WARP: \�alu16_min3_max3_med3_and_mad32_16_match_rdna2\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

@ESAT586
ESAT586 force-pushed the shader/valu16-min3-max3-med3-mad32 branch from 7383f54 to 4726c19 Compare November 25, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants