Skip to content

perf(shader): allocate a program's IR values in chunks - #11798

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:perf/ir-value-pool
Nov 2, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
milescoviello:perf/ir-value-pool

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

IrProgram::CreateValue allocates every IR value separately with std::make_unique, and ~IrProgram frees them one at a time. On the Crimson Desert replay set it is the biggest allocator caller on main: 3.5 % of the profile's samples are in malloc under it, and another 2.2 % are in free under ~IrProgram (the SRT evaluator, #11719/#11755, is next at 3.4 %). Values are allocated during every shader preparation, and each draw or dispatch with a new shader goes through it.

This change keeps a program's values in chunks, starting at 64 values per chunk and doubling up to 4096:

  • CreateValue constructs each new value in the next free slot.
  • ReclaimUnusedValues destroys the unused values and keeps their slots for the values the program creates next.
  • The destructor destroys the live values and frees the chunks.

On 152 Crimson Desert (PPSA25286 01.000.568) shader requests, agc_shader_replay --prepare uses 18.2 % less CPU time (10 of 10 rounds, both orders). All 65 Crimson Desert and 302 MXGP 26 (PPSA30839 01.003.000) SPIR-V outputs are byte-identical, and max RSS stays the same.

How

IrValuePool (in IrProgram.hpp) replaces std::vector<std::unique_ptr<IrValue>>:

  • Storage: raw chunks from ::operator new with IrValue's alignment, as IrValueArena already does for plans. The pool also keeps the live values in creation order and a list of free slots.
  • Create: takes a freed slot if there is one, otherwise the next slot of the newest chunk. If the IrValue constructor throws, nothing is recorded.
  • EraseIf: destroys the matching values, keeps their slots, and leaves the live list in order, as std::erase_if on the old vector did.
  • Moves: moving a program moves the chunks, so value addresses stay valid. agc_ir_reclaim covers move construction and move assignment.

ReclaimUnusedValues iterates in the same order as before, and IDs still come from nextValueId and are never reused.

Memory: compileVariant keeps creating values after the plan's reclaim, and those new values reuse the freed slots, much as free followed by malloc did before. A program holds at most one partly used chunk of up to 4096 values.

Where the time goes: one DWARF profile per side (perf record --call-graph dwarf -F 199, same input) shows:

  • Total samples fall from 3110 to 2491 (−19.9 %).
  • Allocator functions (_int_malloc, unlink_chunk, free, _int_free_merge_chunk and friends) fall from 778 to 512.
  • IrValue accessors (HasUses, Parent, Id, Resolve, IsPhi and others) fall from 433 to 266. The values now sit next to each other, so passes that walk them miss the cache less often.

Tested

Linux, i7-13700K, GCC 15.2, Release with ANYPS5_ENABLE_SPIRV_TOOLS=OFF. I built cmake --build build-rel --target agc_shader_replay at main 1392606 and at this change (c31e8e0fe3).

  • Equivalence: I ran ANYPS5_NO_SHADER_CACHE=1 agc_shader_replay --spv over the same 152 Crimson Desert and 305 MXGP 26 requests as perf(shader): reuse the evaluator's requirement list across values #11719 (input hashes in #11719). Each binary ran in an empty directory per set, and I diffed the sha256sum * | sort -k2 lists. All 65 and 302 SPIR-V outputs are byte-identical to main's.
  • CPU time and memory: each run was ANYPS5_NO_SHADER_CACHE=1 /usr/bin/time -f %M perf stat -x, -e task-clock agc_shader_replay --prepare <the 152 requests> in a fresh empty directory, with the order alternating per round:
Round Order main (ms) this PR (ms) change main max RSS (KB) this PR max RSS (KB)
1 main, this 16563 13498 -18.5 % 271364 269032
2 this, main 15940 13130 -17.6 % 271184 271904
3 main, this 15651 12729 -18.7 % 271732 268944
4 this, main 15912 12962 -18.5 % 271444 269168
5 main, this 16967 13918 -18.0 % 271440 268944
6 this, main 15785 12798 -18.9 % 271460 269220
7 main, this 15690 12805 -18.4 % 271768 271908
8 this, main 15763 12893 -18.2 % 270928 269480
9 main, this 15684 12958 -17.4 % 271020 269016
10 this, main 15645 12859 -17.8 % 271392 269464

Over the 10 rounds, the means are 15.96 s for main and 13.06 s for this PR, an 18.2 % drop (medians 15.77 and 12.93 s, SD 0.45 and 0.37 s). This PR is lower in all 10 rounds. Mean max RSS is 271.4 MB for main and 269.7 MB for this PR.

The binaries' sha256 prefixes are 50c1220f47cc (main) and 8c3d6925612d (this PR). These numbers use glibc's allocator, which titles use on Linux. I haven't measured Windows' default heap or APS5_MIMALLOC=1, where the allocator share of the gain may differ.

  • Tests: I ran the 19 AGC test targets whose sources use IrProgram: agc_ir_reclaim, agc_validate_program, agc_masked_select, agc_redundant_value_elimination, agc_constant_bit_test, agc_constant_phi, agc_constant_select, agc_denormal_flush_elimination, agc_flat_scratch, agc_guarded_memory_loads, agc_integer_folding, agc_integer_narrowing, agc_placeholder_images, agc_scalar_select_mask, agc_shader_device_profile, agc_shader_mode_register, agc_spirv_failure_text, agc_tessellation_lowering and agc_vector_window (20 ctest entries). Each passes 3 of 3 (--repeat until-fail:3) on an RTX 4070 Ti (NVIDIA 615.71.09) at this head. On Windows 11 (WinLibs GCC 15.2, Release, GT 1030 with NVIDIA R580), the same tests except agc_flat_scratch (19 entries) pass 3 of 3 at this head and at its base 13926067ac. agc_ir_reclaim covers what this change has to keep: reclaim counts, IDs that are never reused, and values staying valid across program move construction and move assignment. Under AddressSanitizer (ANYPS5_ENABLE_ASAN=ON, RelWithDebInfo), agc_ir_reclaim and agc_validate_program pass 3 of 3 at this head with no ASan report.

This is a CPU-only shader-compilation change with byte-identical SPIR-V, like #11719, #9132 and #9198. It therefore uses the paired CPU replay that oneandonlydean accepted for those (#11719) instead of title captures.

Rebase and a kept-program fix (10-30)

#12039 landed before this PR, so it is now rebased onto main 91db3f79f7. The one conflict was the allocation line of IrProgram::CreateValue. It is resolved the way #12039's description validated: IrValuePool::Create takes the use arena and CreateValue passes useArena.Resource(). useArena is declared before values, so the pool still destroys its values while the arena is alive, as main's vector did.

The rebased A/B found a memory problem, which the second commit (cb2159e48f) fixes:

  • getSource keeps the program it built a plan from, for the source's first variant compile. When Materialize then throws, no compile runs, so the program stays with the SourceEntry for the life of the process. main keeps it too, but compactly.
  • With the pool, a kept program holds the slots of its peak value count. 87 of the 152 Crimson Desert requests fail in replay (SrtWalker::EvaluateDescriptorSources), and their kept programs held about 1 GB (jemalloc peak profile: all of it under getSource -> ResourceProgram).
  • recompileOnce now releases the kept program when materialization throws. A later request for the source prepares it again.

Replay A/B at cb2159e48f against 91db3f79f7: agc_shader_replay --spv over the 152 Crimson Desert and 305 MXGP requests, ANYPS5_NO_SHADER_CACHE=1, 10 interleaved rounds, task-clock and max RSS:

  • SPIR-V is byte-identical for both sets (65 and 302 modules, 0 differ).
  • CPU: 19.73 s on main and 17.11 s here (-13.3 %), lower in every round.
  • Max RSS: 1,360 MiB on main and 663 MiB here (-51 %), lower in every round. Most of that is the released programs, which main also keeps. I didn't measure main with only the second commit.

At fa13b1cc64 (before the fix), the same A/B gave -12.8 % CPU but +44 % max RSS (comment). On the 367 requests that compile, the pool alone was already lower: 598 vs 617 MiB.

Tests at cb2159e48f: the 20 ctest entries listed under Tested pass 3 of 3 on the RTX 4070 Ti (NVIDIA 615.71.09). On Windows 11 (WinLibs GCC 15.2, Release, GT 1030 with NVIDIA R580), the same tests except agc_flat_scratch (19 entries) pass 3 of 3 at cb2159e48f and at main 91db3f79f7. The replay timings in the Tested list and the title captures below were measured against the earlier base 13926067ac.

Title captures: Super Meat Boy 3D, main 9af563f33d + #11580 (608dd76843) against the same + this PR (f4aa08df21), 3 interleaved rounds, recompiler cache off. Shader.PrepareArtifact worker time is 12,257 ms → 9,732 (-20.6 %) ms (table and traces). Round 2's summary lines:

{"commit":"608dd7684303","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60050.392},"presents":418,"frame_ms":{"p50":125.479,"p90":143.004,"p99":1199.044,"max":4569.628},"gpu_busy_ms_per_present":85.197,"worker_busy_pct":{"queue worker 0x0":85.267,"queue worker 0x20":83.270},"top_phases_ms_per_frame":{"Driver.Packet":242.053,"Driver.Packet.memory_wait":119.506,"Driver.Packet.dispatch":59.516,"Driver.Dispatch":59.349,"Shader.PrepareArtifact":45.222,"GpuMutex.wait.hook":40.966,"Recorder.Sync":39.158,"Driver.Packet.pipeline_drain":39.118},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":42.929,"events_lost":271345,"totals_full":false}},"trace":"aps5-trace-608dd7684303-3.json.gz"}
{"commit":"f4aa08df21bf","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60128.758},"presents":424,"frame_ms":{"p50":126.356,"p90":139.054,"p99":1435.618,"max":3922.779},"gpu_busy_ms_per_present":88.292,"worker_busy_pct":{"queue worker 0x0":86.808,"queue worker 0x20":84.254},"top_phases_ms_per_frame":{"Driver.Packet":242.499,"Driver.Packet.memory_wait":118.853,"Driver.Packet.dispatch":57.704,"Driver.Dispatch":57.577,"Driver.Packet.pipeline_drain":40.397,"Recorder.Sync":40.110,"GpuMutex.wait.hook":37.600,"Shader.PrepareArtifact":34.582},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.547,"events_lost":317419,"totals_full":false}},"trace":"aps5-trace-f4aa08df21bf-3.json.gz"}

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
  • 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: none
  • GPUs tested: NVIDIA RTX 4070 Ti (Linux, 615.71.09) for the AGC tests; the replay itself is CPU-only
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 27, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head c31e8e0. The pool keeps IRValue addresses stable across chunks and moves, preserves creation-order iteration and IDs, destroys reclaimed values exactly once, reuses only their slots, and rolls back the live-list insertion if allocation/construction fails. The existing reclaim tests cover move construction/assignment and stable IDs. The author reports byte-identical SPIR-V over 367 shader outputs, 10 interleaved CPU-replay rounds all favoring the change, 19 focused AGC test targets repeated on Linux/Windows, and ASan runs; hosted product jobs are skipped and I did not rerun them. I accept paired CPU replay for this CPU-only compilation change under the maintainer precedent recorded on #11719/#9132; no game capture is needed for this specific class of change. The automated perf numbers missing label remains an intake/reporting status for maintainers to handle.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Title captures for this change, with the same setup as the captures on #12039. The base is 608dd76843, which is main 9af563f33d + #11580: Super Meat Boy 3D needs #11580 to run past 11 s on main. The comparison is with this PR (head c31e8e0fe3) merged on top of it (f4aa08df21).

Setup: Super Meat Boy 3D (PPSA31162 02.000.001), release libs, 100 s at a 12 GB cap with the same input script, recompiler cache off (ANYPS5_NO_SHADER_CACHE=1) so every shader is prepared, a warm driver cache, APS5_TRACE_SECONDS=0:60, and 3 rounds alternating which side goes first. Linux, RTX 4070 Ti, 615.71.09.

Shader.PrepareArtifact, summed over the trace window (total ms / slices):

Round main workers this PR workers main title thread this PR title thread
1 (main first) 12,262 / 488 9,581 / 493 6,677 / 388 5,127 / 401
2 (PR first) 12,473 / 493 9,889 / 484 6,429 / 378 4,773 / 378
3 (main first) 12,036 / 513 9,727 / 489 5,724 / 372 4,291 / 382
mean 12,257 9,732 (-20.6 %) 6,277 4,731 (-24.6 %)

The summary lines of round 2:

{"commit":"608dd7684303","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60050.392},"presents":418,"frame_ms":{"p50":125.479,"p90":143.004,"p99":1199.044,"max":4569.628},"gpu_busy_ms_per_present":85.197,"worker_busy_pct":{"queue worker 0x0":85.267,"queue worker 0x20":83.270},"top_phases_ms_per_frame":{"Driver.Packet":242.053,"Driver.Packet.memory_wait":119.506,"Driver.Packet.dispatch":59.516,"Driver.Dispatch":59.349,"Shader.PrepareArtifact":45.222,"GpuMutex.wait.hook":40.966,"Recorder.Sync":39.158,"Driver.Packet.pipeline_drain":39.118},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":42.929,"events_lost":271345,"totals_full":false}},"trace":"aps5-trace-608dd7684303-3.json.gz"}
{"commit":"f4aa08df21bf","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.0.0-38-generic","cpu":"13th Gen Intel(R) Core(TM) i7-13700K","cpu_threads":24,"memory_mib":30993,"title_id":"PPSA31162","title":"Super Meat Boy 3D","content_version":"02.000.001","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":60128.758},"presents":424,"frame_ms":{"p50":126.356,"p90":139.054,"p99":1435.618,"max":3922.779},"gpu_busy_ms_per_present":88.292,"worker_busy_pct":{"queue worker 0x0":86.808,"queue worker 0x20":84.254},"top_phases_ms_per_frame":{"Driver.Packet":242.499,"Driver.Packet.memory_wait":118.853,"Driver.Packet.dispatch":57.704,"Driver.Dispatch":57.577,"Driver.Packet.pipeline_drain":40.397,"Recorder.Sync":40.110,"GpuMutex.wait.hook":37.600,"Shader.PrepareArtifact":34.582},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":39.547,"events_lost":317419,"totals_full":false}},"trace":"aps5-trace-f4aa08df21bf-3.json.gz"}

All six traces, summary lines and tools/trace_digest.py outputs are release assets on my fork: pr11798-smb3d-captures.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot removed the perf numbers missing perf pull request without a before and after summary line label Oct 28, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 28, 2026
TheBlenDed pushed a commit to TheBlenDed/AnyPS5 that referenced this pull request Oct 29, 2026
Adapt upstream PR boykopovar#11798 at c31e8e0 to this integration. Preserve the local Values() accessor as a non-owning view and cover large-program growth, reclamation and moves. Paired focused validation passes on the saved main and candidate; synthetic prepared SPIR-V is byte-identical. Real Yotei loading and shader-corpus performance remain unmeasured.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 5021 core/shader/recompiler/Recompiler.cpp
PR 5609 core/shader/recompiler/Recompiler.cpp
PR 6412 core/shader/recompiler/Recompiler.cpp
PR 6488 core/shader/recompiler/Recompiler.cpp
PR 6858 core/shader/recompiler/Recompiler.cpp
PR 6924 core/shader/recompiler/Recompiler.cpp
PR 7223 core/shader/recompiler/Recompiler.cpp
PR 7410 core/shader/recompiler/Recompiler.cpp
PR 7642 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrProgram.hpp
core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp
core/shader/recompiler/Recompiler.cpp
PR 8004 core/shader/recompiler/Recompiler.cpp
PR 9020 core/shader/recompiler/Recompiler.cpp
PR 9335 core/shader/recompiler/Recompiler.cpp
PR 9356 core/shader/recompiler/Recompiler.cpp
PR 9387 core/shader/recompiler/Recompiler.cpp
PR 9508 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrProgram.hpp
core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp
core/shader/recompiler/Recompiler.cpp
PR 10359 core/shader/recompiler/Recompiler.cpp
PR 10757 core/shader/recompiler/Recompiler.cpp
PR 11351 core/shader/recompiler/Recompiler.cpp
PR 11483 core/shader/recompiler/Recompiler.cpp
PR 12324 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrProgram.hpp
core/shader/recompiler/IntermediateRepresentation/src/IrProgram.cpp
core/shader/recompiler/Recompiler.cpp
PR 12548 core/shader/recompiler/Recompiler.cpp
PR 12662 core/shader/recompiler/Recompiler.cpp
PR 12790 core/shader/recompiler/Recompiler.cpp
PR 12828 core/shader/recompiler/Recompiler.cpp
PR 12840 core/shader/recompiler/Recompiler.cpp
PR 12944 core/shader/recompiler/Recompiler.cpp
PR 12980 core/shader/recompiler/Recompiler.cpp
PR 14032 core/shader/recompiler/Recompiler.cpp
PR 14853 core/shader/recompiler/Recompiler.cpp
PR 14871 core/shader/recompiler/Recompiler.cpp
PR 14944 core/shader/recompiler/Recompiler.cpp

@TechLevelZero

Copy link
Copy Markdown
Contributor

✅ NVIDIA GPU test: passed

PR head c31e8e0 merged onto main f6fddd6 · tested 2026-10-30 08:49 UTC · 22 min

GPU NVIDIA RTX 2000 Ada Generation, driver 595.99.02, Vulkan 1.4.329
Host Ubuntu 26.04.1 LTS, kernel 7.0.0-38-generic, container ubuntu:24.04
Logs build and test logs for this run
Build ✅ passed (Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON)
Tests ✅ 2,261 passed · 4 failed (0 new, 2 also fail on main, 2 unstable on main) · 41 skipped (ANYPS5_REQUIRE_VULKAN=1)
Test setup CPU-only tests without a GPU; GPU tests (and any CPU-only test that failed or skipped without one) once on the GPU with VK_LAYER_KHRONOS_validation and ANYPS5_NO_SHADER_CACHE=1; main ran the same way
Test selection 2,306 of 2,950 tests run: the ones this change can reach, chosen by tools/select_tests.py from main's build graph
Vulkan validation ✅ no new validation IDs vs main (862 tests run on the GPU)
Slow tests (>30 s, GPU tests timed with validation layers) ⚠️ agc_driver_bda_device 70.7 s, ⚠️ agc_driver_graphics_maps_fallback 36.8 s
Intermittent validation messages (5, seen once in the parallel run, not reproduced when the test was re-run alone 2 times; not counted against this PR)
agc_driver_depth_clear_hle: VUID-vkCmdBindDescriptorSets-commandBuffer-recording (3x)
agc_driver_depth_clear_hle: VUID-vkCmdBindPipeline-commandBuffer-recording (3x)
agc_driver_depth_clear_hle: VUID-vkCmdDispatch-commandBuffer-recording (3x)
agc_driver_depth_clear_hle: VUID-vkCmdPipelineBarrier-commandBuffer-recording (3x)
agc_driver_depth_clear_hle: VUID-vkCmdPushConstants-commandBuffer-recording (3x)
Unstable on main (2, these also failed in recent main runs on this machine; not counted against this PR)
agc_driver_depth_clear_hle: failed in 1 of the last 7 main runs
agc_driver_submit_progress: failed in 5 of the last 7 main runs
Failing on main too (2, not counted against this PR)
guest_mmap_executable
guest_socket_options_multicast
Validation IDs already present on main (4 IDs in 391 tests)
VUID-VkDeviceCreateInfo-pNext-pNext (391 tests): agc_compute_pipeline_replay, agc_driver, agc_driver_address_passes, agc_driver_address_passes_draw_transitions, agc_driver_address_passes_targets_split, agc_driver_alias_copy_label, agc_driver_array_layer_target_cpu, agc_driver_bda_span_reads_draw_pipeline, and 383 more
VUID-vkCmdCopyImage-srcImage-01548 (11 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_cpu_write_after_memo, agc_driver_depth_plane_cpu_write_after_page_unwatch, agc_driver_depth_plane_cpu_write_after_partial_unwatch, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_partial_store_unimported, agc_driver_depth_storage_dispatch, and 3 more
VUID-vkCmdCopyImage-srcImage-01551 (11 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_cpu_write_after_memo, agc_driver_depth_plane_cpu_write_after_page_unwatch, agc_driver_depth_plane_cpu_write_after_partial_unwatch, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_partial_store_unimported, agc_driver_depth_storage_dispatch, and 3 more
VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (2 tests): agc_driver_prepared_exports, agc_driver_srgb8_color_target
Skipped (41)
agc_driver_bda_reads_shadowed_results: skipped, the device reports watched host memory written when it imports or reads it
agc_driver_bda_reads_shadowed_results_per_build: skipped, the device reports watched host memory written when it imports or reads it
agc_driver_msaa_dcc_compared_import_2: skipped, the device reports host imports that keep write protection, so the write-watch probe compares none of them
agc_driver_msaa_dcc_compared_import_4: skipped, the device reports host imports that keep write protection, so the write-watch probe compares none of them
agc_driver_msaa_dcc_compared_import_8: skipped, the device reports host imports that keep write protection, so the write-watch probe compares none of them
agc_driver_sample_order_grid_reference: skipped, the device does not keep depth drawn at custom sample locations at those locations, even after a transition that names them: the composite of uniform renders holds the guest's depth in 0 of 3005 sloped samples, so no path can be compared with it bit for bit
agc_driver_sparse_dma_buf_import: skipped, host imports are unavailable: /dev/udmabuf is not accessible
agc_driver_stencil_ref_export: skipped, the device lacks VK_EXT_shader_stencil_export
agc_driver_vertex_elected_atomic_max: skipped, the device runs vertex programs in subgroups, so they keep their cross-lane reductions
agc_host_import_sparse: skipped, host imports are unavailable: /dev/udmabuf is not accessible
agc_import_settle_initial_dcc: skipped, the device has no deferred settle of host imports: imports under 1 MiB wait for one only on Windows
agc_unit_shadow_completion_store: skipped, the device reports watched host memory written when it imports or reads it
guest_http_uri_locale: 
guest_net_ctl: skipped, the host has no IPv4 address
guest_video_out_color_space: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_hdr: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_high_frame_rate: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_output_mode: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_resolution_status: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_wqhd_detection: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
video_out_flip_controls: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip_in_order: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop_aftergpu: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop_main: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_fresh: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or plat
Reproduce
git fetch origin c31e8e0fe3ff08f37d63a08a8ee2ab7d9a59dd5b f6fddd60d826831f70f7c62e25423610ee742bcc
git checkout f6fddd60d826831f70f7c62e25423610ee742bcc && git merge --no-ff c31e8e0fe3ff08f37d63a08a8ee2ab7d9a59dd5b
git submodule update --init --recursive
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON
cmake --build build && cmake --build build --target libs
VK_DRIVER_FILES=/nonexistent/icd.json ctest --test-dir build -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 32 --output-on-failure
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation \
  ctest --test-dir build -R '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 4 --output-on-failure
# failures are then re-run alone: --timeout 360 --parallel 2; new validation IDs: the test alone, twice, --parallel 1

Automated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 30, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 30, 2026
@milescoviello

Copy link
Copy Markdown
Contributor Author

Marking this as draft: the rebased head trades memory for its speed, and I need to fix that first.

The replay A/B on the rebased head (fa13b1cc64 against its base main 91db3f79f7, agc_shader_replay --spv over the 152 Crimson Desert and 305 MXGP requests, 10 interleaved rounds, ANYPS5_NO_SHADER_CACHE=1):

  • Output: byte-identical SPIR-V for both sets (65 and 302 modules, 0 differ).
  • CPU: 19.67 s on main and 17.14 s here (-12.8 %), lower in all 10 rounds.
  • Max RSS: 1.39 GB on main and 2.00 GB here (+44 %), in all 10 rounds.

It's the same with jemalloc (1.42 vs 2.06 GB) and with glibc's mmap threshold pinned at 128 KiB (1.37 vs 1.95 GB), so it isn't the allocator. Each of the 10 largest MXGP requests run on its own peaks lower here (for example 107 vs 79 MB), and the MXGP set alone is flat (621 vs 630 MB). The whole difference is in the Crimson Desert set (811 vs 1,398 MB), whose 152 requests share 65 modules.

My reading is that it's the programs a SourceEntry keeps. Once ReclaimUnusedValues runs, a pool keeps the slots of the values it erased (it reuses them, but never frees a chunk), while main frees each one. So every kept program holds memory for its peak value count instead of its live count. I'll confirm that and come back with a fix (for example, releasing whole chunks no live value uses before a program is kept), then rerun this A/B.

AI-assisted: yes (Claude Code)

@milescoviello
milescoviello marked this pull request as draft October 30, 2026 21:26
…rialize

getSource keeps the program it built a plan from, for the source's first
variant compile. When Materialize then threw, no compile ran, so the
program stayed with the SourceEntry for the life of the process. With the
value pool such a program holds the slots of its peak value count. Over
the replay corpus, the 90 requests whose descriptor sources do not
evaluate kept about 1 GB this way. recompileOnce now releases the program
when materialization throws; a later request for the source prepares it
again.

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

Copy link
Copy Markdown
Contributor Author

Fixed, and back to ready for review. The head is now cb2159e48f, one commit on fa13b1cc64.

Cause. A jemalloc peak profile put the extra memory in IrProgram::CreateValue, all of it under getSource -> ResourceProgram. getSource keeps the program it built a plan from, for the source's first variant compile. When Materialize then throws, no compile runs, so the program stays with the SourceEntry for good. 87 of the 152 Crimson Desert replay requests fail that way (SrtWalker::EvaluateDescriptorSources). main keeps those programs too, but compactly. With the pool, each one held the slots of its peak value count.

Fix. recompileOnce now releases the kept program when materialization throws. A later request for the source prepares it again.

Replay A/B at cb2159e48f against main 91db3f79f7 (10 interleaved rounds, 457 requests, --spv, ANYPS5_NO_SHADER_CACHE=1):

  • SPIR-V is byte-identical (65 and 302 modules, 0 differ).
  • CPU: 19.73 s on main and 17.11 s here (-13.3 %), lower in every round.
  • Max RSS: 1,360 MiB on main and 663 MiB here (-51 %), lower in every round. Most of the drop is the released programs, which main also keeps. I didn't measure main with only the fix.

Tests at cb2159e48f: the 20 IrProgram ctest entries pass 3 of 3 on Linux (RTX 4070 Ti, NVIDIA 615.71.09). On the Windows VM (GT 1030, R580), the 19 that build there pass 3 of 3 at the head and at main. The description's "Rebase and a kept-program fix" section has the details.

AI-assisted: yes (Claude Code)

@milescoviello
milescoviello marked this pull request as ready for review October 30, 2026 23:01
@TechLevelZero

Copy link
Copy Markdown
Contributor

✅ NVIDIA GPU test: passed

PR head cb2159e merged onto main 7fcab08 · tested 2026-10-30 23:36 UTC · 26 min

GPU NVIDIA RTX 2000 Ada Generation, driver 595.99.02, Vulkan 1.4.329
Host Ubuntu 26.04.1 LTS, kernel 7.0.0-38-generic, container ubuntu:24.04
Logs build and test logs for this run
Build ✅ passed (Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON)
Tests ✅ 2,315 passed · 5 failed (0 new, 2 also fail on main, 3 unstable on main) · 41 skipped (ANYPS5_REQUIRE_VULKAN=1)
Test setup CPU-only tests without a GPU; GPU tests (and any CPU-only test that failed or skipped without one) once on the GPU with VK_LAYER_KHRONOS_validation and ANYPS5_NO_SHADER_CACHE=1; main ran the same way
Test selection 2,361 of 3,041 tests run: the ones this change can reach, chosen by tools/select_tests.py from main's build graph
Vulkan validation ✅ no new validation IDs vs main (864 tests run on the GPU)
Slow tests (>30 s, GPU tests timed with validation layers) ⚠️ agc_driver_bda_device 51.4 s, ⚠️ agc_driver_graphics 31.2 s, ⚠️ agc_driver_graphics_maps_fallback 30.6 s
Intermittent validation messages (1, seen once in the parallel run, not reproduced when the test was re-run alone 2 times; not counted against this PR)
agc_driver_depth_clear_hle: VUID-vkCmdCopyBuffer-commonparent (6x)
Unstable on main (3, these also failed in recent main runs on this machine; not counted against this PR)
agc_driver_depth_clear_hle: failed in 1 of the last 10 main runs
agc_driver_submit_progress: failed in 6 of the last 10 main runs
agc_submit_progress: failed in 6 of the last 10 main runs
Failing on main too (2, not counted against this PR)
guest_mmap_executable
guest_socket_options_multicast
Validation IDs already present on main (3 IDs in 380 tests)
VUID-VkDeviceCreateInfo-pNext-pNext (380 tests): agc_compute_pipeline_replay, agc_driver, agc_driver_address_passes, agc_driver_address_passes_draw_transitions, agc_driver_address_passes_targets_split, agc_driver_alias_copy_label, agc_driver_alpha_to_coverage, agc_driver_array_layer_target, and 372 more
VUID-vkCmdCopyImage-srcImage-01548 (12 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_cpu_write_after_memo, agc_driver_depth_plane_cpu_write_after_page_unwatch, agc_driver_depth_plane_cpu_write_after_partial_unwatch, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_partial_store_unimported, agc_driver_depth_plane_sampling, and 4 more
VUID-vkCmdCopyImage-srcImage-01551 (12 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_cpu_write_after_memo, agc_driver_depth_plane_cpu_write_after_page_unwatch, agc_driver_depth_plane_cpu_write_after_partial_unwatch, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_partial_store_unimported, agc_driver_depth_plane_sampling, and 4 more
Skipped (41)
agc_driver_bda_reads_shadowed_results: skipped, the device reports watched host memory written when it imports or reads it
agc_driver_bda_reads_shadowed_results_per_build: skipped, the device reports watched host memory written when it imports or reads it
agc_driver_msaa_dcc_compared_import_2: skipped, the device reports host imports that keep write protection, so the write-watch probe compares none of them
agc_driver_msaa_dcc_compared_import_4: skipped, the device reports host imports that keep write protection, so the write-watch probe compares none of them
agc_driver_msaa_dcc_compared_import_8: skipped, the device reports host imports that keep write protection, so the write-watch probe compares none of them
agc_driver_sample_order_grid_reference: skipped, the device does not keep depth drawn at custom sample locations at those locations, even after a transition that names them: the composite of uniform renders holds the guest's depth in 0 of 3005 sloped samples, so no path can be compared with it bit for bit
agc_driver_stencil_ref_export: skipped, the device lacks VK_EXT_shader_stencil_export
agc_driver_vertex_elected_atomic_max: skipped, the device runs vertex programs in subgroups, so they keep their cross-lane reductions
agc_host_import_sparse: skipped, host imports are unavailable: /dev/udmabuf is not accessible
agc_import_settle_initial_dcc: skipped, the device has no deferred settle of host imports: imports under 1 MiB wait for one only on Windows
agc_unit_shadow_completion_store: skipped, the device reports watched host memory written when it imports or reads it
guest_http_uri_locale: 
guest_net_ctl: skipped, the host has no IPv4 address
guest_video_out_color_space: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_hdr: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_high_frame_rate: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_output_mode: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_resolution_status: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_wqhd_detection: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
video_out_flip_controls: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip_in_order: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop_aftergpu: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop_main: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_fresh: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_labels: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is eithe
Reproduce
git fetch origin cb2159e48f24dbedf5dd3fc291932b1b5e33eb0c 7fcab08f71e463b1768730a626a0f5472d336708
git checkout 7fcab08f71e463b1768730a626a0f5472d336708 && git merge --no-ff cb2159e48f24dbedf5dd3fc291932b1b5e33eb0c
git submodule update --init --recursive
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON
cmake --build build && cmake --build build --target libs
VK_DRIVER_FILES=/nonexistent/icd.json ctest --test-dir build -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 32 --output-on-failure
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation \
  ctest --test-dir build -R '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 4 --output-on-failure
# failures are then re-run alone: --timeout 360 --parallel 2; new validation IDs: the test alone, twice, --parallel 1

Automated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs.

@oneandonlydean
oneandonlydean merged commit cbc4cc7 into boykopovar:main Nov 2, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance change shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants