Skip to content

Shaders: lower v_bfe_u32/v_bfe_i32 to one Ubfe/Ibfe - #2954

Open
Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:perf/vector-bitfield-extract
Open

Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:perf/vector-bitfield-extract

Conversation

@Ingenioeren

Copy link
Copy Markdown
Contributor

What

Lower v_bfe_i32 and v_bfe_u32 to one DXIL Ibfe or Ubfe call per vector instruction. Add an instruction-count regression test and extend the existing synthetic GPU test.

Why

The DXIL operations use the low five bits of both width and offset and produce zero for width zero. When a field reaches or crosses bit 31, DXIL shifts the source right arithmetically for Ibfe and logically for Ubfe, which gives the same sign or zero extension as the previous lowering. The operands and results are 32-bit integers. DXIL specification

Each synthetic shader takes three runtime inputs and stores one result to a UAV. The count includes emitted setup, live instructions, and terminators. The counter is the same test-only helper as in #2893, so whichever PR lands second can drop its copy.

Synthetic shader origin/main (63bcca4) Branch Reduction
v_bfe_i32 34 19 44.1%
v_bfe_u32 27 19 29.6%

How tested

  • cargo test --locked --offline -p victory-shader vector_bitfield_extract_instruction_count -- --nocapture measured the counts above. On main, I applied the test-only changes and recorded the baseline before adding the 19-instruction budget.
  • cargo test --locked --offline -p victory-shader bfe_signed_and_unsigned_match_rdna2 -- --nocapture passed on the hardware GPU and WARP on both revisions with identical expected output bits. Cases include masked offset and width, zero width, and fields ending at or crossing bit 31.
  • cargo test --locked --offline -p victory-shader added one passing test on the branch, from 300 to 301. Both runs had the same two local dxil.dll validation failures and three ignored tests.
  • cargo test --locked --offline -p victory-shader --test resource_tracking passed three tests with one ignored on both revisions.
  • cargo clippy -p victory-shader --locked --offline --all-targets passed.
  • cargo run -p xtask -- policy --staged passed for all three staged files.

All shader inputs were synthesized in test code. No game files, game launch, hardware game run, or game performance benchmark.

AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.

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

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

Independent test report for eb73a3ab7995695a56d8239104623460c7250ce9, with the exact parent 63bcca43c6a00a758510398fa43096ce7e4f7df0 as the control. Claim: #1633.

Setup: Windows 11 Pro 10.0.26200, Rust 1.98.1. Validator is VICTORY_DXIL = Windows SDK 10.0.26100 x64 dxil.dll 1.8.2502.11; it loaded, since the test that unwraps Validator::load passed. Environment had VICTORY_D3D12_WARP=1 and VICTORY_WARP=1, so the "hardware" slot of run_cases also opens WARP. A probe confirmed that Host::new(false) and Host::new(true) are both Microsoft Basic Render Driver (vendor 0x1414, device 0x8c, DXGI_ADAPTER_FLAG_SOFTWARE). Nothing ran on a physical GPU. All inputs were synthetic.

Instruction count: cargo test -j 4 --locked -p victory-shader --lib -- vector_bitfield_extract_instruction_count. To read both values, I printed them locally instead of asserting.

Parent with test-only changes Head
v_bfe_u32 27 19
v_bfe_i32 34 19

These match the PR table. With the assert left in, the parent fails on v_bfe_u32: left 27, right 19.

Bit extraction boundaries:

  • bfe_signed_and_unsigned_match_rdna2 passes on the head, and on the parent with the PR's extended cases applied, so outputs are identical across both revisions.
  • My local probe (uncommitted) runs v_bfe_u32 and v_bfe_i32 through WARP readback and compares them with a CPU model of the semantics the existing tests encode. The model takes the low five bits of offset and width, returns 0 for width 0, and when the field reaches or crosses bit 31 it zero-extends (u32) or arithmetic-shifts (i32).
    • Inputs: 6 sources (including 0x80000000, 0x7fffffff, 0xffffffff and 1) × 38 offsets (0–31, 32, 33, 63, 0xffffffff, 0x8000001f, 0xffffffe0) × 15 widths (0, 1, 2, 4, 15–17, 30–33, 37, 63, 0xffffffe1, 0xffffffff).
    • That is 3420 cases per op. Every result matched, on both revisions, for both ops.

Full suite: cargo test -j 4 --locked -p victory-shader --lib -- --test-threads=1

Passed Failed Ignored
Head 299 4 3
Parent 298 4 3

The same 4 synthetic_tests fail on both revisions:

  • a_mask_joined_with_data_at_a_uniform_loop_is_its_number
  • gathers_at_a_level_match_the_hardware
  • permutes_mask_high_address_bits_and_divergent_branches
  • permutes_read_the_lane_named_and_zero_from_lanes_left_out

They are hardware-labelled checks ("no pipeline for a wave of 32", and gather texel expectations). VICTORY_WARP=1 forces them onto WARP, so they are an artifact of my setup, not this PR.

Lint and policy:

  • cargo clippy -j 4 --locked -p victory-shader --all-targets -- -D warnings: clean.
  • cargo run -j 4 -p xtask -- policy: 191 files ok.

Overlap and limits:

This is an independent test only, not a review. Prepared with Claude Code (Claude Opus 5.5).

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

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::valu_tests::bfe_signed_and_unsigned_match_rdna2: 1 passed, 0 failed (RTX 5090 hardware + WARP, including the new edge cases for 5-bit offset/width masking and bit-31 boundary).
  • cargo test -p victory-shader --lib emit::lower::tests::vector_bitfield_extract_instruction_count: 1 passed, 0 failed (19 DXIL instructions for both v_bfe_u32 and v_bfe_i32).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Lowering v_bfe_u32 / v_bfe_i32 directly to DXIL tertiary.i32 Ubfe (52) / Ibfe (51) matches RDNA2's 5-bit offset/width semantics and shrinks the generated DXIL cleanly.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

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

Reviewed and verified against main (63bcca43) on Windows 11 (RTX 5090 + WARP + dxil.dll, VICTORY_D3D12_DEBUG=1) — clean and ready to merge:

  • Lowering v_bfe_u32 / v_bfe_i32 (crates/victory-shader/src/emit/lower.rs:7531-7537) to a single dx.op.tertiary.i32 (Ubfe 52 / Ibfe 51, args &[w, off, a]) matches RDNA2 5-bit off[4:0] / w[4:0] masking semantics (including width == 0/32 -> 0, offset >= 32, and offset + width > 32 sign extension from bit 31), while properly keeping self.bfe(...) for 7-bit-width scalar s_bfe_{u32,i32} (lower.rs:3944).
  • Note on 512 KiB file-size cap: on 63bcca43, emit/lower.rs grows to 521,372 B (+809 B, 2,916 B under 524,288 B).

Local verification:

  • bfe_signed_and_unsigned_match_rdna2 + vector_bitfield_extract_instruction_count + emit::lower::tests: 34/34 passed on both RTX 5090 and WARP (dxil.dll + DEBUG=1)
  • emit::semantics_tests::integers_match_the_hardware: 1/1 passed
  • cargo clippy --all-targets -- -D warnings + cargo xtask policy (192 files ok): 0 warnings

alesanfe added a commit to alesanfe/VIctory that referenced this pull request Nov 26, 2026
… UAV writeback

- Marvisak#2952: a stream with neither a depth-stencil subobject nor a depth
  format defaults to disabled depth testing (apply_stream_defaults)
- Marvisak#2931: feature-level query rejects count == 0 and writes 0 on UNSUPPORTED
- Marvisak#2961: reject shader-visible RTV and DSV heap creation
- Marvisak#2865: guard placed heaps against null and layouts against empty planes
- Marvisak#2954: lower v_bfe_u32/v_bfe_i32 to one Ubfe/Ibfe
- Marvisak#2948: a root UAV a shader only reads is not written back
  (written_root_uavs through Shader/Lowered/HostShader + cache VSH2)

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

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

Verified: DXIL Ibfe(51)/Ubfe(52) already apply the 5-bit width/offset masks, so the old m31 pre-mask plus composite expansion was redundant. Opcode mapping is right (signed→51, unsigned→52) and the rewritten hardware test now discriminates arg order — e.g. (0xffffffff, off 0, width 31) expects 0x7fffffff, which a swapped (width,value) order could not produce — plus the exact instruction-count anchor (19) pins the single-op lowering.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. In Lower::valu_op (crates/victory-shader/src/emit/lower.rs), v_bfe_u32/v_bfe_i32 now lower to DXIL Ubfe/Ibfe (ops 52/51, width, offset, value) instead of main's open-coded Lower::bfe; s_bfe_* still use the helper. I checked that the two agree. Both use only the low five bits of offset and width and give 0 for width 0. For a field that runs past bit 31, main's arithmetic shift, mask and sign-extend gives the same result as Ibfe's plain shift. The extended bfe_signed_and_unsigned_match_rdna2 cases (offset and width above 31, fields ending at bit 31) run through the host compiler and pass, alongside main's existing cases.

One note: vector_bitfield_extract_instruction_count asserts an exact count of 19 written instructions. That pins today's surrounding codegen, so unrelated lowering changes may have to update it. A bound (for example <=) would be less brittle. This is not a blocker. #2893 adds the same test-only written_instructions helper in bitcode.rs, so whichever lands second drops its copy.

CI: passing (check).

Recommending a look, not approving.


Generated by Claude Code

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

AMD hardware run of eb73a3a. RX 6750 XT (RDNA2), driver 32.0.21045.29003, Windows 11, Rust 1.98.1, on main 63bcca4.

cargo test -p victory-shader --lib -- bfe_signed_and_unsigned vector_bitfield_extract --test-threads=1: 2 passed, 0 failed, so bfe_signed_and_unsigned_match_rdna2 agrees with the hardware adapter and the count test holds.

The whole victory-shader suite with --test-threads=1: 303 passed, 0 failed, 3 ignored (main on this machine: 302 passed, 0 failed).

Unit tests only, no game files.

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.

7 participants