Shaders: lower s_bcnt0_i32_b32/b64 and s_ff0_i32_b32/b64 #165

Closed
n-romanovich wants to merge 3 commits from upstream-pr/165 into main
n-romanovich commented 2026-11-24 01:02:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-24T01:02:13Z.


Implements DXIL lowering for the zero-bit counting and find-first-zero scalar ALU instructions in victory-shader:

  • s_bcnt0_i32_b32: bitwise inverts src0 and counts set bits via DXIL unaryBits.i32 (31), updating SCC (result != 0).
  • s_ff0_i32_b32: bitwise inverts src0 and finds the lowest set bit via DXIL unaryBits.i32 (32, returning -1 when all bits are 1).
  • s_bcnt0_i32_b64, s_ff0_i32_b64: shares the 64-bit split-dword lowering paths with s_bcnt1_i32_b64 and s_ff1_i32_b64 over inverted dwords, without requiring DXIL INT64.

Testing

  • Added emit::tests::scalar_bitcount_and_first_zero_execute_on_gpu in crates/victory-shader/src/emit/mod.rs covering 32-bit and 64-bit inputs, inline -1, high-dword hits, and SCC updates.
  • Validated emitted DXIL with dxil.dll and executed on D3D12 (NVIDIA GeForce RTX 5060 Ti).
  • cargo fmt -p victory-shader -- --check: passed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: passed (0 warnings).
  • cargo xtask policy: passed (asset policy: 151 files ok).
  • cargo test -p victory-shader: 70 passed, 0 failed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6572193201 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165). Originally posted by [n-romanovich](https://github.com/n-romanovich) on 2026-11-24T01:02:13Z. --- Implements DXIL lowering for the zero-bit counting and find-first-zero scalar ALU instructions in `victory-shader`: - `s_bcnt0_i32_b32`: bitwise inverts `src0` and counts set bits via DXIL `unaryBits.i32` (`31`), updating `SCC` (`result != 0`). - `s_ff0_i32_b32`: bitwise inverts `src0` and finds the lowest set bit via DXIL `unaryBits.i32` (`32`, returning `-1` when all bits are `1`). - `s_bcnt0_i32_b64`, `s_ff0_i32_b64`: shares the 64-bit split-dword lowering paths with `s_bcnt1_i32_b64` and `s_ff1_i32_b64` over inverted dwords, without requiring DXIL `INT64`. ### Testing - Added `emit::tests::scalar_bitcount_and_first_zero_execute_on_gpu` in `crates/victory-shader/src/emit/mod.rs` covering 32-bit and 64-bit inputs, inline `-1`, high-dword hits, and `SCC` updates. - Validated emitted DXIL with `dxil.dll` and executed on D3D12 (NVIDIA GeForce RTX 5060 Ti). - `cargo fmt -p victory-shader -- --check`: passed. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: passed (0 warnings). - `cargo xtask policy`: passed (`asset policy: 151 files ok`). - `cargo test -p victory-shader`: 70 passed, 0 failed.
DaviTostes commented 2026-11-24 06:54:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:18:48Z.


Tested 60e32ce on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 57 passed, 0 failed (2 test binaries), including the 1 test this PR adds.
  • The full workspace still needs #112 for the victory-gpu E0425.

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118976015 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7118976015). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:18:48Z. --- Tested `60e32ce` on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `cargo test -p victory-shader`: **57 passed, 0 failed** (2 test binaries), including the 1 test this PR adds. - The full workspace still needs #112 for the `victory-gpu` E0425. No game data used.
Sunkiller-qc commented 2026-11-24 06:54:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Sunkiller-qc on 2026-11-24T01:29:38Z.


This duplicates #148 (opened earlier, which also adds s_flbit_i32_b64/_i64), and #163 covers the same opcodes too. The lowering here looks correct, and the GPU test is a plus. It might be worth agreeing with #148's author on which one goes in, e.g. #148's lowering plus this test.

Static review, not built or run.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119153363 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7119153363). Originally posted by [Sunkiller-qc](https://github.com/Sunkiller-qc) on 2026-11-24T01:29:38Z. --- This duplicates #148 (opened earlier, which also adds `s_flbit_i32_b64/_i64`), and #163 covers the same opcodes too. The lowering here looks correct, and the GPU test is a plus. It might be worth agreeing with #148's author on which one goes in, e.g. #148's lowering plus this test. Static review, not built or run.
Lyrnc commented 2026-11-24 06:56:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:22:47Z.


RDNA2 hardware check of the four 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 (including 0 and 0xffffffff) 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 when all bits are 1, SCC unchanged: 130/130 each.

(#148 covers the same four plus s_flbit_i32_b64/_i64, also matching.)

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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123872025 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7123872025). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T06:22:47Z. --- RDNA2 hardware check of the four 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 (including `0` and `0xffffffff`) 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` when all bits are 1, SCC unchanged: 130/130 each. (#148 covers the same four plus `s_flbit_i32_b64/_i64`, also matching.) 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.
DeerSilver commented 2026-11-24 11:04:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DeerSilver on 2026-11-24T10:51:07Z.


Tested head 50d8a98 merged onto main ffe310e on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only.

Merged onto current main, the test build fails. 6638098 ("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument to resource::track:

error[E0061]: this function takes 4 arguments but 3 arguments were supplied
  --> crates/victory-shader/src/emit/mod.rs (in the new test)
   |     resource::track(&p, &native.layout, native.stage)
   |     argument #4 of type `Option<&structure::Structured>` is missing

There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break cargo test on main. The call this PR adds needs the fourth argument. main's own tests pass Some(&st); None keeps the old three-argument behaviour.

With , None added locally to that one call, everything passes:

  • The PR's GPU test ran on the RTX 5060 (DXGI adapter 0 on this laptop; no "no SM 6.6 device" skip) and passed:
    • scalar_bitcount_and_first_zero_execute_on_gpu
  • cargo test --workspace --locked: 343 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored on main.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129329535 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7129329535). Originally posted by [DeerSilver](https://github.com/DeerSilver) on 2026-11-24T10:51:07Z. --- Tested head `50d8a98` merged onto `main` `ffe310e` on **NVIDIA GeForce RTX 5060 Laptop GPU** (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only. **Merged onto current `main`, the test build fails.** `6638098` ("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument to `resource::track`: ``` error[E0061]: this function takes 4 arguments but 3 arguments were supplied --> crates/victory-shader/src/emit/mod.rs (in the new test) | resource::track(&p, &native.layout, native.stage) | argument #4 of type `Option<&structure::Structured>` is missing ``` There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break `cargo test` on `main`. The call this PR adds needs the fourth argument. `main`'s own tests pass `Some(&st)`; `None` keeps the old three-argument behaviour. **With `, None` added locally to that one call**, everything passes: - The PR's GPU test ran on the RTX 5060 (DXGI adapter 0 on this laptop; no "no SM 6.6 device" skip) and passed: - `scalar_bitcount_and_first_zero_execute_on_gpu` - `cargo test --workspace --locked`: **343 passed, 0 failed, 6 ignored**, against 342 passed / 6 ignored on `main`. - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. AI-assisted (Claude Code).
alexdeliu commented 2026-11-24 18:05:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T17:23:00Z.


Merged onto main @ ffe310eb, this PR (50d8a98c) no longer compiles its tests. crates/victory-shader/src/emit/mod.rs:824 calls resource::track with 3 arguments. Since 6638098c it takes a 4th, lanes: Option<&Structured>; main's tests pass Some(&st) with let st = structure::structure(&p).unwrap();.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139110631 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7139110631). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T17:23:00Z. --- Merged onto `main` @ `ffe310eb`, this PR (`50d8a98c`) no longer compiles its tests. `crates/victory-shader/src/emit/mod.rs:824` calls `resource::track` with 3 arguments. Since 6638098c it takes a 4th, `lanes: Option<&Structured>`; main's tests pass `Some(&st)` with `let st = structure::structure(&p).unwrap();`. <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>
n-romanovich commented 2026-11-24 18:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-24T17:53:14Z.


Thanks for testing and pointing this out, @alexdeliu!

Merged latest main and updated resource::track in the test to pass Some(&st) per 6638098c. Verified with cargo clippy --all-targets --locked -- -D warnings and re-ran the GPU test — both compile cleanly and pass with 0 warnings.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139847300 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7139847300). Originally posted by [n-romanovich](https://github.com/n-romanovich) on 2026-11-24T17:53:14Z. --- Thanks for testing and pointing this out, @alexdeliu! Merged latest main and updated resource::track in the test to pass Some(&st) per 6638098c. Verified with cargo clippy --all-targets --locked -- -D warnings and re-ran the GPU test — both compile cleanly and pass with 0 warnings.
alexdeliu commented 2026-11-24 18:06:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T17:55:56Z.


@n-romanovich rechecked cee01b43 merged onto main @ ffe310eb on the RX 9070: both CI clippy runs are clean and cargo test --workspace passes (343 tests).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139911763 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7139911763). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T17:55:56Z. --- @n-romanovich rechecked `cee01b43` merged onto `main` @ `ffe310eb` on the RX 9070: both CI clippy runs are clean and `cargo test --workspace` passes (343 tests).
upstream-sync closed this pull request 2026-11-25 00:05:17 +00:00
Marvisak commented 2026-11-25 00:06:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:00:20Z.


Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #148. s_bcnt0/s_ff0 (b32 and b64) lowering equal in effect to #148's, which also covers s_flbit_i32_b64/_i64. Its GPU test builds HostShader with the old field set and does not compile on main.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148031008 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/165#issuecomment-7148031008). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:00:20Z. --- Automated triage for the maintainers. Closing because another open pull request covers the same change. See #148. s_bcnt0/s_ff0 (b32 and b64) lowering equal in effect to #148's, which also covers s_flbit_i32_b64/_i64. Its GPU test builds HostShader with the old field set and does not compile on main. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!165
No description provided.