Shaders: lower s_bcnt0_i32_b32/_b64, s_ff0_i32_b32/_b64 and s_flbit_i32_b64/_i64 #148

Open
github-bot wants to merge 1 commit from github-bot/gh-148 into main AGit
Owner

@⁠Rareuncommon on GitHub · 2026-11-24 00:58 UTC · original
Rareuncommon:shaders/scalar-zero-counts-bit-scans → main · +122 −0 · 1 files · 1 commits · open

Six scalar instructions that the emitter refuses by name now translate. One file, crates/victory-shader/src/emit/lower.rs: 91 lines of lowering and a 31-line unit test (122 added, none removed).

What is in it

  • s_bcnt0_i32_b32 / s_bcnt0_i32_b64: the count of clear bits, as the set bits of the complement. SCC = result != 0, as ir::writes_scc already expects for bcnt0.
  • s_ff0_i32_b32 / s_ff0_i32_b64: the lowest clear bit, -1 for none, as s_ff1 of the complement. No SCC.
  • s_flbit_i32_b64 / s_flbit_i32_i64: the first bit from the top of the pair (for the signed form, the first that differs from the sign), counted from bit 63, -1 for none. The high dword answers unless it has no such bit; then it is all sign or zero, and the low dword is searched for a bit that differs from it.

They follow the forms already there (s_bcnt1_i32_b64, s_ff1_i32_b64, s_flbit_i32_b32) and use the same DXIL bit operations (31, 32, 33, 34). Plain data only: a lane mask as the source is still refused, as before.

How I found them

A local probe that encodes every SOP1, SOP2, VOP1, VOP2 and VOP3 opcode of gfx10/opcodes.rs into a one-instruction compute program and runs it through ir::build → structure → lower: 219 of 364 lowered before this change, 225 after. The probe is not in this PR. I cannot say how many of the title's programs use these six; I have no game files.

Tested

macOS 27 arm64, Rust 1.98.1, debug, no GPU and no game files:

  • cargo test -p victory-core -p victory-formats -p victory-shader: 8, 20 (3 ignored) and 57 passed, 0 failed. The new test, scalar_zero_counts_and_bit_scans_lower, checks that each of the six lowers, and that the SCC of the two zero counts reaches a branch.
  • cargo fmt -p victory-shader -- --check: clean. cargo clippy -p victory-shader --all-targets: no new warnings. cargo xtask policy: 151 files ok.

Checked on hardware by others (see the comments)

  • @⁠Dathinel: head a038004 merged onto d2a4bae, 240 UAV readbacks on an RTX 3050 Laptop GPU and 240 on WARP, all matching a CPU model of the ISA; main refused all six.
  • @⁠Lyrnc: the six instructions on RDNA2 silicon (gfx1036), 130 cases each, all matching, SCC included.
  • @⁠DaviTostes: cargo test -p victory-shader on Arch Linux, 57 passed.

Not tested (by me)

  • The values. The value tests (valu_tests, diff_tests) are Windows-only and need a GPU, which I do not have, so nothing here has executed the emitted DXIL. The arithmetic is by reading the existing forms and the ISA rules; a run on a GPU by someone who has one would be welcome.
  • The Windows-only crates (victory-gpu, victory-hle, victory-loader) were not built.
  • victory-pe's four own-image tests fail on macOS on main too (they read the test binary as a PE); not touched here.

No game data: the test is seven hand-written instruction words per case.

AI-assisted: yes (Claude Code). I am not the first to ask whether that is welcome here (#22); say so if it is not and I will close this.

🤖 Generated with Claude Code

**@⁠Rareuncommon** on GitHub · 2026-11-24 00:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/148) `Rareuncommon:shaders/scalar-zero-counts-bit-scans` → `main` · +122 −0 · 1 files · 1 commits · open Six scalar instructions that the emitter refuses by name now translate. One file, `crates/victory-shader/src/emit/lower.rs`: 91 lines of lowering and a 31-line unit test (122 added, none removed). **What is in it** - `s_bcnt0_i32_b32` / `s_bcnt0_i32_b64`: the count of clear bits, as the set bits of the complement. SCC = result != 0, as `ir::writes_scc` already expects for `bcnt0`. - `s_ff0_i32_b32` / `s_ff0_i32_b64`: the lowest clear bit, -1 for none, as `s_ff1` of the complement. No SCC. - `s_flbit_i32_b64` / `s_flbit_i32_i64`: the first bit from the top of the pair (for the signed form, the first that differs from the sign), counted from bit 63, -1 for none. The high dword answers unless it has no such bit; then it is all sign or zero, and the low dword is searched for a bit that differs from it. They follow the forms already there (`s_bcnt1_i32_b64`, `s_ff1_i32_b64`, `s_flbit_i32_b32`) and use the same DXIL bit operations (31, 32, 33, 34). Plain data only: a lane mask as the source is still refused, as before. **How I found them** A local probe that encodes every SOP1, SOP2, VOP1, VOP2 and VOP3 opcode of `gfx10/opcodes.rs` into a one-instruction compute program and runs it through `ir::build` → `structure` → `lower`: 219 of 364 lowered before this change, 225 after. The probe is not in this PR. I cannot say how many of the title's programs use these six; I have no game files. **Tested** macOS 27 arm64, Rust 1.98.1, debug, **no GPU and no game files**: - `cargo test -p victory-core -p victory-formats -p victory-shader`: 8, 20 (3 ignored) and 57 passed, 0 failed. The new test, `scalar_zero_counts_and_bit_scans_lower`, checks that each of the six lowers, and that the SCC of the two zero counts reaches a branch. - `cargo fmt -p victory-shader -- --check`: clean. `cargo clippy -p victory-shader --all-targets`: no new warnings. `cargo xtask policy`: 151 files ok. **Checked on hardware by others (see the comments)** - @⁠Dathinel: head `a038004` merged onto `d2a4bae`, 240 UAV readbacks on an RTX 3050 Laptop GPU and 240 on WARP, all matching a CPU model of the ISA; `main` refused all six. - @⁠Lyrnc: the six instructions on RDNA2 silicon (gfx1036), 130 cases each, all matching, SCC included. - @⁠DaviTostes: `cargo test -p victory-shader` on Arch Linux, 57 passed. **Not tested** (by me) - The values. The value tests (`valu_tests`, `diff_tests`) are Windows-only and need a GPU, which I do not have, so nothing here has executed the emitted DXIL. The arithmetic is by reading the existing forms and the ISA rules; a run on a GPU by someone who has one would be welcome. - The Windows-only crates (`victory-gpu`, `victory-hle`, `victory-loader`) were not built. - `victory-pe`'s four own-image tests fail on macOS on `main` too (they read the test binary as a PE); not touched here. No game data: the test is seven hand-written instruction words per case. AI-assisted: yes (Claude Code). I am not the first to ask whether that is welcome here (#22); say so if it is not and I will close this. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Six scalar instructions the emitter refused by name:

- s_bcnt0_i32_b32/_b64: the count of clear bits, as the set bits of the complement; SCC = result != 0.
- s_ff0_i32_b32/_b64: the lowest clear bit (-1 for none), as s_ff1 of the complement.
- s_flbit_i32_b64, s_flbit_i32_i64: the first bit from the top of the pair (for the signed form, the first
  that differs from the sign), counted from bit 63, -1 for none. The high dword answers unless it has no such
  bit; then the low dword is searched for a bit that differs from it.

On plain data only; a lane mask as the source is still refused, as before.

AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:13 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:30 UTC · original

Tested a038004 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 57 passed, 0 failed (2 test binaries), including the {adds} test{'s' if adds != 1 else ''} this PR adds.
  • Scope note: #165 and #163 also lower s_bcnt0/s_ff0; three PRs now cover this.
  • The full workspace still needs #112 for the victory-gpu E0425.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/148#issuecomment-7119173171) Tested `a038004` on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test -p victory-shader`: **57 passed, 0 failed** (2 test binaries), including the {adds} test{'s' if adds != 1 else ''} this PR adds. - Scope note: **#165** and **#163** also lower `s_bcnt0`/`s_ff0`; three PRs now cover this. - The full workspace still needs #112 for the `victory-gpu` E0425. No game data used.
Author
Owner

@⁠Dathinel on GitHub · 2026-11-24 05:48 UTC · original

I ran the six instructions on Windows 11 build 26200, with your head a038004 merged onto d2a4bae1 (test merge 77725d6f). All 240 UAV readbacks matched on an RTX 3050 Laptop GPU, and all 240 matched on WARP. The device used for the hardware dispatches reported NVIDIA 10de:25a2, driver 32.0.16.1062.

For each opcode, the synthetic shader checked ten operands with both initial SCC states, returning either the instruction result or SCC. This covered all-zero and all-one inputs, a first zero in the upper dword, and signed versus unsigned FLBIT. BCNT0 set SCC from its result. FF0 and FLBIT preserved incoming SCC. Unmodified main refused all six instructions at lowering.

To reproduce, cherry-pick the test-only commit onto the test merge, leave VICTORY_WARP unset, and run:

cargo test --frozen -p victory-shader --features host --lib vic11_ -- --nocapture --test-threads=1

The reference was a CPU model of the ISA. This run used synthetic inputs. Codex prepared the fixture and Claude reviewed the method and its limits.

**@⁠Dathinel** on GitHub · 2026-11-24 05:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/148#issuecomment-7123298419) I ran the six instructions on Windows 11 build 26200, with your head `a038004` merged onto `d2a4bae1` (test merge `77725d6f`). **All 240 UAV readbacks matched on an RTX 3050 Laptop GPU, and all 240 matched on WARP.** The device used for the hardware dispatches reported NVIDIA `10de:25a2`, driver `32.0.16.1062`. For each opcode, the synthetic shader checked ten operands with both initial SCC states, returning either the instruction result or SCC. This covered all-zero and all-one inputs, a first zero in the upper dword, and signed versus unsigned FLBIT. BCNT0 set SCC from its result. FF0 and FLBIT preserved incoming SCC. Unmodified `main` refused all six instructions at lowering. To reproduce, cherry-pick the [test-only commit](https://github.com/Dathinel/VIctory/commit/b635b13ca7ccbdb0ce6f1a297ebdc69f584f0b72) onto the test merge, leave `VICTORY_WARP` unset, and run: ```sh cargo test --frozen -p victory-shader --features host --lib vic11_ -- --nocapture --test-threads=1 ``` The reference was a CPU model of the ISA. This run used synthetic inputs. Codex prepared the fixture and Claude reviewed the method and its limits.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:22 UTC · original

RDNA2 hardware check of the six instructions: all match.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 130 cases: low dwords from edge values and random, high dwords 0, 0xffffffff or random. SCC was read right after each instruction.

  • s_bcnt0_i32_b32 / _b64: clear-bit count, SCC = result != 0: 130/130 each.
  • s_ff0_i32_b32 / _b64: lowest clear bit, -1 for none: 130/130 each, SCC unchanged.
  • s_flbit_i32_b64: first set bit counted from bit 63, -1 for none: 130/130.
  • s_flbit_i32_i64: first bit that differs from the sign, from bit 63, -1 for none (all zeros and all ones): 130/130, SCC unchanged.

(#165 covers four of these and matches too.)

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/148#issuecomment-7123871437) RDNA2 hardware check of the six instructions: **all match.** **Setup.** AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 130 cases: low dwords from edge values and random, high dwords `0`, `0xffffffff` or random. SCC was read right after each instruction. - `s_bcnt0_i32_b32` / `_b64`: clear-bit count, SCC = result != 0: 130/130 each. - `s_ff0_i32_b32` / `_b64`: lowest clear bit, `-1` for none: 130/130 each, SCC unchanged. - `s_flbit_i32_b64`: first set bit counted from bit 63, `-1` for none: 130/130. - `s_flbit_i32_i64`: first bit that differs from the sign, from bit 63, `-1` for none (all zeros and all ones): 130/130, SCC unchanged. (#165 covers four of these and matches too.) AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 17:22 UTC · original

Merged onto main @ ffe310eb, this PR (a0380049) no longer compiles its tests. crates/victory-shader/src/emit/lower.rs:7435 calls the lowers(&words) test helper. Since 25b0f888 it is lowers(words, rsrc2); main's tests pass 0.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

**@⁠alexdeliu** on GitHub · 2026-11-24 17:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/148#issuecomment-7139108265) Merged onto `main` @ `ffe310eb`, this PR (`a0380049`) no longer compiles its tests. `crates/victory-shader/src/emit/lower.rs:7435` calls the `lowers(&words)` test helper. Since 25b0f888 it is `lowers(words, rsrc2)`; main's tests pass `0`. <sub>Checked with CI's steps (both clippy runs with `-D warnings`, `cargo test --workspace`) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87</sub>
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:26 UTC · original · edited 2026-11-25 01:15 UTC

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/148#issuecomment-7148505484) · edited 2026-11-25 01:15 UTC <!-- victory-triage v1 head=a0380049aacd0a0f04ac854faf41c9ded8c83a51 verdict=needs-rebase at=2026-11-25T01:15:05Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/lower.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 10:34 UTC · original

I rebased the six existing scalar operations onto 940d32f in e4b0c11 with the original Git author intact. The conflict was between adjacent tests in lower.rs. Both tests are present, and the existing #148 test now passes the rsrc2 argument to lowers.

43069c3 is a separate test commit. The same synthetic fixture was rejected at s_bcnt0_i32_b32 on 940d32f. With the rebase, it passed 48 WARP dispatch cases covering all six operations, including zero-result SCC and the boundary between the two dwords. The VALU suite passed 29 tests with DXIL validation. Shader Clippy and the staged asset policy also passed.

The commits are on codex2/rebase-scalar-bitops-148 if you want to take the rebase. If you fold the test into another commit, please retain Co-authored-by: andrewpozdnakov7-jpg <[email protected]>. Claim

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 10:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/148#issuecomment-7159888019) I rebased the six existing scalar operations onto `940d32f` in [`e4b0c11`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/e4b0c11bafbcd68b927e648449533aaa40f0fce5) with the original Git author intact. The conflict was between adjacent tests in `lower.rs`. Both tests are present, and the existing #148 test now passes the `rsrc2` argument to `lowers`. [`43069c3`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/43069c3a50a9534d5f5263fddda5642e905c6a07) is a separate test commit. The same synthetic fixture was rejected at `s_bcnt0_i32_b32` on `940d32f`. With the rebase, it passed 48 WARP dispatch cases covering all six operations, including zero-result `SCC` and the boundary between the two dwords. The VALU suite passed 29 tests with DXIL validation. Shader Clippy and the staged asset policy also passed. The commits are on [`codex2/rebase-scalar-bitops-148`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/codex2/rebase-scalar-bitops-148) if you want to take the rebase. If you fold the test into another commit, please retain `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`. [Claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633)
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-148'
git switch 'github-bot/gh-148'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-148'
git switch 'github-bot/gh-148'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-148'
git switch 'main'
git merge --squash 'github-bot/gh-148'
git switch 'main'
git merge --ff-only 'github-bot/gh-148'
git switch 'main'
git merge 'github-bot/gh-148'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!148
No description provided.