Lower RDNA2 32-bit bitfield extracts with masked HLSL shifts #127

Merged
KomaGuy merged 4 commits from upstream-pr/128 into main 2026-11-21 17:55:32 +00:00
KomaGuy commented 2026-11-21 16:41:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KomaGuy on 2026-11-21T16:41:29Z.


Change

Align 32-bit bitfield extraction (v_bfe_u32, v_bfe_i32, s_bfe_u32, and s_bfe_i32) in tools/ffx_dispatch_aot.py and tests/ffx_dispatch_io.hlsli with AMD RDNA2 (gfx1013 / gfx1030) hardware semantics under HLSL Shader Model 6 shift rules:

  • v_bfe_i32 / s_bfe_i32 when offset + width > 32 or width == 0: Replaces asint(x << (32 - offset - width)) >> (32 - width) with bfe_i, avoiding modulo-32 shift-count underflow when offset + width > 32 (which extracted low bits instead of sign-extending [31:offset]) and returning 0 when width == 0 instead of asint(x << ...).
  • v_bfe_u32 / v_bfe_i32 5-bit operand masking: Masks vector offset (S1[4:0]) and width (S2[4:0]) to & 31u, without modifying scc.
  • s_bfe_u32 and s_bfe_i32 scalar control operand (S1) and SCC update: Unpacks offset = S1 & 31u and 7-bit width = (S1 >> 16) & 127u in HLSL rather than int(inputs[1], 0), supporting SGPR S1 operands, negative inline constants, s_bfe_i32, and width >= 32 without (1u << 32) - 1 wrapping to 0 in HLSL, and sets scc = dest != 0u; (with scc also declared in predecessor() and s_cselect_b32 supported alongside src_scc consumers).

Verification

Tested on Linux x86_64:

  • python3 -B tests/ffx_dispatch_aot_check.py llvm-mc-19 llvm-objcopy-19 libLLVM-19.so.1 — passed (existing packed-coordinate and i8 network checks, synthetic gfx1013 BFE + SCC consumer sequences with v_cndmask_b32_e64 ..., src_scc and s_cselect_b32 across translate() and predecessor(), stale-SCC set/clear evaluation after s_cmp_*, vector-BFE SCC preservation, and 2,016 bit-level RDNA2 reference checks).
  • python3 -B tests/pixel_aot_check.py binary llvm-mc-19 llvm-objcopy-19 libLLVM-19.so.1 — passed.
  • python3 -B tests/script_state_check.py, bcpack_check.py, resource_ranges_check.py, dxcontainer_part_offsets_check.py, layout_chunks_check.py — all passed.

Evidence and follow-up

  • The test fixtures in tests/ffx_dispatch_aot_check.py are assembled from original inline gfx1013 instructions with llvm-mc and checked against an independent bit-array RDNA2 BFE reference and generated-shader SCC evaluation under the repository's MIT license.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531560336 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/128). Originally posted by [KomaGuy](https://github.com/KomaGuy) on 2026-11-21T16:41:29Z. --- ## Change Align 32-bit bitfield extraction (`v_bfe_u32`, `v_bfe_i32`, `s_bfe_u32`, and `s_bfe_i32`) in `tools/ffx_dispatch_aot.py` and `tests/ffx_dispatch_io.hlsli` with AMD RDNA2 (`gfx1013` / `gfx1030`) hardware semantics under HLSL Shader Model 6 shift rules: - **`v_bfe_i32` / `s_bfe_i32` when `offset + width > 32` or `width == 0`:** Replaces `asint(x << (32 - offset - width)) >> (32 - width)` with `bfe_i`, avoiding modulo-32 shift-count underflow when `offset + width > 32` (which extracted low bits instead of sign-extending `[31:offset]`) and returning `0` when `width == 0` instead of `asint(x << ...)`. - **`v_bfe_u32` / `v_bfe_i32` 5-bit operand masking:** Masks vector `offset` (`S1[4:0]`) and `width` (`S2[4:0]`) to `& 31u`, without modifying `scc`. - **`s_bfe_u32` and `s_bfe_i32` scalar control operand (`S1`) and `SCC` update:** Unpacks `offset = S1 & 31u` and 7-bit `width = (S1 >> 16) & 127u` in HLSL rather than `int(inputs[1], 0)`, supporting SGPR `S1` operands, negative inline constants, `s_bfe_i32`, and `width >= 32` without `(1u << 32) - 1` wrapping to `0` in HLSL, and sets `scc = dest != 0u;` (with `scc` also declared in `predecessor()` and `s_cselect_b32` supported alongside `src_scc` consumers). ## Verification Tested on Linux x86_64: - `python3 -B tests/ffx_dispatch_aot_check.py llvm-mc-19 llvm-objcopy-19 libLLVM-19.so.1` — passed (existing packed-coordinate and i8 network checks, synthetic `gfx1013` `BFE` + `SCC` consumer sequences with `v_cndmask_b32_e64 ..., src_scc` and `s_cselect_b32` across `translate()` and `predecessor()`, stale-`SCC` set/clear evaluation after `s_cmp_*`, vector-`BFE` `SCC` preservation, and 2,016 bit-level RDNA2 reference checks). - `python3 -B tests/pixel_aot_check.py binary llvm-mc-19 llvm-objcopy-19 libLLVM-19.so.1` — passed. - `python3 -B tests/script_state_check.py`, `bcpack_check.py`, `resource_ranges_check.py`, `dxcontainer_part_offsets_check.py`, `layout_chunks_check.py` — all passed. ## Evidence and follow-up - The test fixtures in `tests/ffx_dispatch_aot_check.py` are assembled from original inline `gfx1013` instructions with `llvm-mc` and checked against an independent bit-array RDNA2 `BFE` reference and generated-shader `SCC` evaluation under the repository's MIT license.
upstream-sync closed this pull request 2026-11-24 06:41:26 +00:00
R0Y00 commented 2026-11-24 06:47:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:16:53Z.


Please cover a scalar BFE followed by an SCC-dependent instruction. The PR computes the extracted value but says SCC is left to compares. Either preserve BFE's SCC semantics or explicitly reject consumers that would read a stale flag, with a fixture for that path.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066355289 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/128#issuecomment-7066355289). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:16:53Z. --- Please cover a scalar BFE followed by an SCC-dependent instruction. The PR computes the extracted value but says SCC is left to compares. Either preserve BFE's SCC semantics or explicitly reject consumers that would read a stale flag, with a fixture for that path.
KomaGuy commented 2026-11-24 06:47:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KomaGuy on 2026-11-21T17:37:19Z.


Updated in 03c61dc to preserve scalar BFE SCC semantics:

  • s_bfe_u32 and s_bfe_i32 now emit scc = <dest> != 0u; (v_bfe_* leaves scc untouched), predecessor() declares scc, and s_cselect_b32 is supported alongside src_scc consumers.
  • Added assembled gfx1013 fixtures in tests/ffx_dispatch_aot_check.py covering s_bfe_u32 and s_bfe_i32 followed by v_cndmask_b32_e64 ..., src_scc and s_cselect_b32, evaluating the generated statements to check clearing a prior scc = 1 (zero field and width == 0), setting scc = 1 after a prior scc = 0 (nonzero unsigned and negative signed extracts), and preserving scc across an intervening v_bfe_u32.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066619659 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/128#issuecomment-7066619659). Originally posted by [KomaGuy](https://github.com/KomaGuy) on 2026-11-21T17:37:19Z. --- Updated in `03c61dc` to preserve scalar BFE `SCC` semantics: - `s_bfe_u32` and `s_bfe_i32` now emit `scc = <dest> != 0u;` (`v_bfe_*` leaves `scc` untouched), `predecessor()` declares `scc`, and `s_cselect_b32` is supported alongside `src_scc` consumers. - Added assembled `gfx1013` fixtures in `tests/ffx_dispatch_aot_check.py` covering `s_bfe_u32` and `s_bfe_i32` followed by `v_cndmask_b32_e64 ..., src_scc` and `s_cselect_b32`, evaluating the generated statements to check clearing a prior `scc = 1` (zero field and `width == 0`), setting `scc = 1` after a prior `scc = 0` (nonzero unsigned and negative signed extracts), and preserving `scc` across an intervening `v_bfe_u32`.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
R0Y00/Xbox2Windows!127
No description provided.