shader: lower s_cmov_b32, s_cmov_b64 and s_cmovk_i32 #92

Open
github-bot wants to merge 0 commits from github-bot/gh-92 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:26 UTC · original
lcg007xd-c:shader-s-cmov → main · +95 −0 · 2 files · 1 commits · open

What is broken

The scalar conditional moves decode, but a program using one fails to translate with s_cmov_b32 is not supported (or the matching name):

Instruction Encoding
s_cmov_b32 SOP1 0x05
s_cmov_b64 SOP1 0x06
s_cmovk_i32 SOPK 0x02

The rest of the pipeline already handles them:

  • the IR reads SCC and the old destination for them (ir.rs, "These change (or may keep) the destination's old value");
  • resource tracking follows a descriptor picked by s_cmov (resource.rs).

Only the lowering was missing. I found the gap with a scalar sweep through the CPU harness of #62, the same sweep as #89 and #91.

Semantics

AMD's RDNA2 ISA (§12.2 opcodes 5–6, §12.4 opcode 2): if (SCC) D = S0. For s_cmovk_i32 the value is signext(SIMM16). Otherwise D keeps its value. They write no SCC.

The change (emit/lower.rs, in sop)

  • 32-bit forms: a select on SCC between the new value and the destination's old value.
  • s_cmov_b64: the same select for each dword, beside s_cselect_b64.

Tests

  • emit::lower::tests::scalar_conditional_moves_translate (every host): after s_mov into s[12:13] and s_cmp_eq_u32 s0, s1, each conditional move decodes, and a program that moves s12 to v0 and stores it translates. Without the lower.rs change it fails: s_cmov_b32 [be8c0502]: GFX10 code at 0x30: s_cmov_b32 is not supported.
  • valu_tests::scalar_conditional_moves_keep_the_destination_when_scc_is_clear (Windows/GPU): each form with SCC set and clear, reading both dwords for s_cmov_b64. The sign-extended 0x8000 checks s_cmovk_i32.
  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli: four input sets per form, SCC both ways. All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:26 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/92) `lcg007xd-c:shader-s-cmov` → `main` · +95 −0 · 2 files · 1 commits · open ## What is broken The scalar conditional moves decode, but a program using one fails to translate with `s_cmov_b32 is not supported` (or the matching name): | Instruction | Encoding | |---|---| | `s_cmov_b32` | SOP1 0x05 | | `s_cmov_b64` | SOP1 0x06 | | `s_cmovk_i32` | SOPK 0x02 | The rest of the pipeline already handles them: - the IR reads SCC and the old destination for them (`ir.rs`, "These change (or may keep) the destination's old value"); - resource tracking follows a descriptor picked by `s_cmov` (`resource.rs`). Only the lowering was missing. I found the gap with a scalar sweep through the CPU harness of #62, the same sweep as #89 and #91. ## Semantics AMD's RDNA2 ISA (§12.2 opcodes 5–6, §12.4 opcode 2): `if (SCC) D = S0`. For `s_cmovk_i32` the value is `signext(SIMM16)`. Otherwise D keeps its value. They write no SCC. ## The change (`emit/lower.rs`, in `sop`) - **32-bit forms:** a select on SCC between the new value and the destination's old value. - **`s_cmov_b64`:** the same select for each dword, beside `s_cselect_b64`. ## Tests - **`emit::lower::tests::scalar_conditional_moves_translate`** (every host): after `s_mov` into `s[12:13]` and `s_cmp_eq_u32 s0, s1`, each conditional move decodes, and a program that moves `s12` to `v0` and **stores** it translates. Without the `lower.rs` change it fails: `s_cmov_b32 [be8c0502]: GFX10 code at 0x30: s_cmov_b32 is not supported`. - **`valu_tests::scalar_conditional_moves_keep_the_destination_when_scc_is_clear`** (Windows/GPU): each form with SCC set and clear, reading both dwords for `s_cmov_b64`. The sign-extended `0x8000` checks `s_cmovk_i32`. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`: four input sets per form, SCC both ways. All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
SOP1 0x05/0x06 and SOPK 0x02 decoded but failed to translate
("s_cmov_b32 is not supported"). AMD's RDNA2 ISA: if SCC, D = S0 (for
s_cmovk_i32 the sign-extended 16-bit immediate); otherwise D keeps its
value. The IR already reads SCC and the old destination for them, and
resource tracking already follows descriptors through s_cmov; only the
lowering was missing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:44 +00:00
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 06:11 UTC · original

I can take native Windows GPU validation at exact head 91f81aea7b4ac5319958a26dd72a5ad91aa23d62. I will run both existing exact regressions and add focused fixed-expectation coverage for SCC preservation, destination retention and conditional-immediate boundaries. I will verify a targeted negative control and restored-source recompilation on RTX 4060 Laptop and WARP with the SDK validator, then report counts, skipped cases and the remaining native AMD/game scope with a small patch. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 06:11 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/92#issuecomment-7235140569) I can take native Windows GPU validation at exact head `91f81aea7b4ac5319958a26dd72a5ad91aa23d62`. I will run both existing exact regressions and add focused fixed-expectation coverage for SCC preservation, destination retention and conditional-immediate boundaries. I will verify a targeted negative control and restored-source recompilation on RTX 4060 Laptop and WARP with the SDK validator, then report counts, skipped cases and the remaining native AMD/game scope with a small patch. AI-assisted with Codex.
Author
Owner

@⁠lmouz823-lang on GitHub · 2026-11-28 06:21 UTC · original

Native Windows GPU validation completed for exact head 91f81aea7b4ac5319958a26dd72a5ad91aa23d62. The original translation and GPU tests each execute exactly one test and pass. The original GPU test runs 16 dispatches across RTX 4060 Laptop and WARP.

I added one 67-line fixed-expectation regression. Two input pairs include zero and nonzero values in each old and replacement dword. The test reads the conditional result and outgoing SCC separately for s_cmov_b32, both halves of s_cmov_b64, and s_cmovk_i32 with immediates 0, 0x7fff, 0x8000 and 0xffff. Its two SCC configurations use a constant carry seed of 0 or 1. The existing test supplies an additional uniform comparison-based condition.

The new test has 13 operation/readback configurations times two incoming flags, giving 26 shaders. Each shader runs two inputs on two backends, for 104 passing dispatches. It checks retention of the old destination when SCC is clear, replacement when SCC is set, sign extension of the immediate, and preservation of SCC. Expectations follow AMD's RDNA2 ISA, Table 12, SOP1 opcodes 5/6 and SOPK opcode 2.

A temporary IR mutation incorrectly marks s_cmov_b32 as an SCC writer. The existing result-only GPU test still passes. The new test fails on hardware with incoming SCC 0 and old low dword 0x89abcdef, observing outgoing SCC 1 where the fixed expectation is 0. That exact test runs one test and exits 101. After restoring the IR bytes and removing diagnostics, I confirmed fresh compilation before the final three exact selections passed again. Each has zero failed or ignored tests and no skip diagnostics.

The explicitly selected SDK dxil.dll 1.8.2502.11 successfully created IDxcValidator. Temporary markers after load and after DXIL validation plus the container-hash assertion confirmed all 26 new modules and all four original modules passed those checks. The markers were removed for the final runs. Independent review checked the fixed values, encodings, registers, counts and restoration evidence.

Environment was Windows 11 Pro build 26200, i9-14900HX, NVIDIA 610.88 and Rust 1.98.1 MSVC, release. Package Clippy with --all-targets -- -D warnings, asset policy, new-function rustfmt and git diff --check pass. Whole-file rustfmt exits 1 on both original and final source, with the same 17 existing formatting hunks after file/line headers are removed. Native AMD execution, game behavior, other platforms and the remaining shader/workspace suites remain unverified. Coverage is limited to the listed synthetic patterns and incoming flags.

Reproduction uses an installed SDK validator and the project's Windows build prerequisites.

$env:VICTORY_STRICT_TESTS = '1'
$env:VICTORY_DXIL = 'C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll'
$tests = @(
    'emit::lower::tests::scalar_conditional_moves_translate',
    'emit::valu_tests::scalar_conditional_moves_keep_the_destination_when_scc_is_clear',
    'emit::valu_tests::scalar_conditional_moves_preserve_scc_and_extend_immediates'
)
foreach ($test in $tests) {
    cargo test --release --locked -p victory-shader --lib $test -- --exact --nocapture --test-threads=1
}

AI assistance from Codex covered the test, execution, independent review and this report.

Focused test patch against this PR head
diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs
index a1c4697..e07c7b3 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -638,6 +638,73 @@ fn v_fract_f32_never_returns_one() {
     });
 }
 
+#[test]
+fn scalar_conditional_moves_preserve_scc_and_extend_immediates() {
+    // Old destination s[0:1], replacement s[2:3], including zero in each half.
+    let cases = [
+        [0, 0x89AB_CDEF, 0xFEDC_BA98, 0],
+        [0x89AB_CDEF, 0, 0, 0xFEDC_BA98],
+    ];
+    // Fixed result pairs when SCC is set; only the 64-bit form reads high.
+    let moves = [
+        ("s_cmov_b32", 0xBE8C_0502, false, [[0xFEDC_BA98, 0], [0, 0]]),
+        (
+            "s_cmov_b64",
+            0xBE8C_0602,
+            true,
+            [[0xFEDC_BA98, 0], [0, 0xFEDC_BA98]],
+        ),
+        ("s_cmovk_i32 0", 0xB10C_0000, false, [[0, 0], [0, 0]]),
+        (
+            "s_cmovk_i32 0x7fff",
+            0xB10C_7FFF,
+            false,
+            [[0x7FFF, 0], [0x7FFF, 0]],
+        ),
+        (
+            "s_cmovk_i32 0x8000",
+            0xB10C_8000,
+            false,
+            [[0xFFFF_8000, 0], [0xFFFF_8000, 0]],
+        ),
+        (
+            "s_cmovk_i32 0xffff",
+            0xB10C_FFFF,
+            false,
+            [[u32::MAX, 0], [u32::MAX, 0]],
+        ),
+    ];
+    for (name, mov, wide, expected) in moves {
+        let reads: &[u8] = if wide { &[12, 13, 15] } else { &[12, 15] };
+        for prior in [false, true] {
+            for &read in reads {
+                let seed = if prior { 0xC1 } else { 0x80 };
+                let shader = build_valu_cs(&[
+                    0xBE8C_0400,                // s_mov_b64 s[12:13], s[0:1]
+                    sop2(0x00, 14, seed, 0x81), // -1 + 1 carries, 0 + 1 does not
+                    mov,
+                    sop2(0x0A, 15, 0x81, 0x80), // read SCC separately from the result
+                    vop1(0x001, 0, sgpr(read)),
+                ]);
+                run_cases(&shader, &cases, |backend, i, input, got| {
+                    let half = usize::from(read == 13);
+                    let want = if read == 15 {
+                        u32::from(prior)
+                    } else if prior {
+                        expected[i][half]
+                    } else {
+                        input[half]
+                    };
+                    assert_eq!(
+                        got, want,
+                        "{backend}: {name}, s{read}, prior SCC {prior}, input {input:x?}"
+                    );
+                });
+            }
+        }
+    }
+}
+
 #[test]
 fn scalar_conditional_moves_keep_the_destination_when_scc_is_clear() {
     // s_mov_b32 s12, s3; s_mov_b32 s13, s2; s_cmp_eq_u32 s0, s1; then the
**@⁠lmouz823-lang** on GitHub · 2026-11-28 06:21 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/92#issuecomment-7235244792) Native Windows GPU validation completed for exact head `91f81aea7b4ac5319958a26dd72a5ad91aa23d62`. The original translation and GPU tests each execute exactly one test and pass. The original GPU test runs 16 dispatches across RTX 4060 Laptop and WARP. I added one 67-line fixed-expectation regression. Two input pairs include zero and nonzero values in each old and replacement dword. The test reads the conditional result and outgoing SCC separately for `s_cmov_b32`, both halves of `s_cmov_b64`, and `s_cmovk_i32` with immediates `0`, `0x7fff`, `0x8000` and `0xffff`. Its two SCC configurations use a constant carry seed of 0 or 1. The existing test supplies an additional uniform comparison-based condition. The new test has 13 operation/readback configurations times two incoming flags, giving 26 shaders. Each shader runs two inputs on two backends, for **104 passing dispatches**. It checks retention of the old destination when SCC is clear, replacement when SCC is set, sign extension of the immediate, and preservation of SCC. Expectations follow [AMD's RDNA2 ISA, Table 12, SOP1 opcodes 5/6 and SOPK opcode 2](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture). A temporary IR mutation incorrectly marks `s_cmov_b32` as an SCC writer. The existing result-only GPU test still passes. The new test fails on hardware with incoming SCC 0 and old low dword `0x89abcdef`, observing outgoing SCC 1 where the fixed expectation is 0. That exact test runs one test and exits 101. After restoring the IR bytes and removing diagnostics, I confirmed fresh compilation before the final three exact selections passed again. Each has zero failed or ignored tests and no skip diagnostics. The explicitly selected SDK `dxil.dll` 1.8.2502.11 successfully created `IDxcValidator`. Temporary markers after load and after DXIL validation plus the container-hash assertion confirmed all 26 new modules and all four original modules passed those checks. The markers were removed for the final runs. Independent review checked the fixed values, encodings, registers, counts and restoration evidence. Environment was Windows 11 Pro build 26200, i9-14900HX, NVIDIA 610.88 and Rust 1.98.1 MSVC, release. Package Clippy with `--all-targets -- -D warnings`, asset policy, new-function rustfmt and `git diff --check` pass. Whole-file rustfmt exits 1 on both original and final source, with the same 17 existing formatting hunks after file/line headers are removed. Native AMD execution, game behavior, other platforms and the remaining shader/workspace suites remain unverified. Coverage is limited to the listed synthetic patterns and incoming flags. Reproduction uses an installed SDK validator and the project's Windows build prerequisites. ```powershell $env:VICTORY_STRICT_TESTS = '1' $env:VICTORY_DXIL = 'C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll' $tests = @( 'emit::lower::tests::scalar_conditional_moves_translate', 'emit::valu_tests::scalar_conditional_moves_keep_the_destination_when_scc_is_clear', 'emit::valu_tests::scalar_conditional_moves_preserve_scc_and_extend_immediates' ) foreach ($test in $tests) { cargo test --release --locked -p victory-shader --lib $test -- --exact --nocapture --test-threads=1 } ``` AI assistance from Codex covered the test, execution, independent review and this report. <details> <summary>Focused test patch against this PR head</summary> ```diff diff --git a/crates/victory-shader/src/emit/valu_tests.rs b/crates/victory-shader/src/emit/valu_tests.rs index a1c4697..e07c7b3 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -638,6 +638,73 @@ fn v_fract_f32_never_returns_one() { }); } +#[test] +fn scalar_conditional_moves_preserve_scc_and_extend_immediates() { + // Old destination s[0:1], replacement s[2:3], including zero in each half. + let cases = [ + [0, 0x89AB_CDEF, 0xFEDC_BA98, 0], + [0x89AB_CDEF, 0, 0, 0xFEDC_BA98], + ]; + // Fixed result pairs when SCC is set; only the 64-bit form reads high. + let moves = [ + ("s_cmov_b32", 0xBE8C_0502, false, [[0xFEDC_BA98, 0], [0, 0]]), + ( + "s_cmov_b64", + 0xBE8C_0602, + true, + [[0xFEDC_BA98, 0], [0, 0xFEDC_BA98]], + ), + ("s_cmovk_i32 0", 0xB10C_0000, false, [[0, 0], [0, 0]]), + ( + "s_cmovk_i32 0x7fff", + 0xB10C_7FFF, + false, + [[0x7FFF, 0], [0x7FFF, 0]], + ), + ( + "s_cmovk_i32 0x8000", + 0xB10C_8000, + false, + [[0xFFFF_8000, 0], [0xFFFF_8000, 0]], + ), + ( + "s_cmovk_i32 0xffff", + 0xB10C_FFFF, + false, + [[u32::MAX, 0], [u32::MAX, 0]], + ), + ]; + for (name, mov, wide, expected) in moves { + let reads: &[u8] = if wide { &[12, 13, 15] } else { &[12, 15] }; + for prior in [false, true] { + for &read in reads { + let seed = if prior { 0xC1 } else { 0x80 }; + let shader = build_valu_cs(&[ + 0xBE8C_0400, // s_mov_b64 s[12:13], s[0:1] + sop2(0x00, 14, seed, 0x81), // -1 + 1 carries, 0 + 1 does not + mov, + sop2(0x0A, 15, 0x81, 0x80), // read SCC separately from the result + vop1(0x001, 0, sgpr(read)), + ]); + run_cases(&shader, &cases, |backend, i, input, got| { + let half = usize::from(read == 13); + let want = if read == 15 { + u32::from(prior) + } else if prior { + expected[i][half] + } else { + input[half] + }; + assert_eq!( + got, want, + "{backend}: {name}, s{read}, prior SCC {prior}, input {input:x?}" + ); + }); + } + } + } +} + #[test] fn scalar_conditional_moves_keep_the_destination_when_scc_is_clear() { // s_mov_b32 s12, s3; s_mov_b32 s13, s2; s_cmp_eq_u32 s0, s1; then the ``` </details>
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/92/head:github-bot/gh-92'
git switch 'github-bot/gh-92'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-92'
git switch 'github-bot/gh-92'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-92'
git switch 'github-bot/gh-92'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-92'
git switch 'main'
git merge --squash 'github-bot/gh-92'
git switch 'main'
git merge --ff-only 'github-bot/gh-92'
git switch 'main'
git merge 'github-bot/gh-92'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.