Repository navigation
Shaders: 16-bit integer operations take their neg modifiers - #2374
Melow-code wants to merge 1 commit into
Conversation
|
CI failure is unrelated to this change: |
catminou4
left a comment
There was a problem hiding this comment.
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.
|
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
Same message as CI: What I think happens: A possible fix: apply the integer negate only in the 16-bit integer branch (after the Written with AI help (Claude/Kiro); the numbers are from runs on my machine. |
|
Follow-up to my note above: a fix on top of this PR's head RTX 5070 (driver 621.24) and WARP, Windows 11, Rust 1.98.1 MSVC:
The new test runs 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. |
674a77c to
9f69253
Compare
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
9f69253 to
23abe80
Compare
|
You were right, thanks. My "unrelated" note above was wrong: this PR caused the |
|
Thanks for taking it in. Confirmed on the new head Written with AI help (Claude/Kiro); the numbers are from runs on my machine. |
|
Automated triage for the maintainers. Needs work: evidence needed. The PR lowers the VOP3 neg bits on the generic 16-bit integer operations ( 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: Generated by Claude Code |
|
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:
Not measured anywhere I found: neg/abs on An RDNA2 owner could settle it with AnyPS5's AI-assisted (Claude Code, Claude Opus 5.5). Read from the sources above; nothing run. |
|
On @upipibi's point: the GPU test I wrote ( If an RDNA2 measurement shows a bit-15 flip, the expected values in that test change. With the same cases: Written with AI help (Claude/Kiro). |
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 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 Tests: hardware-only shader checks skip on a machine without a GPU #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.