fix(recompiler): apply the S_BFE_U32 width when the vertex fetch scanner folds constants #2371

Merged
fisherjoey merged 1 commit from upstream-pr/2371 into main 2026-10-10 06:16:17 +00:00
fisherjoey commented 2026-10-09 16:40:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T16:40:36Z.


Closes #2370

What

The embedded vertex fetch scanner folded S_BFE_U32 on constant SGPRs as a plain right shift and ignored the width field. It now calls scalarBfeU32, which matches the translator's sBfeU32: offset S1 & 31, width (S1 >> 16) & 0x7f clamped to 32, and 0 when the width is 0.

The other ops that reach that switch are S_AND_B32, S_LSHL_B32, S_ADD_I32 and S_ADD_U32 (isAttributePropagationAlu). The two adds land in the default case, which adds, so those are correct.

Tested

New test agc_embedded_vertex_fetch. It decodes a vertex program that builds an SMEM offset with s_mov_b32 and s_bfe_u32, loads a buffer descriptor from the buffer table at that offset, and fetches with buffer_load_format_xyzw on the vertex index. It checks the buffer slot the plan records.

case expected slot main d70b8998 this branch
S0 0x9e000000, S1 0x001e0000 (width 30) 31457280 165675008 31457280
S0 0xfff00320, S1 0x00080004 (width 8, offset 4) 3 16773123 3
S0 0x00000320, S1 0 (width 0) 0 50 0
S0 0x00000320, S1 0x007f0000 (width above 32) 50 50 50
  • ctest -R agc_ -j1 on Linux (Nobara 44, GCC 16): 257 of 258 on an RTX 5070 Ti (NVIDIA 595.91.07) and 258 of 258 on RADV (Ryzen 5 7600X iGPU, Mesa 26.2.1). The NVIDIA failure, agc_driver_dcc_keys_first_write, fails the same way on main d70b8998.
  • tools/check_conventions.py: passes.
  • Windows: not tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw
  • No notes, investigation .md files or images added to the repository
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4803249058 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2371). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T16:40:36Z. --- Closes #2370 ### What The embedded vertex fetch scanner folded `S_BFE_U32` on constant SGPRs as a plain right shift and ignored the width field. It now calls `scalarBfeU32`, which matches the translator's `sBfeU32`: offset `S1 & 31`, width `(S1 >> 16) & 0x7f` clamped to 32, and 0 when the width is 0. The other ops that reach that switch are `S_AND_B32`, `S_LSHL_B32`, `S_ADD_I32` and `S_ADD_U32` (`isAttributePropagationAlu`). The two adds land in the `default` case, which adds, so those are correct. ### Tested New test `agc_embedded_vertex_fetch`. It decodes a vertex program that builds an SMEM offset with `s_mov_b32` and `s_bfe_u32`, loads a buffer descriptor from the buffer table at that offset, and fetches with `buffer_load_format_xyzw` on the vertex index. It checks the buffer slot the plan records. | case | expected slot | `main` d70b8998 | this branch | |---|---|---|---| | S0 0x9e000000, S1 0x001e0000 (width 30) | 31457280 | 165675008 | 31457280 | | S0 0xfff00320, S1 0x00080004 (width 8, offset 4) | 3 | 16773123 | 3 | | S0 0x00000320, S1 0 (width 0) | 0 | 50 | 0 | | S0 0x00000320, S1 0x007f0000 (width above 32) | 50 | 50 | 50 | - `ctest -R agc_ -j1` on Linux (Nobara 44, GCC 16): 257 of 258 on an RTX 5070 Ti (NVIDIA 595.91.07) and 258 of 258 on RADV (Ryzen 5 7600X iGPU, Mesa 26.2.1). The NVIDIA failure, `agc_driver_dcc_keys_first_write`, fails the same way on `main` d70b8998. - `tools/check_conventions.py`: passes. - Windows: not tested. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw - [x] No notes, investigation `.md` files or images added to the repository - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Kikoarl commented 2026-10-09 17:44:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:44:12Z.


Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Release, -DBUILD_TESTING=ON, #2371 (f00c2f2d) tested on current main (0fdda7be).

  • tests/agc_embedded_vertex_fetch_tests: passes cleanly.
  • tools/check_conventions.py: passes cleanly.
  • Confirms proper bitfield extract width application during vertex fetch constant folding.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086154431 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2371#issuecomment-6086154431). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:44:12Z. --- Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Release, `-DBUILD_TESTING=ON`, #2371 (`f00c2f2d`) tested on current `main` (`0fdda7be`). - `tests/agc_embedded_vertex_fetch_tests`: passes cleanly. - `tools/check_conventions.py`: passes cleanly. - Confirms proper bitfield extract width application during vertex fetch constant folding.
popcoder565 commented 2026-10-09 19:42:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T19:42:34Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head f00c2f2d, merged onto main 8b393b6e in one build with 31 other PRs that had no Windows run.

  • This PR's tests pass: agc_embedded_vertex_fetch.
  • Full ctest in that build: 520 of 521. The one failure is guest_offline_imports, broken by #2359 (reported there).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088020528 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2371#issuecomment-6088020528). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T19:42:34Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `f00c2f2d`, merged onto `main` 8b393b6e in one build with 31 other PRs that had no Windows run. - This PR's tests pass: `agc_embedded_vertex_fetch`. - Full ctest in that build: 520 of 521. The one failure is `guest_offline_imports`, broken by #2359 (reported there). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
fma-rc commented 2026-10-10 01:27:03 +00:00 (Migrated from github.com)

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


Hardware measurement of s_bfe_u32, 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; ieee 0, dx10_clamp 1, denorm32 0, denorm16 3, round32 0, round16 0, fp16_overflow 0.

405 rows: sources 0x9e000000, 0xfff00320, 0x00000320, 0xffffffff, 0x80000001 × offsets 0, 4, 31 × widths 0, 1, 8, 30, 31, 32, 33, 64, 127, each with the remaining S1 bits clear, with bit 23 set, and with 0xff00ffe0 set. Every row equals this PR's scalarBfeU32 (compiled unchanged); main's src0 >> (src1 & 31) differs on 123 of them. SCC is result != 0 in every row.

PR head f00c2f2d merged onto main 0c0dc1e9, Linux, GCC 16.2.1, Release: agc_embedded_vertex_fetch passes. With EmbeddedVertexFetch.cpp reverted to main it fails with slots 165675008, 16773123 and 50, as in the PR's table.

Not measured: gfx1013.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092171768 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2371#issuecomment-6092171768). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-10T01:27:03Z. --- Hardware measurement of `s_bfe_u32`, 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; ieee 0, dx10_clamp 1, denorm32 0, denorm16 3, round32 0, round16 0, fp16_overflow 0. 405 rows: sources 0x9e000000, 0xfff00320, 0x00000320, 0xffffffff, 0x80000001 × offsets 0, 4, 31 × widths 0, 1, 8, 30, 31, 32, 33, 64, 127, each with the remaining S1 bits clear, with bit 23 set, and with 0xff00ffe0 set. Every row equals this PR's `scalarBfeU32` (compiled unchanged); `main`'s `src0 >> (src1 & 31)` differs on 123 of them. SCC is `result != 0` in every row. PR head f00c2f2d merged onto `main` 0c0dc1e9, Linux, GCC 16.2.1, Release: `agc_embedded_vertex_fetch` passes. With `EmbeddedVertexFetch.cpp` reverted to `main` it fails with slots 165675008, 16773123 and 50, as in the PR's table. Not measured: gfx1013. AI-assisted: yes.
fisherjoey commented 2026-10-10 01:41:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-10T01:41:54Z.


@fma-rc thanks for the hardware rows. The widths 33, 64 and 127 also settle the one thing I hadn't checked on hardware: a width of 32 or more keeps every bit above the offset, rather than wrapping modulo 32. That matches the translator's extractBits32, which this PR copies.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092296970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2371#issuecomment-6092296970). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-10T01:41:54Z. --- @fma-rc thanks for the hardware rows. The widths 33, 64 and 127 also settle the one thing I hadn't checked on hardware: a width of 32 or more keeps every bit above the offset, rather than wrapping modulo 32. That matches the translator's `extractBits32`, which this PR copies. AI-assisted: yes
adriadam10 commented 2026-10-10 06:16:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:16:14Z.


Checked on a merged tree: agc_embedded_vertex_fetch passes and fails with the fix reverted; HW oracle (gfx1035, 405 rows over sources × offsets × widths 0..127 × high S1 bits) matches scalarBfeU32 on every row, main differs on 123. Consistent with the runs by @Kikoarl, @popcoder565 and @fma-rc. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094563893 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2371#issuecomment-6094563893). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:16:14Z. --- Checked on a merged tree: `agc_embedded_vertex_fetch` passes and fails with the fix reverted; HW oracle (gfx1035, 405 rows over sources × offsets × widths 0..127 × high S1 bits) matches `scalarBfeU32` on every row, `main` differs on 123. Consistent with the runs by @Kikoarl, @popcoder565 and @fma-rc. Merging.
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!2371
No description provided.