Skip to content

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

Closed
geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:fix/shader-scalar-bitops-zero-rev
Closed

geiyiliyabumo wants to merge 1 commit into
Marvisak:mainfrom
geiyiliyabumo:fix/shader-scalar-bitops-zero-rev

Conversation

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

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.

@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.

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests.

Ten scalar bitops (bcnt0/ff0/flbit 32+64/brev64/bitset64/bitreplicate) lowered without INT64; the test asserts the INT64 feature flag stays clear and values match RDNA2 per backend.

@adamdotpy

Copy link
Copy Markdown

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 shader: lower 6 RDNA2 integer dot-product instructions without INT64 #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 Shaders: scalar buffer loads from a UAV read it raw #1055/Shaders: scalar loads from a root view's address read its memory #1056/Shaders: name what a scalar memory read is, and which descriptor dword it reads #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.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

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.

@Marvisak

Copy link
Copy Markdown
Owner

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 Marvisak closed this 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, crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

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.

5 participants