WIP: perf(vk): FNV-1a 64 in hand-written x86-64 (lsasm_fnv1a64, lsasm_fnv1a64_words) #2133
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!2133
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/asm-fnv1a64"
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
FNV-1a 64 is written out as a byte loop in many places of the renderer and the host: the program keys and the
translator's stamp (
gpu/vk/vk_shader.cpp), the program cache's stamp (vk_progcache.cpp), the prediction's hashes(
vk_predict.cpp), the state lists' and the seed's hashes (vk_draw.cpp), the guest texture hash (vk_texture.cpp),the drawn write-back hash (
drawn_writeback.h) and the texture copies' hash (host/nvn_native.cpin_hash).This adds
gpu/asm/fnv.Son the asm kit:lsasm_fnv1a64(h = (h ^ byte) * 0x100000001b3, eight bytes unrolled astep) and
lsasm_fnv1a64_words(the same over 8-byte little-endian words, then the bytes left:drawn_writeback.h'shash), one body for every CPU level (each step waits on the multiply before it: no vector form), and calls them in
place of each loop, each with the seed it began with, so every value is the same as before, the ones kept in files on
disk included (the state lists, the program cache's stamp, the seed's stamp).
guest_hashanddrawn_writeback.h'shash begin at 1469598103934665603, not FNV's basis, and keep doing so. The result expected: the same hashes, no slower; the micro-benchmark (Measurements) finds the same speed, and the word hash slower on 13-byte inputs.
No switch: the values are the C's, bit for bit, so there is nothing to compare but speed.
From 971f8074 on
exp/x86-asm-sync(whoseguest_hashhad moved to FNV's basis: not taken). Depends on #1947 (the asmkit, which now carries the GPU-visible ranges' sort,
lsasm_sort_ranges): its commits show in this diff until #1947merges.
Read first:
gpu/asm/fnv.S, then the call sites listed inSITESoftests/test_asm_fnv.py.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23 (
CC/CXX,RX_CPMset); head7c2f3707on #1947's head
cffa2f46(the asm kit, on main9dd41326). Through the PC's job queue, each run in a hidden console,tests/run_all.py --strict --exact --jobs 2 --timeout 1200: the change's own checks (light jobpr-tb-AsmCut5-light,log
t-AsmCut5-asm-fnv):test_asm_fnv.py,test_capture_past_end.py,test_drawn_writeback.py,test_pipeline_state_computes.py,test_pipelines_outlive_cache_format.py,test_predict_types.py,test_prepare_seed_stamp.py,test_prepared_stamps.py,test_scene_tonemap_pass.pypass (9 of 9).--changed cffa2f46(build jobpr-tb-AsmCut5-build, logt-AsmCut5-chg-asm-fnv): 273 checks, 250 pass, 23 fail, each of whichfails on #1947's head @
cffa2f46too (a worktree atcffa2f46in the queue's jobs, logst-AsmCut5-base-kit-changedandt-AsmCut5-base-kit-moreandt-AsmCut5-base-kit-own):test_dev_packs.py,test_device_lost_exit.py,test_draw_binds.py,test_draw_stream_visibility.py,test_frame_overlap.py,test_fsr_uniform_ownership.py,test_host_guest_writes.py,test_linux_pressure.py,test_low_vram_texture_mips.py,test_mesh_conv.py,test_pass_barrier.py,test_pipeline_play_cache.py,test_prepare_cache_save.py,test_readback_timeout_source.py,test_recorder_own_idle.py,test_recorder_sync.py,test_sampler_admission.py,test_staging_burst.py,test_target_reuse_memory.py,test_texel_buffer_ownership.py,test_ub_update_batch.py,test_upscaler_rows.py,test_vk_occlusion_queries.py.It has run in the game on this PC: the in-play A/B in Measurements (held job
fb-g2133, the Market, 18 runs withthe warm-ups), with no FATAL, device lost or Vulkan error in any run.
Still to test:
each started once and left to prepare (this PR makes the shader caches again once: the translator's sources
changed), then Kokiri Forest, then Hyrule Field, three minutes each turning the camera, uncapped
(
HOS_FPS_MAX=240), each warmed a minute, then at least three interleaved runs of each (AB AB AB), expecting thecaches kept at the second start, the same picture, no FATAL, and fps and frame times of this PR at least those of
#1947's head; a difference beyond the runs' spread is a gain or a loss, one within it is no difference measured
(by hand)
#1947's head and on this PR, warmed and interleaved, expecting the second start to keep the shader caches the first
made, the same picture, and frame times within the runs' spread or better on this PR (@Beanstalk linux+nvidia,
linux+amd, windows+amd, windows+intel, steamdeck: start the game twice, then Kokiri Forest three minutes, #1947's
head against this PR, warmed and interleaved, the log, frame times and a screenshot)
python3 -I -B tests/test_asm_fnv.py --cxx g++and--cxx clang++, expecting PASS (by hand)Measurements
Verdict: no gain:
lsasm_fnv1a64is at the C byte loop's speed (0.97-1.01×) at every size from 8 bytes to32 KB, and
lsasm_fnv1a64_wordsat the C word loop's (1.00-1.01×) from 4 KB to 4 MB; on 13-byte inputs (the bytetail alone) the word kernel is slower, 0.89× (SSE2) and 0.94× (AVX2). Each step waits on the multiply before it, in C
as in the kernel. What this PR is then worth: the same hashes at the same speed, slower on very short word hashes;
no speed gain. A micro-benchmark on authored input.
In play (the Market, this head against its parent, two builds, In play below): no gain or loss shown in game time
(calm game_ms -0.3 %, p = 0.78) and no figure that times the hashing; the render thread's figures lean for the head by
7 to 15 %, which the hashes cannot make at the micro-benchmark's parity, and are not claimed.
Baseline: the C this PR replaces, as #1947's head
cffa2f46has it (the asm kit on main9dd41326, which this PR depends on), not main itself.The micro-benchmark: FNV-1a 64 as
cffa2f46computes it (hash_bytescut fromgpu/vk/vk_shader.cpp, anddrawn_writeback.h's word hash) againstlsasm_fnv1a64called as this PR'scode_keycalls it anddrawn_writeback.h's hash cut from this PR (lsasm_fnv1a64_words), over 8 MB of authored bytes. A script on this PC, not in this PR (asm_fnv_bench.py, PrTrials'trials/tools/bench), run alone in the PC's job queue on 2026-11-22: 3 rounds of 201 repetitions per case and arm, i7-9700, llvm-mingw clang 23.1.5-O2 -ffp-contract=off, arms SSE2 (-march=x86-64) and AVX2 (-march=x86-64-v3, what the release builds the back end for). The arms take turns at going first; their outputs were compared at every repetition and were equal. Each cell: the median of the three rounds' medians, [lowest–highest round median], in ns per byte. Change: before ÷ after (above 1: this PR faster).cffa2f467c2f3707code_key: a 216-byte control section and a 4 KB code area (16), SSE2code_key: a 216-byte control section and a 4 KB code area (16), AVX2code_key: 216 B and a 32 KB code area (2), SSE2code_key: 216 B and a 32 KB code area (2), AVX2graphics_key's written types (8,192), SSE2graphics_key's written types (8,192), AVX2cffa2f467c2f3707In play (held job
fb-g2133, 12:03 to 12:49 UTC on 2026-11-23): the Market, at 60 s a run, chosen for more draws,render-target sets and copies a frame than Kokiri Forest. The two builds are this head
7c2f3707(rt-fnv-7c2f3707) andits parent, the asm kit
cffa2f46(rt-kit-cffa2f46): a warm-up each, then kit and head interleaved (kit head head kit,four times), every run loading at create (
RX_VK_LOAD=create), the output pinned at 2560x1440 on a desktop at 800x600at 30 Hz (the PC's only screen then). No run was left out; no load marker or strike file appeared, and no run's log has a
commit-limit line. Reach: main's program picks line (every 200,000 picks) counts the picks that reach
program_pick,whose
graphics_keyhashes withlsasm_fnv1a64in the head: about 1,600-1,700 over a run, nearly all in the load; insteady play 0 to 7 in a run's last 800,000 picks (about 2,000 frames at about 370 picks a frame), so the program keys
are hashed well under once a frame there. The other hashes have no count in these logs: the state lists' and the seed's
hashes, the guest texture hash, the drawn write-back hash and the texture copies'
pin_hash(main's ASTC reuse lineshows 0 MB of blocks hashed in every run).
cffa2f46(C loops), 8 runs7c2f3707, 8 runsa frame's replay time (ms): submit (render thread)What is the kernel's stage and what is not: no log line times the hashing or counts most of its calls, so no figure
here is the kernels' own time. The program keys are hashed in
program_pickon the render thread, under once a frame insteady play; the other hashes run where their callers run (texture lookups, the state lists, write-backs, texture copies)
and are not timed apart. The render thread's lean cannot be the hashes' own time: the replay's submit is 1.0 ms a frame
less with the head, and at the micro-benchmark's 0.97-1.01× a 3 % saving on hashing would need about 33 ms of hashing a
frame, more than a whole frame (about 14 ms), or about 37 MB hashed a frame at 0.9 ns a byte. This is this head against
its parent: two builds, not one build's switch (this PR has none). The play profile the back end is built with was
recorded before this change, so functions this change edits can build without their profile in the head's build (a
compile with clang's profile warnings on showed that for #1994's file; not checked for this PR's). The lean can come
from that as well as from chance (#2099's head leaned the same way against the same kit, #2132's the other way); it is
not claimed. The capture's scan in the load window (code this PR does not touch) was 4.3 % slower with the head
(p = 0.031), one of about 25 figures; not claimed either. Timing the hashing itself would need a local diagnostic build
of this head that counts and times each call site, with a switch back to the C loops, run on and off in one runtime (as
was done for #2136).
Risks
What could break: a hash that came out different would drop a cache file kept on disk (the state lists, the program
cache, the seed) or miss a texture or a write-back it should find; the test checks both kernels against the C for
every length 0-4096 at 8 alignments, with chained seeds and guard pages, and that each call site passes its old seed.
Players' programs are translated again once after updating (
gpu/vk/vk_shader.cppis one of the translator'ssources,
nx2/translator_files.py). A revert is enough. Renderer and host files: the runtime is rebuilt.Overlap: #2026 (also siantm's) rewrites
pin_hashinhost/nvn_native.cto hash a texture's bytes before itsserial in place (
TEX_KEY), the lines this PR moves tolsasm_fnv1a64; the two conflict there. #2026 goes first; thisPR then rebases onto it and hands the kernel those same bytes in place.
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
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:
Description: Risks now says that this conflicts with #2026 in
pin_hash(host/nvn_native.c), the overlap BeanBot pointed to (#issuecomment-69523), and that #2026 goes first, this one rebasing onto it. The other two pull requests named there (#2099, #2130) touch none of the same lines: each merges with this one without a conflict. No push.@Beanstalk This computes the FNV-1a hashes of shader and pipeline keys, texture hashes and cache stamps with a hand-written x86-64 kernel (gpu/vk, host), at startup and in every scene. It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. No switch: a revert gives back main's code. Please start from the kokiri-start save and take the picture right after it loads and settles, before any input, with a main-against-itself picture on the same PC at that point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The PR's second start on each PC should start from the caches its first start wrote. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. On the second start the shader and pipeline caches must hit as they do on main.@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.Measurements: the micro-benchmark (trials script, run alone on this PC 2026-11-22, 3 rounds, SSE2 and AVX2): figures and Verdict; the Summary's expectation sentence now cites them
@Beanstalk One correction to my request 69758 above, for its picture point: please take each picture 5 s (300 frames at 60 fps) after the first frame whose picture is not a single uniform colour, with no input in between, and take the main-against-itself picture from the same point. A picture taken during the load's fade-in from black is not comparable: run 70269 on #2156 caught the fade in 3 of 4 starts.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.@Beanstalk This replaces my request 69758 on this head and the corrections under it, in a form the farm can run: it cannot time a picture from the first frame that is not a uniform colour (its answer on #2131), so each picture here follows a fixed wait after the map event. This computes the FNV-1a hashes of shader and pipeline keys, texture hashes and cache stamps with a hand-written x86-64 kernel (gpu/vk, host), at startup and in every scene. It sits on #1947 (
cffa2f46, the asm kit), so its diff against its merge base includes that PR's commits; the farm cannot take it as the comparison, so please compare with main. No switch: a revert gives back main's code. Please start from the kokiri-start save and take the picture 10 s (600 frames at 60 fps) after the map event that ends the load, a fixed wait with no input in between (the farm cannot time it from the first frame that is not a uniform colour), with a main-against-itself picture on the same PC from the same point; then, for frame times only, play Kokiri Forest and Hyrule Field, two minutes each; at least two starts a side, interleaved, the PR against main on the same PC, on Windows with an RTX 4070, Windows with an Arc B580, Linux with an RX 6600 (RADV) and the Steam Deck. The PR's second start on each PC should start from the caches its first start wrote. The same picture as main within main's own difference from itself; frame time p99 equal to main's or better, within the run-to-run spread. The log must have no FATAL, device lost or validation error line. On the second start the shader and pipeline caches must hit as they do on main.@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
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.