Shaders/Lower: sign-extend v_mad_i64_i32 literal, guard clamp/omod, and split v_mad_u64_u32 carry compare #3082
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!3082
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3082"
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?
@RVZO6 on GitHub · 2026-11-26 06:57 UTC · original
RVZO6:rvzo/mad-i64-u64-fixes→main· +116 −4 · 2 files · 1 commits · openProblem
Three correctness issues in 64-bit integer multiply-add lowering (
crates/victory-shader/src/emit/lower.rs):v_mad_i64_i3232-bit literal insrc2is zero-extended instead of sign-extended:src64andsrc64_dwordscheckedinst.name().ends_with("_i64"), missingv_mad_i64_i32(which ends in"_i32") even though its 64-bit addendsrc2is signedi64(AMD RDNA2 ISA literal extension rules; Mesaaco_opcodes.pyv_mad_i64_i32src(U32, U32, I64)). A negative 32-bit literal such as-100(0xFFFF_FF9C) zero-extended to+4_294_967_196(hi = 0) instead of sign-extending to-100(hi = 0xFFFF_FFFF).v.clampandv.omodsilently ignored onv_mad_u64_u32andv_mad_i64_i32:Both opcodes bypass
write_iand callself.writedirectly, silently droppingv.clamp(a valid saturating modifier onVOP3b_I64_I1_I32_I32_I64:HasClamp = 1in LLVMVOP3Instructions.td) andv.omod.v_mad_u64_u32carry-out emits 64-biticmp ult i64:Line 6033 was the last remaining 64-bit integer ordering comparison in
lower.rs, which AMD's D3D12 shader compiler misorders as if low and high dwords were swapped (documented in #1599 /lower.rs:7958).Fix
inst.name() == "v_mad_i64_i32"alongsideends_with("_i64")for 32-bit literal sign-extension insrc64andsrc64_dwords.v.clamp || v.omod != 0withErr("integer output modifiers")inv_mad_u64_u32/v_mad_i64_i32, matchingwrite_i.src2viasrc64_dwords(keepingflags::INT64) and computev_mad_u64_u32carry-out via 32-bit unsigned dword comparisons:(hi <u add_hi) || (hi == add_hi && lo <u add_lo).Tests & Verification
emit::valu_tests::v_mad_i64_i32_and_v_mad_u64_u32_literal_modifiers_and_carryincrates/victory-shader/src/emit/valu_tests.rs:op 0x176 clamp=true omod=0 should be refused, got None).v_mad_i64_i32negative-literal cases (both low and high dwords), and all 6v_mad_u64_u32carry-out boundary cases pass on WARP SM 6.6 +dxil.dllvalidation (victory-shader: 304 passed, 0 failed; clippy default &--all-featuresclean;xtask policyclean).Claimed in #1633.
AI-assisted: T3 Code (Antigravity, gemini-4-neon).
Three correctness fixes for 64-bit integer multiply-add lowering in crates/victory-shader/src/emit/lower.rs: 1. Sign-extend 32-bit literals in v_mad_i64_i32 src2: src64 and src64_dwords checked inst.name().ends_with("_i64"), which missed v_mad_i64_i32 (ending in "_i32") even though its 64-bit addend src2 is signed i64. A negative 32-bit literal such as -100 (0xFFFF_FF9C) was zero-extended to +4_294_967_196 instead of sign-extended to -100. 2. Reject unmodeled clamp and omod modifiers on v_mad_u64_u32 and v_mad_i64_i32: Both opcodes bypass write_i and call self.write directly, silently dropping v.clamp (valid on VOP3b_I64_I1_I32_I32_I64 with HasClamp = 1 in LLVM's VOP3Instructions.td) and v.omod. Reject v.clamp || v.omod != 0 with Err("integer output modifiers"), matching write_i. 3. Compute v_mad_u64_u32 carry-out via 32-bit unsigned dword comparisons: Replace the last remaining 64-bit integer ordering compare in lower.rs (icmp ult i64 r, addend) with (hi <u add_hi) || (hi == add_hi && lo <u add_lo), avoiding AMD D3D12's swapped-dword 64-bit integer ordering bug (#1599).@oni-swr on GitHub · 2026-11-26 07:11 UTC · original
Independent Windows verification of #3082 at
079d8f99against exact parent63bcca43(carry-out ofv_mad_u64_u32only; the signed-literal part is covered by #2254/#2774 and the author's own WARP test, so I left it alone). Synthetic shaders, no game files, no physical GPU used.Adapter. Only
Host::new(true)(WARP) was used; no hardware path was run. Before any dispatch the test read the DXGI adapter behind the device:Microsoft Basic Render Driver, software flag set, vendor 0x1414, device 0x8c (d3d10warp.dll10.0.26100.9818). It skips if that is not confirmed; it did not skip.VICTORY_D3D12_WARP=1was set in the environment, but the shader test host does not read it (it readsVICTORY_WARP, and I passedtrueexplicitly), so the adapter evidence is the DXGI query above, not that variable.Validation. Windows SDK 10.0.26100.0
dxil.dll(1.8.2502.11), loaded explicitly and required (the sharedbuild_valu_cssilently skips validation if the DLL does not load). The carry, low-dword and high-dword shaders all validated.Test.
v_mad_u64_u32 v[6:7], s[12:13], v1, v2, v[3:4], three shaders (carry lane bit, result low dword, result high dword), compared with a hostu64::overflowing_addoracle ona*b + addend. 20,844 cases / 62,532 dispatches:Results (WARP).
079d8f9963bcca431*1 + 0xffffffffreports carry 1What this does and does not show. The patched lowering agrees with the integer oracle on WARP across all of these, including equal high dwords and full-width wrap, so the dword form of the comparison is correct. The oracle can fail: the mutated carry is caught. But the exact parent also passes on WARP, so this test cannot tell parent from patch; WARP does not reproduce the 64-bit
icmp ultmisordering the PR describes (#1599), and I make no claim about AMD's or any hardware compiler's behaviour. The motivation for the change needs an AMD adapter.Not covered: signed
v_mad_i64_i32, literals, clamp/omod refusal, any hardware GPU.AI use: Claude Code (Claude Sonnet 5.5) for the work; coordinated by Codex (gpt-6.2-sol). Coordination: #1633
@Dukestar69 on GitHub · 2026-11-26 07:09 UTC · original · approved these changes
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP +dxil.dll):cargo test -p victory-shader v_mad_ -- --test-threads=1(RTX 5090 & WARP): 1 passed, 0 failed on both (v_mad_i64_i32_and_v_mad_u64_u32_literal_modifiers_and_carry).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).
@Marvisak on GitHub · 2026-11-26 08:22 UTC · original
Automated triage for the maintainers.
Needs work. This overlaps two open PRs that already carry the triage merge-candidate label:
v_mad_i64_i32literal sign-extension inLower::src64/src64_dwordsis #2254, which cites RDNA 2 ISA §6.2.1.clamponv_mad_u64_u32/v_mad_i64_i32is in #171, which also reworks the carry-out of the same match arm.The part only this PR has is the carry-out of
v_mad_u64_u32computed on the two dwords instead oficmp ult i64. That is consistent with the documented AMD workaround for 64-bitv_cmpinlower.rs. The dword comparisonhi < add_hi || (hi == add_hi && lo < add_lo)is correct, and oni-swr's WARP oracle found no mismatches. Please narrow this PR to that carry change, and coordinate with #171 so that its signed carry (sum < prodon i64) gets the same dword form. A narrowed PR would be re-reviewed. CI: passing.Generated by Claude Code
@fuzeofficlal on GitHub · 2026-11-26 08:29 UTC · original · requested changes
The signed literal extension in
src64_dwords(crates/victory-shader/src/emit/lower.rs:6797-6805) and the split unsigned carry comparison (hi_lt || (hi_eq && lo_lt)) look right, and localvalu_tests+cargo clippy -p victory-shader --all-targets -- -D warningspass on Windows 11 (RTX 5090 + WARP).Two issues remain in the modified MAD lowering branch (
crates/victory-shader/src/emit/lower.rs:6014-6055):sdstlowering forv_mad_i64_i32: Bothv_mad_i64_i32andv_mad_u64_u32decode as VOP3B with an explicit scalar lane-mask destination (gfx10/fields.rs,ir.rs,structure.rs), yetemit/lower.rs:6038-6055only populateslane_bitswhen!signed. If a shader consumes the scalar destination ofv_mad_i64_i32(for example in a subsequentv_cndmask_b32), lowering fails with a missing lane-mask value. Per LLVM's AMDGPU lowering (AMDGPURegisterBankInfo::applyMappingMAD_64_32), signedsdstis bit 64 of the 65-bit mathematical sum (sign(P) ^ sign(A) ^ (R <u A)— note this differs from both unsigned carry and signedi64overflow). Please lowersdstforv_mad_i64_i32as well and add a test that consumessdst.clampvsomodrefusal rationale:emit/lower.rs:6014-6016rejects bothclampandomodtogether. Whileomodis float-only, LLVM's GFX10/GFX1030 tables allowclampon both MAD opcodes. Please document the unsupportedclamprejection separately fromomod(or clarify expected hardware behavior forclampon bothvdstandsdst).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.