Skip to content

Shaders: 16-bit integer operations take their neg modifiers - #2374

Closed
Melow-code wants to merge 1 commit into
Marvisak:mainfrom
Melow-code:upstream-negmod
Closed

Melow-code wants to merge 1 commit into
Marvisak:mainfrom
Melow-code:upstream-negmod

Conversation

@Melow-code

@Melow-code Melow-code commented Nov 25, 2026 •

Copy link
Copy Markdown

The 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 in half_src. The f16 callers of half_src also flip the sign, so they negated their source twice. This PR caused the v_cmp_class_f16_matches_rdna2 failure 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-shader on the 2080 Ti: 246 passed, 3 failed. The same 3 fail on main (gather, two heap-buffer tests).
  • Same with VICTORY_WARP=1: 247 passed, 2 failed (gathers_at_a_level…, permutes_read_the_lane_named…, as in Tests: hardware-only shader checks skip on a machine without a GPU #1911). On the base commit, WARP also fails v_cmp_class_f16_matches_rdna2; this fix makes it pass.
  • New tests: 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 the v_add_nc_u16 lowering 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

Copy link
Copy Markdown
Author

CI failure is unrelated to this change: v_cmp_class_f16_matches_rdna2 fails on WARP at line 1568 (f16 class comparison). The same test passes on other PRs based on the same commit. Rerunning CI.

@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: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — 16-bit integer operations take their neg modifiers.

@welisonzika14-jpg

Copy link
Copy Markdown

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 674a77c merged onto main 940d32f:

v_cmp_class_f16_matches_rdna2 hardware same on WARP
main 940d32f passes 2/2 passes 2/2
this PR fails 2/2 fails 2/2

Same message as CI: v_cmp_class_f16(-hi(0x3c000000), 0x008) = 0, want 1. Every other test is the same as main here.

What I think happens: half_src is also used by the f16 float paths, and those apply neg themselves as a sign flip (half_fsrc, the v_cmp_class_f16 arm around line 7232, cvt_norm16). With the new two's-complement negate inside half_src, a negated f16 source is now negated twice. For the failing case: 0x3C00 (1.0) becomes 0xC400 after the integer negate, then 0x4400 (+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 operation check), not in half_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

Copy link
Copy Markdown

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 of half_src into 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:

test this PR PR + patch
v_cmp_class_f16_matches_rdna2 (hardware / WARP) fails / fails passes / passes
a_16bit_integer_with_a_neg_modifier_lowers (yours) passes passes
new a_16bit_integer_neg_modifier_negates_the_low_half (hardware / WARP) - passes / passes
cargo test -p victory-shader --lib 244 ok, 4 fail 246 ok, 3 fail (the 3 known NVIDIA ones, #1744)
cargo clippy -p victory-shader --all-targets -- -D warnings clean

The new test runs v_add_nc_u16 with 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)
diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs
index 2267af2..9b63b96 100644
--- a/crates/victory-shader/src/emit/lower.rs
+++ b/crates/victory-shader/src/emit/lower.rs
@@ -6201,15 +6201,7 @@ impl<'a> Lower<'a> {
             x
         };
         let m = self.s.m.i32(0xFFFF);
-        let x = self.s.m.bin(c, Bin::And, x, m);
-        let x = if v.neg >> k & 1 != 0 {
-            let z = self.s.m.i32(0);
-            let x = self.s.m.bin(c, Bin::Sub, z, x);
-            self.s.m.bin(c, Bin::And, x, m)
-        } else {
-            x
-        };
-        Ok(x)
+        Ok(self.s.m.bin(c, Bin::And, x, m))
     }
 
     /// An f16 source as an f32 (exact), with its abs/neg modifiers.
@@ -6473,8 +6465,8 @@ impl<'a> Lower<'a> {
             return self.write_half(inst, r, guard);
         }
         // 16-bit integers: computed on the low halves, the low 16 bits
-        // kept; the per-operand neg modifier is applied in `half_src`,
-        // the clamp, output modifier and abs are not lowered yet.
+        // kept; the per-operand neg modifier is applied below, the clamp,
+        // output modifier and abs are not lowered yet.
         if v.clamp || v.omod != 0 || v.abs != 0 {
             return Err("modifiers on a 16-bit integer operation");
         }
@@ -6499,6 +6491,16 @@ impl<'a> Lower<'a> {
             }
             _ => {
                 let (mut a, mut b) = (self.half_src(inst, 0)?, self.half_src(inst, 1)?);
+                // The VOP3 neg, as the console's two's complement on the low
+                // 16 bits (only here: the f16 paths flip the sign themselves).
+                let m16 = self.s.m.i32(0xFFFF);
+                for (k, x) in [(0, &mut a), (1, &mut b)] {
+                    if v.neg >> k & 1 != 0 {
+                        let z = self.s.m.i32(0);
+                        let n = self.s.m.bin(c, Bin::Sub, z, *x);
+                        *x = self.s.m.bin(c, Bin::And, n, m16);
+                    }
+                }
                 let k16 = self.s.m.i32(16);
                 let sext = |l: &mut Self, x: V| {
                     let t = l.s.m.bin(c, Bin::Shl, x, k16);
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index 3bf9d61..939766f 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -1747,3 +1747,35 @@ fn relative_moves_reach_only_what_their_clamped_index_can() {
         refused.err()
     );
 }
+
+#[test]
+fn a_16bit_integer_neg_modifier_negates_the_low_half() {
+    // v_mov_b32 v0, 0; v_add_nc_u16 v0, -v1, v2 or v0, v1, -v2 (VOP3 0x303,
+    // neg on src0 or src1): the operand is negated as a 16-bit integer.
+    let cases: &[(u16, u16, u16, u16)] = &[
+        // (v1, v2, -v1 + v2, v1 + -v2)
+        (1, 10, 9, 0xFFF7),
+        (0, 5, 5, 0xFFFB),
+        (0xFFFF, 2, 3, 0xFFFD),
+        (0x8000, 0, 0x8000, 0x8000),
+        (10, 3, 0xFFF9, 7),
+    ];
+    for (neg, what, pick) in [(1u32 << 29, "-v1 + v2", 0usize), (1 << 30, "v1 + -v2", 1)] {
+        let [w0, w1] = vop3(0x303, 0, vgpr(1), vgpr(2), 0);
+        let shader = build_valu_cs(&[vop1(0x001, 0, 128), w0, w1 | neg]);
+        let inputs: Vec<[u32; 4]> = cases
+            .iter()
+            .map(|c| [u32::from(c.0), u32::from(c.1), 0, 0])
+            .collect();
+        run_cases(&shader, &inputs, |backend, i, _, got| {
+            let want = if pick == 0 { cases[i].2 } else { cases[i].3 };
+            assert_eq!(
+                got & 0xFFFF,
+                u32::from(want),
+                "{backend}: v_add_nc_u16 {what} with v1={:#06x}, v2={:#06x}",
+                cases[i].0,
+                cases[i].1,
+            );
+        });
+    }
+}

Written with AI help (Claude/Kiro); the numbers are from runs on my machine.

The 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.
half_src does not apply it, because its f16 callers (half_fsrc,
v_cmp_class_f16, v_cvt_norm_*16_f16) flip the sign themselves: a neg in
both places negates the source twice. The clamp, the output modifier
and the abs stay refused.

welisonzika14-jpg found the double negation in the review, posted the
same fix and wrote a_16bit_integer_neg_modifier_negates_the_low_half.

Assisted-by: Claude Opus 4.6 (Claude Code): first version
Assisted-by: Claude Opus 5.5 (Claude Code, Windows): double-neg fix, tests, GPU and WARP runs
Assisted-by: Claude Opus 5.5 (Claude Code, lead): review
@Melow-code

Copy link
Copy Markdown
Author

You were right, thanks. My "unrelated" note above was wrong: this PR caused the v_cmp_class_f16 failure. The branch is now one commit with the neg in a separate half_isrc (same idea as your patch) and your GPU test, credited in the commit and the description.

@welisonzika14-jpg

Copy link
Copy Markdown

Thanks for taking it in. Confirmed on the new head 23abe80 (merged onto main 940d32f), RTX 5070, driver 621.24: v_cmp_class_f16_matches_rdna2 and a_16bit_integer_neg_modifier_negates_the_low_half both pass on the hardware device, and victory-shader is 247 ok with only the 3 known NVIDIA failures from #1744. Nothing new in victory-gpu.

Written with AI help (Claude/Kiro); the numbers are from runs on my machine.

Copy link
Copy Markdown
Owner

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 new Lower::half_isrc (crates/victory-shader/src/emit/lower.rs). The current head correctly keeps the f16 paths' own sign flip (the double negation that broke v_cmp_class_f16_matches_rdna2 is 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: check passing.


Generated by Claude Code

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

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:

  • 16-bit, RDNA2: feat(shader): accept neg/abs on 16-bit integer DPP and SDWA sources boykopovar/AnyPS5#19592 (gfx1036, 163 cases): DPP16/SDWA v_cvt_f16_u16/i16, the i16 exponent of v_ldexp_f16, VOPC SDWA v_cmp_lt_i16/u16. Neg flips bit 15, abs clears it, both set it.
  • 16-bit, RDNA4, one VOP3 form: a comment on that PR (Helldiverr, 2026-11-23, gfx1201) found the same rule for VOP3 v_ldexp_f16's i16 source.
  • 32-bit: AnyPS5#19577, #25964 and #25799 (gfx1036, #25964 also gfx1201) found neg flips bit 31 and abs clears it.

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_isrc gives other values, and the new test fixes them in place. For v_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, v5 is .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

Copy link
Copy Markdown

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 + v2 for (1, 10) becomes 0x800B, (0, 5) becomes 0x8005, (0x8000, 0) becomes 0, and v1 + -v2 for (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).

@Melow-code Melow-code closed this by deleting the head repository Nov 25, 2026
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.

5 participants