fix(shader): lower the packed pixel ancillary when its extract operands fold after it #1904

Merged
fisherjoey merged 3 commits from upstream-pr/1904 into main 2026-10-10 06:04:58 +00:00
fisherjoey commented 2026-10-08 23:11:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T23:11:12Z.


Closes #1903

#1179 is merged; this PR is now one commit (6b41cf4d) on top of it.

What

#1179 lowers the packed pixel ancillary word in one pass after the fold loop, and only when every reader of the word extracts a modelled field. In real shaders the word also has an exec-masked SelectU32 copy. MaskedSelectEliminator removes that copy, but it runs after ConstantFolder and the fold does not run again. So while the fold runs, the copy is still a reader, #1179's check refuses the whole word, and ShaderInfoCollector then throws on the extract that still reads it.

When the check refuses the word, each in-range BitFieldUExtract / BitFieldSExtract that reads it (directly or through Identity copies) is now rewritten on its own, and the other readers keep the word. When every reader is an extract, #1179's whole-word lowering runs unchanged.

Tested

Linux (Nobara 44, GCC 16), RTX 5070 Ti with NVIDIA 595.91.07, and RADV on a Ryzen 5 7600X iGPU with Mesa 26.2.1.

New check ExtractVectorMaskedCopy() in core/libs/tests/PixelAncillary.cpp: the IR a vector v_bfe_u32 produces (masked offset, clamped count) plus a SelectU32(lane < 16, word, 0) copy of the word. After the fold, the extract must read the Layer (16, 11) or SampleId (8, 4) builtin. ExtractVector() is the same without the copy.

pixel_ancillary
#1179 b4e02e2d + the tests only ExtractVector passes, ExtractVectorMaskedCopy fails
this branch passes
  • ctest -R 'agc_|pixel_ancillary' -j1: 225 of 225 on NVIDIA and 225 of 225 on RADV.
  • tools/check_conventions.py --base b4e02e2d: passes.
  • Stray (PPSA02100): with #1179 alone, 0 of 4 S5a shaders replay offline and all 6 stay deferred in game, with a fatal error at draw. With this change on main all 4 compile. A rerun on this stacked branch is pending.

Checklist

  • Based on current main (includes #1179); 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 (#1179 merged)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793881478 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1904). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T23:11:12Z. --- Closes #1903 #1179 is merged; this PR is now one commit (6b41cf4d) on top of it. ### What #1179 lowers the packed pixel ancillary word in one pass after the fold loop, and only when every reader of the word extracts a modelled field. In real shaders the word also has an exec-masked `SelectU32` copy. `MaskedSelectEliminator` removes that copy, but it runs after `ConstantFolder` and the fold does not run again. So while the fold runs, the copy is still a reader, #1179's check refuses the whole word, and `ShaderInfoCollector` then throws on the extract that still reads it. When the check refuses the word, each in-range `BitFieldUExtract` / `BitFieldSExtract` that reads it (directly or through `Identity` copies) is now rewritten on its own, and the other readers keep the word. When every reader is an extract, #1179's whole-word lowering runs unchanged. ### Tested Linux (Nobara 44, GCC 16), RTX 5070 Ti with NVIDIA 595.91.07, and RADV on a Ryzen 5 7600X iGPU with Mesa 26.2.1. New check `ExtractVectorMaskedCopy()` in `core/libs/tests/PixelAncillary.cpp`: the IR a vector `v_bfe_u32` produces (masked offset, clamped count) plus a `SelectU32(lane < 16, word, 0)` copy of the word. After the fold, the extract must read the Layer (16, 11) or SampleId (8, 4) builtin. `ExtractVector()` is the same without the copy. | | `pixel_ancillary` | |---|---| | #1179 b4e02e2d + the tests only | `ExtractVector` passes, `ExtractVectorMaskedCopy` fails | | this branch | passes | - `ctest -R 'agc_|pixel_ancillary' -j1`: 225 of 225 on NVIDIA and 225 of 225 on RADV. - `tools/check_conventions.py --base b4e02e2d`: passes. - Stray (PPSA02100): with #1179 alone, 0 of 4 S5a shaders replay offline and all 6 stay deferred in game, with a fatal error at draw. With this change on `main` all 4 compile. A rerun on this stacked branch is pending. ### Checklist - [x] Based on current `main` (includes #1179); 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 (#1179 merged) - [x] AI-assisted: yes
klosixgaming-arch commented 2026-10-09 00:36:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-09T00:36:03Z.

Review: APPROVED


Solid recompiler fix. The root cause is identified precisely (the folder visits the GetBuiltin before the vector v_bfe_u32 extract's operands fold, leaving the ancillary unlowered), and the one-line fix in the BitFieldUExtract/BitFieldSExtract fold case is the right place. The new ExtractVector test builds the exact IR a vector extract emits (masked offset + clamped count) and asserts it lowers to the correct Layer/SampleId builtin, with a main-fails / branch-passes matrix. Tested on hardware (RTX 5070 Ti). Conventions clean.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5464463668 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1904#pullrequestreview-5464463668). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-09T00:36:03Z. **Review:** APPROVED --- Solid recompiler fix. The root cause is identified precisely (the folder visits the `GetBuiltin` before the vector `v_bfe_u32` extract's operands fold, leaving the ancillary unlowered), and the one-line fix in the `BitFieldUExtract`/`BitFieldSExtract` fold case is the right place. The new `ExtractVector` test builds the exact IR a vector extract emits (masked offset + clamped count) and asserts it lowers to the correct Layer/SampleId builtin, with a main-fails / branch-passes matrix. Tested on hardware (RTX 5070 Ti). Conventions clean. Written with AI assistance.
TheAggie commented 2026-10-09 05:33:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-09T05:33:57Z.


A real-title data point for this: vkQuake (PPSA99010, the homebrew Vulkan port, through RADV) hits exactly this case and gets past it with this PR.

Its layered blit pixel shader takes the render target index with v_bfe_u32 v5, v1, 16, 13 from the ancillary VGPR. In the IR after folding on current main, the extract still reads GetBuiltin PackedAncillary: the offset and count are BitwiseAnd32(16, 0x1f) and UMin32(BitwiseAnd32(13, 0x1f), ISub32(32, ...)) in a later block than the GetBuiltin, so they fold only after lowerPackedAncillary has already visited it. Preparing the shader then fails with packed pixel ancillary input has an unsupported live use, and every present fails.

With this PR's commit cherry-picked onto my local test branch (current main plus my open RADV PRs), the shader prepares, and with further fixes of mine vkQuake goes on to draw its first frames. Windows 11, RTX 5070 Ti (driver 610.88), WinLibs GCC 15.2.0, Release. I didn't need #1934's select/phi case for this shader.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074972454 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1904#issuecomment-6074972454). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-09T05:33:57Z. --- A real-title data point for this: vkQuake (PPSA99010, the homebrew Vulkan port, through RADV) hits exactly this case and gets past it with this PR. Its layered blit pixel shader takes the render target index with `v_bfe_u32 v5, v1, 16, 13` from the ancillary VGPR. In the IR after folding on current `main`, the extract still reads `GetBuiltin PackedAncillary`: the offset and count are `BitwiseAnd32(16, 0x1f)` and `UMin32(BitwiseAnd32(13, 0x1f), ISub32(32, ...))` in a later block than the `GetBuiltin`, so they fold only after `lowerPackedAncillary` has already visited it. Preparing the shader then fails with `packed pixel ancillary input has an unsupported live use`, and every present fails. With this PR's commit cherry-picked onto my local test branch (current `main` plus my open RADV PRs), the shader prepares, and with further fixes of mine vkQuake goes on to draw its first frames. Windows 11, RTX 5070 Ti (driver 610.88), WinLibs GCC 15.2.0, Release. I didn't need #1934's select/phi case for this shader. AI-assisted (Claude Code).
fisherjoey commented 2026-10-09 14:31:36 +00:00 (Migrated from github.com)

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


Overlap note: #1179 (open since 10-07) also handles a packed ancillary extract whose offset and count fold late. On Stray it doesn't cover this PR's case. After the first fold, the word also has an exec-masked SelectU32 copy as a reader. MaskedSelectEliminator removes that copy only later, and #1179's reader check runs once, before that, so it refuses the word. This PR rewrites each in-range extract when it is visited, so the masked copy doesn't block it.

Stray, with #1179 in place of this PR: 6 shaders are refused with unsupported live use (used by BitFieldUExtract 16 11), and one is fatal at a draw. With this PR, all 6 compile. Details on #1179: https://github.com/boykopovar/AnyPS5/pull/1179#issuecomment-6082989392.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082991948 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1904#issuecomment-6082991948). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T14:31:36Z. --- Overlap note: #1179 (open since 10-07) also handles a packed ancillary extract whose offset and count fold late. On Stray it doesn't cover this PR's case. After the first fold, the word also has an exec-masked `SelectU32` copy as a reader. `MaskedSelectEliminator` removes that copy only later, and #1179's reader check runs once, before that, so it refuses the word. This PR rewrites each in-range extract when it is visited, so the masked copy doesn't block it. Stray, with #1179 in place of this PR: 6 shaders are refused with `unsupported live use (used by BitFieldUExtract 16 11)`, and one is fatal at a draw. With this PR, all 6 compile. Details on #1179: https://github.com/boykopovar/AnyPS5/pull/1179#issuecomment-6082989392. AI-assisted: yes
adriadam10 commented 2026-10-10 06:04:55 +00:00 (Migrated from github.com)

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


Checked: build + ctest of libs/Agc/AgcDriver green (pixel_ancillary passes); reverting the ConstantFolder.cpp change makes pixel_ancillary fail, so ExtractVectorMaskedCopy pins the fix. Each in-range extract is rewritten on its own when another reader (the masked SelectU32 copy) blocks the whole-word lowering; with only extracts left, #1179's path is unchanged and other readers still throw. Confirmed on vkQuake (PPSA99010, RADV) by @TheAggie.

Note: #1934 touches the same function and test file and conflicts with this one; whichever lands second will need a rebase. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094479506 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1904#issuecomment-6094479506). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:04:55Z. --- Checked: build + ctest of libs/Agc/AgcDriver green (`pixel_ancillary` passes); reverting the `ConstantFolder.cpp` change makes `pixel_ancillary` fail, so `ExtractVectorMaskedCopy` pins the fix. Each in-range extract is rewritten on its own when another reader (the masked `SelectU32` copy) blocks the whole-word lowering; with only extracts left, #1179's path is unchanged and other readers still throw. Confirmed on vkQuake (PPSA99010, RADV) by @TheAggie. Note: #1934 touches the same function and test file and conflicts with this one; whichever lands second will need a rebase. 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!1904
No description provided.