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

Open
github-bot wants to merge 1 commit 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
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.