perf(vk): the shader preparation's programs let their memory go once their pipelines are noted (RX_VK_STATE_GIVE_BACK) #3115
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3115
Loading…
Reference in a new issue
No description provided.
Delete branch "decohered/Longshot:perf/state-list-give-back"
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?
Summary
The first time you run it after an update, it caches all the shader code it read to build the pipelines in memory. On a 16GB machine, that's a lot of memory to hold onto for the duration of the session.
vk: malloc:stayed at 4,664-4,683 MB in use for the whole 90 minutes, against 1,133-1,761 MB at a later start; private memory was 4,663 MB against 1,259, and the process went 4-5 GB into swapAnd now, when a start loads its programs at the game's create calls, eligible state-list programs are released after their pipelines are noted, with their records in the program cache's list -- to be found again by those create calls.
RX_VK_STATE_GIVE_BACK=0keeps themThe mesh fallback programs that were already loaded before the title are also freed of their graphics modules now, when all of their pipelines were found in the caches. Previously, only a precompile start did this; otherwise the SPIR-V would remain in the driver's heap for every other start.
RX_VK_KEEP_MODULES=1keeps themprogram_cache_complete_graphics)Start with
program_cache_give_backingpu/vk/vk_progcache.cppandstate_list_prepareingpu/vk/vk_draw.cpp, thentests/test_state_list_give_back.py.Testing
Tested: I've been trying builds with both of these on my Steam Machine and every time I start it, about 7800 shader modules are released before the title. The build before them released 4947.
Steam Machine: Navi 33 8 GB, 16 GB of RAM, SteamOS 3.10.1, kernel 7.3.1-valve3, RADV steamos-26.05.20
tests/test_state_list_give_back.pypasses with clang++ and g++ 16.2.0: eligible state-list programs let go once their pipelines are noted, with their records in the program cache's list, with and without the shader store;RX_VK_STATE_GIVE_BACK=0keeps themtests/test_linux_pressure.py, which reads the changedvk_draw.cpp, passes, and so dotests/test_program_cache_one_variant.py,tests/test_program_cache_cleanup.py,tests/test_loaded_modules_drop.pyandtests/test_spirv_pack.pyThe renderer builds whole at
93a958212on Linux.mise run teston Linux at93a958212: 863 pass, 12 skip (Windows only or a device it needs), 16 fail. The same 16 fail on main @76092c5e5with the same tools (the toolchain from nixpkgs; mise's pinned one isn't set up here), so none come from this change.The 16
byte_compile_tracked, f2i_immediate, global_load_16, native_suatom, test_check_cache, test_checks_container, test_cloth_kernels, test_dav1d_av1, test_game_module_imports, test_lifter, test_lifter_semantics, test_native_zstd, test_run_all_junit, test_thinlto_tls, test_ui_renderer, test_utf8_paths
Still to test:
vk: state list: N of its programs let go againline andvk: malloc:in use dropping after it (@Beanstalk start the game for the first time after clearing its program cache on a Windows PC, and report the vk: state list and vk: malloc lines)Measurements
Verdict: The pass before the title lets 7,808 shader modules go, where the build before these let 4,947 go.
Baseline: both columns are builds of my own branch on the Steam Machine (not on the forge): before @ 39380e715, without these commits, at the start that prepared the shaders; after @ 6cb6989ef, with them, at an ordinary start.
Risks
If a program was let go and is wanted again for a new pipeline, it will be rebuilt from the file on its worker, so the first draw with a new state might take a little bit longer.
RX_VK_STATE_GIVE_BACK=0andRX_VK_KEEP_MODULES=1will preserve everything as before.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Possibly related, for context:
@review
Recommendation: Approve. A partial program-cache write can cause the new give-back path to discard the only usable copy of a shader variant. The claimed memory gain and the cost of reloading shaders also remain unmeasured against the PR’s base. Nothing blocking is left: what remains is for the author before merging (below). Recommend approval; not approved on the forge.
The change releases state-list programs after noting their pipelines and allows more retained programs to release graphics shader modules. That could reduce memory use, at the cost of rereading and recreating shaders when they are needed later.
Should fix (2): not blocking, but worth doing before merging.
gpu/vk/vk_progcache.cpp:1678: A matching record header is treated as proof that a newly saved variant can be reloaded, even though the cache writer does not check its writes or flush. (more on the line) Do: Check and propagate the complete record write and flush result, and only list or give back a newly saved variant after its whole record was written successfully.tests/test_state_list_give_back.py:59: The test claims to cover the switch being off but only searches for the switch and conditional as source text; it never executes that branch. (more on the line) Do: Exercise the actual give-back decision with the switch both unset and set to 0, and assert that the programs and records remain untouched in the latter case.Testing before merging (3) (runs on the systems it can change; it doesn't hold the recommendation)
gpu/vk/vk_draw.cpp:3402: The first-start memory benefit and reload cost have not been measured against the base on equivalent runs. (more on the line) Do: Run interleaved base and head first starts with equivalent caches, settings and scene on the Steam Machine; report RAM, startup time, cache size, frame times and omitted draws, with repeated runs.gpu/vk/vk_draw.cpp:13324: The ordinary-start module release and its first-use cost need a comparable run on the systems affected by the Linux memory policy. (more on the line) Do: Compare base and head ordinary starts and a representative play route on Steam Deck, reporting RAM, startup time, frame times and omitted draws over repeated runs.gpu/vk/vk_draw.cpp:3402: The create-time give-back path has not been exercised on Windows or the other affected Tier 1 GPU configurations. (more on the line) Do: Run a first start and representative play on Windows and the affected NVIDIA and AMD Tier 1 configurations, checking shader reloads, output and omitted draws.@fixcan do L1 and L2.Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven. The PR reports 7,808 modules released versus 4,947, but those counts come from different starts of separate branch builds; they do not measure this head against its base or establish the RAM saved.
Other systems: The give-back path affects create-time loading on Windows and Linux, including native mesh GPUs. The module-drop change affects Linux systems selected by the existing memory policy, including the Steam Deck and mesh fallback. Reloads can affect first-use latency and omitted draws; the supplied runs do not quantify those effects across the affected systems.
Switches: RX_VK_STATE_GIVE_BACK is added, default on for create-time loading, with a switches.def row; its default is not yet supported by comparative measurements. RX_VK_KEEP_MODULES is unchanged and retains the newly eligible graphics modules when enabled.
Reviewed:
93a958212eagainst76092c5e5a: Read the available repository rules, PR template, standards, commits, diff, new test and test harness, and the changed functions with their cache, loader, startup and module-drop paths. The full large renderer files and unrelated code were skimmed around those paths. docs/review-checklist.md and ../Tooling/AGENTS.md were absent.; nothing run (a code review).Checked and fine:
Testing: No code, scripts or tests were run, as requested. The PR reports a Linux renderer build and game-free checks; CI is still running.
Overlaps and merge order: Unknown; the network is closed and no overlapping pull requests were supplied.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -3395,0 +3399,4 @@// (precompile_run, program_find): there they stay. RX_VK_STATE_GIVE_BACK=0: kept, as beforestatic const bool give_back = env_on_or("RX_VK_STATE_GIVE_BACK", 1);if (give_back && programs_load_at_create()) {const ProgramsGivenBack back = program_cache_give_back(keys);L3 (should fix): The first-start memory benefit and reload cost have not been measured against the base on equivalent runs.
When: The new path destroys programs and later loads them again at create calls; its effect on peak and session RAM, startup time, frame times and omitted draws is unknown. The reported comparison uses different starts and commits outside this base-to-head comparison.
Do: Run interleaved base and head first starts with equivalent caches, settings and scene on the Steam Machine; report RAM, startup time, cache size, frame times and omitted draws, with repeated runs.
L5 (should fix): The create-time give-back path has not been exercised on Windows or the other affected Tier 1 GPU configurations.
When: Those systems can now discard and reload state-list programs, but the supplied in-game evidence covers only RADV on the author’s Linux machine.
Do: Run a first start and representative play on Windows and the affected NVIDIA and AMD Tier 1 configurations, checking shader reloads, output and omitted draws.
@ -13298,0 +13321,4 @@// kept for the session at every start: 2,291 mesh fallback programs on a Steam Machine, 269 MB of SPIR-V in the// driver's heap (4,372 modules let go there, their compute stages'; the 4,582 others stayed). One with a note// that did not read or a pipeline in no cache keeps them for that pipeline's first drawif (built && !left) program_cache_note_built(p);L4 (should fix): The ordinary-start module release and its first-use cost need a comparable run on the systems affected by the Linux memory policy.
When: A new graphics pipeline for a released fallback program must reread its shader and recreate modules on a worker; the reported module counts do not show the resulting RAM or frame-time effect on a Steam Deck.
Do: Compare base and head ordinary starts and a representative play route on Steam Deck, reporting RAM, startup time, frame times and omitted draws over repeated runs.
@ -1646,0 +1675,4 @@FILE* const in = own_in ? own_in : disk_in;for (const DiskRecord& d : listed->second) {uint64_t variant = 0;if (program_cache_variant(in, key, d, &variant) && variant == p.variant) return true;L1 (should fix): A matching record header is treated as proof that a newly saved variant can be reloaded, even though the cache writer does not check its writes or flush.
When: If the disk fills after the first 1,024 bytes of a newly translated variant are written, program_cache_variant can read its header and answers and return a match. Give-back then destroys the in-memory program, but a later full record read fails on the truncated payload, leaving that variant unavailable for its draw.
Do: Check and propagate the complete record write and flush result, and only list or give back a newly saved variant after its whole record was written successfully.
@ -0,0 +56,4 @@("const ProgramsGivenBack back = program_cache_give_back(keys);","state_list_prepare keeps the programs it read in for the session: their modules' SPIR-V stays in the heap, and ""no create call loads them"),('static const bool give_back = env_on_or("RX_VK_STATE_GIVE_BACK", 1);', "the give-back has no switch, or is off unset"),L2 (should fix): The test claims to cover the switch being off but only searches for the switch and conditional as source text; it never executes that branch.
When: A change that leaves those strings present but calls give-back unconditionally elsewhere can still pass, even though RX_VK_STATE_GIVE_BACK=0 no longer preserves the programs.
Do: Exercise the actual give-back decision with the switch both unset and set to 0, and assert that the programs and records remain untouched in the latter case.
@fix
93a958212e8eae0b7ac8Recommendation: Approve. The code is ready: only testing is left. The memory gain and first-use costs still need comparable measurements, and the affected Windows and Tier 1 configurations need coverage. The reported module counts compare different starts on branch builds, so they do not establish the gain against the base. Recommend approval; not approved on the forge.
What
@fixchangedI changed this pull request: the new head is
8eae0b7ac8. I rebased it ontomain(309af878f1). I edited its description to match the change.Changed
Nothing was built or run: the checks run again on the new head.
The change releases eligible state-list programs after pipeline notes are made and releases more graphics modules on memory-constrained Linux systems. It may reduce retained memory, at the cost of reloading programs or remaking modules when new pipelines are needed.
Testing before merging (3) (runs on the systems it can change; it doesn't hold the recommendation)
gpu/vk/vk_draw.cpp:3402: The first-start memory benefit and reload cost have not been measured against the base on equivalent runs. When: The new path destroys programs and later loads them again at create calls; its effect on peak and session RAM, startup time, frame times and omitted draws is unknown. The reported comparison uses different starts and commits outside this base-to-head comparison. Do: Run interleaved base and head first starts with equivalent caches, settings and scene on the Steam Machine; report RAM, startup time, cache size, frame times and omitted draws, with repeated runs.gpu/vk/vk_draw.cpp:13324: The ordinary-start module release and its first-use cost need a comparable run on the systems affected by the Linux memory policy. When: A new graphics pipeline for a released fallback program must reread its shader and recreate modules on a worker; the reported module counts do not show the resulting RAM or frame-time effect on a Steam Deck. Do: Compare base and head ordinary starts and a representative play route on Steam Deck, reporting RAM, startup time, frame times and omitted draws over repeated runs.gpu/vk/vk_draw.cpp:3402: The create-time give-back path has not been exercised on Windows or the other affected Tier 1 GPU configurations. When: Those systems can now discard and reload state-list programs, but the supplied in-game evidence covers only RADV on the author’s Linux machine. Do: Run a first start and representative play on Windows and the affected NVIDIA and AMD Tier 1 configurations, checking shader reloads, output and omitted draws.Checks: they run again on this head; a failure shows on the pull request.
Also noticed (1; not asked for, not blocking)
More: does it help, other systems, switches, what was read
Does it help: Unproven: the report gives 7,808 modules released versus 4,947, but compares an ordinary start with a preparation start on two branch builds. Equivalent base-versus-PR memory and first-use measurements are missing.
Other systems: The give-back path can affect Windows and Linux across GPU vendors; module release also affects Linux systems selected by the memory policy. The supplied evidence covers a Steam Machine on RADV, but does not establish the outstanding platform coverage or reload costs.
Switches: RX_VK_STATE_GIVE_BACK is added, on by default, with a table entry; its default's benefit and reload cost remain unmeasured against the base. RX_VK_KEEP_MODULES remains available to retain modules.
Reviewed:
8eae0b7ac8against309af878f1: Read the repository instructions and pull request template, the fix diff, the give-back and cache-write paths, and the state-list test driver. Skimmed the surrounding cache initialization, tests, and base diff; the base diff contains 360 files, including other changes, and was not read in full.; nothing run (a code review).Checked and fine:
Testing: No commands that build, test, or run project code were executed. The description reports Linux tests and a renderer build; CI has not yet run on the fix.
Overlaps and merge order: The supplied base diff includes commits for other pull requests; their merge order was not established.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, after its fix. Nothing was run; a maintainer decides.