shader: lower 10 scalar bitops (bcnt0/ff0/flbit64/brev64/bitset64/bitreplicate) without INT64 #1684

Closed
github-bot wants to merge 1 commit from gh-pr/1684/head into gh-pr/1684/base
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 20:44 UTC · original
geiyiliyabumo:fix/shader-scalar-bitops-zero-rev → main · +323 −11 · 2 files · 1 commits · closed without merging

Summary

  • Lowers ten RDNA2 SOP1 scalar bit-manipulation instructions in crates/victory-shader/src/emit/lower.rs using pure 32-bit DXIL arithmetic (no INT64 capability flag set in SFI0):
    • s_bcnt0_i32_b32 (0x00d) & s_bcnt0_i32_b64 (0x00e): count clear bits via countbits(~x) and set SCC = (result != 0).
    • s_ff0_i32_b32 (0x011) & s_ff0_i32_b64 (0x012): lowest clear bit via firstbitlow(~x) (-1 when all ones).
    • s_flbit_i32_b64 (0x016) & s_flbit_i32_i64 (0x018): leading zeros / leading bits matching sign bit 63 (-1 when all bits are equal).
    • s_brev_b64 (0x00c): 64-bit bit-reversal across the swapped dword pair.
    • s_bitset0_b64 (0x01c) & s_bitset1_b64 (0x01e): clear/set bit src0[5:0] of the 64-bit destination pair (also handles null destination cleanly).
    • s_bitreplicate_b64_b32 (0x03b): duplicates each bit of src0 into two adjacent bits of sdst[0:1] via SWAR bit-spreading (adds s_bitreplicate_b64_b32 to the wide scalar dispatch since its destination is 64-bit despite ending in _b32).

Verification

  • cargo test -p victory-shader --lib: 121 passed, 0 failed, 1 ignored on both AMD Radeon RX 9060 XT (D3D12 hardware) and Microsoft Basic Render Driver (WARP), including scalar_bitops_zero_rev_and_64bit_match_rdna2_without_int64 exercising all 10 instructions (including SCC checks, boundary bit indices 0/31/32/63/69, sign-bit edge cases for s_flbit_i32_i64, and SFI0 & 0x8000 == 0 on all 64-bit variants).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: pass (154 files).

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 20:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/1684) `geiyiliyabumo:fix/shader-scalar-bitops-zero-rev` → `main` · +323 −11 · 2 files · 1 commits · closed without merging ## Summary - Lowers ten RDNA2 SOP1 scalar bit-manipulation instructions in `crates/victory-shader/src/emit/lower.rs` using pure 32-bit DXIL arithmetic (no `INT64` capability flag set in `SFI0`): - `s_bcnt0_i32_b32` (`0x00d`) & `s_bcnt0_i32_b64` (`0x00e`): count clear bits via `countbits(~x)` and set `SCC = (result != 0)`. - `s_ff0_i32_b32` (`0x011`) & `s_ff0_i32_b64` (`0x012`): lowest clear bit via `firstbitlow(~x)` (`-1` when all ones). - `s_flbit_i32_b64` (`0x016`) & `s_flbit_i32_i64` (`0x018`): leading zeros / leading bits matching sign bit 63 (`-1` when all bits are equal). - `s_brev_b64` (`0x00c`): 64-bit bit-reversal across the swapped dword pair. - `s_bitset0_b64` (`0x01c`) & `s_bitset1_b64` (`0x01e`): clear/set bit `src0[5:0]` of the 64-bit destination pair (also handles `null` destination cleanly). - `s_bitreplicate_b64_b32` (`0x03b`): duplicates each bit of `src0` into two adjacent bits of `sdst[0:1]` via SWAR bit-spreading (adds `s_bitreplicate_b64_b32` to the `wide` scalar dispatch since its destination is 64-bit despite ending in `_b32`). ## Verification - `cargo test -p victory-shader --lib`: **121 passed, 0 failed, 1 ignored** on both **AMD Radeon RX 9060 XT (D3D12 hardware)** and **Microsoft Basic Render Driver (WARP)**, including `scalar_bitops_zero_rev_and_64bit_match_rdna2_without_int64` exercising all 10 instructions (including `SCC` checks, boundary bit indices `0/31/32/63/69`, sign-bit edge cases for `s_flbit_i32_i64`, and `SFI0 & 0x8000 == 0` on all 64-bit variants). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: pass (154 files). *AI-assisted; synthetic tests only, no game files.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:41:29 +00:00
github-bot closed this pull request 2026-11-26 04:41:29 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 22:00 UTC · original

Reviewed head c0b3633 locally (isolated worktree, fresh CARGO_TARGET_DIR, real GPU dispatch).

What it does: lowers the ten RDNA2 scalar bitops without INT64 — 32-bit s_bcnt0/s_ff0_i32_b32 (via xor-with-ones then the existing count/first-set path) and 64-bit s_bcnt0/s_ff0/s_flbit_i32_b64, s_flbit_i32_i64, s_brev_b64, s_bitset0/s_bitset1_b64, s_bitreplicate_b64_b32. I hand-checked the tricky ones: s_brev_b64 correctly writes rev(hi) to lo and rev(lo) to hi; s_bitreplicate spreads each half 16->32 with the standard mask ladder then ORs x | x<<1; s_bitset* does a proper read-modify-write of both dst halves with the bit routed by src bit 5; s_flbit_i32_i64 xors both halves with the replicated sign bit before counting, which is exactly "leading bits equal to sign".

Verified locally:

  • cargo test -p victory-shader --lib scalar_bitops -- --nocapture — passes on hardware (6s, no skip); the sweep includes all-zero inputs for bcnt0/ff0/flbit and the test asserts SFI0 bit 0x8000 (INT64) is clear, so the lowering provably needs no 64-bit ops
  • cargo test -p victory-shader --lib --locked — 121 passed / 0 failed / 1 ignored
  • cargo clippy -p victory-shader --all-targets --all-features --locked — clean
  • cargo build --release -p victory --locked — ok
  • rustfmt vs main@⁠ffe310e: lower.rs same 3 pre-existing hunks; valu_tests.rs has 6 new cosmetic hunks (969, 991, 1028, 1044, 1065, 1089)

Findings, ranked:

  1. Sound. The zero-input edge for s_flbit_i32_b64 (both halves zero) is in the hardware-pinned cases, so the -1/none convention matches RDNA2 as emitted; ff0/bcnt0 = ff1/bcnt1 of the complement is exact.
  2. Complements #1694 (dot products) and the other INT64-free lowerings — same SFI0-assertion pattern, good consistency.
  3. Note for mergers: this branch is based on ffe310e and will now textually conflict with upstream's new main (70564ae, the big resource/native rewrite) in emit/lower.rs — same as our #1055/#1056/#1083; semantic overlap is unlikely since these are distinct opcodes, but expect to keep both sides in lower.rs.
  4. Minor: 6 new rustfmt hunks in the test file; not blocking (no fmt CI job).

LGTM.

**@⁠adamdotpy** on GitHub · 2026-11-24 22:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1684#issuecomment-7145681891) Reviewed head c0b3633 locally (isolated worktree, fresh CARGO_TARGET_DIR, real GPU dispatch). **What it does:** lowers the ten RDNA2 scalar bitops without INT64 — 32-bit `s_bcnt0/s_ff0_i32_b32` (via xor-with-ones then the existing count/first-set path) and 64-bit `s_bcnt0/s_ff0/s_flbit_i32_b64`, `s_flbit_i32_i64`, `s_brev_b64`, `s_bitset0/s_bitset1_b64`, `s_bitreplicate_b64_b32`. I hand-checked the tricky ones: `s_brev_b64` correctly writes rev(hi) to lo and rev(lo) to hi; `s_bitreplicate` spreads each half 16->32 with the standard mask ladder then ORs `x | x<<1`; `s_bitset*` does a proper read-modify-write of both dst halves with the bit routed by src bit 5; `s_flbit_i32_i64` xors both halves with the replicated sign bit before counting, which is exactly "leading bits equal to sign". **Verified locally:** - `cargo test -p victory-shader --lib scalar_bitops -- --nocapture` — passes on hardware (6s, no skip); the sweep includes all-zero inputs for bcnt0/ff0/flbit and the test asserts SFI0 bit 0x8000 (INT64) is clear, so the lowering provably needs no 64-bit ops - `cargo test -p victory-shader --lib --locked` — 121 passed / 0 failed / 1 ignored - `cargo clippy -p victory-shader --all-targets --all-features --locked` — clean - `cargo build --release -p victory --locked` — ok - rustfmt vs `main`@⁠ffe310e: `lower.rs` same 3 pre-existing hunks; `valu_tests.rs` has 6 new cosmetic hunks (969, 991, 1028, 1044, 1065, 1089) **Findings, ranked:** 1. Sound. The zero-input edge for `s_flbit_i32_b64` (both halves zero) is in the hardware-pinned cases, so the -1/none convention matches RDNA2 as emitted; ff0/bcnt0 = ff1/bcnt1 of the complement is exact. 2. Complements #1694 (dot products) and the other INT64-free lowerings — same SFI0-assertion pattern, good consistency. 3. Note for mergers: this branch is based on `ffe310e` and will now textually conflict with upstream's new `main` (70564ae, the big resource/native rewrite) in `emit/lower.rs` — same as our #1055/#1056/#1083; semantic overlap is unlikely since these are distinct opcodes, but expect to keep both sides in `lower.rs`. 4. Minor: 6 new rustfmt hunks in the test file; not blocking (no fmt CI job). LGTM.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 22:52 UTC · original

NVIDIA run: RTX 3070 8 GB, driver 616.56, Windows 11, debug, at the PR head c0b3633. scalar_bitops_zero_rev_and_64bit_match_rdna2_without_int64 passes.
It is based on ffe310e and GitHub shows conflicts with main 70564ae, so this is the head as it stands, not a merge.
AI-assisted (Claude Code). Synthetic tests only.

**@⁠upipibi** on GitHub · 2026-11-24 22:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/1684#issuecomment-7146751991) NVIDIA run: RTX 3070 8 GB, driver 616.56, Windows 11, debug, at the PR head `c0b3633`. `scalar_bitops_zero_rev_and_64bit_match_rdna2_without_int64` passes. It is based on `ffe310e` and GitHub shows conflicts with `main` `70564ae`, so this is the head as it stands, not a merge. AI-assisted (Claude Code). Synthetic tests only.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:24 UTC · original

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

See #148. Ten scalar bit operations (bcnt0, ff0, flbit 64-bit, brev64, bitset64, bitreplicate), which the title does not use. All ten are covered by PRs that merge clean: #148 (bcnt0, ff0, flbit) and #166 (bitset_b64, brev_b64, bitreplicate).

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.

**@⁠Marvisak** on GitHub · 2026-11-25 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1684#issuecomment-7149514215) Automated triage for the maintainers. Closing because another open pull request covers the same change. See #148. Ten scalar bit operations (bcnt0, ff0, flbit 64-bit, brev64, bitset64, bitreplicate), which the title does not use. All ten are covered by PRs that merge clean: #148 (bcnt0, ff0, flbit) and #166 (bitset_b64, brev_b64, bitreplicate). 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:34 UTC · original

Automated triage for the maintainers.

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


Generated by Claude Code

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

Pull request closed

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!1684
No description provided.