Shader recompilation with SPIRV-Tools optimization grows host memory by about 0.6 GiB/s (Demon's Souls, Linux) #4166

Closed
opened 2026-10-12 00:21:23 +00:00 by btsouth · 4 comments
btsouth commented 2026-10-12 00:21:23 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by btsouth on 2026-10-12T00:21:23Z.


Note

Posted by an AI agent on behalf of Tyler.

On Linux, with ANYPS5_ENABLE_SPIRV_TOOLS=ON and the default optimizer, Demon's Souls PPSA01342 v1.005 (US) grows to about 16.6 GiB in its first 26 s of start-up while it compiles shaders. With APS5_SPIRV_OPT=none and nothing else changed, it stays near 3 GiB.

Setup: main bab7b328, Release, GCC 16.2.1, Arch Linux kernel 7.2.8, i7-14700F, RTX 4070 SUPER on driver 615.78.08, 31 GiB RAM. The process was pinned to 16 hardware threads (taskset -c 0-15). The default run had 181 modules in its shader cache from an earlier run; the other started with an empty cache.

Resident plus swapped memory, sampled from /proc/<pid>/smaps every 5 s (MiB):

Time Default APS5_SPIRV_OPT=none
5 s 2361 1882
10 s 5821 2465
15 s 7138 2791
20 s 11850 2703
26 s 17022 3182

At 26 s in the default run, 15.0 GiB was in anonymous host mappings of 64 MiB or more and 1.3 GiB in anonymous host mappings between 2 and 64 MiB. Guest mappings below 64 GiB held 44 MiB and the direct memory memfd 10 MiB, so the growth is host-side. The 64 MiB mappings have the shape of glibc malloc arenas, but I haven't confirmed which allocations they hold. Left running, an earlier default run reached about 27.3 GiB (18.2 GiB resident plus 9.0 GiB swapped) after 6.5 minutes and about 740 compiled shaders.

This lines up with the slow optimized compile noted in #2552. As noted there, APS5_SPIRV_OPT=none still optimizes specialized modules, so this compares the default against that switch, not against no optimization at all.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5806586201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4166). Originally posted by [btsouth](https://github.com/btsouth) on 2026-10-12T00:21:23Z. --- > [!NOTE] > Posted by an AI agent on behalf of Tyler. On Linux, with `ANYPS5_ENABLE_SPIRV_TOOLS=ON` and the default optimizer, Demon's Souls PPSA01342 v1.005 (US) grows to about 16.6 GiB in its first 26 s of start-up while it compiles shaders. With `APS5_SPIRV_OPT=none` and nothing else changed, it stays near 3 GiB. Setup: main bab7b328, Release, GCC 16.2.1, Arch Linux kernel 7.2.8, i7-14700F, RTX 4070 SUPER on driver 615.78.08, 31 GiB RAM. The process was pinned to 16 hardware threads (`taskset -c 0-15`). The default run had 181 modules in its shader cache from an earlier run; the other started with an empty cache. Resident plus swapped memory, sampled from `/proc/<pid>/smaps` every 5 s (MiB): | Time | Default | `APS5_SPIRV_OPT=none` | |---|---|---| | 5 s | 2361 | 1882 | | 10 s | 5821 | 2465 | | 15 s | 7138 | 2791 | | 20 s | 11850 | 2703 | | 26 s | 17022 | 3182 | At 26 s in the default run, 15.0 GiB was in anonymous host mappings of 64 MiB or more and 1.3 GiB in anonymous host mappings between 2 and 64 MiB. Guest mappings below 64 GiB held 44 MiB and the direct memory memfd 10 MiB, so the growth is host-side. The 64 MiB mappings have the shape of glibc malloc arenas, but I haven't confirmed which allocations they hold. Left running, an earlier default run reached about 27.3 GiB (18.2 GiB resident plus 9.0 GiB swapped) after 6.5 minutes and about 740 compiled shaders. This lines up with the slow optimized compile noted in #2552. As noted there, `APS5_SPIRV_OPT=none` still optimizes specialized modules, so this compares the default against that switch, not against no optimization at all.
014-code commented 2026-10-12 04:52:10 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by 014-code on 2026-10-12T04:52:10Z.


Implemented and opened PR #4288: https://github.com/boykopovar/AnyPS5/pull/4288

What I found:

  • The default AnyPS5 path in ValidateAndOptimizeSpirv() uses SPIRV-Tools CreateScalarReplacementPass(0) twice.
  • In SPIRV-Tools, 0 means no element-count limit. For function-scope arrays and structs, the pass creates replacement variables and rewrites loads/stores for the composite elements.
  • A large or nested shader composite can therefore cause very large temporary host-side IR during optimization. This matches the issue's host memory growth and does not require GPU VRAM pressure to reproduce.

What I changed:

  • Added one ScalarReplacementLimit = 64 constant in the AnyPS5 optimizer.
  • Passed that limit to both scalar-replacement registrations.
  • This keeps scalar replacement for small composites while preventing large composites from being expanded without a bound.
  • Added a regression case using a valid 1,000-element function array, two storage buffers, a branch merge, and a memory barrier. The test validates the optimized SPIR-V and bounds the optimized module size. With the old unlimited setting, the same synthetic module grew to 18,229 words; with the 64-element limit it was 233 words.

How I verified it:

  • Ubuntu 24.04 Docker, Release build, SPIRV-Tools enabled, Mesa/lavapipe.
  • agc_spirv_optimizer_tests passed with the default optimizer, APS5_SPIRV_OPT=none, and APS5_SPIRV_OPT=full.
  • Focused ctest agc_spirv_optimizer: 1/1 passed.
  • Conventions and git diff --check: passed.
  • No RTX 3080 or Demon's Souls run was available, so the title-level memory reduction still needs confirmation from the original reporter.

The regression module contains a storage-only narrow type so the custom AnyPS5 pipeline is exercised consistently even when the full switch is set. The explicit SPIRV-Tools RegisterPerformancePasses(true) path for ordinary modules remains intentionally unchanged; the fix targets AnyPS5's default/custom path reported by this issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117976225 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4166#issuecomment-6117976225). Originally posted by [014-code](https://github.com/014-code) on 2026-10-12T04:52:10Z. --- Implemented and opened PR #4288: https://github.com/boykopovar/AnyPS5/pull/4288 What I found: - The default AnyPS5 path in ValidateAndOptimizeSpirv() uses SPIRV-Tools CreateScalarReplacementPass(0) twice. - In SPIRV-Tools, 0 means no element-count limit. For function-scope arrays and structs, the pass creates replacement variables and rewrites loads/stores for the composite elements. - A large or nested shader composite can therefore cause very large temporary host-side IR during optimization. This matches the issue's host memory growth and does not require GPU VRAM pressure to reproduce. What I changed: - Added one ScalarReplacementLimit = 64 constant in the AnyPS5 optimizer. - Passed that limit to both scalar-replacement registrations. - This keeps scalar replacement for small composites while preventing large composites from being expanded without a bound. - Added a regression case using a valid 1,000-element function array, two storage buffers, a branch merge, and a memory barrier. The test validates the optimized SPIR-V and bounds the optimized module size. With the old unlimited setting, the same synthetic module grew to 18,229 words; with the 64-element limit it was 233 words. How I verified it: - Ubuntu 24.04 Docker, Release build, SPIRV-Tools enabled, Mesa/lavapipe. - agc_spirv_optimizer_tests passed with the default optimizer, APS5_SPIRV_OPT=none, and APS5_SPIRV_OPT=full. - Focused ctest agc_spirv_optimizer: 1/1 passed. - Conventions and git diff --check: passed. - No RTX 3080 or Demon's Souls run was available, so the title-level memory reduction still needs confirmation from the original reporter. The regression module contains a storage-only narrow type so the custom AnyPS5 pipeline is exercised consistently even when the full switch is set. The explicit SPIRV-Tools RegisterPerformancePasses(true) path for ordinary modules remains intentionally unchanged; the fix targets AnyPS5's default/custom path reported by this issue.
oneandonlydean commented 2026-10-13 07:38:10 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T07:38:10Z.


#4402 is on main: the bundled SPIRV-Tools releases its traversal maps as it goes. Does host memory still grow at about 0.6 GiB/s with the optimizer on?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140712492 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4166#issuecomment-6140712492). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T07:38:10Z. --- #4402 is on main: the bundled SPIRV-Tools releases its traversal maps as it goes. Does host memory still grow at about 0.6 GiB/s with the optimizer on?
btsouth commented 2026-10-14 11:51:41 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by btsouth on 2026-10-14T11:51:41Z.


Note

Posted by an AI agent on behalf of Tyler.

The offline optimizer no longer runs away on the saved Demon's Souls modules with #4402. I have not rerun the game, so I cannot confirm the exact in-game 0.6 GiB/s start-up rate.

I rebuilt the default pipeline against the same bundled SPIRV-Tools pin, comparing main 2a2a3dc650 (with #4402) against #4402's merge parent ebf417a51c. One fresh process per module, one worker, 16 GiB memory cap, no swap, 120 s timeout:

Inputs Before #4402: peak RSS MiB / wall s / completed With #4402: peak RSS MiB / wall s / completed
Largest, 914,375 instructions ≥16,270 / 19.65 / 0 of 1, OOM 2,796 / 65.25 / 1 of 1
Next 10 largest ≥16,270 to ≥16,355 / 17.54 to 26.52 / 0 of 10, OOM 834 to 2,796 / 39.50 to 72.72 / 10 of 10
Four mid-size modules 834 to 1,404 / 1.90 to 3.01 / 4 of 4 85 to 112 / 1.20 to 1.70 / 4 of 4

All completed runs passed input and final output validation. OOM peaks are sampled lower bounds, and wall times are from a shared devbox. These are the same saved inputs as the earlier pass-isolation test, generated by an older local patched recompiler build, not newly emitted current-main shaders. The offline runs do not reproduce the game's start-up ordering or concurrency.

#4402 fixes the reproduced traversal-map failure without #4288. #4288 still addresses a separate scalar-replacement expansion risk. The largest module here has no function-local arrays or structs to split, so this result does not establish whether that limit is needed for other shaders.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6167697121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4166#issuecomment-6167697121). Originally posted by [btsouth](https://github.com/btsouth) on 2026-10-14T11:51:41Z. --- > [!NOTE] > Posted by an AI agent on behalf of Tyler. The offline optimizer no longer runs away on the saved Demon's Souls modules with #4402. I have not rerun the game, so I cannot confirm the exact in-game 0.6 GiB/s start-up rate. I rebuilt the default pipeline against the same bundled SPIRV-Tools pin, comparing main `2a2a3dc650` (with #4402) against #4402's merge parent `ebf417a51c`. One fresh process per module, one worker, 16 GiB memory cap, no swap, 120 s timeout: | Inputs | Before #4402: peak RSS MiB / wall s / completed | With #4402: peak RSS MiB / wall s / completed | |---|---|---| | Largest, 914,375 instructions | ≥16,270 / 19.65 / 0 of 1, OOM | 2,796 / 65.25 / 1 of 1 | | Next 10 largest | ≥16,270 to ≥16,355 / 17.54 to 26.52 / 0 of 10, OOM | 834 to 2,796 / 39.50 to 72.72 / 10 of 10 | | Four mid-size modules | 834 to 1,404 / 1.90 to 3.01 / 4 of 4 | 85 to 112 / 1.20 to 1.70 / 4 of 4 | All completed runs passed input and final output validation. OOM peaks are sampled lower bounds, and wall times are from a shared devbox. These are the same saved inputs as the earlier pass-isolation test, generated by an older local patched recompiler build, not newly emitted current-main shaders. The offline runs do not reproduce the game's start-up ordering or concurrency. #4402 fixes the reproduced traversal-map failure without #4288. #4288 still addresses a separate scalar-replacement expansion risk. The largest module here has no function-local arrays or structs to split, so this result does not establish whether that limit is needed for other shaders.
oneandonlydean commented 2026-10-14 18:45:58 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T18:45:58Z.


Thanks, that matches: #4402 stops the runaway. Closing; please reopen if host memory still grows like this at start-up in the game on current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175754450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4166#issuecomment-6175754450). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T18:45:58Z. --- Thanks, that matches: #4402 stops the runaway. Closing; please reopen if host memory still grows like this at start-up in the game on current main.
Sign in to join this conversation.
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#4166
No description provided.