WIP: perf(vk): a program load no draw asked for is published when the game frees its programs (RX_VK_LOAD_RETIRE=0 to disable) #2031
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
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!2031
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/vk-load-retire"
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
With programs loaded at create (
RX_VK_LOAD=create), a load job leavesjob_by_key, and the count thatprograms_load_wait's first test reads goes down, only when a draw's pick asks for one of its keys (load_publish).The game makes programs it never draws: their jobs stayed for the session, holding what they read, and with one such job
the count never came back to 0, so every slow pick took
jobs_muand looked its key up for nothing.With this, the game's free call of the last program of a unit notes the unit's job under that call's
serial, only if a job no pick has published exists (
programs_freed; no lock a pick holds while it translates), andthe replay's part of that same call (
do_program_released, in the stream's order, through the synchronous, queued andheld paths alike: every draw recorded before the free has been replayed, none after it) takes that note only and
publishes the job if it is done, under
shader_mu, exactly as a pick of one of its keys would have(
programs_retire_freed). A job not done then is kept once, with no cap, and published at the replay's next presentor release once its worker is done (
load_donetells the replay;programs_retire_ready, 64 at most a pass).load_publishno longer takes the job's own lock (every caller holdsshader_muand has seen the job done), so aworker set aside after it finished holds no pick or release up. With the switch off nothing is indexed by unit, looked
up or noted.
programs_life_flagis an atomic stored once. WithRX_VK_LOG_PROGRAM_LIFE=1the census counts theslow picks that looked a key up (and found none) and the retire passes and their time.
gpu/vk/vk_shader.cppisunchanged, so the translator's stamp is too. The result expected: the slow picks no longer take the jobs' lock once
the unused loads are gone; not measured yet.
The switch:
RX_VK_LOAD_RETIRE(new, a kill switch, bool, its row ingpu/vk/switches.def), on by default asdocs/dev/standards.md (Switches) asks of a switch's first push; read once at the first load queued at create or free
call;
=0notes nothing and such loads wait for a pick, as on main.From bd3ea307 on
exp/x86-asm-sync(there the call was made fromprogram_releasedinvk_shader.cpp; here fromdo_program_released).Read first:
load_queue,programs_freed,programs_retire_freedandprograms_retire_readyingpu/vk/vk_programs.cpp,do_program_releasedandlog_frame_statsingpu/vk/vk_replay.cpp, thentests/test_program_load_wait.py.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head
5149692aon main9dd41326. The checks ran atbfac5c28on mainb62932a9, the same commits before a rebase onto9dd41326, which added only lift files (#1905) that none of these checks reads. Light queue jobsfb-2029-3andfb-2029-4of this PC's job queue (the first found compile errors in the new fixtures, fixed before the second), each check in a hidden console throughtests/run_all.py --strict --exact --timeout 1200 --jobs 2:tests/test_program_load_wait.py(the fixture with the switch set, unset and 0, the switch's read included),tests/test_program_life_clock.pyandtests/test_switch_table.py: 3 passed. At the test commitbd1607fdtests/test_program_load_wait.pyfails, as expected (it does not build against the earlier code: noload_queue, no serial on the release). The Windows game build's own compile ofgpu/vk/vk_programs.cppandgpu/vk/vk_replay.cpp(syntax only) passes. Not run this round: every check--changedpicks (a heavy batch), the Linux-target compile (the local tool could not read its build profile),tests/test_vbytes.py(hangs on this PC).It has NOT run in the game on this PC yet.
Still to test:
RX_VK_LOAD=create,RX_VK_LOG_PROGRAM_LIFE=1: cold and warm scenetransitions (Kokiri Forest into Hyrule Field and back, Hyrule Field into Castle Town Market), on main @
9dd41326, this branch withRX_VK_LOAD_RETIRE=0and this branch unset (on), each warmed a minute, interleaved (ABC ABC ABC), expectingthe same picture and no FATAL, loads published at free calls counted unset, fewer slow-pick lookups unset than with
=0in the census line, no longer transition hitches, and average fps, 1% lows, median and p99 frame times and RAM and
VRAM of this branch unset at least those of the other two; a difference beyond the runs' spread is a gain or a loss,
one within it is no difference measured (by hand)
RX_VK_LOAD_RETIRE=0and unset, warmed and interleaved, expecting the same picture and no FATAL (the test farm: linux+nvidia,linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest into Hyrule Field and back and into Castle Town
Market, RX_VK_LOAD=create, main @
9dd41326, this branch with RX_VK_LOAD_RETIRE=0 and unset, warmed up, interleaved,the log, frame times and a screenshot)
python3 -I -B tests/test_program_load_wait.py --cxx g++, expecting PASS (by hand)mise run testandmise run lintat this head, expecting every failure tofail on main too and lint to pass (by hand; neither tool is installed on this PC)
Measurements
Verdict: not measured in play yet: no micro-benchmark of this port and no run in the game on this PC yet.
RX_VK_LOAD_RETIREships on, as docs/dev/standards.md (Switches) asks of a switch's first push (
=0is main's way); the in-game A/B(unset against
=0and main) in Still to test decides whether it stays.Risks
What could break (on, the default): a job published while a draw still needs it would make that draw's pick load the program
again (a hitch), or, published before it is done, a missing program (a FATAL or a wrong draw); the job is published only
when done, at its own free call's release or later, under
shader_mu, as a pick would. The test runs the source'sload_queue,programs_freed,programs_retire_freed,programs_retire_ready,load_doneandload_publishonstand-ins, set, unset and
0: two programs of one unit, two units freed before either release, final frees with no job,a burst of 65 jobs not done, and a job whose lock its worker still holds. With
=0, what changes from main:load_publishtakes no job lock,programs_life_flagis atomic, the census counts with the life log on, and a loadflag read at each replay present and release. Turn it off with
RX_VK_LOAD_RETIRE=0. Renderer code: theruntime is rebuilt; the translator's stamp is unchanged, so players' programs are not translated again.
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.Possibly related, for context:
Recommendation: Request changes. A burst can permanently drop retirement candidates, and a worker that finishes after the last free call can leave the slow-pick lock active for the rest of the session. The new path can also stall replay behind a low-priority worker, and the PR has no in-game evidence of a gain.
The change moves completed load-on-create results into the shader cache when the game frees a program, aiming to spare later slow picks a jobs-lock lookup. It adds a per-unit index and moves cache insertion onto the release path. Publishing keeps the loaded programs and their Vulkan resources available for reuse.
@fixwill do (11)gpu/vk/vk_programs.cpp:706: The 64-entry retry cap silently drops every remaining unfinished candidate. (more on the line)gpu/vk/vk_programs.cpp:718: An unfinished candidate is retried only on another program release. (more on the line)gpu/vk/vk_programs.cpp:710: Seeing state 2 does not guarantee job->mu is free. (more on the line)gpu/vk/vk_replay.cpp:6633: The release callback drains a global list of guest-side notices, so the callback's stream position does not establish the position of the jobs it publishes. (more on the line)gpu/vk/vk_programs.cpp:665: The final free of a tracked unit queues a notice even if it never had a load job or a pick already published it. (more on the line)gpu/vk/vk_programs.cpp:548: The kill switch leaves the new unit index active. (more on the line)gpu/vk/vk_programs.cpp:712: The new replay read of programs_life_flag races with programs_life_on, which assigns the plain bool on each guest create or free. (more on the line)tests/program_load_wait_test.cpp:189: The fixture writes the retirement notice and fills job_by_unit itself, so it passes if the production queue stops indexing jobs or if programs_freed stops pushing notices. (more on the line)tests/test_program_load_wait.py:32: Compiler selection differs between standalone and suite runs. (more on the line)tests/test_program_load_wait.py:61: ATTEMPT_OFF and the optional fixture stubs are unused by the only caller. (more on the line)For the author (1) (a run, a measurement or a decision)
Checks: Still running:
PR description / Description follows the template (pull_request_target),CI / Conventional Commit title (pull_request),CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request),CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request),CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request),CI / Workflow lint (actionlint) (pull_request),CI / Website build and checks (pull_request).More: does it help, other systems, switches, what was read
Does it help: Unproven. There are zero in-game runs or timing comparisons for
cd54e46aagainst75317e1f. The reported Windows batch had 128 passes in 156 checks, with 27 failures also seen on base and one timeout that passed on retry. Those results exercise code paths but give no frame-time, lock-avoidance, or memory numbers.Other systems: The retirement path affects synchronous replay, threaded replay, and releases deferred behind queue events. It covers graphics, native mesh, mesh fallback, and compute loads on all vendors in create mode. Start mode gets the new pending-flag check on replay releases. The change can shift CPU work into scene transitions and change host heap use and replay pacing. Windows and Linux builds and Tier 1 GPU behavior remain unverified in game. The host API, translator stamp, cache file formats, saves, audio, and input have no direct changes.
Switches:
RX_VK_LOAD_RETIREis a developer kill switch, on when unset and read once at the first tracked free in create mode.0,off,false,no, and an empty value disable retirement. The sorted switch-table row is present. The on default still needs Tier 1 A/B evidence, and the off path currently retains new bookkeeping. No other runtime switch changes.Reviewed:
cd54e46a73against75317e1f31: Read AGENTS.md, CONTRIBUTING.md, the Forgejo template, developer standards, vk_programs.cpp, vk_programs.h, and both changed test files in full. Read every diff hunk and the surrounding switch-table and replay sections. Traced release callers, queue deferral, shader picks, cache ownership, unit state, switch parsing, and test-runner compiler selection. Read the commit and supplied test report, comments, reviews, and CI status. Unrelated replay and switch-table sections and source checks were skimmed. No local review checklist or neighboring Tooling checkout was present.; nothing run (a code review).Checked and fine:
Testing: Read-only review. No project code, builds, or tests were run. Git diff whitespace check passed. The PR reports a Windows LLVM-MinGW pass for the focused test. Linux GCC and Clang results, in-game runs, and CI results are pending.
Overlaps and merge order: No competing PR found in the available checkout. Local main touches the replay file and switch table in separate hunks. The cited source commit bd3ea307 was unavailable locally.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -540,6 +545,7 @@ void load_at_create(const nvn_prog& prog, const CreatedProgram& made) {census.units_queued++;std::lock_guard lk(jobs_mu);for (const uint64_t key : job->keys) job_by_key[key] = job;job_by_unit[unit] = job;L6 (minor): The kill switch leaves the new unit index active. With RX_VK_LOAD_RETIRE=0, every queued job still inserts into job_by_unit, every publishing pick checks it, and every tracked free still looks up the unit before removing its serial. This leaves extra work in the rollback and A/B control. Gate retirement-only bookkeeping on the latched switch and align the table and test with when it is read.
@ -627,0 +662,4 @@// (longshot) the last program of those stages: its unit's job, should no pick have asked for it, is retired at the replay's// part of this call (programs_retire_freed)if (unit && load_retire_on())if (const program_life::Unit* u = table.find(unit); u && !u->live) {L5 (minor): The final free of a tracked unit queues a notice even if it never had a load job or a pick already published it. In synchronous play, a series of such frees can allocate and free a vector per release and take shader_mu and jobs_mu for each miss. Filter using pending-job state before queuing retirement and reuse the existing release's shader lock where possible.
@ -642,0 +703,4 @@}if (!job) continue; // (a pick published it, or nothing was queued for those stages)if (job->state.load(std::memory_order_acquire) != 2) { // (queued or running: nothing to publish yet)if (later.size() < kRetireLater) later.push_back(unit);L1: The 64-entry retry cap silently drops every remaining unfinished candidate. If replay falls behind a burst of 65 last-program frees while the workers are busy, the 65th job can finish later and stay indexed for the rest of play, keeping slow picks on the jobs-lock path. Bound the work per pass while preserving the remaining distinct candidates, or hand pending jobs off through completion. Cover a burst above 64.
@ -642,0 +707,4 @@continue;}// (a program of the same stages made since the free is no reason to wait: publishing is what its first pick would do)load_publish(job);L3: Seeing state 2 does not guarantee job->mu is free. load_done stores 2 while holding the mutex, and load_publish then blocks on it. A below-normal-priority worker preempted between the store and unlock can stall a free for a shader nobody needs while replay holds both replay_mu and shader_mu. Make completed-job publication nonblocking. The release/acquire state already publishes the finished results, and publication itself is serialized by shader_mu.
@ -642,0 +709,4 @@// (a program of the same stages made since the free is no reason to wait: publishing is what its first pick would do)load_publish(job);census.units_retired++;if (programs_life_flag)L7: The new replay read of programs_life_flag races with programs_life_on, which assigns the plain bool on each guest create or free. In threaded replay, release A can reach this read while the game handles free B. Assigning the same value still races in C++. Publish the flag once before threads use it, or make all accesses atomic.
@ -642,0 +715,4 @@}if (later.empty()) return;std::lock_guard lk(retire_mu);retire_units.insert(retire_units.end(), later.begin(), later.end());L2: An unfinished candidate is retried only on another program release. If a transition frees the last unused program and its worker finishes just afterward, unrelated slow picks can keep taking jobs_mu throughout the next area even though the load has completed. Have completion mark eligible work ready for a bounded replay drain that runs without another free, keeping publication off the worker.
@ -6630,1 +6630,4 @@program_released(p);// (longshot, RX_VK_LOAD_RETIRE) and the load its create call queued, if no draw ever asked for it (vk_programs.cpp): what it// read goes to the programs' lists here, in the stream's order as this call is, under the lock a pick would publish it underprograms_retire_freed();L4: The release callback drains a global list of guest-side notices, so the callback's stream position does not establish the position of the jobs it publishes. With release A, draw B, release B queued in that order, the game can add B's notice before replay handles A. A can then publish B and count it as unused before the earlier draw gets its pick. Carry the retirement candidate with its matching release through the synchronous, queued, and held-release paths. Make a candidate eligible for retry only after that release reaches replay.
@ -183,0 +186,4 @@// a job no draw ever asked for, retired when the game frees the last program of its unit (programs_retire_freed, which the// replay's part of the free call runs; it takes shader_mu itself). Done: published as a pick would publish it. d, which read// nothing, only leaves the lists, and the count a pick's first test reads comes back to 0const auto freed = [](uint64_t unit) { // (programs_freed's part: the unit left with no program is noted)L8: The fixture writes the retirement notice and fills job_by_unit itself, so it passes if the production queue stops indexing jobs or if programs_freed stops pushing notices. The Python checks still find their expected strings in both cases. Exercise the real queue bookkeeping and free-to-replay handoff with two serials sharing a unit, asserting the first free does nothing and the final free publishes only at its replay position. Run that behavior with the switch both on and off.
@ -20,0 +29,4 @@"""The C++ compiler with no --cxx: CXX, then the configured llvm-mingw (LONGSHOT_MINGW), then clang++, g++ or c++"""if os.environ.get("CXX"):return os.environ["CXX"]mingw = os.environ.get("LONGSHOT_MINGW")L9 (minor): Compiler selection differs between standalone and suite runs. On Windows with LLVM-MinGW configured through LONGSHOT_MINGW alone, run_all can skip the check or pick a different clang++ before default_cxx runs. On Linux with LLVM-MinGW configured for cross-builds, the standalone test can select it and try to launch a Windows executable. Share native compiler selection between both entry points, honor --cxx and CXX, and limit the LLVM-MinGW fallback to Windows.
@ -20,0 +58,4 @@}"""KILL_ON = ((None, 1), ("0", 0), ("1", 1), ("2", 1), ("off", 0)) # (unset: on; 0 or off: off; any other value: on)ATTEMPT_OFF = ((None, 0), ("0", 0), ("1", 1), ("2", 1), ("off", 0)) # (unset: off)L10 (minor): ATTEMPT_OFF and the optional fixture stubs are unused by the only caller. Remove them and use the existing block import for the switch reader too.
cd54e46a735149692a16Thank you for the review (#2031 (comment)). The branch is rebased on main @
9dd41326(the PR's own commit unchanged by the rebase, now7b8a597c), with a test commitbd1607fdand a fix commit5149692a; head5149692a. Point by point:retire_wanted), and published 64 at most a pass, the rest at the next (5149692a). The fixture frees 65 units whose jobs no worker has done, replays their releases (all 65 kept), finishes the workers, and requires 64 published at the first present and the 65th at the second (bd1607fd).load_donenow tells the replay when a job it waits for is done (retire_ready; fences pair the flag with the done state), andprograms_retire_readypublishes it at the replay's next present or release, never on the worker (5149692a); the burst above is published with no further free call.load_publishno longer takes the job's lock: every caller holdsshader_muand has seen the job done with acquire, andpublishedisshader_mu's (5149692a). The fixture holds the job's lock after storing done, as a worker set aside there would, and requires the release to finish and publish within 5 s (bd1607fd).do_program_releasedpasses the serial of the release it replays, so a release takes only its own call's note, through the synchronous, queued and held paths alike (they all carry the program) (5149692a). The fixture frees two units before the replay reaches either release: the first release publishes its own job only, a pick between them publishes the second, and the second release then finds it published (bd1607fd).jobs_muat that free only), so a release without a note takes no lock and allocates nothing (5149692a); the fixture checks the final frees of a unit a pick published and of one never queued note nothing (bd1607fd). The release's ownshader_muis taken inprogram_released(vk_shader.cpp); sharing it would move that lock out of the translator's file, so the retirement takes its own, only for a release that carries a note.load_queue), erased from that index (load_publish), looked up at a free or noted (programs_freed); the row says it is read once at the first load queued at create or free call, whichever comes first (5149692a). The fixture runs with it set, unset and0, and off requires the unit index to stay empty and nothing noted (bd1607fd).programs_life_flagis now astd::atomic<bool>stored once, inprograms_life_on's initializer (5149692a).load_queue(the tail ofload_at_create, now a function of its own), freed by the source'sprograms_freedand released byprograms_retire_freed(serial); two serials of one unit: the first free notes nothing and its release publishes nothing, the second is noted at the game's call and published only at its own release; set, unset and0(bd1607fd).--cxxthe check finds the compiler asrun_all.pydoes (CXX, then clang++, g++, c++ on PATH, throughtests/compiler_command.py); the LONGSHOT_MINGW fallback is gone (bd1607fd).blockimport;ATTEMPT_OFFis gone too: the switch reads as a kill switch (KILL_ON, unset on) (bd1607fd).RX_VK_LOG_PROGRAM_LIFE=1the census line now counts the slow picks that tookjobs_muand those that found no job, and the retire passes and their time (5149692a). The in-game comparison you describe (main,=0, unset; cold and warm transitions; average fps, 1% lows, median and p99 frame times, the lookup counts, RAM and VRAM; Tier 1 Windows, Linux, Deck) is pending: it has not run in the game on this PC, and it is in Still to test.RX_VK_LOAD_RETIREstays on by default (a kill switch,=0is main's way), as docs/dev/standards.md (Switches) asks of a switch's first push; it is not measured in play yet, and the in-game A/B (unset against=0and main) in Still to test decides whether it stays.At the test commit the check does not build against the PR's earlier code (no
load_queue, and the release passes no serial: the interface L4 and L8 ask for), so it fails there; job fb-2029-4.Tested here (Windows 11, i7-9700, RTX 4060, llvm-mingw clang 23; queue jobs fb-2029-3, fb-2029-4, light):
tests/test_program_load_wait.py(the fixture with the switch set, unset and 0, the switch's read included),tests/test_program_life_clock.pyandtests/test_switch_table.py: 3 passed. At the test commitbd1607fdtests/test_program_load_wait.pyfails, as expected (it does not build against the earlier code: noload_queue, no serial on the release). The Windows game build's own compile ofgpu/vk/vk_programs.cppandgpu/vk/vk_replay.cpp(syntax only) passes. (Run atbfac5c28onb62932a9; the push is the same commits rebased onto9dd41326, which added only lift files.) Not run this round: every check--changedpicks (a heavy batch), the Linux-target compile (the local tool could not read its build profile),tests/test_vbytes.py(hangs on this PC). The change has NOT run in the game on this PC yet. The description is updated.@Beanstalk This retires programs loaded at create (gpu/vk/vk_programs.cpp), when programs load at create (RX_VK_LOAD=create), on by default; RX_VK_LOAD_RETIRE=0 is main's way. This head is on main
9dd41326: please compare with main at that merge base. Please play with RX_VK_LOAD=create and RX_VK_LOG_PROGRAM_LIFE=1 on both sides: Kokiri Forest into Hyrule Field and back, then Castle Town Market started from the market-entrance save, the PR against main on the same PC, on the PR's side with RX_VK_LOAD_RETIRE unset and with RX_VK_LOAD_RETIRE=0, with a picture in each scene compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture (no missing program), no crash or FATAL line; unset, the census line counts loads published at free calls and fewer slow-pick lookups than with it off; no longer hitches at the transitions than main. Please report the census line.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.