D3D12-SHADER-FAST-HASH: hash the translator's tables with a multiply hasher, not SipHash (-10 to -15 % of full-archive translate time) #2750
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2750"
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?
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T15:27:35Z.
Claim: D3D12-SHADER-FAST-HASH (comment).
AI-assisted: Claude Sonnet 5.5 (Cursor agent). No game files are in this pull request: the numbers are counts and times from running the translator on the author's own copy of the archive, and the tool that hashed the output was a local patch that is not part of it.
Depends on: #2745 (it adds two tables to
Lowerthat this pull request gives the same hasher; the branch has #2745 merged in, so until that lands the diff shows both. This pull request alone is the six filesbitcode.rs,bitcode/opt.rs,fxhash.rs,io.rs,lib.rsand the tables oflower.rs).What
std::collections::HashMaphashes with SipHash. The keys of the translator's tables are numbers it made itself (values, blocks, types, sites) while reading the user's own file, so nothing gains from SipHash's resistance to chosen keys, andstructure.rsalready says "SipHash dominated run time" about its BDD tables and has its own hasher for them. The other tables did not.Sampling profile (the built-in sampler,
LEONIDA_PROFILE,diagnostics) of a whole-archiveleonida translate, 24,055 samples at 5 ms over 16 threads, mimalloc build: SipHash (RandomState) was under a frame of the translator in 21 % of the samples: 13.7 % inModule::merge(the value-numbering tablesknown,readandFactsofemit/bitcode/opt.rs), 1.6 % in the interning tables ofemit/bitcode.rs, 1.2 % inio::raise, 1.7 % inLower::newandLower::computed_with, and the rest in the tables ofLower. After this change SipHash is under 1 % of the samples (230 of 23,545).leonida-shader/src/fxhash.rs:FxHasher(a multiplication per word, then a rotation of the product so that the well mixed high bits are where a table takes its bucket from),FxHashMapandFxHashSet.Module(types, constants, functions), ofModule::merge(known,read,joins,Facts), ofio::raiseand the tablesLowerand its pairedLaneStatekeep by value, block, site and descriptor (and the two index tables #2745 adds).read_phis, a closure).structure.rsand the tables that are iterated for their order are left as they were. A hasher changes only the order a table is walked in, so the output is the thing to check, and it is below.I said in the claim I would look at
ir.rsandspill.rstoo; I did not change them. After this change SipHash is under 1 % of the samples, so what is left of it is not worth a larger diff.Result
Same machine (Ryzen 7 7800X3D, 16 threads), whole archive
leonida translate <shaders.rpf>(LEONIDA_HOST_RDNA=1, 26,084 programs, 40.6 M IR instructions),--release, every build with the same local patch that appendsblake3(dxil)of each emitted program to a file, alternating builds. CPU is user + kernel.System heap (the build
docs/README-fxceless.mdshows):mainmain+ #2745--features diagnostics,mimalloc(the game's build):mainmain+ #2745Against
main+ #2745, which is what this pull request adds: about -10 % wall and -12 % CPU on the system heap, -15 % wall and -15 % CPU under mimalloc. Againstmain(both pull requests together): -26 % wall and -28 % CPU on the system heap, -18 % wall and -20 % CPU under mimalloc. (Each pull request's own effect when measured onmainalone, the same way, before #2745 was merged in: 63.8 / 64.1 / 64.2 s -> 58.6 / 58.4 / 59.0 s and 885 / 889 / 889 -> 797 / 794 / 802 CPU-seconds on the system heap, 45.4 / 45.1 / 45.3 -> 40.6 / 40.2 / 41.6 s and 636 / 633 / 633 -> 562 / 558 / 562 under mimalloc.)Output unchanged: the DXIL of all 25,689 emitted programs (15,799 distinct; the rest are duplicate containers) hashes the same in every run of every build in the tables, 24 runs after the last edit of the translator's code (12 of this pull request's builds, 12 of
mainandmain+ #2745) and the 12 runs I made of the standalone version of this change (old and new builds) before #2745 was merged in, compared as multisets of (hash, length) because the label of a duplicate container is whichever thread gets there first and differs between runs of the same build.Tests
fxhash::tests(3): equal keys hash equal, and near keys, a string and the same with zeros after it, and slices of different length differ; the low bits (a bucket) and the top seven (a tag) of the hashes of 131,072 small numbers, multiples of 4,096 and 65,536, pairs and triples spread within 3 times the mean (controlled: without the rotation of the product the multiples of 4,096 all land in one bucket and it fails); a map with the hasher agrees with aBTreeMapover 20,000 random inserts, removals and lookups.cargo test -p leonida-shader --release --no-fail-fast: 389 passed, 1 failed, 4 ignored (394), doc tests 0. The one failure,emit::valu_tests::rdna_forms_give_what_the_spelled_out_ones_give(mad_legacy: written alike), fails the same onmainwithout this change; it is what #2624 fixes.cargo clippy -p leonida-shader --release --all-targets: 3 warnings in the library and 7 in its tests, none in a line this change touches (opt.rs:1225and:1685,lower.rs:1698and:2517,twin_bias_tests.rs,water_tests.rs: all older than this change bygit blame).python -I scripts/check_no_game_data.pyon the seven files: clean.leonida translatewithout--validateor--pipelines), so no GPU or driver to name.Not covered: the effect on a title's frame time (it is the translation, not the frame). What is left of the translator's time is the BDD tables of
structure.rs(#2726 is open there),spill::build,ir::buildandModule::merge's own work.Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T15:34:11Z.
Independent synthetic DXIL comparison for PR #2750
MEASURED (3 fresh processes per arm). On CachyOS x86-64 with Rust 1.98.1, I compared
mainbe769080plus #274588d0dbf9(treee815bee7) against the same main plus #275055996ef0(tree2efcd9b3). The diff between the two trees is the six hasher files. I built withcargo +1.98.1 test --locked --offline -p leonida-shader --lib --no-run, then ran each test binary three times with--test-threads=1. The same test-only instrumentation in both arms captured bytes atModule::writeand the end of shader lowering, keyed by test and emission ordinal.A second agent independently compared the six directories and checked the counts against the raw output. This was CPU-only translation of synthetic fixtures. AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T20:35:59Z.
Native Windows check of
2e78c2ab4on currentmaind8ca18240, where #2745 has landed, so the merge (aa8ee507f) differs frommainby this PR's 6 files only. Windows 11, MSVC x64, Rust 1.98.1, the fullcargo test -p leonida-shader --libon a hardware GPU (not WARP), so the DXIL validation and GPU dispatch/readback tests run with the new hasher's table order.mainlists 405 tests (400 run + 5 ignored), the merge 408, so the 3 additions are the only difference and no existing test changes result.Not covered: translation time and output identity on the title's real shaders.
AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:13:27Z.
Review: APPROVED
Clean, zero-dependency
FxHasherimplementation ind3d12/crates/leonida-shader/src/fxhash.rswith tail-length mixing (rest.len() << 56), thorough distribution/collision/BTreeMap parity tests, and strictly scoped map/set swaps acrossbitcode.rs,bitcode/opt.rs,lower.rs, andio.rs(confirmed againstmainwith no conflicts or unused imports after#2687). Independently verified on Windows/GPU (403/0 tests and byte-identical DXIL output) and all CI checks are green. Merging.(AI-assisted review.)
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T11:18:02Z.
Posted after the merge: the head tested below is the one that was merged.
RUN (independent run on Windows, #2750 head
b75e391a8, merged locally into maine56aff1cewithgit merge --no-ff, no conflicts. #2745 is already in main, so the merge adds only the six files of this pull request. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)Commands, in
d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with--locked, at low priority,RUST_TEST_THREADS=8:cargo test -p leonida-shader --no-fail-fast: main 405 passed / 0 failed / 5 ignored; merge 408 / 0 / 5 (the 3 newfxhashtests). Therdna_forms_give_what_the_spelled_out_ones_givefailure named in the body is fixed on main (#2624): it passes on both sides.cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 405 / 0 / 5; merge 408 / 0 / 5.fxhashmodule), so the red is by mutation of the new hasher, with its tests kept:finish(self.0instead ofself.0.rotate_left(26)):small_number_keys_spread_over_buckets_and_tagsfails with "i * 4096: a bucket of 131072 against a mean of 32". This is the control the body describes.write(self.add(u64::from_le_bytes(word))):the_same_key_hashes_the_same_and_near_keys_differfails on"a" and "a\0"(the same hash for both).git statusclean),cargo test -p leonida-shader --lib fxhash: 3 passed.cargo clippy -p leonida-shader --all-targets, default and--features diagnostics, on main and on the merge: 13 warnings on each side, the same set, none in a changed line.python -I tools/d3d12_switches.py --check d3d12/docs/switches.mdis clean on the merge.Whole-archive translation, offline (no game run), to check the timing and the output claims:
cargo build --release -p leonida --locked(system heap, no features) of main and of the merge;leonida translate <shaders.rpf>withLEONIDA_HOST_RDNA=1, every logical core (32 threads), at low priority. The two builds ran alternately, 5 runs each. Other jobs were running on the PC, so wall time is noisy.diagnostics,mimallocbuild was not timed here.--list, which prints per program a hash of the emitted shader and of what the host binds for it. Both sides give the same multiset of hashes (same number of emitted programs, same number of distinct hashes, no hash differs) and the same summary of failure reasons. The only difference is which program is quoted as the example of two failure reasons, which depends on which thread gets there first.RandomStatealready seeds every map at random, so a table whose iteration order reached the output would already make two runs of main differ. The new hasher only makes that order fixed.Not covered here: the game (this changes translation time, not frame time) and the
mimallocbuild.Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.