Skip to content

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

Open
Rareuncommon wants to merge 1 commit into
Marvisak:mainfrom
Rareuncommon:shaders/scalar-zero-counts-bit-scans
Open

Rareuncommon wants to merge 1 commit into
Marvisak:mainfrom
Rareuncommon:shaders/scalar-zero-counts-bit-scans

Conversation

@Rareuncommon

@Rareuncommon Rareuncommon commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Six scalar instructions that the emitter refuses by name now translate.

Rebuilt on main 63bcca4: 6926670 replaces a038004. Two things changed in the rebuild, and nothing in the rules:

  • The test calls lowers(&words, 0), the helper's form on main (thanks @alexdeliu).
  • The four 64-bit forms moved to a new crates/victory-shader/src/emit/lower/scalar_bits.rs, next to bvh.rs. With them inline, lower.rs came to 526,633 bytes and cargo xtask policy refused it (limit 524,288; main is at 520,563). The operations emitted are the same ones as in a038004.

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.

Files: emit/lower.rs +40 (the two 32-bit forms, the call for the 64-bit ones, the test), emit/lower/scalar_bits.rs +57.

Tested

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

  • cargo test -p victory-shader: 170 passed, 0 failed, 2 ignored, with three of main's tests left out locally: they name emit::validator, which is Windows-only, so the test build does not compile on macOS on main either. The change that leaves them out is not in this PR.
  • 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. With the 64-bit arm taken out it fails with "s_bcnt0_i32_b64 is not supported".
  • cargo xtask policy: 192 files ok. rustfmt --check: the new file is clean, and lower.rs has the same 33 differences as on main, none in the lines added here.
  • cargo clippy -p victory-shader (library only): nothing reported for the new code.

Checked on hardware by others, on the earlier head a038004 (see the comments)

  • @Dathinel: 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. That measurement is of the rules, which did not change.
  • @DaviTostes: cargo test -p victory-shader on Arch Linux.
  • @andrewpozdnakov7-jpg rebased a038004 separately and ran 48 WARP cases on that rebase; this head is my own rebuild, not that branch.

Not tested (by me)

  • The values at 6926670: nothing here has executed the emitted DXIL. The value tests are Windows-only and need a GPU, which I do not have. A rerun on a GPU of this head would be welcome, since the 64-bit code was moved.
  • The Windows build, cargo clippy --all-targets -- -D warnings and cargo test --workspace as CI runs them.
  • I cannot say how many of the title's programs use these six; I have no game files.

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

AI-assisted: yes (Claude Code).

🤖 Generated with Claude Code

@DaviTostes

Copy link
Copy Markdown

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

No game data used.

@Dathinel

Copy link
Copy Markdown
Contributor

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.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally on macOS 26 arm64, rustc 1.98.1 (aarch64-apple-darwin) at head a038004:

  • cargo test -p victory-shader --locked: compiles clean, all ungated tests pass (no regressions vs main's 56).

Scope note: valu_tests/image_tests in victory-shader are #[cfg(all(test, windows))] — they need the Windows host backend (D3D12/dxil.dll), so they did not run here. What I verified is compilation on macOS plus all ungated tests. For this PR that means the new s_bcnt0/s_ff0/s_flbit lowering coverage still needs a Windows run before merge — I can't execute it on this machine. The lowering changes themselves compile everywhere.

@alexdeliu

Copy link
Copy Markdown
Contributor

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 25b0f88 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

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

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 <andrewpozdnakov7-jpg@users.noreply.github.com>. Claim

…32_b64/_i64

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.

The four 64-bit forms are in a new emit/lower/scalar_bits.rs, next to bvh.rs: with them inline, lower.rs
passes the asset policy's 512 KiB file limit.

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

AI-assisted (Claude Code).
@Rareuncommon
Rareuncommon force-pushed the shaders/scalar-zero-counts-bit-scans branch from a038004 to 6926670 Compare November 26, 2026 22:43

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — verified rustfmt (scalar_bits.rs and the added hunks in lower.rs), cargo xtask policy, cargo clippy (--all-targets and --all-features), emit::lower::tests on hardware and WARP (34 passed, including scalar_zero_counts_and_bit_scans_lower), and emit::valu_tests on hardware (35 passed).

Checked all six instructions (s_bcnt0_i32_b32/_b64, s_ff0_i32_b32/_b64, s_flbit_i32_b64, and s_flbit_i32_i64) against RDNA2 SOP1 semantics and DXIL unaryBits.i32 (opcodes 31–34):

  • Bit-scan and zero-count edge cases (0, -1, u64::MAX, i64::MIN, i64::MAX, and cross-dword boundaries where hi is all-zero or all-sign) match hardware semantics.
  • SCC handling matches RDNA2 (s_bcnt0_i32_b32 writes SCC = (r != 0) via lower.rs:4063-4066, s_bcnt0_i32_b64 writes SCC = (r != 0) in scalar_bits.rs:46-47, and s_ff0_*/s_flbit_* leave SCC untouched).

Non-blocking suggestion: consider validating the emitted DXIL in scalar_zero_counts_and_bit_scans_lower (via validator::Validator) and/or adding hardware/WARP execution cases in valu_tests.rs covering 0, u64::MAX, negative s_flbit_i32_i64 inputs, and SCC == 0.

Isuru-A pushed a commit to cocainebunnylaboratories/cblvictoryvi that referenced this pull request Nov 27, 2026
DeMaarco pushed a commit to DeMaarco/VIctory that referenced this pull request Nov 29, 2026
…he paired wide form before the approximate one
DeMaarco pushed a commit to DeMaarco/VIctory that referenced this pull request Nov 29, 2026
Marvisak#159 and Marvisak#148 both moved the version from 5 to 6, so the merge kept 6,
but translations cached at 6 by Marvisak#159 do not have Marvisak#148's forms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants