shader: lower s_bcnt0_i32_b32/b64 and s_ff0_i32_b32/b64 #95

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

@⁠lcg007xd-c on GitHub · 2026-11-25 22:29 UTC · original
lcg007xd-c:shader-s-zero-bits → main · +104 −0 · 2 files · 1 commits · open

What is broken

The zero-bit counterparts of the supported s_bcnt1_*/s_ff1_* decode, but a program using one fails to translate with s_bcnt0_i32_b32 is not supported (or the matching name):

SOP1 opcode Instruction
0x0D s_bcnt0_i32_b32
0x0E s_bcnt0_i32_b64
0x11 s_ff0_i32_b32
0x12 s_ff0_i32_b64

They are part of the scalar gap the sweep through the CPU harness of #62 turned up (see #89).

Semantics (AMD's RDNA2 ISA, §12.2, opcodes 13, 14, 17, 18)

  • s_bcnt0: counts the zero bits of S0, and SCC = (D != 0).
  • s_ff0: gives the position of the first zero bit from the bottom, or −1 if there is none. It writes no SCC.

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

  • Approach: each is the supported s_bcnt1/s_ff1 lowering (DXIL Countbits/FirstbitLo) applied to the complement of S0.
  • 64-bit forms:
    • s_bcnt0_i32_b64 adds the two dwords' counts.
    • s_ff0_i32_b64 takes the low dword's position if it has a zero bit, else 32 plus the high dword's (−1 if neither has one), as s_ff1_i32_b64 does.
    • s_bcnt0_i32_b64 sets SCC itself, because the 64-bit forms return early.
  • 32-bit s_bcnt0: its SCC comes from the shared path.

Tests

  • emit::lower::tests::scalar_zero_bit_counts_translate (every host): all four decode, and a program that moves s12 to v0 and stores it translates. Without the lower.rs change it fails: s_bcnt0_i32_b32 [be8c0d00, 7e00020c]: GFX10 code at 0x24: s_bcnt0_i32_b32 is not supported.
  • valu_tests::scalar_zero_bit_counts_and_scans_match_rdna2 (Windows/GPU): four hand-computed cases per opcode. They include all ones (count 0, scan −1), a zero only in the high dword (scan 63), and all zeros.
  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I compared all four against Rust's count_zeros/trailing_zeros on 65 64-bit inputs, plus SCC for both s_bcnt0 widths. 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:29 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/95) `lcg007xd-c:shader-s-zero-bits` → `main` · +104 −0 · 2 files · 1 commits · open ## What is broken The zero-bit counterparts of the supported `s_bcnt1_*`/`s_ff1_*` decode, but a program using one fails to translate with `s_bcnt0_i32_b32 is not supported` (or the matching name): | SOP1 opcode | Instruction | |---|---| | 0x0D | `s_bcnt0_i32_b32` | | 0x0E | `s_bcnt0_i32_b64` | | 0x11 | `s_ff0_i32_b32` | | 0x12 | `s_ff0_i32_b64` | They are part of the scalar gap the sweep through the CPU harness of #62 turned up (see #89). ## Semantics (AMD's RDNA2 ISA, §12.2, opcodes 13, 14, 17, 18) - **`s_bcnt0`:** counts the zero bits of S0, and `SCC = (D != 0)`. - **`s_ff0`:** gives the position of the first zero bit from the bottom, or −1 if there is none. It writes no SCC. ## The change (`emit/lower.rs`, in `sop`) - **Approach:** each is the supported `s_bcnt1`/`s_ff1` lowering (DXIL `Countbits`/`FirstbitLo`) applied to the complement of S0. - **64-bit forms:** - `s_bcnt0_i32_b64` adds the two dwords' counts. - `s_ff0_i32_b64` takes the low dword's position if it has a zero bit, else 32 plus the high dword's (−1 if neither has one), as `s_ff1_i32_b64` does. - `s_bcnt0_i32_b64` sets SCC itself, because the 64-bit forms return early. - **32-bit `s_bcnt0`:** its SCC comes from the shared path. ## Tests - **`emit::lower::tests::scalar_zero_bit_counts_translate`** (every host): all four decode, and a program that moves `s12` to `v0` and **stores** it translates. Without the `lower.rs` change it fails: `s_bcnt0_i32_b32 [be8c0d00, 7e00020c]: GFX10 code at 0x24: s_bcnt0_i32_b32 is not supported`. - **`valu_tests::scalar_zero_bit_counts_and_scans_match_rdna2`** (Windows/GPU): four hand-computed cases per opcode. They include all ones (count 0, scan −1), a zero only in the high dword (scan 63), and all zeros. - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I compared all four against Rust's `count_zeros`/`trailing_zeros` on 65 64-bit inputs, plus SCC for both `s_bcnt0` widths. 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 0x0D, 0x0E, 0x11 and 0x12 decoded but failed to translate
("s_bcnt0_i32_b32 is not supported"). AMD's RDNA2 ISA: the number of
zero bits (SCC = result != 0) and the position of the first zero bit
from the bottom (-1 if there is none). They lower as the supported
s_bcnt1/s_ff1 forms of the complement, the 64-bit ones over both dwords.

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:51 +00:00
Author
Owner

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

I can take Windows GPU validation at exact head 59fe40f8b427d8fbf2ef51809f6e5b2e99bb64ca. I will run the existing translation and result tests on RTX 4060 Laptop and WARP with the SDK validator, then add a fixed-expectation SCC regression. It will check both incoming SCC values, count instructions updating SCC and find-first-zero instructions preserving it, with result readback as well. A temporary omission of the new 64-bit count SCC write will provide a negative control, followed by source restoration and fresh final runs. I will report exact counts and any test-only patch here. AI-assisted with Codex.

**@⁠lmouz823-lang** on GitHub · 2026-11-28 04:09 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/95#issuecomment-7233759845) I can take Windows GPU validation at exact head `59fe40f8b427d8fbf2ef51809f6e5b2e99bb64ca`. I will run the existing translation and result tests on RTX 4060 Laptop and WARP with the SDK validator, then add a fixed-expectation SCC regression. It will check both incoming SCC values, count instructions updating SCC and find-first-zero instructions preserving it, with result readback as well. A temporary omission of the new 64-bit count SCC write will provide a negative control, followed by source restoration and fresh final runs. I will report exact counts and any test-only patch here. AI-assisted with Codex.
Author
Owner

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

Native Windows validation completed for exact head 59fe40f8b427d8fbf2ef51809f6e5b2e99bb64ca on RTX 4060 Laptop and WARP.

The unchanged head's translation and GPU tests each execute exactly one test and pass. I added one GPU regression for the flags that the current result-only GPU test does not read. It uses five fixed input pairs, incoming SCC 0 and 1, and separate result/SCC readbacks for all four operations. 80 dispatches per backend, 160 total, pass with VICTORY_STRICT_TESTS=1 and no skips or ignored tests.

The fixed table checks that both s_bcnt0 widths overwrite SCC for zero and nonzero results, while both s_ff0 widths preserve SCC for results including 0 and -1. It also includes an all-zero 64-bit input, whose count is 64. Incoming flags are set with s_add_u32 s14, s2, 1, using s2=0 or u32::MAX, then read through s_cselect_b32. The reference is AMD's RDNA2 ISA, SOP1 opcodes 13, 14, 17 and 18, printed pages 112–113.

A negative control forces only s_bcnt0_i32_b64's SCC output to false, keeping its data result correct. The original GPU test still passes. The new test fails on hardware for input (0xfffffff7, 0), incoming SCC 0, count 33, reporting SCC 0 where the fixed table expects 1. After restoring the implementation and removing diagnostics, a fresh compilation and final runs of the new GPU test, original GPU test and translation test each pass. The final patch adds only the 72-line synthetic test.

The explicit SDK dxil.dll 1.8.2502.11 created IDxcValidator. Temporary success markers after validation and the container-hash assertion confirmed all eight new modules and all four original modules passed both checks. Those markers were removed before the final runs. Independent review verified the expectations, input seeding, encodings, counts, final diff and execution 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 (195 files), git diff --check and rustfmt on the added function pass. Native AMD/RDNA2 execution, game behavior and the full workspace suite remain unverified.

Reproduction uses an installed SDK validator and the 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_zero_bit_counts_translate',
    'emit::valu_tests::scalar_zero_bit_counts_and_scans_match_rdna2',
    'emit::valu_tests::scalar_zero_bit_operations_write_or_preserve_scc'
)
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.

72-line synthetic 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 193ad12..808f1c9 100644
--- a/crates/victory-shader/src/emit/valu_tests.rs
+++ b/crates/victory-shader/src/emit/valu_tests.rs
@@ -723,6 +723,78 @@ fn scalar_zero_bit_counts_and_scans_match_rdna2() {
     }
 }
 
+#[test]
+fn scalar_zero_bit_operations_write_or_preserve_scc() {
+    // RDNA2 ISA SOP1 opcodes 13, 14, 17 and 18 (printed pages 112-113).
+    // Each fixed row is [result, SCC with incoming 0, SCC with incoming 1].
+    // Counts overwrite SCC; scans preserve it regardless of their result.
+    let inputs = [
+        [u32::MAX, u32::MAX],
+        [0xFFFF_FFF7, 0],
+        [0, u32::MAX],
+        [u32::MAX, 0x7FFF_FFFF],
+        [0, 0],
+    ];
+    let checks: [(u16, &str, [[u32; 3]; 5]); 4] = [
+        (
+            0x0D,
+            "s_bcnt0_i32_b32",
+            [[0, 0, 0], [1, 1, 1], [32, 1, 1], [0, 0, 0], [32, 1, 1]],
+        ),
+        (
+            0x0E,
+            "s_bcnt0_i32_b64",
+            [[0, 0, 0], [33, 1, 1], [32, 1, 1], [1, 1, 1], [64, 1, 1]],
+        ),
+        (
+            0x11,
+            "s_ff0_i32_b32",
+            [
+                [u32::MAX, 0, 1],
+                [3, 0, 1],
+                [0, 0, 1],
+                [u32::MAX, 0, 1],
+                [0, 0, 1],
+            ],
+        ),
+        (
+            0x12,
+            "s_ff0_i32_b64",
+            [
+                [u32::MAX, 0, 1],
+                [3, 0, 1],
+                [0, 0, 1],
+                [63, 0, 1],
+                [0, 0, 1],
+            ],
+        ),
+    ];
+    // s_add_u32 s14, s2, 1 has carry 0 for s2=0, and carry 1 for s2=MAX.
+    let cases: Vec<_> = [0, u32::MAX]
+        .into_iter()
+        .flat_map(|seed| inputs.map(|[lo, hi]| [lo, hi, seed, 0]))
+        .collect();
+    for (op, name, expected) in checks {
+        for (read, label) in [(12, "result"), (13, "SCC")] {
+            let shader = build_valu_cs(&[
+                sop2(0x00, 14, 2, 0x81),
+                0xBE80_0000 | 12 << 16 | u32::from(op) << 8,
+                sop2(0x0A, 13, 0x81, 0x80), // SCC as 1 or 0 in s13
+                vop1(0x001, 0, sgpr(read)),
+            ]);
+            run_cases(&shader, &cases, |backend, i, input, got| {
+                let prior = usize::from(input[2] == u32::MAX);
+                let column = if read == 12 { 0 } else { 1 + prior };
+                let want = expected[i % inputs.len()][column];
+                assert_eq!(
+                    got, want,
+                    "{backend}: {name} {label}, prior SCC {prior}, input {input:x?}"
+                );
+            });
+        }
+    }
+}
+
 #[test]
 fn v_cvt_norm_i16_and_u16_f16_round_the_exact_product() {
     // Every 251st f16 pattern, plus the values where an f32 product would
**@⁠lmouz823-lang** on GitHub · 2026-11-28 04:22 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/95#issuecomment-7233978217) Native Windows validation completed for exact head `59fe40f8b427d8fbf2ef51809f6e5b2e99bb64ca` on RTX 4060 Laptop and WARP. The unchanged head's translation and GPU tests each execute exactly one test and pass. I added one GPU regression for the flags that the current result-only GPU test does not read. It uses five fixed input pairs, incoming SCC 0 and 1, and separate result/SCC readbacks for all four operations. **80 dispatches per backend, 160 total, pass** with `VICTORY_STRICT_TESTS=1` and no skips or ignored tests. The fixed table checks that both `s_bcnt0` widths overwrite SCC for zero and nonzero results, while both `s_ff0` widths preserve SCC for results including 0 and -1. It also includes an all-zero 64-bit input, whose count is 64. Incoming flags are set with `s_add_u32 s14, s2, 1`, using s2=0 or `u32::MAX`, then read through `s_cselect_b32`. The reference is [AMD's RDNA2 ISA, SOP1 opcodes 13, 14, 17 and 18, printed pages 112–113](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture). A negative control forces only `s_bcnt0_i32_b64`'s SCC output to false, keeping its data result correct. The original GPU test still passes. The new test fails on hardware for input `(0xfffffff7, 0)`, incoming SCC 0, count 33, reporting SCC 0 where the fixed table expects 1. After restoring the implementation and removing diagnostics, a fresh compilation and final runs of the new GPU test, original GPU test and translation test each pass. The final patch adds only the 72-line synthetic test. The explicit SDK `dxil.dll` 1.8.2502.11 created `IDxcValidator`. Temporary success markers after validation and the container-hash assertion confirmed all eight new modules and all four original modules passed both checks. Those markers were removed before the final runs. Independent review verified the expectations, input seeding, encodings, counts, final diff and execution 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 (195 files), `git diff --check` and rustfmt on the added function pass. Native AMD/RDNA2 execution, game behavior and the full workspace suite remain unverified. Reproduction uses an installed SDK validator and the 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_zero_bit_counts_translate', 'emit::valu_tests::scalar_zero_bit_counts_and_scans_match_rdna2', 'emit::valu_tests::scalar_zero_bit_operations_write_or_preserve_scc' ) 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>72-line synthetic 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 193ad12..808f1c9 100644 --- a/crates/victory-shader/src/emit/valu_tests.rs +++ b/crates/victory-shader/src/emit/valu_tests.rs @@ -723,6 +723,78 @@ fn scalar_zero_bit_counts_and_scans_match_rdna2() { } } +#[test] +fn scalar_zero_bit_operations_write_or_preserve_scc() { + // RDNA2 ISA SOP1 opcodes 13, 14, 17 and 18 (printed pages 112-113). + // Each fixed row is [result, SCC with incoming 0, SCC with incoming 1]. + // Counts overwrite SCC; scans preserve it regardless of their result. + let inputs = [ + [u32::MAX, u32::MAX], + [0xFFFF_FFF7, 0], + [0, u32::MAX], + [u32::MAX, 0x7FFF_FFFF], + [0, 0], + ]; + let checks: [(u16, &str, [[u32; 3]; 5]); 4] = [ + ( + 0x0D, + "s_bcnt0_i32_b32", + [[0, 0, 0], [1, 1, 1], [32, 1, 1], [0, 0, 0], [32, 1, 1]], + ), + ( + 0x0E, + "s_bcnt0_i32_b64", + [[0, 0, 0], [33, 1, 1], [32, 1, 1], [1, 1, 1], [64, 1, 1]], + ), + ( + 0x11, + "s_ff0_i32_b32", + [ + [u32::MAX, 0, 1], + [3, 0, 1], + [0, 0, 1], + [u32::MAX, 0, 1], + [0, 0, 1], + ], + ), + ( + 0x12, + "s_ff0_i32_b64", + [ + [u32::MAX, 0, 1], + [3, 0, 1], + [0, 0, 1], + [63, 0, 1], + [0, 0, 1], + ], + ), + ]; + // s_add_u32 s14, s2, 1 has carry 0 for s2=0, and carry 1 for s2=MAX. + let cases: Vec<_> = [0, u32::MAX] + .into_iter() + .flat_map(|seed| inputs.map(|[lo, hi]| [lo, hi, seed, 0])) + .collect(); + for (op, name, expected) in checks { + for (read, label) in [(12, "result"), (13, "SCC")] { + let shader = build_valu_cs(&[ + sop2(0x00, 14, 2, 0x81), + 0xBE80_0000 | 12 << 16 | u32::from(op) << 8, + sop2(0x0A, 13, 0x81, 0x80), // SCC as 1 or 0 in s13 + vop1(0x001, 0, sgpr(read)), + ]); + run_cases(&shader, &cases, |backend, i, input, got| { + let prior = usize::from(input[2] == u32::MAX); + let column = if read == 12 { 0 } else { 1 + prior }; + let want = expected[i % inputs.len()][column]; + assert_eq!( + got, want, + "{backend}: {name} {label}, prior SCC {prior}, input {input:x?}" + ); + }); + } + } +} + #[test] fn v_cvt_norm_i16_and_u16_f16_round_the_exact_product() { // Every 251st f16 pattern, plus the values where an f32 product would ``` </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/95/head:github-bot/gh-95'
git switch 'github-bot/gh-95'

Merge

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