Skip to content

fix(shader): bound SPIR-V scalar replacement - #4288

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
014-code:fix/spirv-optimizer-memory-4166
Oct 17, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
014-code:fix/spirv-optimizer-memory-4166

Conversation

@014-code

@014-code 014-code commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

CreateScalarReplacementPass(0) treats zero as unlimited. The default AnyPS5 SPIR-V pipeline registered that pass twice, allowing large function-scope arrays and nested composites to expand into an unbounded number of replacement variables while shaders were being optimized. This can cause excessive temporary IR growth during shader compilation.

This changes both AnyPS5 registrations to a shared ScalarReplacementLimit of 64. Smaller composites keep the existing scalar-replacement optimization; larger composites remain aggregate values instead of being expanded without a bound. 64 is a conservative power-of-two ceiling that preserves useful optimization for small composites while placing a predictable upper bound on per-composite expansion. It is an engineering guardrail, not a title-specific tuning result.

The optimizer test now covers a valid 1,000-element function array passed through branching and a memory barrier. It validates the optimized result and bounds the resulting module size, so removing the limit regresses the test without requiring a GPU or a title dump.

This PR does not claim that scalar replacement is the root cause of #4166: no isolated title-level memory A/B measurement is available here. Accordingly, #4166 remains open.

Tested

Ubuntu 24.04 under WSL2, Release build, bundled SPIRV-Tools enabled:

  • Configured with cmake -S . -B build-wsl-4288 -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON.
  • Built agc_spirv_optimizer_tests.
  • ctest --test-dir build-wsl-4288 -R '^agc_spirv_optimizer$' --output-on-failure: passed (1/1).
  • Ran agc_spirv_optimizer_tests with the default optimizer, APS5_SPIRV_OPT=none, and APS5_SPIRV_OPT=full: all passed.
  • python tools/check_conventions.py --base d92a8dad390af1807cc91622b924252141816a5a --head HEAD --title 'fix(shader): bound SPIR-V scalar replacement': run against the updated PR body.
  • git diff --check d92a8dad390af1807cc91622b924252141816a5a..HEAD: passed.

No RTX 3080 or Demon’s Souls run was available here. The regression is exercised at the CPU-side optimizer boundary; no title-level memory improvement is claimed.

Checklist

  • Based on current main; no other open PR implements the scalar-replacement limit
  • 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 in TechnicalDebt (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@Mouhand-Kaddo

Copy link
Copy Markdown
Contributor

Tested the patch at 6589b8d1a9979514c546f4b87dbbd5b1c00538f8 on Linux, rebased locally onto public main c36fd0a0eb57c01866befd370c88329ca42df759, with SPIRV-Tools enabled.

Applying only the new regression test to main gives:

large function array was scalarized
1 optimizer check(s) failed

After applying the production change, agc_spirv_optimizer_tests passes with the default optimizer, APS5_SPIRV_OPT=full, and APS5_SPIRV_OPT=none. The combined candidate build's registered agc_spirv_optimizer test also passes.

I am investigating Demon's Souls on an i7-13700K / RX 6900 XT, RADV Mesa 26.2.4. Main previously approached exhaustion of 31 GiB RAM and 8 GiB swap during startup, but I have not yet established a title-level memory improvement or main-menu success from this patch. Separate descriptor-table failures are currently being investigated before another game launch.

Testing performed with AI assistance.

@Mouhand-Kaddo

Copy link
Copy Markdown
Contributor

Linux follow-up for the tested head 6589b8d1a9979514c546f4b87dbbd5b1c00538f8. Demon's Souls (PPSA01342) reached the main menu, with New Game and Settings visible in a native emulator frame and interaction confirmed by the user. Selecting New Game then produced a black screen; gameplay is not validated.

This was a combined integration stack: public-main snapshot c36fd0a0eb57c01866befd370c88329ca42df759, ported #757/#758, #4288, #3991, #4402, the descriptor-layout fix now in #4456, and a local split-descriptor-load correction. It is not an isolated success claim for this PR. The final menu run used GDB to investigate an earlier buffer descriptor uses an unsupported type abort. That earlier failure remains undiagnosed.

The earlier failing-main/passing-fix scalar-replacement regression remains the isolated evidence for this PR. The title result below is a combined-stack result.

Reproduction detail: the title run temporarily raised Linux udmabuf size_limit_mb from 64 to 2048 because larger GPU imports failed at the default limit. The user restored 64 afterward and readback confirmed it. Default shader optimization remained enabled. RX 6900 XT/RADV was receiving GPU work; CPU shader compilation was a major startup bottleneck. Game assets were accessed through an enforced read-only view of the Windows gaming drive, without copying the game or changing its inputs. Disposable test caches have now been removed. AI-assisted testing/report.

@Mouhand-Kaddo

Copy link
Copy Markdown
Contributor

Retest on Linux using updated #4456 head 4f5f6d671cdb0ffd3a9c43eedbdf03e433e80ebe as the integration base. Its six focused PR checks pass three times each; the previous null-shader-module mock failure is reproduced on its parent and fixed on this head.

The title support stack remains #757/#758/#4288/#3991/#4402 plus the local grouped split-load correction. Five integration shader/graphics checks pass. Demon's Souls PPSA01342 rendered language/display settings and the online/offline prompt, then aborted with exit 134:

[videoout] presentation failed: compute shader 0x2489fbb00: runtime image descriptor is invalid

This run did not verify main menu. The earlier c36-based integration did reach it. I have not isolated which rebased-stack change causes the descriptor failure, so this is not a regression attribution to this PR. No memory guard/OOM event stopped the run. One brief debugger snapshot was taken during startup, then detached.

The temporary Linux udmabuf limit was again 2048 MiB for the title run and is now verified back at 64. Assets were read directly through an enforced read-only view of the Windows gaming drive, without copying the game. Original input hashes and timestamps are unchanged. AI-assisted testing/report.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Is there a measurement that scalar replacement is what grows memory in #4166? Mouhand-Kaddo's runs show the new test failing on main, but no title memory with and without the limit, so Fixes #4166 is still a guess. Please show the issue's memory table with and without the limit, or drop the Fixes line so the issue stays open. And why 64?

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Needs a rebase: #4580 and #4600 are merged and this now conflicts with them in core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp.

RINNECODER added a commit to RINNECODER/AnyPS5 that referenced this pull request Oct 14, 2026
…time CPU (#482)

* perf(shader): rewrite high-fan-out IR uses in linear time

Ported from upstream PR boykopovar#4035

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): index specialization tables by SPIR-V id

Ported from upstream PR boykopovar#4772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): honor disabled optimization during specialization

Ported from upstream PR boykopovar#4827

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): discard helper functions unreachable after specialization

Ported from upstream PR boykopovar#5064

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): drop module constants nothing references after specialization

Ported from upstream PR boykopovar#5315

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(shader): cap SPIR-V scalar replacement

Ported from upstream PR boykopovar#4288

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(recompiler): iterate structurizer fixpoints in post-order and bound shared merge splitting growth

Includes the stacked upstream commit that structurizes refused control flow
with reach flags, which this change builds on.

Ported from upstream PR boykopovar#5028

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): emit image_bvh_intersect_ray as one shared function per variant

Ported from upstream PR boykopovar#5455

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(shader): avoid redundancy traversal map copies

Patches the bundled SPIRV-Tools redundancy elimination (only built with
ANYPS5_ENABLE_SPIRV_TOOLS=ON).

Ported from upstream PR boykopovar#5511

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(metal): accept a specialized BDA module that dropped one of its two BDA descriptors

Specialization removes descriptor variables the final module no longer
references. With helper functions now discarded after specialization, a
compute module can drop its page-table variable while keeping the fault
buffer, and the bridge then rejected the dispatch ("BDA ABI requires
page-table and fault-buffer descriptor metadata"). Require at least one of
the two roles instead of both; the module's own descriptors are still
checked against the prepared plan.

Not a port; needed by the port of upstream PR boykopovar#5064.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
moong15 added a commit to moong15/AnyPS5 that referenced this pull request Oct 14, 2026
# Conflicts:
#	core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp
@oneandonlydean

Copy link
Copy Markdown
Collaborator

The 10-13 questions are still open: is there a measurement that scalar replacement is what grows memory in #4166 (the issue's memory table with and without the limit), or else please drop the Fixes #4166 line; and why 64? It also still needs the rebase for tests/SpirvOptimizer.cpp after #4580 and #4600.

@014-code
014-code force-pushed the fix/spirv-optimizer-memory-4166 branch from 9ca7532 to 6a880ab Compare October 15, 2026 07:00
@014-code

Copy link
Copy Markdown
Contributor Author

Pushed an updated revision at 6a880abf2866ca025ff167deb2b023e0eaf83277 and rebased the PR onto current main (d92a8dad390af1807cc91622b924252141816a5a).

Addressed the review points:

Validation on Ubuntu 24.04 under WSL2 with bundled SPIRV-Tools enabled:

  • Release CMake configure completed.
  • agc_spirv_optimizer_tests built successfully.
  • Registered CTest agc_spirv_optimizer: passed (1/1).
  • Direct optimizer test passed with the default mode, APS5_SPIRV_OPT=none, and APS5_SPIRV_OPT=full.
  • git diff --check passed.

No RTX 3080 or Demon’s Souls run was performed, and no title-level memory improvement is claimed.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6176 core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp L293

@014-code

Copy link
Copy Markdown
Contributor Author

Thanks for flagging this. This is an insertion-point overlap in core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp, not a functional dependency or duplicate implementation.

The two PRs are independent, so Depends on remains none. If both are merged, the conflict can be resolved by retaining both test blocks and their respective calls; no production-code conflict is expected.

@JJenkx JJenkx mentioned this pull request Oct 16, 2026
11 tasks done
@oneandonlydean

Copy link
Copy Markdown
Collaborator

That answers 10-13 and 10-14: Fixes #4166 is gone, 64 is explained and the optimizer test conflict is resolved. The overlap with #6176 is only the test insertion point, so Depends on: none stands. The Windows guest_shader_link timeout in the first CI run was the runner hang class (that test never reaches the optimizer); a rerun passed on Windows and Linux.

@oneandonlydean
oneandonlydean merged commit 3b10c7e into boykopovar:main Oct 17, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants