Shaders: 16-bit integer operations take their neg modifiers #2374
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!2374
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2374/head"
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?
@Melow-code on GitHub · 2026-11-25 12:13 UTC · original
upstream-negmod→main· +75 −3 · 2 files · 1 commits · closed without mergingThe generic 16-bit integer operations (
v_add_nc_u16,v_sub_nc_u16,v_mul_lo_u16, the shifts, the min/max pairs) refused the per-operand neg modifier, so any program with it stopped at "modifiers on a 16-bit integer operation".The neg is applied in a new
half_isrc, as a 16-bit two's complement. The first version applied it inhalf_src. The f16 callers ofhalf_srcalso flip the sign, so they negated their source twice. This PR caused thev_cmp_class_f16_matches_rdna2failure in CI, so that failure was not unrelated. @welisonzika14-jpg found it first and posted the same fix in the review; their GPU test is included. The clamp, the output modifier and the abs stay refused.Tested
Windows 11 Pro 26300, Ryzen 7 5800X3D, RTX 2080 Ti (driver 32.0.16.2140), 32 GB, rustc 1.100.0.
cargo test -p victory-shaderon the 2080 Ti: 246 passed, 3 failed. The same 3 fail onmain(gather, two heap-buffer tests).VICTORY_WARP=1: 247 passed, 2 failed (gathers_at_a_level…,permutes_read_the_lane_named…, as in #1911). On the base commit, WARP also failsv_cmp_class_f16_matches_rdna2; this fix makes it pass.an_f16_neg_modifier_negates_the_source_once(GPU),a_16bit_integer_neg_modifier_negates_the_low_half(GPU, from @welisonzika14-jpg, their runs: RTX 5070 and WARP pass) and thev_add_nc_u16lowering test.Assisted by Claude Code: Opus 4.6 wrote the first version, Opus 5.5 on the Windows PC fixed the double neg and ran the GPU tests, and the lead session (Opus 5.5) reviewed it.
@Melow-code on GitHub · 2026-11-25 12:22 UTC · original
CI failure is unrelated to this change:
v_cmp_class_f16_matches_rdna2fails on WARP at line 1568 (f16 class comparison). The same test passes on other PRs based on the same commit. Rerunning CI.@welisonzika14-jpg on GitHub · 2026-11-25 13:07 UTC · original
I think the CI failure is caused by this change, not a flake. RTX 5070 (driver 621.24) and WARP, Windows 11, Rust 1.98.1 MSVC, head
674a77cmerged ontomain940d32f:v_cmp_class_f16_matches_rdna2hardwaremain940d32fSame message as CI:
v_cmp_class_f16(-hi(0x3c000000), 0x008) = 0, want 1. Every other test is the same asmainhere.What I think happens:
half_srcis also used by the f16 float paths, and those applynegthemselves as a sign flip (half_fsrc, thev_cmp_class_f16arm around line 7232,cvt_norm16). With the new two's-complement negate insidehalf_src, a negated f16 source is now negated twice. For the failing case:0x3C00(1.0) becomes0xC400after the integer negate, then0x4400(+4.0) after the sign flip. +4.0 is not "-normal", so bit 3 (0x008) reads 0. That would affect any f16 float op with a neg modifier, not only this test.A possible fix: apply the integer negate only in the 16-bit integer branch (after the
modifiers on a 16-bit integer operationcheck), not inhalf_src. I have not written or tested that fix.Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
@welisonzika14-jpg on GitHub · 2026-11-25 13:32 UTC · original
Follow-up to my note above: a fix on top of this PR's head
674a77c, if it helps. It moves the 16-bit integer negate out ofhalf_srcinto the integer branch, so the f16 paths keep their own sign flip, and adds a GPU test for the negated operand (the readback the description says needs a Windows host).RTX 5070 (driver 621.24) and WARP, Windows 11, Rust 1.98.1 MSVC:
v_cmp_class_f16_matches_rdna2(hardware / WARP)a_16bit_integer_with_a_neg_modifier_lowers(yours)a_16bit_integer_neg_modifier_negates_the_low_half(hardware / WARP)cargo test -p victory-shader --libcargo clippy -p victory-shader --all-targets -- -D warningsThe new test runs
v_add_nc_u16with neg on src0 and on src1 and checks the low 16 bits (-1 + 10 = 9,1 + -10 = 0xFFF7,-0x8000 = 0x8000, ...). It assumes the two's-complement meaning from your description; I did not check that against console hardware.patch (git am-able file on request)
Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
@Melow-code on GitHub · 2026-11-25 13:39 UTC · original
You were right, thanks. My "unrelated" note above was wrong: this PR caused the
v_cmp_class_f16failure. The branch is now one commit with the neg in a separatehalf_isrc(same idea as your patch) and your GPU test, credited in the commit and the description.@welisonzika14-jpg on GitHub · 2026-11-25 14:13 UTC · original
Thanks for taking it in. Confirmed on the new head
23abe80(merged ontomain940d32f), RTX 5070, driver 621.24:v_cmp_class_f16_matches_rdna2anda_16bit_integer_neg_modifier_negates_the_low_halfboth pass on the hardware device, andvictory-shaderis 247 ok with only the 3 known NVIDIA failures from #1744. Nothing new invictory-gpu.Written with AI help (Claude/Kiro); the numbers are from runs on my machine.
@Marvisak on GitHub · 2026-11-25 14:53 UTC · original
Automated triage for the maintainers.
Needs work: evidence needed. The PR lowers the VOP3 neg bits on the generic 16-bit integer operations (
v_add_nc_u16, shifts, min/max, ...) as a 16-bit two's-complement negate, in a newLower::half_isrc(crates/victory-shader/src/emit/lower.rs). The current head correctly keeps the f16 paths' own sign flip (the double negation that brokev_cmp_class_f16_matches_rdna2is gone). But what the hardware does with neg bits on an integer operation is the open question. The public RDNA2 ISA describes the VOP3 NEG field as negating floating-point inputs, and LLVM's GFX10 assembler does not accept neg modifiers on these integer opcodes. Two's complement is a guess, and the GPU test only checks that guess on the host, not console behaviour.What would change the verdict: evidence for the semantics (console hardware output, the title's compiler emitting such an instruction together with the value it expects, or documentation), and the name of a program in the title that hits this refusal. Without that, keeping the refusal is safer than a possibly wrong value.
CI:
checkpassing.Generated by Claude Code
@upipibi on GitHub · 2026-11-25 16:35 UTC · original
On the triage's open question (what VOP3 neg/abs do to a 16-bit integer source): I found no measurement of the opcodes this PR lowers, and nothing from console hardware. What I found are measurements of other integer forms on PC AMD GPUs, and every one of them treats neg as a sign-bit flip, not a two's complement:
v_cvt_f16_u16/i16, the i16 exponent ofv_ldexp_f16, VOPC SDWAv_cmp_lt_i16/u16. Neg flips bit 15, abs clears it, both set it.v_ldexp_f16's i16 source.Not measured anywhere I found: neg/abs on
v_add_nc_u16,v_sub_nc_u16,v_mul_lo_u16,v_mad_u16/i16, the 16-bit shifts and min/max. If they follow the measured forms,half_isrcgives other values, and the new test fixes them in place. Forv_add_nc_u16 -v1 + v2: (1, 10) gives 9 under this PR and 0x800B under a bit-15 flip; (0, 5) gives 5 vs 0x8005; (0x8000, 0) gives 0x8000 vs 0.An RDNA2 owner could settle it with AnyPS5's
tools/hw-oracle. LLVM won't assemble neg on these opcodes, so encode it by hand:v_add_nc_u16 v10, -v4, v5is.long 0xD703000A, 0x20020B04(abs: word 0 | 0x100), with v4 = 0, 1, 0x7FFF, 0x8000, 0xFFFF and v5 = 10.AI-assisted (Claude Code, Claude Opus 5.5). Read from the sources above; nothing run.
@welisonzika14-jpg on GitHub · 2026-11-25 18:52 UTC · original
On @upipibi's point: the GPU test I wrote (
a_16bit_integer_neg_modifier_negates_the_low_half, now in this PR) checks the two's-complement meaning from the PR description, so it locks that meaning in. It was a check of the lowering, not of console hardware, and my RTX 5070 cannot tell which meaning RDNA2 uses.If an RDNA2 measurement shows a bit-15 flip, the expected values in that test change. With the same cases:
-v1 + v2for (1, 10) becomes 0x800B, (0, 5) becomes 0x8005, (0x8000, 0) becomes 0, andv1 + -v2for (1, 10) becomes 0x800B. I'm happy to update the test then, or it can be marked as following the description until someone measures it.Written with AI help (Claude/Kiro).
Pull request closed