perf(shader): judge first-lane and constant reads by what they read #6053
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6053"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by ThatsLeo on 2026-10-14T20:56:39Z.
What
Since #5181, a resource plan keeps a control-flow condition only when
Detail::Evaluator::MayEvaluatefinds that the evaluator could compute it. As @AnthonyClemens found on Resident Evil 2 (#5181),MayEvaluatetookReadFirstLaneandReadConstas evaluable without looking at what they read. A condition behind either was kept with everything it depends on, though the evaluator could not compute it.MayEvaluatenow judges both the way the evaluator reads them:ReadFirstLaneis evaluable when its source is, judged under the read's active mask. Inside it, a runtime select on that mask needs only its first arm, which is the arm the evaluator takes there.ReadConstis evaluable when the SRT read its slot names is. A slot that is not an immediate below the number of SRT reads is not evaluable, as in the evaluator.#6095 compiles these walks and treats both opcodes the same way (a first-lane read under its own mask, a constant read through its slot), so the rule matches it too.
The rule is one way only. A value the evaluator can compute is never judged not evaluable, so no condition the walk could use is dropped. A value judged evaluable may still fail at run time (for example a
ReadConstanswered by the clean-memory evaluator, which has no active mask); that costs memory, as before.Tested
Fedora 44, GCC 16.2.1, Release, Intel Arc (Meteor Lake iGPU, Mesa 26.2.3 ANV).
verifyMayEvaluate(agc_shader_memory) has eight new cases, each checked against the evaluator itself:MayEvaluateand the evaluator recursed until the stack overflowed on these. Both now track the first-lane reads they are inside and judge such a read not evaluable.ReadFirstLaneandReadConstalso left the list of opcodes the over-constants sweep skips.ctest(1360 tests) on Arc and on lavapipe, rebased ontomain47a266cf9. All pass except two that don't involve this change:application_heap_lazy, on both: a segfault inapplication_heap_tests, which links onlylibc. #6107 is about it.agc_driver_sample_multisample_target, on Arc only. It fails the same way onmain: the Arc has no multisampled storage images.agc_shader_memoryalso passes withAPS5_VERIFY_SRT_COMPACT=1. On earlier bases,agc_driver_preparation_scope_settlefailed sometimes on Arc. It is flaky without this change: run alone ten times, it failed 6 times on this branch, 8 on #5181's head and 5 onmain19fc5a8ed.Plan sizes, Stray (PPSA02101) and Dreaming Sarah (PPSA02929): I extracted the plans of the 227 distinct shader requests I have dumped from both titles (
ExtractPlan), with and without this commit. They hold the same 36,699 values either way: these plans contain noReadFirstLane, and their 4,594ReadConsts all read slots that can be evaluated. So on these two titles the commit changes nothing. I have no title where the case occurs.@AnthonyClemens measured 5.6 GB down to about 0.43 GB of decoded plans on Resident Evil 2, but with a pass that differs from this one and with both opcodes changed at once. A run of this branch with a warm cache would give the number for this rule, and show how the saving splits between the two opcodes.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:13:56Z.
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.
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtEvaluator.hppcore/shader/recompiler/Optimization/src/SrtWalker/SrtEvaluator.cppdocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppdocs/dev/ARCHITECTURE.mdcore/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtEvaluator.hppcore/shader/recompiler/Optimization/src/SrtWalker/SrtEvaluator.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-16T23:29:28Z.
One case I'd like covered.
MayEvaluatecatches a cycle throughexpanding, but aReadFirstLanestarts a nestedMayEvaluatewith freshknownandexpandingsets. A value that reaches itself through a first-lane read is therefore only stopped byResolveInvariantPhiturning every loop phi into its invariant input. Could you add a test with a loop-carried value read throughReadFirstLane(it should come out not evaluable, without recursing), or state that invariant whereMayEvaluateis described, so a later change to phi resolution can't turn it into unbounded recursion?Imported from boykopovar/AnyPS5. Originally posted by ThatsLeo on 2026-10-17T14:15:37Z.
@oneandonlydean Added in
e14a4e513, with the branch rebased ontomain47a266cf9.ResolveInvariantPhidid stop your loop case, but a slot can also reach itself through a first-lane read with no phi in between, and nothing stopped that.verifyMayEvaluatehas two new cases:ReadFirstLane(phi) + 1around a loop, read throughReadFirstLane.ReadConstwhose slot holdsReadFirstLaneof that sameReadConst.The second one overflowed the stack, in
MayEvaluateand also in the evaluator, which already did the same onmain: each first-lane read starts a nestedBasicEvaluatorwith fresh visiting state.The fix:
MayEvaluatepasses the set of first-lane reads it is inside down to the nested walk. One already in the set is judged not evaluable.MayEvaluateand inEvaluate.The invariant is now stated in the plan section of
ARCHITECTURE.md.cteston Arc and lavapipe matchesmain(details in the description).AI-assisted: yes