Skip to content

Shaders: lower s_bfe_u64, s_bfe_i64 and s_bfm_b64 - #1395

Open
AnoHunted wants to merge 2 commits into
Marvisak:mainfrom
AnoHunted:shaders-scalar-bitfield-64
Open

AnoHunted wants to merge 2 commits into
Marvisak:mainfrom
AnoHunted:shaders-scalar-bitfield-64

Conversation

@AnoHunted

Copy link
Copy Markdown
Contributor

What

The scalar 64-bit lowering path refused s_bfe_u64, s_bfe_i64 and s_bfm_b64. They now lower:

  • s_bfe_u64 / s_bfe_i64: the field is (src0 >> src1[5:0]) & ((1 << src1[22:16]) - 1); the signed form sign-extends from the field's top bit (width 0 gives 0). SCC is set to whether the result is nonzero.
  • s_bfm_b64: ((1 << src0[5:0]) - 1) << src1[5:0]. It does not write SCC, so SCC keeps its earlier value.

A width of 64 or more (and a field running past bit 63) is not defined by the ISA; it takes every bit, with shift amounts masked, so the lowering cannot go out of range. No test depends on that case. The 32-bit s_bfm_b32 is left as it is.

Why

A shader using any of these three failed to translate. They are the 64-bit counterparts of s_bfe_u32/s_bfe_i32/s_bfm_b32, which already lower.

Related PRs: #163 and #231 contained these ops but are not on main. #447 (64-bit literal sign extension) touches the same function but not these arms; this change is made on current main and does not depend on it. #166 covers different 64-bit bit ops.

Tested

Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1 (hardware and WARP):

  • cargo test -p victory-shader --features host -- --test-threads=1: main 120 passed, 0 failed, 1 ignored; branch 123 passed, 0 failed, 1 ignored (3 new tests).
  • s_bfe_64_extracts_and_sets_scc: runs both ops on the GPU for low dword, high dword and SCC (e.g. 0xFEDCBA9876543210, offset 32, width 16 gives 0xBA98 unsigned and 0xFFFFFFFFFFFFBA98 signed; zero field gives 0 and SCC 0).
  • s_bfm_b64_builds_a_mask_and_keeps_scc: masks such as width 40 / offset 8 give 0x0000FFFFFFFFFF00, and SCC set by an s_cmp before the instruction is read back unchanged afterwards (both 0 and 1, including a nonzero mask).
  • scalar_64_bit_field_ops_lower: hand-assembled encodings lower.

AMD hardware: not tested.

Not tested

  • Widths of 64 and above, and fields past bit 63 (undefined by the ISA).
  • Real AMD hardware.

No game files were used.

The scalar 64-bit path refused all three. s_bfe_u64 and s_bfe_i64 take the
field at src1[5:0] with width src1[22:16] and set SCC to whether the result is
nonzero; the signed form sign-extends from the field's top bit. s_bfm_b64 builds
((1 << src0[5:0]) - 1) << src1[5:0] and does not write SCC. Widths of 64 and up
are not defined by the ISA; they take every bit here so the lowering stays in
range.
@dcastell1

Copy link
Copy Markdown

Verified on native Windows (no Wine): NVIDIA GeForce RTX 5090 (driver 621.24), Windows 11 10.0.26300, Rust 1.98.1, MSVC 14.44.35207, debug build. main@ffe310e baseline on this box: 342 passed, 0 failed, 6 ignored.

cargo test --workspace
345 passed; 0 failed; 6 ignored   (exit 0)

Three more than the baseline; s_bfe_u64, s_bfe_i64 and s_bfm_b64 lowering ships its cases and they run green.

Tree restored afterwards (full diff against a clean checkout); no game data, the tests are synthetic. AI-assisted (opencode): applied the patch to a built tree, ran cargo test --workspace, reverted it.

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

s_bfe_u64/s_bfe_i64/s_bfm_b64 lowering. The bfe packing (offset[5:0], width[22:16] with 7-bit width) matches the SOP2 encoding; width >= 64 takes the all-ones mask path, signed sign-extension is skipped for width 0/full, and SCC is set from the 64-bit nonzero — all verified by the 12-case hardware test including width-0 and sign cases. s_bfm_b64 correctly leaves SCC untouched (pinned by test). The macOS lowering test runs green. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host/diff_tests) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 97 passed, 0 failed.

@matheusfermiano-source

Copy link
Copy Markdown

An AMD run, since the PR says AMD hardware was not tested; under Proton on Linux: scalar_64_bit_field_ops_lower, s_bfe_64_extracts_and_sets_scc, s_bfm_b64_builds_a_mask_and_keeps_scc pass on both GPUs.

  • Head a91e9ad. Built on Linux with llvm-mingw for x86_64-pc-windows-gnullvm (Rust 1.98.1) and run under Proton Experimental (11.0-20261027b) with its vkd3d-proton 3.1.0, one GPU at a time; dxil.dll from DXC 1.9.2609 was loaded, so the test modules were validated.
  • Radeon 660M (RDNA2, RADV Mesa 26.3.1, waves 32..64): whole victory-shader suite 122 passed, 1 failed, 1 ignored. Nothing fails that passes on main (ffe310e). The one failure, emit::tests::stub_pipeline_is_created, fails on main here too: vkd3d-proton accepts the zeroed DXIL hash that the Windows runtime refuses (one of the Wine failures listed in xtask: wine-test runs the Windows tests on a Linux host under Wine #1218).
  • RTX 3050 Laptop (NVIDIA 620.73.04, waves 32..32), whole suite: 122 passed, 1 failed (same failure as on main).

This is RADV, not AMD's Windows driver, so it adds to a Windows run rather than replacing it. Synthetic tests only, no game files. AI-assisted (Claude Code, Opus 5.5); every run was on this machine.

@wwwwwwww3333333

Copy link
Copy Markdown

Native Windows run on AMD RDNA2: RX 6750 XT (RDNA2, gfx1031), AMD driver 32.0.21045.29003, i5-13400F, Windows 11, Rust 1.98.1 MSVC. Head a91e9ad on main ffe310e (clean merge).

cargo test -p victory-shader --lib -- --test-threads=1: 123 passed, 0 failed, 1 ignored (main alone: 120 passed). s_bfe_64_extracts_and_sets_scc, s_bfm_b64_builds_a_mask_and_keeps_scc ran on the hardware adapter and then on WARP, and passed on both. scalar_64_bit_field_ops_lower passes too. The earlier AMD run here was under Proton; this one is AMD's own D3D12 driver.

Synthetic tests only, no game files. AI-assisted (Claude picked the PRs and wrote the batch script; I ran it on my machine).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head a91e9ad onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 123, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Recommended for a maintainer's look: this lowers s_bfe_u64, s_bfe_i64 and s_bfm_b64 (SOP2 0x29, 0x2a, 0x25) in the scalar 64-bit arm of lower.rs, which main refuses today. I checked the arms against the RDNA2 ISA (offset in bits 5:0, width in bits 22:16, SCC = result nonzero for s_bfe, SCC untouched by s_bfm), the hand-assembled encodings in scalar_64_bit_field_ops_lower, and the expected values in the GPU tests; contributors report the tests passing on NVIDIA and AMD (native and vkd3d-proton). CI fails only on synthetic_tests::gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out, which this PR does not touch and which fail the same way on other PRs against current main. Small nit: there is a doubled blank line after s_bfm_b64_builds_a_mask_and_keeps_scc in valu_tests.rs.


Generated by Claude Code

@astrosteveo astrosteveo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Read the diff at a829945 and ran cargo test -p victory-shader --lib merged onto main 940d32f (no conflicts): Arch Linux, Wine 11.22, vkd3d-proton, RTX 5080, driver 620.73.04. main gives 243 passed and 4 failed there (host limits), this PR 246 passed and the same 4. The three more are scalar_64_bit_field_ops_lower, s_bfe_64_extracts_and_sets_scc and s_bfm_b64_builds_a_mask_and_keeps_scc.

The lowering matches the RDNA2 ISA as I read it: offset in bits 5:0, width in bits 22:16, SCC from the result for s_bfe and untouched by s_bfm.

The red CI looks unrelated. That run (00:50 UTC) failed only gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out, which main has skipped on machines without a GPU since #1911 and #1925. A push or "Update branch" should give a fresh result; a re-run keeps the old merge commit. Not checked: that Windows CI then passes.


Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

8 participants