fix(shader): keep the 24-bit factor reads of 16-bit multiplies #8280

Merged
thuanquang merged 1 commit from upstream-pr/8280 into main 2026-10-20 12:30:18 +00:00
thuanquang commented 2026-10-20 08:24:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-20T08:24:13Z.


What

Fixes #8271: agc_driver_mul_lo_u16_high_half fails on AMD's Windows driver 32.0.21045.5002 since #7673 (also reported for an RDNA 2 iGPU in #8266).

#7018 reads the low-half factors of v_mul_lo_u16 and v_pk_mul_lo_u16 as 24-bit fields (BitFieldUExtract(raw, 0, 24)). That is what the GPU does, and it also hides the factors' width from AMD's Windows driver, which stores the full 32-bit product when it knows both factors fit in 16 bits (the 16-bit integer multiplies entry in shader.md). #7673 added a fold of BitFieldUExtract(x, 0, n) into x when x has no bits at or above n. When the program has already masked both factors to 16 bits (v_and_b32 v4, 0xffff, v4), that fold removes the 24-bit reads, and the driver is back to (x & 0xffff) * (y & 0xffff) & 0xffff and stores 0xfffe0001 for 0xffff * 0xffff.

The folder now leaves a BitFieldUExtract in place when an IMul32 uses it. The fold still applies to every other field read. The debt entry says so.

Tested

Windows 11 26200, RX 6700 XT (gfx1031), AMD driver 32.0.21045.5002, Release, ANYPS5_REQUIRE_VULKAN=1, on main 7d876ad3b:

agc_driver_mul_lo_u16_high_half agc_constant_bit_test
this PR passes 3/3, all six cases passes 3/3
this PR with the IMul32 check removed fails 3/3: thread 0 (0xffffffff, 0xffffffff, 0x00000000) wrote 0xfffe0001, expected 0x00000001 fails: a 24-bit field read that a multiply uses is kept, even for a factor masked to 16 bits
  • main a98590dec fails the GPU test the same way, and main with #7673 reverted passes it 3/3.
  • In the SPIR-V dumped with APS5_DUMP_COMPUTE_PIPELINES for the masked-factors case, the module on main differs from the one with #7673 reverted only in the two OpBitFieldUExtract ... 0 24 factor reads (and a repeated & 0xffff), see #8271.
  • agc_constant_bit_test gains two cases that run without a GPU, so CI (lavapipe, which doesn't miscompile this) still catches the fold coming back. One checks that a 24-bit field read of a value masked to 16 bits still folds into the value; the other, that the same read is kept when a multiply uses it.
  • Khronos validation layer (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, layer built from Vulkan-ValidationLayers; with VK_LOADER_DEBUG=layer this tree's run logs Insert instance layer "VK_LAYER_KHRONOS_validation"), ANYPS5_NO_SHADER_CACHE=1, ctest -V on agc_constant_bit_test, agc_driver_mul_lo_u16_high_half, agc_integer_narrowing and agc_driver_sdwa_integer_clamp: 0 VUID- messages on main a98590dec (where the GPU test fails as above) and with this PR (4 of 4 pass), and no was not found but was requested line.
  • Full ctest --parallel 4 --timeout 120 on main 7d876ad3b with this PR: 1717 tests, 2 failed: log_macros and log_macros_timestamps, because ccache rewrites __FILE__ in local builds here.

Not tested: other AMD GPUs and driver versions; #8266 has the same failure on an RDNA 2 iGPU with this driver.

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 (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: Fixes #8271
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6700 XT (gfx1031), AMD Windows driver 32.0.21045.5002, Windows 11 26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4933066738 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8280). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-20T08:24:13Z. --- ### What Fixes #8271: `agc_driver_mul_lo_u16_high_half` fails on AMD's Windows driver 32.0.21045.5002 since #7673 (also reported for an RDNA 2 iGPU in #8266). #7018 reads the low-half factors of `v_mul_lo_u16` and `v_pk_mul_lo_u16` as 24-bit fields (`BitFieldUExtract(raw, 0, 24)`). That is what the GPU does, and it also hides the factors' width from AMD's Windows driver, which stores the full 32-bit product when it knows both factors fit in 16 bits (the 16-bit integer multiplies entry in `shader.md`). #7673 added a fold of `BitFieldUExtract(x, 0, n)` into `x` when `x` has no bits at or above `n`. When the program has already masked both factors to 16 bits (`v_and_b32 v4, 0xffff, v4`), that fold removes the 24-bit reads, and the driver is back to `(x & 0xffff) * (y & 0xffff) & 0xffff` and stores 0xfffe0001 for 0xffff * 0xffff. The folder now leaves a `BitFieldUExtract` in place when an `IMul32` uses it. The fold still applies to every other field read. The debt entry says so. ### Tested Windows 11 26200, RX 6700 XT (gfx1031), AMD driver 32.0.21045.5002, Release, `ANYPS5_REQUIRE_VULKAN=1`, on `main` `7d876ad3b`: | | `agc_driver_mul_lo_u16_high_half` | `agc_constant_bit_test` | |---|---|---| | this PR | passes 3/3, all six cases | passes 3/3 | | this PR with the `IMul32` check removed | fails 3/3: `thread 0 (0xffffffff, 0xffffffff, 0x00000000) wrote 0xfffe0001, expected 0x00000001` | fails: `a 24-bit field read that a multiply uses is kept, even for a factor masked to 16 bits` | - `main` `a98590dec` fails the GPU test the same way, and `main` with #7673 reverted passes it 3/3. - In the SPIR-V dumped with `APS5_DUMP_COMPUTE_PIPELINES` for the masked-factors case, the module on `main` differs from the one with #7673 reverted only in the two `OpBitFieldUExtract ... 0 24` factor reads (and a repeated `& 0xffff`), see #8271. - `agc_constant_bit_test` gains two cases that run without a GPU, so CI (lavapipe, which doesn't miscompile this) still catches the fold coming back. One checks that a 24-bit field read of a value masked to 16 bits still folds into the value; the other, that the same read is kept when a multiply uses it. - Khronos validation layer (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, layer built from Vulkan-ValidationLayers; with `VK_LOADER_DEBUG=layer` this tree's run logs `Insert instance layer "VK_LAYER_KHRONOS_validation"`), `ANYPS5_NO_SHADER_CACHE=1`, `ctest -V` on `agc_constant_bit_test`, `agc_driver_mul_lo_u16_high_half`, `agc_integer_narrowing` and `agc_driver_sdwa_integer_clamp`: 0 `VUID-` messages on `main` `a98590dec` (where the GPU test fails as above) and with this PR (4 of 4 pass), and no `was not found but was requested` line. - Full `ctest --parallel 4 --timeout 120` on `main` `7d876ad3b` with this PR: 1717 tests, 2 failed: `log_macros` and `log_macros_timestamps`, because ccache rewrites `__FILE__` in local builds here. Not tested: other AMD GPUs and driver versions; #8266 has the same failure on an RDNA 2 iGPU with this driver. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: Fixes #8271 - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6700 XT (gfx1031), AMD Windows driver 32.0.21045.5002, Windows 11 26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 08:29:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T08:29:05Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8315 docs/dev/debt/shader.md L118

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libSceAgcDriver/tests/ConstantBitTest.cpp
core/shader/recompiler/Optimization/src/ConstantFolder.cpp
docs/dev/debt/shader.md
PR 5371 docs/dev/debt/shader.md
PR 5426 docs/dev/debt/shader.md
PR 5500 core/shader/recompiler/Optimization/src/ConstantFolder.cpp
docs/dev/debt/shader.md
PR 5941 docs/dev/debt/shader.md
PR 6009 docs/dev/debt/shader.md
PR 6235 docs/dev/debt/shader.md
PR 6280 docs/dev/debt/shader.md
PR 6412 docs/dev/debt/shader.md
PR 6488 docs/dev/debt/shader.md
PR 6977 docs/dev/debt/shader.md
PR 7085 docs/dev/debt/shader.md
PR 7223 docs/dev/debt/shader.md
PR 7332 docs/dev/debt/shader.md
PR 7392 docs/dev/debt/shader.md
PR 7395 docs/dev/debt/shader.md
PR 7484 docs/dev/debt/shader.md
PR 7684 docs/dev/debt/shader.md
PR 7922 docs/dev/debt/shader.md
PR 8004 docs/dev/debt/shader.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6286808002 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8280#issuecomment-6286808002). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T08:29:05Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 8315](https://www.github.com/boykopovar/AnyPS5/pull/8315) | `docs/dev/debt/shader.md` [L118](https://github.com/boykopovar/AnyPS5/blob/02fb1ce5324304f292453b9dd458cea2e3633160/docs/dev/debt/shader.md#L118) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libSceAgcDriver/tests/ConstantBitTest.cpp`<br>`core/shader/recompiler/Optimization/src/ConstantFolder.cpp`<br>`docs/dev/debt/shader.md` | | [PR 5371](https://www.github.com/boykopovar/AnyPS5/pull/5371) | `docs/dev/debt/shader.md` | | [PR 5426](https://www.github.com/boykopovar/AnyPS5/pull/5426) | `docs/dev/debt/shader.md` | | [PR 5500](https://www.github.com/boykopovar/AnyPS5/pull/5500) | `core/shader/recompiler/Optimization/src/ConstantFolder.cpp`<br>`docs/dev/debt/shader.md` | | [PR 5941](https://www.github.com/boykopovar/AnyPS5/pull/5941) | `docs/dev/debt/shader.md` | | [PR 6009](https://www.github.com/boykopovar/AnyPS5/pull/6009) | `docs/dev/debt/shader.md` | | [PR 6235](https://www.github.com/boykopovar/AnyPS5/pull/6235) | `docs/dev/debt/shader.md` | | [PR 6280](https://www.github.com/boykopovar/AnyPS5/pull/6280) | `docs/dev/debt/shader.md` | | [PR 6412](https://www.github.com/boykopovar/AnyPS5/pull/6412) | `docs/dev/debt/shader.md` | | [PR 6488](https://www.github.com/boykopovar/AnyPS5/pull/6488) | `docs/dev/debt/shader.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `docs/dev/debt/shader.md` | | [PR 7085](https://www.github.com/boykopovar/AnyPS5/pull/7085) | `docs/dev/debt/shader.md` | | [PR 7223](https://www.github.com/boykopovar/AnyPS5/pull/7223) | `docs/dev/debt/shader.md` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `docs/dev/debt/shader.md` | | [PR 7392](https://www.github.com/boykopovar/AnyPS5/pull/7392) | `docs/dev/debt/shader.md` | | [PR 7395](https://www.github.com/boykopovar/AnyPS5/pull/7395) | `docs/dev/debt/shader.md` | | [PR 7484](https://www.github.com/boykopovar/AnyPS5/pull/7484) | `docs/dev/debt/shader.md` | | [PR 7684](https://www.github.com/boykopovar/AnyPS5/pull/7684) | `docs/dev/debt/shader.md` | | [PR 7922](https://www.github.com/boykopovar/AnyPS5/pull/7922) | `docs/dev/debt/shader.md` | | [PR 8004](https://www.github.com/boykopovar/AnyPS5/pull/8004) | `docs/dev/debt/shader.md` |
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!8280
No description provided.