Shaders: all 12 s_cmpk_* SOPK scalar comparisons have no hardware test #867

Closed
opened 2026-11-26 04:14:05 +00:00 by github-bot · 3 comments
Owner

@⁠kss306 on GitHub · 2026-11-24 12:59 UTC · original

All twelve s_cmpk_* SOPK scalar comparison instructions are implemented in crates/victory-shader/src/emit/lower.rs but have no hardware test using build_valu_cs and run_cases in crates/victory-shader/src/emit/valu_tests.rs.

Affected instructions

Instruction SOPK opcode Signed?
s_cmpk_eq_i32 0x003 signed
s_cmpk_lg_i32 0x004 signed
s_cmpk_gt_i32 0x005 signed
s_cmpk_ge_i32 0x006 signed
s_cmpk_lt_i32 0x007 signed
s_cmpk_le_i32 0x008 signed
s_cmpk_eq_u32 0x009 unsigned
s_cmpk_lg_u32 0x00a unsigned
s_cmpk_gt_u32 0x00b unsigned
s_cmpk_ge_u32 0x00c unsigned
s_cmpk_lt_u32 0x00d unsigned
s_cmpk_le_u32 0x00e unsigned

Implementation evidence

lower.rs, sop(), handles both s_cmp_* SOPC and s_cmpk_* SOPK in a shared branch:

if let Some(op) = name.strip_prefix("s_cmp_").or(name.strip_prefix("s_cmpk_")) {
    let (a, b) = if name.starts_with("s_cmpk_") {
        let b = if op.ends_with("_i32") {
            // sign-extend the 16-bit immediate to i32
            self.s.m.i32(i32::from(imm.unwrap_or(0) as i16) as u32)
        } else {
            // zero-extend for unsigned
            self.s.m.i32(u32::from(imm.unwrap_or(0)))
        };
        (src32(self, 0)?, b)
    } else {
        (src32(self, 0)?, src32(self, 1)?)
    };
    let p = match &op[..2] { "eq" => pred::EQ, "lg" => pred::NE, … };
    let r = self.s.m.cmp(c, p, a, b);
    self.set(scc, Val::Bit(r));
    return Ok(());
}

The SIMM16 sign-extension (i32::from(imm as i16)) only runs for s_cmpk_*i32 variants and is not shared with any other tested instruction.

Missing test

valu_tests.rs contains no call to build_valu_cs with any s_cmpk_* word. The SOPK encoding puts the opcode and source register in the high 16 bits and the signed 16-bit immediate in the low 16 bits:

SOPK word: 0xB000_0000 | (op << 23) | (ssrc << 16) | (simm16 & 0xFFFF)

For example, s_cmpk_gt_i32 s0, -1 = 0xB500_FFFF (op=0x5=s_cmpk_gt_i32 at SOPK position, ssrc=0, simm16=0xFFFF=-1).

Critical edge case

s_cmpk_gt_i32 s0, -1 checks whether s0 > -1 (i.e., s0 >= 0). If sign-extension were missing (treating 0xFFFF as +65535 instead of -1), all small positive values would compare incorrectly. This is the same class of sign-extension risk that was verified for s_bfe_i32 in bfe_signed_and_unsigned_match_rdna2.

Suggested test

Add fn s_cmpk_matches_s_cmp() to valu_tests.rs. The result is in SCC; reading it back requires v_cndmask_b32 from s6:

#[test]
fn s_cmpk_matches_s_cmp() {
    // Helper: build a 1-instruction SOPK shader that writes SCC, then read via v_cndmask.
    // SOPK encoding: upper 16 = 0xB000 | (op << 7) | ssrc; lower 16 = simm16.
    let sopk = |op: u32, ssrc: u32, simm16: i16| -> u32 {
        0xB000_0000 | (op << 23) | (ssrc << 16) | ((simm16 as u16) as u32)
    };
    let make = |word: u32| build_valu_cs(&[
        word,
        vop3(0x101, 0, 0x80, 0x81, sgpr(6))[0], // v_cndmask_b32 v0, 0, 1, s6
        vop3(0x101, 0, 0x80, 0x81, sgpr(6))[1],
    ]);

    // s_cmpk_eq_i32 s0, 5: s0 == 5 → 1, else 0
    let eq_i = make(sopk(0x3, 0, 5));
    // s_cmpk_gt_i32 s0, -1: s0 > -1 → 1 for s0=0, s0=1; 0 for s0=-1, s0=-2
    let gt_i = make(sopk(0x5, 0, -1));
    // s_cmpk_lt_u32 s0, 10: s0 < 10 (unsigned) → 1 for 0..9, 0 for 10..
    let lt_u = make(sopk(0xd, 0, 10));

    // Cases: [s0 value in inp[0]]
    run_cases(&eq_i, &[[5, 0, 0, 0], [4, 0, 0, 0], [0, 0, 0, 0]], |backend, i, inp, got| {
        let want = if inp[0] == 5 { 1u32 } else { 0 };
        assert_eq!(got, want, "{backend}: s_cmpk_eq_i32({}, 5) = {got}", inp[0] as i32);
    });
    run_cases(&gt_i, &[[0u32, 0, 0, 0], [1, 0, 0, 0], [(-1i32) as u32, 0, 0, 0], [(-2i32) as u32, 0, 0, 0]], |backend, i, inp, got| {
        let want = if (inp[0] as i32) > -1 { 1u32 } else { 0 };
        assert_eq!(got, want, "{backend}: s_cmpk_gt_i32({}, -1) = {got}", inp[0] as i32);
    });
    run_cases(&lt_u, &[[0, 0, 0, 0], [9, 0, 0, 0], [10, 0, 0, 0], [0xFFFF, 0, 0, 0]], |backend, i, inp, got| {
        let want = if inp[0] < 10 { 1u32 } else { 0 };
        assert_eq!(got, want, "{backend}: s_cmpk_lt_u32({}, 10) = {got}", inp[0]);
    });
}

The test skips gracefully when no SM 6.6 device is available and runs on WARP. No game data needed.

AI-assisted: found with Kiro (Claude) and independently confirmed by five separate analysis passes without cross-contamination; no hardware run, no game data.

**@⁠kss306** on GitHub · 2026-11-24 12:59 UTC · [original](https://github.com/Marvisak/VIctory/issues/867) All twelve `s_cmpk_*` SOPK scalar comparison instructions are implemented in `crates/victory-shader/src/emit/lower.rs` but have no hardware test using `build_valu_cs` and `run_cases` in `crates/victory-shader/src/emit/valu_tests.rs`. ## Affected instructions | Instruction | SOPK opcode | Signed? | |---|---|---| | `s_cmpk_eq_i32` | 0x003 | signed | | `s_cmpk_lg_i32` | 0x004 | signed | | `s_cmpk_gt_i32` | 0x005 | signed | | `s_cmpk_ge_i32` | 0x006 | signed | | `s_cmpk_lt_i32` | 0x007 | signed | | `s_cmpk_le_i32` | 0x008 | signed | | `s_cmpk_eq_u32` | 0x009 | unsigned | | `s_cmpk_lg_u32` | 0x00a | unsigned | | `s_cmpk_gt_u32` | 0x00b | unsigned | | `s_cmpk_ge_u32` | 0x00c | unsigned | | `s_cmpk_lt_u32` | 0x00d | unsigned | | `s_cmpk_le_u32` | 0x00e | unsigned | ## Implementation evidence `lower.rs`, `sop()`, handles both `s_cmp_*` SOPC and `s_cmpk_*` SOPK in a shared branch: ```rust if let Some(op) = name.strip_prefix("s_cmp_").or(name.strip_prefix("s_cmpk_")) { let (a, b) = if name.starts_with("s_cmpk_") { let b = if op.ends_with("_i32") { // sign-extend the 16-bit immediate to i32 self.s.m.i32(i32::from(imm.unwrap_or(0) as i16) as u32) } else { // zero-extend for unsigned self.s.m.i32(u32::from(imm.unwrap_or(0))) }; (src32(self, 0)?, b) } else { (src32(self, 0)?, src32(self, 1)?) }; let p = match &op[..2] { "eq" => pred::EQ, "lg" => pred::NE, … }; let r = self.s.m.cmp(c, p, a, b); self.set(scc, Val::Bit(r)); return Ok(()); } ``` The SIMM16 sign-extension (`i32::from(imm as i16)`) only runs for `s_cmpk_*i32` variants and is not shared with any other tested instruction. ## Missing test `valu_tests.rs` contains no call to `build_valu_cs` with any `s_cmpk_*` word. The SOPK encoding puts the opcode and source register in the high 16 bits and the signed 16-bit immediate in the low 16 bits: ``` SOPK word: 0xB000_0000 | (op << 23) | (ssrc << 16) | (simm16 & 0xFFFF) ``` For example, `s_cmpk_gt_i32 s0, -1` = `0xB500_FFFF` (op=0x5=s_cmpk_gt_i32 at SOPK position, ssrc=0, simm16=0xFFFF=-1). ## Critical edge case `s_cmpk_gt_i32 s0, -1` checks whether s0 > -1 (i.e., s0 >= 0). If sign-extension were missing (treating `0xFFFF` as `+65535` instead of `-1`), all small positive values would compare incorrectly. This is the same class of sign-extension risk that was verified for `s_bfe_i32` in `bfe_signed_and_unsigned_match_rdna2`. ## Suggested test Add `fn s_cmpk_matches_s_cmp()` to `valu_tests.rs`. The result is in SCC; reading it back requires `v_cndmask_b32` from `s6`: ```rust #[test] fn s_cmpk_matches_s_cmp() { // Helper: build a 1-instruction SOPK shader that writes SCC, then read via v_cndmask. // SOPK encoding: upper 16 = 0xB000 | (op << 7) | ssrc; lower 16 = simm16. let sopk = |op: u32, ssrc: u32, simm16: i16| -> u32 { 0xB000_0000 | (op << 23) | (ssrc << 16) | ((simm16 as u16) as u32) }; let make = |word: u32| build_valu_cs(&[ word, vop3(0x101, 0, 0x80, 0x81, sgpr(6))[0], // v_cndmask_b32 v0, 0, 1, s6 vop3(0x101, 0, 0x80, 0x81, sgpr(6))[1], ]); // s_cmpk_eq_i32 s0, 5: s0 == 5 → 1, else 0 let eq_i = make(sopk(0x3, 0, 5)); // s_cmpk_gt_i32 s0, -1: s0 > -1 → 1 for s0=0, s0=1; 0 for s0=-1, s0=-2 let gt_i = make(sopk(0x5, 0, -1)); // s_cmpk_lt_u32 s0, 10: s0 < 10 (unsigned) → 1 for 0..9, 0 for 10.. let lt_u = make(sopk(0xd, 0, 10)); // Cases: [s0 value in inp[0]] run_cases(&eq_i, &[[5, 0, 0, 0], [4, 0, 0, 0], [0, 0, 0, 0]], |backend, i, inp, got| { let want = if inp[0] == 5 { 1u32 } else { 0 }; assert_eq!(got, want, "{backend}: s_cmpk_eq_i32({}, 5) = {got}", inp[0] as i32); }); run_cases(&gt_i, &[[0u32, 0, 0, 0], [1, 0, 0, 0], [(-1i32) as u32, 0, 0, 0], [(-2i32) as u32, 0, 0, 0]], |backend, i, inp, got| { let want = if (inp[0] as i32) > -1 { 1u32 } else { 0 }; assert_eq!(got, want, "{backend}: s_cmpk_gt_i32({}, -1) = {got}", inp[0] as i32); }); run_cases(&lt_u, &[[0, 0, 0, 0], [9, 0, 0, 0], [10, 0, 0, 0], [0xFFFF, 0, 0, 0]], |backend, i, inp, got| { let want = if inp[0] < 10 { 1u32 } else { 0 }; assert_eq!(got, want, "{backend}: s_cmpk_lt_u32({}, 10) = {got}", inp[0]); }); } ``` The test skips gracefully when no SM 6.6 device is available and runs on WARP. No game data needed. **AI-assisted:** found with Kiro (Claude) and independently confirmed by five separate analysis passes without cross-contamination; no hardware run, no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:05 +00:00
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 13:06 UTC · original

Daniel added the hardware test on ffe310e. It uses all 12 GFX1030 assembler-checked SOPK comparison encodings and immediately consumes each SCC result with s_cselect_b32, so the readback observes the comparison itself. One correction to the example: s_cmpk_gt_i32 s0, -1 encodes as 0xb280ffff; 0xb500ffff encodes s_cmpk_lg_u32 s0, 0xffff.

All 72 results (six inputs per instruction) matched on RTX 4090 under Wine/vkd3d-proton 3.2. Using the same immediate bits for −1 and 65,535 exercises the signed and unsigned paths, with inputs below, at, and above each comparison point. Wine's WARP enumeration returned the same hardware adapter. The commit adds only the test. Patch on main

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 13:06 UTC · [original](https://github.com/Marvisak/VIctory/issues/867#issuecomment-7132349287) Daniel added the hardware test on `ffe310e`. It uses all 12 GFX1030 assembler-checked SOPK comparison encodings and immediately consumes each SCC result with `s_cselect_b32`, so the readback observes the comparison itself. One correction to the example: `s_cmpk_gt_i32 s0, -1` encodes as `0xb280ffff`; `0xb500ffff` encodes `s_cmpk_lg_u32 s0, 0xffff`. All 72 results (six inputs per instruction) matched on RTX 4090 under Wine/vkd3d-proton 3.2. Using the same immediate bits for −1 and 65,535 exercises the signed and unsigned paths, with inputs below, at, and above each comparison point. Wine's WARP enumeration returned the same hardware adapter. The commit adds only the test. [Patch on main](https://github.com/MichaelSmithAI/VIctory/commit/777177d1eb830e6a5095c77791cdce312bf9b2f5)
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 13:09 UTC · original

I replayed 777177d unchanged on native Windows. All 72 comparisons passed on the default hardware adapter and all 72 passed again on software WARP. Neither backend was skipped. This adds a native D3D12 and software result to the Wine run above.

**@⁠fuzeofficlal** on GitHub · 2026-11-24 13:09 UTC · [original](https://github.com/Marvisak/VIctory/issues/867#issuecomment-7132427686) I replayed `777177d` unchanged on native Windows. All 72 comparisons passed on the default hardware adapter and all 72 passed again on software WARP. Neither backend was skipped. This adds a native D3D12 and software result to the Wine run above.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:55 UTC · original

Automated triage for the maintainers.

Correct: s_cmpk_* is lowered in sop in crates/victory-shader/src/emit/lower.rs (with the 16-bit immediate sign-extended for the _i32 forms), and emit/valu_tests.rs on main has no test for it. Open PR #1046 ("Shaders: hardware test for the s_cmpk_* scalar comparisons") adds that test, and #1875 covers the SOPK forms of the carry chain, so review can continue there. The test in the comments (777177d, all 72 cases on Wine, native hardware and WARP) can be compared against #1046. Note the encoding correction in the comments: 0xb500ffff is s_cmpk_lg_u32, not s_cmpk_gt_i32.

Left for the maintainers.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:55 UTC · [original](https://github.com/Marvisak/VIctory/issues/867#issuecomment-7155305754) <!-- victory-issue-triage v1 verdict=tracked seen=2026-11-24T13:09:24Z at=2026-11-25T06:55:46Z --> Automated triage for the maintainers. Correct: `s_cmpk_*` is lowered in `sop` in `crates/victory-shader/src/emit/lower.rs` (with the 16-bit immediate sign-extended for the `_i32` forms), and `emit/valu_tests.rs` on `main` has no test for it. Open PR #1046 ("Shaders: hardware test for the s_cmpk_* scalar comparisons") adds that test, and #1875 covers the SOPK forms of the carry chain, so review can continue there. The test in the comments (`777177d`, all 72 cases on Wine, native hardware and WARP) can be compared against #1046. Note the encoding correction in the comments: `0xb500ffff` is `s_cmpk_lg_u32`, not `s_cmpk_gt_i32`. Left for the maintainers. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#867
No description provided.