Shaders: lower v_mad_{u16,i16} and saturating clamp on 16-bit integer ops #1211
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!1211
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1211"
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?
@ESAT586 on GitHub · 2026-11-24 16:26 UTC · original
ESAT586:shader/valu16-mad-clamp→main· +162 −2 · 2 files · 1 commits · openWhat's wrong
In
crates/victory-shader/src/emit/lower.rs(valu16/valu16_op):v_mad_u16(VOP3 0x340) andv_mad_i16(VOP3 0x35e) are decoded ingfx10/opcodes.rsbut not lowered, causing shaders using 16-bit integer multiply-add to fail lowering.valu16_oprejectsv.clampon all 16-bit integer ops withErr("modifiers on a 16-bit integer operation"), even though under the AMD RDNA2 ISAclamp = 1selects saturating 16-bit arithmetic onv_add_nc_u16,v_sub_nc_u16,v_subrev_nc_u16,v_mad_u16, andv_mad_i16.Fix
v_mad_u16andv_mad_i16tovalu16'sINTlist and computea * b + din 32 bits (half_srczero-extended for_u16, sign-extended for_i16), preserving the untouched destination half viawrite_half.v.clampspecifically onv_add_nc_u16,v_sub_nc_u16,v_subrev_nc_u16,v_mad_u16, andv_mad_i16:v_add_nc_u16/v_mad_u16: clamp to0xFFFFviaIUGT(u16::MAX * u16::MAX + u16::MAX <= u32::MAX, never wrapsu32).v_sub_nc_u16/v_subrev_nc_u16: clamp to0viaSLT(result is in[-65535, 65535]).v_mad_i16: clamp to[-32768, 32767]viaSLT/SGT(|r| <= 32768 * 32768 + 32768 < 2^31, never wrapsi32).v.clamp.emit::valu_tests::v_mad_16_and_int16_clamp_match_rdna2incrates/victory-shader/src/emit/valu_tests.rstesting wrapping & saturatingv_mad_u16,v_mad_i16,v_add_nc_u16 clamp,v_sub_nc_u16 clamp, and upper-half preservation.Verification (Real GPU + WARP + CI Gates)
Tested on Windows 11, NVIDIA GeForce RTX 3060 Ti (driver
32.0.16.1656), Rust1.98.1:cargo test -p victory-shader --lib v_mad_16_and_int16_clamp_match_rdna2: ok (dxil.dllvalidator + RTX 3060 Ti hardware + Microsoft WARP)cargo test -p victory-shader --locked: 121 passed, 0 failed, 1 ignoredcargo clippy --workspace --all-targets --locked -- -D warnings: pass (0 warnings)cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: pass (0 warnings)cargo xtask policy:asset policy: 154 files okScope & Non-Overlap
#1142(which covers packed VOP3Pv_pk_*); this PR covers single-lanevalu16(v_mad_u16,v_mad_i16, and 16-bit integerclamp). Checked open PRs and issues — no overlap.ed4b40ffc8to5780573f47@shortselow-byte on GitHub · 2026-11-24 17:23 UTC · original
This PR has no CI run yet, so I ran the CI's checks at head
7dffb2a(onmainffe310e) on AMD, to add a second vendor to the RTX 3060 Ti run: Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT (RDNA2).cargo test --workspace --locked -- --test-threads=1: 343 passed, 0 failed, includingv_mad_16_and_int16_clamp_match_rdna2on the AMD device and WARP.cargo clippy --workspace --all-targets --locked -- -D warnings, and with--all-features: clean.cargo xtask policy: ok.@Zipettte on GitHub · 2026-11-24 18:00 UTC · original
Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at
7dffb2a:cargo test -p victory-shaderpasses, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
@Marvisak on GitHub · 2026-11-25 01:18 UTC · original · edited 2026-11-26 05:31 UTC
Automated triage for the maintainers.
Merge candidate. Rebased onto current
main. The change is the same as the head that was a merge candidate before. Inlower.rs(the 16-bit integer path of the VALU lowering),v_mad_u16andv_mad_i16now wrap, with sign extension for the i16 form.v_add_nc_u16,v_sub_nc_u16andv_subrev_nc_u16saturate when CLAMP is set. On the eight ops where the cited RDNA2 measurement found the clamp bit has no effect, CLAMP is accepted and does nothing. CLAMP onv_mad_*is still refused, since nobody has measured it. I checked the opcode numbers in the test againstgfx10/opcodes.rsand the saturation compares (unsigned for add, signed below zero for the zero-extended sub).CI: passing. Recommending a look, not approving.
Generated by Claude Code
@upipibi on GitHub · 2026-11-25 03:39 UTC · original
A hardware data point for the ops this PR still refuses with CLAMP=1, from reading, not a run here. boykopovar/AnyPS5 measured VOP3 16-bit integer ops with and without the clamp bit on gfx1036 (RDNA2), through their hardware oracle (the §9 entry and #22615):
v_add_nc_u16with clamp saturates. That matches this PR's add / sub arms.v_mul_lo_u16,v_lshlrev_b16/v_lshrrev_b16/v_ashrrev_i16andv_min/v_max_{u16,i16}. The result is the wrapped low 16 bits either way.So those eight could take
v.clampas a no-op instead of returningErr("modifiers on a 16-bit integer operation"). It should not saturate there: xcl-dev/xcl#202 madev_mul_lo_u16saturate and the same measurement contradicts it.v_mad_u16/v_mad_i16with clamp weren't part of that measurement.AI-assisted (Claude Code).
@ESAT586 on GitHub · 2026-11-25 11:23 UTC · original
Rebased onto \main\ (\
940d32f) at $sha. Thanks @upipibi for the RDNA2 (\gfx1036) hardware data point! Updated \lower.rs\ to accept \CLAMP=1\ as a no-op (wrapped low 16 bits, no saturation) on \_mul_lo_u16, \_lshlrev_b16, \_lshrrev_b16, \_ashrrev_i16, and \{min,max}{u16,i16}, and added GPU regression tests for all 8 ops. Verified on RTX 3060 Ti + WARP: \_mad_16_and_int16_clamp_match_rdna2\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.@upipibi on GitHub · 2026-11-25 11:48 UTC · original
@ESAT586 I read
21e43afagainst the gfx1036 measurement. The eight ops takeCLAMP=1as a no-op and keep the wrapped low 16 bits (clamp_noopinlower.rs, and the no-op cases in the new test), which is what was measured.v_add_nc_u16andv_sub_nc_u16saturate, which also matches.v_mad_i16sign-extends all three sources, andu16::MAX * u16::MAX + u16::MAXfits in 32 bits, so the> 0xFFFFcompare is exact.One open point:
v_mad_u16/v_mad_i16with clamp. The saturating results thatv_mad_16_and_int16_clamp_match_rdna2checks come from the lowering's model, because the measurement didn't include either op with clamp.v_mul_lo_u16is a reason to be careful here: the same measurement showed its clamp bit does nothing, even though one could expect it to saturate. Until someone measures the twomadops with clamp on RDNA2, I'd name or comment the test so it says which half was measured, or keepv_mad_*with clamp refused.AI-assisted (Claude Code). Read only, nothing run for this.
@ESAT586 on GitHub · 2026-11-25 12:59 UTC · original
Updated at $sha\ per triage and @upipibi's note: kept \CLAMP=1\ refused on \_mad_u16\ / \_mad_i16\ (added refusal regression check) until hardware-measured on RDNA2, while keeping wrapping \mad{u16,i16}, saturating \_{add,sub,subrev}_nc_u16, and the 8 hardware-measured \clamp_noop\ ops. Verified on RTX 3060 Ti + WARP: \_mad_16_and_int16_clamp_match_rdna2\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.
@upipibi on GitHub · 2026-11-25 13:53 UTC · original
@ESAT586 Thanks for keeping the clamp refused there.
ed4b40f(as-is, onmain940d32f), RTX 3070 (616.56) plus WARP, Windows 11, debug,cargo test -p victory-shader --locked:v_mad_16_and_int16_clamp_match_rdna2passes, including the new refusal check forv_mad_u16/v_mad_i16withCLAMP=1. It's slow on this machine, though: it ran for more than 60 s, most of the suite's 82 s.mainhas too (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,a_heap_buffer_read_by_element_takes_the_views_stride,gathers_at_a_level_match_the_hardware).One run only.
AI-assisted (Claude Code). Synthetic tests only.
@fuzeofficlal on GitHub · 2026-11-26 13:54 UTC · original · approved these changes
Reviewed and verified
5780573f4755merged locally ontoorigin/main(63bcca43) on Windows 11 x64 (RTX 5090 andVICTORY_WARP=1, Rust1.98.1MSVC, Windows SDK10.0.26100.0dxil.dll).v_mad_u16/v_mad_i16paths (emit/lower.rs:6937-6955, 7144-7166) have exact 32-bit intermediate precision without overflow (65535^2 + 65535 <= u32::MAX, signed range withini32),v_add_nc_u16/v_sub_nc_u16saturate overflow/underflow cleanly, clamped MAD stays explicitly refused, and the 8clamp_noopops align with thegfx1036hardware data point cited above.emit/lower.rsafter merging ontoorigin/mainis522,266 B(+1,703 B, 2,022 B under the 512 KiB cap).v_subrev_nc_u16(lower.rs:6940, 7107, 7158) currently has no entry ingfx10/opcodes.rs:714-735;rustfmt --edition 2024 --checkflags 3 new lines invalu_tests.rs:2332, 2357, 2368; and a mixed non-zeroOPSELMAD test writing the upper destination half while checking a low-half sentinel would cover the remaining half-selection path.Local verification:
git merge-treeonto63bcca43clean,cargo run --locked -p xtask -- policy(191 files ok),cargo clippy -p victory-shader --all-targets(defaultand--all-features, 0 warnings),emit::lower::tests(33 passed), andemit::valu_tests(36 passed, 0 failed) withdxil.dllvalidation all pass on both RTX 5090 andVICTORY_WARP=1. Looks good to merge.@Dukestar69 on GitHub · 2026-11-26 19:32 UTC · original · approved these changes
LGTM — verified on RTX 5090 and
VICTORY_WARP=1(Windows / D3D12) againstorigin/main(63bcca4).v_mad_u16/v_mad_i16, handling unsigned 16-bit saturating clamp onv_add_nc_u16/v_sub_nc_u16/v_subrev_nc_u16, and treating no-opCLAMPon the remaining 8 16-bit integer ops (while safely rejecting unhandledv_mad_{u16,i16}clamp) is clean and matches RDNA2 semantics.cargo test -p victory-shader --lib emit::valu_tests::v_mad_16_and_int16_clamp_match_rdna2 -- --test-threads=1: passed on RTX 5090 andVICTORY_WARP=1.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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.