Skip to content

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

Merged
adriadam10 merged 8 commits into
boykopovar:mainfrom
QRMOE:fix/shader-bitfield-mask-overflow
Oct 10, 2026
Merged

adriadam10 merged 8 commits into
boykopovar:mainfrom
QRMOE:fix/shader-bitfield-mask-overflow

Conversation

@QRMOE

@QRMOE QRMOE commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

@fisherjoey

Copy link
Copy Markdown
Contributor

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 29e533a.

main 29e533a 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.

QRMOE and others added 8 commits October 10, 2026 02:13
Construct the low-bit mask at offset zero, then shift it in 32-bit arithmetic. AI-assisted: yes.
Cover all width/offset pairs and independent ignored upper operand bits for scalar and vector masks. AI-assisted: yes.
Exercise 4096 width/offset inputs through Vulkan and preserve both SCC values. AI-assisted: yes.
Register the regression with CTest and configure Windows runtime lookup. AI-assisted: yes.
AI-assisted: yes. Full current-main project build passed. Seven focused Windows tests pass; four shader tests also pass in full and none optimization modes with actual Vulkan execution required. Updated Linux and title runs remain pending.
AI-assisted: yes. Full current-main project build passed. Seven focused Windows tests pass; four shader tests also pass in full and none optimization modes with actual Vulkan execution required. Updated Linux and title runs remain pending.
…groups

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@fma-rc

fma-rc commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

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

Radeon 610M (gfx1036), tools/hw-oracle at main 0c0dc1e, 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 ca16eaa merged onto main 0c0dc1e, 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

Copy link
Copy Markdown
Collaborator

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.

@adriadam10
adriadam10 force-pushed the fix/shader-bitfield-mask-overflow branch from ca16eaa to 6fee7e8 Compare October 10, 2026 05:58
@adriadam10
adriadam10 merged commit a223621 into boykopovar:main Oct 10, 2026
1 check passed
SleglJan pushed a commit to SleglJan/AnyPS5 that referenced this pull request Oct 10, 2026
…k-overflow

fix(shader): truncate overflowing 32-bit bitfield masks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants