feat(recompiler): reuse pure IR values recomputed in dominated blocks #4948
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4948"
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 fdfawef324 on 2026-10-13T06:00:50Z.
What
The IR pipeline has no value numbering, and SPIRV-Tools (whose redundancy elimination would remove the repeats) is off by default (
ANYPS5_ENABLE_SPIRV_TOOLS=OFF). So a pure value is emitted again every time the translator rebuilds it. The biggest repeat comes from float operands: in f32 denormal-flush mode every FP instruction flushes each of its sources, and checks each source for NaN, even when the previous instruction already did the same for that register.RedundantValueEliminatorruns incompileVariantjust before emission. It walks the dominator tree. When a value has the same opcode, result type, flags and operands as a value earlier in the same block or in a dominating block, its uses are pointed at that earlier value. Block branch conditions, indirect targets and dynamic reads that named a replaced value are redirected too. A block that the entry does not reach throws; the emitter's IR validation already rejects such a program.What may be merged, and why that keeps semantics
Merged opcodes (the allow-list in
Recomputable):CompositeConstruct*,CompositeExtract*FPAbs32,FPNeg32,FPSaturate32,BitFieldInsert,BitFieldUExtract,BitFieldSExtract,SelectU1/U32/F32SMulHi,UMulHi,IAbs32, shifts, and/or/xor/not,BitReverse32,BitCount32/64,FindUMsb32/64,FindILsb32, integer min/max, integer compares,LogicalOr/And/Xor/NotFPIsNan32,FPAdd/Sub/Mul/Fma/Mad/Min/Max32,FPFloor/Ceil/Trunc/Fract/RoundEven32Why merging these is sound:
s_round_modeors_setregchanging the rounding fields, ands_denorm_mode. So a float op computes the same function everywhere in a program. Every f32 add, sub, mul and fma is emitted withNoContraction, so the driver cannot fuse a shared multiply differently at each use.x + 0.0andx + -0.0, which differ forx = -0.0, stay apart, and so do NaN constants with different payloads. Operand order is not canonicalised, soa * bandb * astay apart.ValidateProgram(..., true)checks that every definition still dominates its uses.Not merged, and why:
Barrier,Waitcnt, exports and every other op with side effects.ReadLane,ReadFirstLane,Ballot, DPP, swizzle, permlane),LaneId, and EXEC/VCC/SCC reads: their result depends on EXEC or on other lanes, and those can change between two ops that look equal.These figures come from the Demon's Souls (PPSA01342) cold-boot compute programs that take 11 s to 400 s in
vkCreateComputePipelineson NVIDIA. Each was replayed from itsAPS5_DUMP_SHADERSrequest withagc_shader_replay, once at this PR and once with the pass call removed (which is main):The other 17 of the 83 requests fail resource analysis, with the same message on main and on this PR.
This removes about 9% of the size of these programs, but it is not what makes them giant. Notes on the size:
OpLoopMerge). There is nos_swappc_b64, so no user-data callees are linked.APS5_SINGLE_LANE=allgives 368101 words. It runs one guest lane per invocation, instead of the two that wave64 needs on a 32-lane NVIDIA subgroup.Those costs come from exact RDNA float semantics and the wave64 layout, so this PR leaves them alone.
On 0x2488d5500,
spirv-opt --redundancy-elimination --eliminate-dead-code-aggressive(built from3rdparty/SPIRV-Tools) took main's 688301 words and this PR's 626242 words to the same 608293 words. This was measured on the previous base.Word counts are module sizes, not speed. This PR claims no frame-rate or pipeline-creation-time change, and pipeline creation was not measured.
Tested
OS Windows Server 2025, RTX 3060, GCC 15.2,
build-devRelease,ANYPS5_ENABLE_SPIRV_TOOLS=OFF, rebased on mainea2bbefb2. The measurements below were taken on main7e181fcc3; after the rebase ontoea2bbefb2onlyagc_redundant_value_eliminationwas rebuilt and re-run, and it passes.agc_redundant_value_eliminationtest, IR cases.ReadFirstLane,ReadLane, DPP, swizzle, shared loads (with nothing, a store or a barrier between them), shared atomics, phis.RedundantValueEliminator.cppfrom the previous revision, which visited unreachable blocks, this case fails.v_mul_f32and av_add_f32and stores the result. The test checks that no pure SPIR-V instruction in a block repeats an earlier one with the same type and operands. It no longer pins a word count.-DANYPS5_ENABLE_SPIRV_TOOLS=ONhere, because configuring needs an FFmpeg download that fails on this machine. Instead I ran both modules throughspirv-optwith the default pass list fromSpirvOptimizer.cpp. Both come out at 1518 words with 0 repeats, so the check also holds there. In that build the check cannot tell whether the pass ran, because SPIRV-Tools removes the repeats itself.agc_shader_replayover all 83 requests, at this PR and with the pass call removed, gives the table above. The same 17 requests fail with the same messages in both builds. All 66 modules from both builds passspirv-opt --target-env=vulkan1.3validation, and a module with one operand id corrupted is rejected.ctest -R "^(agc_|shader_)" -j 4(the libSceAgcDriver and shader recompiler sets, all 385 executables built): 463 tests, none failed,agc_driver_stencil_ref_exportskipped.python -I tools/check_test_registration.py build-dev: 750 test executables, 0 built and not run, none new.ANYPS5_ENABLE_SPIRV_TOOLS=ONbuild, Linux/lavapipe, AMD, pipeline creation timing.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T08:40:05Z.
Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main
534451426together with six other open GPU PRs (their own commits applied, no merge conflicts), so only the tests that belong to this PR say something about it:tests/RedundantValueElimination.cpp, whichctestruns asagc_redundant_value_elimination: 3 of 3 runs passed.Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T13:59:16Z.
I built the PR's
agc_redundant_value_elimination_teststarget on Linux with GCC 12.2, Release, andBUILD_TESTING=ON. CTestagc_redundant_value_eliminationpasses (1/1).This validates the focused eliminator suite. I did not run the vkQuake capture or measure its SPIR-V size or pipeline compile time.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T11:08:25Z.
Checked the opcode allow-list (pure, no hidden inputs), the key (opcode, type, flags, SSA operands / immediate bits), the dominator-scoped table and the unreachable-block throw.
agc_redundant_value_eliminationpasses on the PR merged onto main and fails with the pass call removed. Merging once the full Build is green.