fix(shader): imply a masked condition that is a structural copy of the mask #21758

Merged
edubox merged 2 commits from upstream-pr/21758 into main 2026-11-19 22:18:26 +00:00
edubox commented 2026-11-16 18:44:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by edubox on 2026-11-16T18:44:35Z.


What

ActiveMaskImplies decides whether a mask makes a guarded Select take only its active arm, which is what lets a per-lane descriptor dword be evaluated at bind time. It accepted the condition only when it was the mask itself or a conjunct of an AND mask, comparing resolved pointers. A mask and the condition of the select it guards reach the IR as different nodes whenever they are computed on two paths, so the runtime validator fell back to validating both arms and refused a lane-dependent arm that no lane the console runs ever reads.

The implication now also holds when EquivalentValue finds both values equal, the same notion of equality the recompiler already uses to deduplicate descriptor web arms and to resolve an invariant Phi. The program is threaded into the predicate (every call site has it); the three ResourceTracker helpers that peel a select under a mask became const members so they can reach m_program.

ShaderDiskCache::FormatVersion moves 88 to 89: a value this accepts stops being refused or folded, so the same source can emit a different module.

Evidence: PPSA07642 (The Last of Us Part I) builds the mask and the condition of the S# dword's v_readfirstlane select from the same ballot predicate on two different paths. This is the precondition the descriptor web needs before those reads can be split into arms; those shaders still stop at a Phi inside the arm, which is a separate gap.

Tested

  • New cases in verifyWaterfallRuntimeValues (registered as agc_shader_srt_read_patching): a mask whose conjunct is a structural copy of the select condition is accepted, and the negation of that condition is still refused. The first case fails on main with waterfall: a mask whose conjunct is a structural copy of the select condition was rejected and passes with the change; the second one passes on both.
  • agc_shader_memory_tests (every internal case) with ANYPS5_NO_SHADER_CACHE=1: passes on main and with the change.
  • agc_shader_srt_read_patching, agc_shader_descriptor_phis, agc_driver_descriptor_web_faults and agc_driver_buffer_descriptor_runtime with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer: 4/4 pass on both, four Insert instance layer "VK_LAYER_KHRONOS_validation" lines, no VUID- and no was not found but was requested by env var.
  • check_conventions clean; pr_overlap --names ActiveMaskImplies shows no other branch declaring it.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Unblocks: PPSA07642 (The Last of Us Part I); bring-up notes go to the maintainers on request
  • GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 620.70.06, CachyOS Linux (kernel 7.3.3) for the GPU tests; the planner cases need no GPU
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5342922882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21758). Originally posted by [edubox](https://github.com/edubox) on 2026-11-16T18:44:35Z. --- ### What `ActiveMaskImplies` decides whether a mask makes a guarded `Select` take only its active arm, which is what lets a per-lane descriptor dword be evaluated at bind time. It accepted the condition only when it was the mask itself or a conjunct of an `AND` mask, comparing resolved pointers. A mask and the condition of the select it guards reach the IR as different nodes whenever they are computed on two paths, so the runtime validator fell back to validating both arms and refused a lane-dependent arm that no lane the console runs ever reads. The implication now also holds when `EquivalentValue` finds both values equal, the same notion of equality the recompiler already uses to deduplicate descriptor web arms and to resolve an invariant Phi. The program is threaded into the predicate (every call site has it); the three `ResourceTracker` helpers that peel a select under a mask became `const` members so they can reach `m_program`. `ShaderDiskCache::FormatVersion` moves 88 to 89: a value this accepts stops being refused or folded, so the same source can emit a different module. Evidence: PPSA07642 (The Last of Us Part I) builds the mask and the condition of the S# dword's `v_readfirstlane` select from the same ballot predicate on two different paths. This is the precondition the descriptor web needs before those reads can be split into arms; those shaders still stop at a `Phi` inside the arm, which is a separate gap. ### Tested - New cases in `verifyWaterfallRuntimeValues` (registered as `agc_shader_srt_read_patching`): a mask whose conjunct is a structural copy of the select condition is accepted, and the negation of that condition is still refused. The first case fails on `main` with `waterfall: a mask whose conjunct is a structural copy of the select condition was rejected` and passes with the change; the second one passes on both. - `agc_shader_memory_tests` (every internal case) with `ANYPS5_NO_SHADER_CACHE=1`: passes on `main` and with the change. - `agc_shader_srt_read_patching`, `agc_shader_descriptor_phis`, `agc_driver_descriptor_web_faults` and `agc_driver_buffer_descriptor_runtime` with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`: 4/4 pass on both, four `Insert instance layer "VK_LAYER_KHRONOS_validation"` lines, no `VUID-` and no `was not found but was requested by env var`. - `check_conventions` clean; `pr_overlap --names ActiveMaskImplies` shows no other branch declaring it. ### 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 and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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: none - [x] Unblocks: PPSA07642 (The Last of Us Part I); bring-up notes go to the maintainers on request - [x] GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 620.70.06, CachyOS Linux (kernel 7.3.3) for the GPU tests; the planner cases need no GPU - [x] AI-assisted: yes
mattiolocoding commented 2026-11-17 21:26:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mattiolocoding on 2026-11-17T21:26:49Z.

Review: COMMENTED


Code Review: PR #21758 (commit 0c926efb7497d05f82616952a491215da26e1b83)

Summary of Changes

  • ActiveMaskImplies Structural Equivalence: Extended ActiveMaskImplies to take const IrResourcePlan& program and evaluate EquivalentValue(program, mask, condition) when direct pointer equality fails.
  • Program Threading & Const Correctness: Threaded program across ResourceTracker.cpp, SrtEvaluator.cpp, SrtRuntimeValidator.cpp, and SrtTape.cpp. Converted helper routines (MatchAffineOffset, ExtractBaseRegister, ExtractDwordKey) to Tracker const member methods to provide access to m_program.
  • Cache Invalidation: Bumps ShaderDiskCache::FormatVersion from 88 to 89 to avoid stale deserialization.
  • Unit Testing: Added regression coverage in verifyWaterfallRuntimeValues (ShaderMemory.cpp), verifying both acceptance of structural copies and rejection of negated conditions.

Verification & Analysis

  1. Conventions:
    • python tools/check_conventions.py --base upstream/main passed cleanly with 0 violations.
  2. PR Overlap:
    • python tools/pr_overlap.py --repo boykopovar/AnyPS5 --names ActiveMaskImplies verified against all 1,760 open pull requests: 0 overlaps (main: not declared, open: none).
  3. Correctness & Safety:
    • In GCN/RDNA shader IR where ballot predicates are evaluated along divergent control flow paths before guarding a v_readfirstlane, distinct IR value pointers often represent identical Boolean logic. Checking EquivalentValue resolves these cases without weakening safety guarantees.
    • Cycle safety is preserved because equivalentValueImpl in IrProgram.cpp employs a dedicated VisitedPairs memoization set.
    • Logical conjunction handling remains sound: A \land B \implies C whenever A \implies C or B \implies C.
  4. Concurrency / Cache Versioning:
    • Concur with @janzezno's observation regarding concurrent PRs #21240 / #21745: whoever merges later will need to increment ShaderDiskCache::FormatVersion to the next sequential integer.

The implementation is clean, precise, and meets all repository standards.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5946889046 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21758#pullrequestreview-5946889046). Originally posted by [mattiolocoding](https://github.com/mattiolocoding) on 2026-11-17T21:26:49Z. **Review:** COMMENTED --- ### Code Review: PR #21758 (commit `0c926efb7497d05f82616952a491215da26e1b83`) #### Summary of Changes - **`ActiveMaskImplies` Structural Equivalence**: Extended `ActiveMaskImplies` to take `const IrResourcePlan& program` and evaluate `EquivalentValue(program, mask, condition)` when direct pointer equality fails. - **Program Threading & Const Correctness**: Threaded `program` across `ResourceTracker.cpp`, `SrtEvaluator.cpp`, `SrtRuntimeValidator.cpp`, and `SrtTape.cpp`. Converted helper routines (`MatchAffineOffset`, `ExtractBaseRegister`, `ExtractDwordKey`) to `Tracker` `const` member methods to provide access to `m_program`. - **Cache Invalidation**: Bumps `ShaderDiskCache::FormatVersion` from 88 to 89 to avoid stale deserialization. - **Unit Testing**: Added regression coverage in `verifyWaterfallRuntimeValues` (`ShaderMemory.cpp`), verifying both acceptance of structural copies and rejection of negated conditions. #### Verification & Analysis 1. **Conventions**: - `python tools/check_conventions.py --base upstream/main` passed cleanly with 0 violations. 2. **PR Overlap**: - `python tools/pr_overlap.py --repo boykopovar/AnyPS5 --names ActiveMaskImplies` verified against all 1,760 open pull requests: 0 overlaps (`main: not declared`, `open: none`). 3. **Correctness & Safety**: - In GCN/RDNA shader IR where ballot predicates are evaluated along divergent control flow paths before guarding a `v_readfirstlane`, distinct IR value pointers often represent identical Boolean logic. Checking `EquivalentValue` resolves these cases without weakening safety guarantees. - Cycle safety is preserved because `equivalentValueImpl` in `IrProgram.cpp` employs a dedicated `VisitedPairs` memoization set. - Logical conjunction handling remains sound: $A \land B \implies C$ whenever $A \implies C$ or $B \implies C$. 4. **Concurrency / Cache Versioning**: - Concur with @janzezno's observation regarding concurrent PRs #21240 / #21745: whoever merges later will need to increment `ShaderDiskCache::FormatVersion` to the next sequential integer. The implementation is clean, precise, and meets all repository standards.
MarcZX10 commented 2026-11-19 01:22:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T01:22:25Z.

Review: COMMENTED


Reviewed — no blocking findings

The reviewed change uses value equivalence to recognize structurally copied mask conditions, and the inspected call sites consistently pass the resource plan needed for that comparison.

What I ran

Reviewed head 0c926efb7497d05f82616952a491215da26e1b83 from the prepared diff and repository reader. No commands or tests were run.

Results

Check Result Note
Diff and targeted code review passed Logic and callers inspected
Tests not run Author-reported results were not rerun

Findings

Blocking

None.

Non-blocking

None.

Not verified

Did not read the full changed ShaderMemory.cpp because it was excluded from the review folder as too large; inspected its changed hunk in the diff only. Did not inspect the full ResourceTracker.cpp, SrtEvaluator.cpp, or SrtTape.cpp; only relevant ranges were read. No tests were run. Author-reported evidence and GPU behavior were not independently verified.

Files read: description.md, commits.md, manifest.json, diff.patch; full changed text for SrtInstructionPredicates.hpp and targeted code ranges from SrtInstructionPredicates.cpp, SrtEvaluator.cpp, SrtRuntimeValidator.cpp, and SrtTape.cpp. Excluded from full-text bundle: ShaderMemory.cpp (too large). Other changed files were not read in full.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5967916207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21758#pullrequestreview-5967916207). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T01:22:25Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The reviewed change uses value equivalence to recognize structurally copied mask conditions, and the inspected call sites consistently pass the resource plan needed for that comparison. ## What I ran Reviewed head `0c926efb7497d05f82616952a491215da26e1b83` from the prepared diff and repository reader. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted code review | passed | Logic and callers inspected | | Tests | not run | Author-reported results were not rerun | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Did not read the full changed `ShaderMemory.cpp` because it was excluded from the review folder as too large; inspected its changed hunk in the diff only. Did not inspect the full `ResourceTracker.cpp`, `SrtEvaluator.cpp`, or `SrtTape.cpp`; only relevant ranges were read. No tests were run. Author-reported evidence and GPU behavior were not independently verified. Files read: `description.md`, `commits.md`, `manifest.json`, `diff.patch`; full changed text for `SrtInstructionPredicates.hpp` and targeted code ranges from `SrtInstructionPredicates.cpp`, `SrtEvaluator.cpp`, `SrtRuntimeValidator.cpp`, and `SrtTape.cpp`. Excluded from full-text bundle: `ShaderMemory.cpp` (too large). Other changed files were not read in full. ## To get this to a good state Nothing is required.
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!21758
No description provided.