Shaders: v_mad_i64_i32 zero-extends a negative literal addend #2253
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#2253
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@BastienRodz on GitHub · 2026-11-25 08:59 UTC · original
What happens
v_mad_i64_i32with a 32-bit literal as its 64-bit addend (src2) zero-extends the literal.Lower::src64sign-extends a literal only for operations whose name ends in_i64(added in #447):v_mad_i64_i32ends in_i32, so its addend takes the zero-extending arm. Withsrc2 = -100(0xffffff9c),0 * 0 + src2gives0x00000000_ffffff9cinstead of-100, and10 * 10 + src2gives a high dword of 1 instead of 0.#447 noted
v_mad_i64_i32was handled in #163, which was closed without merging.Expected
RDNA 2 ISA, §6.2.1 "Literal Expansion to 64 bits": "Literal constants are 32-bits, but they can be used as sources which normally require 64-bit data. They are expanded to 64 bits following these rules: … 64-bit signed integer: sign extended to 64 bits". §12.12, opcode 375
V_MAD_I64_I32: "{vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64." So the literal insrc2is sign-extended;v_mad_u64_u32(S2.u64) keeps zero-extending.Where
crates/victory-shader/src/emit/lower.rs,Lower::src64.AI-assisted (Gemini 4 Neon found it, Claude Code reviewed and verified it); synthetic test only, no game data.
@Dathinel on GitHub · 2026-11-25 09:20 UTC · original
Compared #2254 (
5a8a792) and #2261 (440da6e) against their common parent940d32f. Their entirelower.rsmatches after removing comment-only lines. Either production fix covers this issue.I ran #2261's two synthetic regressions on Windows. Restoring only the parent's
lower.rsmade both fail: the DXIL check missed the signed literal, and the hardware returned high dword 0 for0 * 0 + (-100)(expected 0xffffffff). Restoring the fix passed both. Reproduce withcargo test --locked -p victory-shader --lib mad_64_bit_addend -- --nocapture.For choosing test coverage: #2254 also checks a negative product and unsigned carry across bit 32. #2261 adds a GPU-independent DXIL check on Windows when dxc is available. These tests can be selected independently of the identical production change.
AI-assisted comparison and local verification.
@studiostephe on GitHub · 2026-11-25 10:21 UTC · original
Lower::src64sign-extends a 32-bit literal only for operations whose name ends in_i64(added in #447).v_mad_i64_i32ends in_i32, so its 64-bit addend (src2) takes the zero-extending arm. Withsrc2 = -100(0xffffff9c),0 * 0 + src2gives0x00000000_ffffff9cinstead of-100(0xffffffff_ffffff9c), and the high dword of the result is 1 instead of 0.RDNA 2 ISA §6.2.1 "Literal Expansion to 64 bits": "64-bit signed integer: sign extended to 64 bits." §12.12, opcode 375
V_MAD_I64_I32: "{vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64." The addend is a signed 64-bit operand; its literal sign-extends.v_mad_u64_u32(opcode 374,S2.u64) keeps zero-extending.Where:
crates/victory-shader/src/emit/lower.rs,Lower::src64(line 6162 in current main):Fix: also sign-extend for
v_mad_i64_i32:Test:
valu_tests.rs— a Windows-only hardware/WARP test (same harness as the existingsigned_64_bit_operands_sign_extend_their_literaltest, which covers the_i64names). The shader runsv_mad_i64_i32 v[0:1], s0, s0, -100(VOP3 0x177, literal src2) withs0 = 0, moves the high dword to v0, and the harness stores it. Patched: high dword =0xFFFFFFFF(sign-extended −100). Unpatched: high dword =0(zero-extended0xffffff9c).Verified:
cargo test -p victory-shader --locked— 145 passed, 0 failed, 2 ignored (native, Mac; the new test is#[cfg(all(test, windows))]and compiles for the Windows target).cargo check -p victory-shader --profile test --target x86_64-pc-windows-msvc— clean.rustfmt --edition 2024 --check— clean on the changed lines.valu_tests.Not covered:
v_mad_u64_u32is unaffected (its addend is unsigned; zero-extension is correct).Full diff:
qwen3.8_27b running locally on an M1 Mac
shout out to Digital Debris Los Angeles
@Marvisak on GitHub · 2026-11-25 18:47 UTC · original
Automated triage for the maintainers.
Confirmed on main. In
crates/victory-shader/src/emit/lower.rs,Lower::src64sign-extends a literal only when the name ends in_i64. Thev_mad_u64_u32 | v_mad_i64_i32arm takes its addend fromsrc64(inst, 2), so the addend ofv_mad_i64_i32is zero-extended.Two open PRs carry the fix:
#2261 was closed. The diff posted in the comment above is the same change as #2254, so no separate patch is needed.
Generated by Claude Code
@Lyrnc on GitHub · 2026-11-26 18:52 UTC · original
Hardware confirmation on gfx1036 (RDNA2), and a check of #2254 against it.
Setup. The Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, AMD driver 32.0.21042.62, Windows 11). A HIP kernel (assembled by the driver's amd_comgr) ran
v_mad_i64_i32 v[40:41], null, v4, v5, <src2>andv_mad_u64_u32with the same operands, over 76 rows of signed and unsigned edge values and random words. The same words went throughbuild_valu_cson that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch. PC RDNA2 silicon, not the console's GPU. No game data.gfx1036, all 76 rows of each:
src20 * 0 + src2, high dwordv_mad_i64_i32, literal0xffffff9c(-100)0xffffffffv_mad_i64_i32, literal0x800000000xffffffffv_mad_u64_u32, literal0xffffff9c0x00000000v_mad_i64_i32, inline-1-10xffffffffv_mad_u64_u32, inline-1-1(all 64 bits set)0xffffffffSo the literal follows the operand's type, as §6.2.1 says. An inline constant is the same 64-bit value for both opcodes.
Translator (high dword, rows that differ of 76; identical on the Radeon and the 5080):
main63bcca45a8a792)v_mad_i64_i32, literal -100v_mad_i64_i32, literal0x80000000v_mad_u64_u32, literal -100-1, both opcodesv_mad_i64_i32, literal -100, low dword#2254 ran on a local merge with PRs that touch other instructions; it doesn't conflict there.
AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.