fix(shader): read low-half 16-bit multiply factors as 24-bit fields #7018

Merged
thuanquang merged 1 commit from upstream-pr/7018 into main 2026-10-17 11:30:09 +00:00
thuanquang commented 2026-10-17 05:53:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-17T05:53:29Z.


What

On AMD's Windows driver, v_mul_lo_u16 and v_pk_mul_lo_u16 can store the full 32-bit product instead of its low 16 bits: into a register whose high half is zero, 0xffff * 0xffff came out as 0xfffe0001 where the GPU writes 0x00000001.

The recompiler extracts the 16-bit factors, multiplies them and masks the product with 0xffff before the 16-bit write. The SPIR-V it emits is correct: dumped from the recompile result, it has OpIMul of the two extracted values followed by OpBitwiseAnd with 0xffff. On driver 32.0.21045.5002 (RX 6700 XT) the stored value keeps all 32 bits when both factors are values the driver knows fit in 16 bits, the product is reduced to 16 bits, and nothing else is merged into the stored value. Measured shapes, each a separate shader:

Shape main vs the GPU
v_and_b32 v10, 0xffff, v6 then v_mul_lo_u16 v10, v4, v5 wrong
v_add_nc_u16 into a cleared v10, then v_mul_lo_u16 v10, v10, v5 wrong
factors masked to 16 bits by the program, destination high half zero wrong
v_pk_mul_lo_u16 of factors whose high halves are zero wrong
v_mov_b32 v10, v6 (unknown old value) then v_mul_lo_u16 correct
v_pk_mul_lo_u16 of factors whose low halves are zero, or of any values correct
the same product reduced by v_and_b32, v_bfe_u32 or a shift pair, 16-bit factors wrong
factors extracted as 24-bit fields, or v_mul_u32_u24, then v_and_b32 0xffff correct

A factor taken from the low half of its source is now read as the low 24 bits of the register (readU16Factor); the low 16 bits of the product depend only on the low 16 bits of the factors, so the stored value is unchanged on drivers that keep the mask. High-half (op_sel) factors are read as before. The technical debt entry records the driver behaviour, since nothing else in the code says why the field is 24 bits wide.

The GPU side of the table comes from running the same instructions directly on the GPU with the tools/hw-oracle template, launched through the driver's HIP runtime; there the GPU also keeps the destination's old high half for these VOP3 16-bit ops (with 0xabcd0000 prefilled, 0xabcd survives), which the recompiler already models.

Tested

Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, ANYPS5_REQUIRE_VULKAN=1:

  • New agc_driver_mul_lo_u16_high_half: six single-multiply shaders over 64 threads of edge and random values, checking the whole register: the first five shapes of the table, plus v_pk_mul_lo_u16 with low halves zero. On main it fails: mul lo u16: v_mul_lo_u16 into a register whose high half is zero: thread 0 (0xffffffff, 0xffffffff, 0x00000000) wrote 0xfffe0001, expected 0x00000001. With this change it passes 3/3. On drivers that keep the mask it passes before and after, so lavapipe CI does not show the difference.
  • Against the GPU: the seven v_mul_lo_u16/v_pk_mul_lo_u16 shapes over 432 rows each: main 192, 256, 0, 192, 192, 0 and 0 mismatches; with this change 0 in every shape.
  • agc_driver_vop3_integer, agc_driver_packed_alu, agc_driver_packed_integer_clamp and agc_driver_packed_integer_negate pass.
  • Full ctest --parallel 4 --timeout 120 on main 2bfe0045d with this change: 1368 tests, 3 fail: agc_driver_lds_mixed_width_atomics (#5271) and agc_driver_layered_volume_vertex_no_gpl (#4768), which fail on main on this GPU too, and agc_driver_preparation_scope_settle, which failed once under --parallel 4, passes 5/5 alone and contains neither instruction.
  • python tools/check_conventions.py --base origin/main with this title and body passes.
  • Not tested: other AMD drivers (RADV, newer Adrenalin), NVIDIA, Intel, and titles.

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
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4897731581 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7018). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-17T05:53:29Z. --- ### What On AMD's Windows driver, `v_mul_lo_u16` and `v_pk_mul_lo_u16` can store the full 32-bit product instead of its low 16 bits: into a register whose high half is zero, `0xffff * 0xffff` came out as `0xfffe0001` where the GPU writes `0x00000001`. The recompiler extracts the 16-bit factors, multiplies them and masks the product with `0xffff` before the 16-bit write. The SPIR-V it emits is correct: dumped from the recompile result, it has `OpIMul` of the two extracted values followed by `OpBitwiseAnd` with `0xffff`. On driver 32.0.21045.5002 (RX 6700 XT) the stored value keeps all 32 bits when both factors are values the driver knows fit in 16 bits, the product is reduced to 16 bits, and nothing else is merged into the stored value. Measured shapes, each a separate shader: | Shape | `main` vs the GPU | |---|---| | `v_and_b32 v10, 0xffff, v6` then `v_mul_lo_u16 v10, v4, v5` | wrong | | `v_add_nc_u16` into a cleared `v10`, then `v_mul_lo_u16 v10, v10, v5` | wrong | | factors masked to 16 bits by the program, destination high half zero | wrong | | `v_pk_mul_lo_u16` of factors whose high halves are zero | wrong | | `v_mov_b32 v10, v6` (unknown old value) then `v_mul_lo_u16` | correct | | `v_pk_mul_lo_u16` of factors whose low halves are zero, or of any values | correct | | the same product reduced by `v_and_b32`, `v_bfe_u32` or a shift pair, 16-bit factors | wrong | | factors extracted as 24-bit fields, or `v_mul_u32_u24`, then `v_and_b32 0xffff` | correct | A factor taken from the low half of its source is now read as the low 24 bits of the register (`readU16Factor`); the low 16 bits of the product depend only on the low 16 bits of the factors, so the stored value is unchanged on drivers that keep the mask. High-half (`op_sel`) factors are read as before. The technical debt entry records the driver behaviour, since nothing else in the code says why the field is 24 bits wide. The GPU side of the table comes from running the same instructions directly on the GPU with the `tools/hw-oracle` template, launched through the driver's HIP runtime; there the GPU also keeps the destination's old high half for these VOP3 16-bit ops (with `0xabcd0000` prefilled, `0xabcd` survives), which the recompiler already models. ### Tested Windows 11 26200, AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, `ANYPS5_REQUIRE_VULKAN=1`: - New `agc_driver_mul_lo_u16_high_half`: six single-multiply shaders over 64 threads of edge and random values, checking the whole register: the first five shapes of the table, plus `v_pk_mul_lo_u16` with low halves zero. On `main` it fails: `mul lo u16: v_mul_lo_u16 into a register whose high half is zero: thread 0 (0xffffffff, 0xffffffff, 0x00000000) wrote 0xfffe0001, expected 0x00000001`. With this change it passes 3/3. On drivers that keep the mask it passes before and after, so lavapipe CI does not show the difference. - Against the GPU: the seven `v_mul_lo_u16`/`v_pk_mul_lo_u16` shapes over 432 rows each: `main` 192, 256, 0, 192, 192, 0 and 0 mismatches; with this change 0 in every shape. - `agc_driver_vop3_integer`, `agc_driver_packed_alu`, `agc_driver_packed_integer_clamp` and `agc_driver_packed_integer_negate` pass. - Full `ctest --parallel 4 --timeout 120` on `main` `2bfe0045d` with this change: 1368 tests, 3 fail: `agc_driver_lds_mixed_width_atomics` (#5271) and `agc_driver_layered_volume_vertex_no_gpl` (#4768), which fail on `main` on this GPU too, and `agc_driver_preparation_scope_settle`, which failed once under `--parallel 4`, passes 5/5 alone and contains neither instruction. - `python tools/check_conventions.py --base origin/main` with this title and body passes. - Not tested: other AMD drivers (RADV, newer Adrenalin), NVIDIA, Intel, and titles. ### 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](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] Unblocks: none - [x] GPUs tested: AMD Radeon RX 6700 XT (gfx1031), driver 32.0.21045.5002, Windows 11 26200 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-17 06:41:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T06:41:50Z.


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 4072 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
PR 5021 docs/dev/debt/shader.md
PR 5371 docs/dev/debt/shader.md
PR 5426 docs/dev/debt/shader.md
PR 5500 docs/dev/debt/shader.md
PR 5782 core/shader/recompiler/Translation/src/VectorInstructions.cpp
docs/dev/debt/shader.md
PR 5941 core/shader/recompiler/Translation/src/VectorInstructions.cpp
docs/dev/debt/shader.md
PR 6009 core/shader/recompiler/Translation/src/VectorInstructions.cpp
docs/dev/debt/shader.md
PR 6222 docs/dev/debt/shader.md
PR 6235 docs/dev/debt/shader.md
PR 6412 docs/dev/debt/shader.md
PR 6503 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp
core/shader/recompiler/Translation/src/VectorInstructions.cpp
PR 6767 docs/dev/debt/shader.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6230801499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7018#issuecomment-6230801499). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T06:41:50Z. --- <!-- pr-overlap --> 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 4072](https://www.github.com/boykopovar/AnyPS5/pull/4072) | `core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `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) | `docs/dev/debt/shader.md` | | [PR 5782](https://www.github.com/boykopovar/AnyPS5/pull/5782) | `core/shader/recompiler/Translation/src/VectorInstructions.cpp`<br>`docs/dev/debt/shader.md` | | [PR 5941](https://www.github.com/boykopovar/AnyPS5/pull/5941) | `core/shader/recompiler/Translation/src/VectorInstructions.cpp`<br>`docs/dev/debt/shader.md` | | [PR 6009](https://www.github.com/boykopovar/AnyPS5/pull/6009) | `core/shader/recompiler/Translation/src/VectorInstructions.cpp`<br>`docs/dev/debt/shader.md` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `docs/dev/debt/shader.md` | | [PR 6235](https://www.github.com/boykopovar/AnyPS5/pull/6235) | `docs/dev/debt/shader.md` | | [PR 6412](https://www.github.com/boykopovar/AnyPS5/pull/6412) | `docs/dev/debt/shader.md` | | [PR 6503](https://www.github.com/boykopovar/AnyPS5/pull/6503) | `core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp`<br>`core/shader/recompiler/Translation/src/VectorInstructions.cpp` | | [PR 6767](https://www.github.com/boykopovar/AnyPS5/pull/6767) | `docs/dev/debt/shader.md` |
LeandroLP23 commented 2026-10-17 08:08:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-17T08:08:57Z.


Another AMD driver, since the description lists other AMD drivers as untested.

Windows 11 (26300), Radeon 860M (RDNA 3.5, 1002:1114), driver 32.0.13058.8001, WinLibs GCC 15.2.0, Release, ANYPS5_REQUIRE_VULKAN=1. Tree: main 17cfb1f4c with #7018, #7034, #7036, #7038, #7040, #7041 and #7042 merged together (only docs/dev/debt/shader.md conflicted, for #7040); "main" is the same tree with the six recompiler files reverted to main and the tests kept.

  • agc_driver_mul_lo_u16_high_half: passes 3 of 3 with this change, and also passes on main: this driver keeps the 0xffff mask, as the description expects for drivers other than 32.0.21045.5002. So the change is neutral here.
  • agc_driver_packed_alu passes 3 of 3.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6231564382 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7018#issuecomment-6231564382). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-17T08:08:57Z. --- Another AMD driver, since the description lists other AMD drivers as untested. Windows 11 (26300), Radeon 860M (RDNA 3.5, `1002:1114`), driver 32.0.13058.8001, WinLibs GCC 15.2.0, Release, `ANYPS5_REQUIRE_VULKAN=1`. Tree: `main` `17cfb1f4c` with #7018, #7034, #7036, #7038, #7040, #7041 and #7042 merged together (only `docs/dev/debt/shader.md` conflicted, for #7040); "`main`" is the same tree with the six recompiler files reverted to `main` and the tests kept. - `agc_driver_mul_lo_u16_high_half`: passes 3 of 3 with this change, and **also passes on `main`**: this driver keeps the `0xffff` mask, as the description expects for drivers other than 32.0.21045.5002. So the change is neutral here. - `agc_driver_packed_alu` passes 3 of 3. AI-assisted: yes (Claude Code).
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!7018
No description provided.