fix(shader): truncate overflowing 32-bit bitfield masks #1605

Merged
QRMOE merged 8 commits from upstream-pr/1605 into main 2026-10-10 05:58:21 +00:00
QRMOE commented 2026-10-08 14:11:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-08T14:11:52Z.


What

Fixes S_BFM_B32 and V_BFM_B32 lowering when the masked width and offset add to more than 32. Construct the low-bit mask at offset zero and shift in 32-bit arithmetic so overflow bits are discarded. Width/offset masking, vector EXEC masking and scalar SCC preservation remain intact. For width 2 and offset 31, the expected result is 0x80000000.

Semantics: AMD RDNA 1.0 ISA, section 12.1 opcode 36 and section 12.12 opcode 867; SPIR-V OpBitFieldInsert.

The GPU regression uses zero-stride byte buffers with OOB_SELECT=3, the raw bounds mode in RDNA ISA section 8.1, Table 35. The earlier fixture selected structured bounds with stride zero and dropped stores when run against current main. The descriptor is corrected; all result and SCC assertions remain, with actual/expected values added to failure messages. The technical-debt entry is placed with related shader entries to avoid shared insertion conflicts.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 UCRT/POSIX/SEH, Ninja Release, BUILD_TESTING=ON, ANYPS5_ENABLE_SPIRV_TOOLS=ON; updated to main d70b89989473ba1f6ae13e44e079e67f1f8a44b0.

The complete default project build succeeded on the combined validation tree containing these two independent PRs. Seven focused tests passed: guest_hostname, guest_errors, guest_standard_streams, bitfield_mask, agc_driver_bitfield_mask, agc_driver_scalar_sop2, and agc_driver_vop3_integer_alu. The four shader tests also passed with APS5_SPIRV_OPT=full and none. ANYPS5_REQUIRE_VULKAN=1 required actual GPU execution on the AMD Radeon RX 9070 XT. Conventions, whitespace and test registration checks passed. The complete CTest suite was not run.

bitfield_mask checks 32,768 translation/constant-folding cases. agc_driver_bitfield_mask executes 4,096 scalar/vector input pairs, checks SCC=0 and SCC=1 preservation and passes in all three optimization modes. Shader lowering is unchanged from the previously reviewed head. The updated GPU fixture has not been rerun on Linux. The earlier independent Linux/NVIDIA/RADV report applies to the previous head.

Expected results come from the ISA; native RDNA hardware-oracle measurements and game compatibility remain unverified and recorded in docs/dev/TechnicalDebt.md. Decoder coverage is unchanged.

Checklist

  • Updated to current main
  • One topic per PR
  • No new code comments
  • No silent success stubs introduced
  • Reports and logs kept outside the repository
  • No third-party dependencies added
  • Generic behavior
  • Depends on: none
  • AI-assisted: yes — OpenAI Codex assisted with code, tests and this description.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4788099620 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1605). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-08T14:11:52Z. --- ### What Fixes `S_BFM_B32` and `V_BFM_B32` lowering when the masked width and offset add to more than 32. Construct the low-bit mask at offset zero and shift in 32-bit arithmetic so overflow bits are discarded. Width/offset masking, vector EXEC masking and scalar SCC preservation remain intact. For width 2 and offset 31, the expected result is `0x80000000`. Semantics: [AMD RDNA 1.0 ISA](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna-shader-instruction-set-architecture.pdf), section 12.1 opcode 36 and section 12.12 opcode 867; [SPIR-V OpBitFieldInsert](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpBitFieldInsert). The GPU regression uses zero-stride byte buffers with `OOB_SELECT=3`, the raw bounds mode in RDNA ISA section 8.1, Table 35. The earlier fixture selected structured bounds with stride zero and dropped stores when run against current main. The descriptor is corrected; all result and SCC assertions remain, with actual/expected values added to failure messages. The technical-debt entry is placed with related shader entries to avoid shared insertion conflicts. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 UCRT/POSIX/SEH, Ninja Release, `BUILD_TESTING=ON`, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`; updated to main `d70b89989473ba1f6ae13e44e079e67f1f8a44b0`. The complete default project build succeeded on the combined validation tree containing these two independent PRs. Seven focused tests passed: `guest_hostname`, `guest_errors`, `guest_standard_streams`, `bitfield_mask`, `agc_driver_bitfield_mask`, `agc_driver_scalar_sop2`, and `agc_driver_vop3_integer_alu`. The four shader tests also passed with `APS5_SPIRV_OPT=full` and `none`. `ANYPS5_REQUIRE_VULKAN=1` required actual GPU execution on the AMD Radeon RX 9070 XT. Conventions, whitespace and test registration checks passed. The complete CTest suite was not run. `bitfield_mask` checks 32,768 translation/constant-folding cases. `agc_driver_bitfield_mask` executes 4,096 scalar/vector input pairs, checks SCC=0 and SCC=1 preservation and passes in all three optimization modes. Shader lowering is unchanged from the previously reviewed head. The updated GPU fixture has not been rerun on Linux. The earlier independent Linux/NVIDIA/RADV report applies to the previous head. Expected results come from the ISA; native RDNA hardware-oracle measurements and game compatibility remain unverified and recorded in `docs/dev/TechnicalDebt.md`. Decoder coverage is unchanged. ### Checklist - [x] Updated to current main - [x] One topic per PR - [x] No new code comments - [x] No silent success stubs introduced - [x] Reports and logs kept outside the repository - [x] No third-party dependencies added - [x] Generic behavior - [x] Depends on: none - [x] AI-assisted: yes — OpenAI Codex assisted with code, tests and this description.
fisherjoey commented 2026-10-09 01:22:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T01:22:25Z.


Linux results on NVIDIA and RADV, since this was tested on Windows with an RX 9070 XT. Everything passes here, including the new bitfield_mask and agc_driver_bitfield_mask.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main 29e533ac.

main 29e533ac main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 183 / 183 184 / 184
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 183 / 183 184 / 184
CPU tests (-E agc_driver) 279 / 280 280 / 281

The one CPU failure is guest_dynamic_loader (subprocess aborted), which fails the same way on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6072325140 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1605#issuecomment-6072325140). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T01:22:25Z. --- Linux results on NVIDIA and RADV, since this was tested on Windows with an RX 9070 XT. Everything passes here, including the new `bitfield_mask` and `agc_driver_bitfield_mask`. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` 29e533ac. | | `main` 29e533ac | `main` + this PR | |---|---|---| | RTX 5070 Ti, driver 595.91.07, `ctest -R agc_driver -j1` | 183 / 183 | 184 / 184 | | RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same | 183 / 183 | 184 / 184 | | CPU tests (`-E agc_driver`) | 279 / 280 | 280 / 281 | The one CPU failure is `guest_dynamic_loader` (subprocess aborted), which fails the same way on `main`.
fma-rc commented 2026-10-10 01:27:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-10T01:27:02Z.


Hardware measurement for the TechnicalDebt entry this PR adds, and a run of the PR.

Radeon 610M (gfx1036), tools/hw-oracle at main 0c0dc1e9, Linux 7.2.9, hsa-rocr 7.2.4, clang 23.1.1. Controls: ieee 0, dx10_clamp 1, denorm32 0, denorm16 3, round32 0, round16 0, fp16_overflow 0.

Inputs are the test's own domain: widths 0–31 × offsets 0–31 × upper bits 0, 32, 0x80000000, 0xffffffe0 (4,096 pairs).

  • v_bfm_b32, wave32 and wave64: 8,192 rows, all equal to expectedMask from tests/execution/BitfieldMask.cpp (compiled unchanged).
  • s_bfm_b32, wave32, SCC preset to 0 and to 1: 8,192 rows, all equal to expectedMask; SCC unchanged in every row.

PR head ca16eaa4 merged onto main 0c0dc1e9, Release, GCC 16.2.1: bitfield_mask, agc_driver_bitfield_mask, agc_driver_scalar_sop2 and agc_driver_vop3_integer_alu pass on an RTX 5080 (615.71.09) and on RADV (Mesa 26.1.4, same Radeon 610M). With IntegerInstructions.cpp reverted to main, agc_driver_bitfield_mask still passes on both devices and bitfield_mask fails (s_bfm_b32 width=2 offset=31 did not fold to the expected mask).

Not measured: gfx1013; s_bfm_b32 in wave64.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092171593 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1605#issuecomment-6092171593). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-10T01:27:02Z. --- Hardware measurement for the TechnicalDebt entry this PR adds, and a run of the PR. Radeon 610M (gfx1036), `tools/hw-oracle` at `main` 0c0dc1e9, Linux 7.2.9, hsa-rocr 7.2.4, clang 23.1.1. Controls: ieee 0, dx10_clamp 1, denorm32 0, denorm16 3, round32 0, round16 0, fp16_overflow 0. Inputs are the test's own domain: widths 0–31 × offsets 0–31 × upper bits 0, 32, 0x80000000, 0xffffffe0 (4,096 pairs). - `v_bfm_b32`, wave32 and wave64: 8,192 rows, all equal to `expectedMask` from `tests/execution/BitfieldMask.cpp` (compiled unchanged). - `s_bfm_b32`, wave32, SCC preset to 0 and to 1: 8,192 rows, all equal to `expectedMask`; SCC unchanged in every row. PR head ca16eaa4 merged onto `main` 0c0dc1e9, Release, GCC 16.2.1: `bitfield_mask`, `agc_driver_bitfield_mask`, `agc_driver_scalar_sop2` and `agc_driver_vop3_integer_alu` pass on an RTX 5080 (615.71.09) and on RADV (Mesa 26.1.4, same Radeon 610M). With `IntegerInstructions.cpp` reverted to `main`, `agc_driver_bitfield_mask` still passes on both devices and `bitfield_mask` fails (`s_bfm_b32 width=2 offset=31 did not fold to the expected mask`). Not measured: gfx1013; `s_bfm_b32` in wave64. AI-assisted: yes.
adriadam10 commented 2026-10-10 05:58:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T05:58:03Z.


Checked s_bfm_b32/v_bfm_b32 on RDNA2 hardware (gfx1035): all 1024 width/offset pairs with upper operand bits 0, 32, 0x80000000 and 0xffffffe0, plus width/offset ≥ 32. 4103 rows, 0 mismatches against the new lowering. SCC is unchanged on s_bfm_b32, and width 2/offset 31 gives 0x80000000. Build and ctest are green. bitfield_mask fails without the fix.

Added on your branch:

  • Rebased onto main without the merge commit.
  • agc_driver_bitfield_mask now skips when the subgroup size is below 32, as WriteLane.cpp does. Its scalar half uses v_writelane, and it failed on CI's lavapipe (8-lane subgroups) with bitfield mask width=1 offset=8 scalar result 1, expected 256.
  • The TechnicalDebt line now says that the results match gfx1035 and were not measured on the console.

Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094428100 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1605#issuecomment-6094428100). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T05:58:03Z. --- Checked `s_bfm_b32`/`v_bfm_b32` on RDNA2 hardware (gfx1035): all 1024 width/offset pairs with upper operand bits 0, 32, 0x80000000 and 0xffffffe0, plus width/offset ≥ 32. 4103 rows, 0 mismatches against the new lowering. SCC is unchanged on `s_bfm_b32`, and width 2/offset 31 gives `0x80000000`. Build and ctest are green. `bitfield_mask` fails without the fix. Added on your branch: - Rebased onto main without the merge commit. - `agc_driver_bitfield_mask` now skips when the subgroup size is below 32, as `WriteLane.cpp` does. Its scalar half uses `v_writelane`, and it failed on CI's lavapipe (8-lane subgroups) with `bitfield mask width=1 offset=8 scalar result 1, expected 256`. - The TechnicalDebt line now says that the results match gfx1035 and were not measured on the console. Merging once CI is green.
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
boykopovar/AnyPS5!1605
No description provided.