WIP: perf(vk): a compute program's c0 layout kept by each thread between the words it reads (RX_VK_CS_LAYOUT_FAST) #2004
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2004
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/vk-cs-layout-fast"
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
Every word of c0 a compute stage reads from the driver bank (the pick's answers, each texture and image handle, three or four words of each storage buffer at the descriptors) asks
cs_layout(gpu/vk/vk_bank.cpp) for the program's layout, and each ask took a mutex and a map lookup for five numbers that never change for a serial. Now each thread keeps the last layout it was given, good while the map has not been emptied: the map's entries are written once and never changed, andcs_layout_epochmoves, under the mutex, when the map is emptied at 4,096 programs, so what a thread kept is what the map would give. The layouts are the same; the result expected is fewer lock takes in compute work.Switch:
RX_VK_CS_LAYOUT_FAST(kind kill, on by default, the branch's default; its row ingpu/vk/switches.def).RX_VK_CS_LAYOUT_FAST=0: the mutex and the map at every word, main's code. The switch is read once, at its first use (soHOS_ABcannot alternate it); unset is on, while empty, whitespace-only,0,false,offandnoturn it off, ignoring case and surrounding spaces. Other values turn it on; its first read logs what it chose.From bd3ea307 on
exp/x86-asm-sync.Read first:
cs_layout_fast,cs_layout_epochandcs_layoutingpu/vk/vk_bank.cpp.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head
46b04b8bon main @75317e1f. Every check ran throughtests/run_all.py --strict --exactin one build job of the PC's job queue, in a hidden console (logt-dc3-vk-cs-layout-fast):tests/test_cs_layout_fast.py(new:CsLayout,cs_layout_of,cs_layout_fast,cs_layout_epochandcs_layoutcut fromvk_bank.cppover a fake guest memory, both ways: programs taken in turn keep their own layouts, a layout is read once, and a layout one thread kept is read again after another thread emptied the map; and the switch's reader, cut from the source, runs in fresh processes: unset,0,off,false,1,onand another value, twice each, and set to the other answer after its first read): pass.--changed 75317e1f; its own are among them): 30 passed, 8 failed. Failed:tests/test_frame_overlap.py,tests/test_dev_packs.py,tests/test_linux_pressure.py,tests/test_present_queue_default.py,tests/test_dev_ab.py,tests/test_crash_dialog.py,tests/test_target_reuse_memory.py,tests/test_vk_occlusion_queries.py: each fails on main @75317e1ftoo (the same checks on main in the same queue batch, logt-dc3-base).It has NOT run in the game on this PC yet.
Still to test:
75317e1f, this PR withRX_VK_CS_LAYOUT_FAST=0and this PR with it unset (on), each warmed a minute in the scene first, then interleaved (main, =0, unset, unset, =0, main), two runs each, expecting the same picture all three ways, =0 as main, and unset the replay's time a frame the same as or lower than =0's and main's (by hand)RX_VK_CS_LAYOUT_FAST=0and unset, warmed, in ABBA order, expecting the same picture every way and no crash or validation error (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri Forest a minute, main against this PR with RX_VK_CS_LAYOUT_FAST=0 and unset, ABBA, the same picture)tests/test_cs_layout_fast.pywith--cxx g++and--cxx clang++, expecting PASS (by hand)Measurements
Verdict: not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game A/B is in Still to test.
RX_VK_CS_LAYOUT_FASTships on (kind kill), the branch's default; it is not measured on main.Risks
A layout kept past the map's emptying would read a compute program's bank words at the wrong places (wrong compute results); the epoch is moved under the mutex and checked by the test across threads. A translator stamp change drops translated programs, the preparation record and matching main and play Vulkan pipeline cache files; preparation can translate programs and rebuild driver stage libraries.
nx2/translator_files.pypreserves the previous stamp for this exact cache-only change, so a prepared base install keeps those caches when the other stamped sources are unchanged. Turned off byRX_VK_CS_LAYOUT_FAST=0. The renderer is compiled again after updating.Checklist
Ready for review
main; the title is a Conventional Commit. The branch is behind main and needs retesting after updating.mise run testpasses (any failure that also fails onmainis named). Not yet: only the checks in Testing ran.main, or Measurements says why there is nothing to measure. Not yet (Measurements).third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def. Its row is in; not measured yet.tests/that reads changed source is updated, not weakened. Updated; the revised check has not run yet.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.Ready to merge
main, measured again or a comment says why the numbers still hold.Recommendation: Request changes. This can remove a mutex and map lookup from repeated compute-bank reads, but the fixture passes even if every read still takes the mutex. The update also discards compatible pipeline caches and can force a full shader preparation before players see any proven frame-time gain.
The renderer keeps the last compute c0 layout on each thread. The epoch and by-value copy are sound on inspection. The change adds a small amount of thread-local state and extra work on a miss, and changes the shader cache stamp for everyone who updates.
@fixwill do (8)tests/test_cs_layout_fast.py:85: The read counter stays flat on both a thread-local hit and a locked map hit. (more on the line)gpu/vk/vk_bank.cpp:94: Keep the existing stamp for this cache-policy-only change through a narrowly scoped compatibility change, or land it with a translator change that already requires the same rebuild.gpu/vk/vk_bank.cpp:103: Count lookups and lock entries in a profiling build. If frequent misses erase the saving, pass one resolved layout through the repeated bank reads for a dispatch or program query.tests/test_cs_layout_fast.py:23: The runner resolves{cxx}from CXX or PATH before the fixture can use its LONGSHOT_MINGW fallback. (more on the line)gpu/vk/switches.def:313: The row and PR say any other set value enables the switch. (more on the line)tests/test_cs_layout_fast.py:123: The fixture ignores whether_putenv_sorsetenvactually changed the value. (more on the line)75317e1f. It also omits the applicable source-reading-check box and the template's Ready to merge section. Restore those boxes and update the current-main claim when the head is retested.For the author (3) (a run, a measurement or a decision)
gpu/vk/vk_bank.cpp:94: A player updating an already prepared install can lose the existing Vulkan pipeline files as well as the translated programs. (more on the line)gpu/vk/vk_bank.cpp:103: The planned forest walk only calls for replay time and can miss frame-pacing costs or workloads that alternate compute programs. (more on the line)Checks: Still running:
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),Guide screenshots / Guide images and local UI source review (pull_request),PR description / Description follows the template (pull_request_target),Third-party parts / As their notes say (pull_request),Third-party parts / The same as upstream (pull_request).More: does it help, other systems, switches, what was read
Does it help: Unproven. The author reports the Windows fixture passed and the changed checks returned 30 passes and 8 failures also seen on base
75317e1f. There are no in-game runs, lock-count or replay-time measurements, or frame-time comparisons for head46b04b8b.Other systems: The shared CPU path serves compute translation and program picks, descriptor reads, bulk bank fills, and upscale slot selection on Windows and Linux across GPU vendors, including Steam Deck. Seed translation still bypasses this cache, and graphics banks and c2 take their existing paths. The stamp also reaches graphics and compute program records, preparation records, and Vulkan pipeline caches. The diff adds no GPU allocations or direct save, audio, or input changes.
Switches: RX_VK_CS_LAYOUT_FAST is a developer kill switch, on when unset and read and logged once at first non-seed use. Empty, 0, false, off, and no disable it under the shared parser. HOS_AB cannot alternate it. The row is present, but its set-value wording is inaccurate and the on default has no hardware measurements yet.
Reviewed:
46b04b8b74against75317e1f31: Read AGENTS.md, CONTRIBUTING.md, the Forgejo template, developer standards, vk_bank.cpp and test_cs_layout_fast.py in full. Read the full diff and commit, the changed switch row and neighboring rows, and the relevant renderer, host, cache, and test-runner code. Skimmed the switch table outside the changed section and the reported runs. docs/review-checklist.md, the sibling Tooling checkout, and the named run logs were absent. The diff contains no generated files or translations.; nothing run (a code review).Checked and fine:
Testing: Read-only review. No builds, tests, or project scripts were run. The Windows results are author-reported. The supplied CI snapshot has checks pending and lists no head-only failures.
Overlaps and merge order: No overlapping open PR was visible in the supplied evidence. Two commits are already on local main since the merge base. The shared switch table changed at an unrelated row.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -310,6 +310,7 @@ SW(RX_VK_COMPUTE_SERIAL, hw, bool, "on with Intel's Windows driver, off elsewherSW(RX_VK_COMPUTE_WARM_APART, hw, bool, "on with Mesa's Intel driver, off elsewhere", "1 or 0 says on any GPU whether the preparation compiles a compute program's variants as items of their own, after the translation.")SW(RX_VK_COND_ALWAYS, diag, bool, "off: render conditions are evaluated", "=1 (test) makes every conditional-render condition hold, so everything conditional is drawn.")SW(RX_VK_CPU_OVER_GPU, attempt, bool, "off: CPU writes are taken over GPU contents only for textures the GPU merely cleared", "Takes whole pages the CPU wrote after a GPU write over any GPU-written texture, not only cleared ones.")SW(RX_VK_CS_LAYOUT_FAST, kill, bool, "on: a compute program's c0 layout is kept by each thread between the words it reads", "=0 (or false, off, no) looks the layout up under its lock at every word, as before; any other value is on. Read once, at its first use (HOS_AB cannot alternate it).")L7 (minor): The row and PR say any other set value enables the switch. Inherited
RX_VK_CS_LAYOUT_FAST=and a whitespace-only value disable it throughenv_on_or. Describe the shared boolean rule in both places and add a whitespace-only switch case, which also avoids any platform-specific treatment of empty environment values at process startup.@ -78,0 +91,4 @@}();return on;}std::atomic<uint64_t> cs_layout_epoch{1}; // (moved, under cs_layout's mutex, when its map is emptied)L2: A player updating an already prepared install can lose the existing Vulkan pipeline files as well as the translated programs.
pcache_drop_stale()deletes the old files and preparation can rebuild the driver's stage libraries. Measure an in-place update from a prepared base and the following warm start, with the same cache starting state for each run. Record time to play, the preparation and cache-drop log lines, and first-use stutter on a slow compiler such as RADV.@ -80,0 +100,4 @@};static thread_local Last last{0, 0, {}};const bool fast = cs_layout_fast();if (fast && last.epoch == cs_layout_epoch.load(std::memory_order_acquire) && last.serial == s.prog.serial) return last.l;L4 (minor): The planned forest walk only calls for replay time and can miss frame-pacing costs or workloads that alternate compute programs. Compare the merge base, head off, and head on with repeatable inputs and interleaved runs at identical settings. Include a compute-heavy scene and a representative heavy gameplay scene. Report the spread, average FPS, 1% lows, median and p99 frame times, replay CPU time, and how many lookups bypass the lock. Use that evidence to choose the default.
@ -0,0 +20,4 @@sys.path.insert(0, str(Path(__file__).resolve().parent)) # (tests/srccut.py: python -I leaves this folder off the path)from srccut import block, between # noqa: E402RUN_ALL_ARGS = ["--cxx", "{cxx}"]L6 (minor): The runner resolves
{cxx}from CXX or PATH before the fixture can use its LONGSHOT_MINGW fallback. A Windows checkout with bundled llvm-mingw but no C++ driver on PATH gets a missing-tool failure under--stricteven though the fixture can use the installed compiler. Use one discovery path for direct and suite runs that honors--cxx, CXX, and bundled llvm-mingw.@ -0,0 +82,4 @@CHECK(la.ubo == 1 && la.ssbo == 2 && la.tex == 3 && la.img == 4 && la.push == 32);CHECK(lb.ubo == 6 && lb.ssbo == 7 && lb.tex == 8 && lb.img == 9 && lb.push == 48);}const int read_before = reads.load();L1: The read counter stays flat on both a thread-local hit and a locked map hit. For example, adding
false &&to the fast-hit condition leaves the runtime checks and the source-text checks at line 179 passing. Assert lock or map entries through a fixture hook and check repeated same-serial calls with the switch on and off. Use the same hook to check that a miss after eviction repopulates the fast path, and replace the source-text assertions with those behavior checks.@ -0,0 +120,4 @@if (argc < 2) return 2;const bool first = t::@FN@();#ifdef _WIN32_putenv_s("@NAME@", argv[1]);L8 (minor): The fixture ignores whether
_putenv_sorsetenvactually changed the value. If the update fails, a reader that rereads the environment but logs only once could pass the read-once check. Fail on a nonzero return and verify the replacement value is visible throughgetenvbefore calling the reader again.@fix
I didn't fix this pull request: it failed (the Codex runner answered 409: busy). Nothing was pushed.
Automated fix by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@fix
46b04b8b74ce3a172a21Thanks for the review. The branch is rebased on main @
40bfceb0(the PR's own commit unchanged,git range-diff), with a test commit and a fix commit on top; headce3a172a. The checks below ran in build jobsfb-2002-1(onf1f2dc5b, with the mutants and the old heads) andfb-2002-2(on85a59e6f) andfb-2002-3(on40bfceb0, website files only since) of this PC's job queue (Windows 11, llvm-mingw clang 23). Nothing has run in the game yet. These commits answer the review that HumanBean's "@fix" asked for (BeanBot's review of46b04b8b, every point below); the branch's head is nowce3a172a, so a next "@fix" starts from it.5a928283. In the cut,cs_layout'sstd::mutexis swapped for one that counts its locks (the only edit to the cut code), and the source-text checks are gone. The fixture runs with the switch=1,=0and unset and checks: the same program 100 times takes no lock when on and 100 when off; two programs in turn take one a call either way; another thread's first call takes one and its next ones none when on; after another thread emptied the map and the program's control section changed, the next call takes the lock once and reads the new layout, and the 100 after it take none when on. A copy of the head withfalse &&before the kept-layout test now fails (100 lock takes where none were expected, =1); at the test commit the check fails only on the new default (L4).nx2/translator_files.pystampsvk_bank.cppby its tokens, so any change to its code moves the stamp, and every place this lock could be skipped is in that file; a narrower exception would need the stamp to leave part of a stamped file out, which can hide a real translator change. So the change keeps the stamp move, and Risks now says what it costs; whether it lands beside another change that moves the stamp anyway is the maintainer's choice of merge order. The update measurement you describe (an in-place update from a prepared install, then a warm start, time to play, the cache-drop and preparation lines, first-use stutter, RADV included) is in Still to test, pending for the author.pcache_drop_staledeletes the pipeline cache files made for the old stamp (the main file and the play files, with its log line), that the program cache and the prepare record start over, that every program is translated again and the stage libraries are made again, whatever the switch says, and that this change keeps none of those caches.RX_VK_CS_LAYOUT_FASTnow ships off (ce3a172a, kind attempt): unset is main's code. Still to test asks for main against this PR unset (expected the same) andscripts/dev.py ab --env RX_VK_CS_LAYOUT_FAST=1(on against off, one runtime) in Hyrule Field, the Market and the scene a profiled run shows with the most compute work, with the spread, average FPS, 1% lows, median and p99 frame times and the replay's CPU time; pending for the author, and the default is chosen from those runs. Passing one resolved layout through a dispatch's bank reads is the other route if the kept layout misses often; it is a larger change and waits for those numbers.{cxx}: fixed in5a928283, as in #2002: noRUN_ALL_ARGS; the check finds its compiler itself, one way for a direct run and the suite's:--cxx,CXX,LONGSHOT_MINGW's clang++ (toolchain.py, linked static), then PATH. Run throughtests/run_all.py --strict --exactwith no compiler on PATH,CXXunset andLONGSHOT_MINGWset, the check failed at the PR's old head46b04b8b(not found: cxx (--strict)) and passes at the new head.ce3a172a) and the description now give the shared rule: unset, a blank value (spaces only) and0,false,offornoin any case are off, any other value on. The switch's cases (5a928283) addno,OFF," "and" 0 "(off), each in a fresh process, twice. All pass at the head._putenv_s/setenvunchecked: fixed in5a928283. The child fails (exit 3, saying why) when the call returns nonzero orgetenvdoes not give the new value, before the reader is called again.f1f2dc5bnow, the description's Tested names that base, and the source-reading checks box (ticked) and the Ready to merge section are back.@Beanstalk This adds RX_VK_CS_LAYOUT_FAST, off by default: with RX_VK_CS_LAYOUT_FAST=1 each thread keeps the last compute program layout it was given instead of taking a lock for every driver bank read (gpu/vk/vk_bank.cpp). Whatever the switch says, the first start after updating translates the compute programs once again and drops the old pipeline cache (the log's "files made for another translator dropped"). It has not run in the game on these PCs yet. Please play Kokiri Forest for three minutes, the PR against main on the same PC, on the PR's side with RX_VK_CS_LAYOUT_FAST=1 and with it unset, with a picture compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture, no crash and no validation error; set, frame time p99 equal to unset or better; unset, frame times as main's apart from the one retranslation.
@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.