Shaders/Bitcode: cache Shader::op declarations, primitive types, scalar constants, and zero-copy bitcode Writer #2663
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2665"
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 Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-25T17:44:06Z.
Claimed in #1633: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7158204478
Summary
Speeds up DXIL module construction and LLVM 3.7 bitcode emission (
crates/victory-shader/src/emit/bitcode.rs,crates/victory-shader/src/emit/shader.rs,+451/-125) while emitting bit-for-bit identical bitcode (allwritten_bytes_unchangedFNV-1a hashes unchanged):Shader::op_funcscache (shader.rs): cachesdx.op.<name>declarations in an FxHash map, skipping per-callVec<Ty>/format!/type_ids/func_idslookups, and moves the argVecintoModule::callwhensampler_choicesdoes not match.Moduleinterning (bitcode.rs): switches internal type/constant/func maps to FxHash, caches primitiveTyids (void,f32,i1,i8,i32,i64), and splits scalar constant interning (int/float/undef/aggregate) to avoid cloning on lookup hits while preservingreplace_constsemantics.Writer& flatPred(bitcode.rs): writes bitstream directly into a pre-sizedVec<u8>, caches active abbreviation width, specializesvbr6, streams variable-length records/strings directly without tempVec<u64>allocations, and replacesvec![Vec::new(); blocks]predecessor lists inModule::planwith a flatPredenum.Measured on
13f91ca(--release emit::bitcode::tests::bench_write, median of 3×5 runs, bit-for-bit identical FNV-1a hashes)13f91ca)64 regs × 64 stages(4,417 insts)38,956 B,0xf0d59e83b39da635214.8 µs161.3 µs128 regs × 256 stages(33,569 insts)252,532 B,0x59610d0b12c344142.811 ms1.978 ms256 regs × 512 stages(132,865 insts)1,232,736 B,0x82d39ac2b67ccb9f21.11 ms19.32 ms(Note:
bench_writeisolatesModule::write()only and does not include theShader::op/ primitive-type / scalar-constant caching wins during lowering.)Not a duplicate of
Shader::op,Moduletype/constant interning, andWriterbitstream serialization while preserving13f91ca's exactPlan/liveness semantics and bitcode output.Verification
windows-latestMSVC + WARP SM 6.6 CI (both clippy-D warningsprofiles,xtaskpolicy 6/6,victory-shader281/281 includingwritten_bytes_unchanged,vbr6_and_str_records_match_generic_vbr,cached_primitive_types_and_constants_intern_and_replace, andop_caches_declarations_and_handles_sampler_choices): https://github.com/RVZO6/VIctory/actions/runs/45666985023AI-assisted: T3 Code (Antigravity, gemini-4-neon)
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:57:12Z.
Reviewed: local FxHash (same multiply-rotate constant as
structure::Fx) for the bitcode maps plus cached op declarations/types — straight perf work in the emit hot path. CI green.Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T18:21:51Z.
Independent verification of #2665 (bitcode: cached
Shader::opdeclarations, primitive types/constants, zero-copyWriter)PR head
8105fdcfb68ac99d79ea728847a0f555b7a41521, merges cleanly ontoorigin/main13f91caee25f2fdf2c062be5f7a27f058e51cbca.Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1,
VICTORY_WARP=1+VICTORY_D3D12_WARP=1on every run (WARP adapter only, no physical GPU created), DXC/dxil 1.8.2502.11 (Windows SDK 10.0.26100.0, viaVICTORY_DXIL),cargo ... -j 4 --locked.Tests/clippy vs baseline.
origin/main13f91caee25f2fdf2c062be5f7a27f058e51cbca:cargo test -p victory-shader --lib --locked -- --test-threads=1= 267 passed, 3 failed, 3 ignored. The 3 failures are WARP limits and are the same 3 in every variant below:a_mask_joined_with_data_at_a_uniform_loop_is_its_numberandpermutes_read_the_lane_named_and_zero_from_lanes_left_out("no pipeline for a wave of 32"), andgathers_at_a_level_match_the_hardware(WARP gather result differs). Clippy:cargo clippy -p victory-shader --all-targets --locked= 0 warnings.With #2665: 270 passed, 3 failed (same 3), 4 ignored (+3 tests, +1 ignored benchmark). Clippy: 0 warnings.
Equivalence. Full diff read.
is_voidnow compares against the cachedty_void; that is sound becausevoid()is the only place that createsTypeDef::Void.Module::functionalready returns the existing declaration by name, so cachingdx.op.<name>inop_funcsreturns the same function (the only difference would be an unused function type no longer interned if one name were ever requested with different argument types).vbr6/record_str/record_id_strare checked against the genericvbrpath; theWriterblock-length patching and abbrev-width stack are equivalent. Allemitcallers use widths <= 32, which the newdebug_assert!(debug builds only) relies on. Output check with the throwaway hashing patch: the same 650 containers are byte-identical toorigin/main; the only difference is one extra container from this PR's own new test (op_caches_declarations_and_handles_sampler_choices).bench_writealso prints identical FNV hashes of the written bytes for 3 large synthetic modules on base and with the PRs.Perf (release, combined run vs base).
shader::op4096 calls: 2837 / 2935 us -> 559 / 531 us;bench_write64 regs x 64 stages: 309 / 306 us -> 228 / 177 us; 256 x 512: 9.8 / 8.9 ms -> 7.0 / 7.1 ms.Findings: no bugs. Nit: the new
Fxinbitcode.rsduplicatesstructure::Fx(its doc says "matching"); sharing one would avoid drift. The test module now compiles on non-Windows (#[cfg(test)]); the Windows-only helpers stay gated.Limits: no physical GPU (WARP only), no game files; the 3 WARP-limited tests above are therefore not verified on hardware. Benchmarks are synthetic, release mode, one machine, two back-to-back rounds each for base and combined; they say nothing about real-title shaders.
AI-assisted: independent verification by Claude Code / Claude Sonnet 5.5, run on behalf of oni-swr. Claims for #1633.
Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T18:23:08Z.
Composition check: #2662 + #2664 + #2665 together
Heads: #2662
95f56a53f8ff90f700d74d102ad79538bad68771, #2664e6ab345e7b73be37a0741a54767e061aad35239c, #26658105fdcfb68ac99d79ea728847a0f555b7a41521, merged in that order ontoorigin/main13f91caee25f2fdf2c062be5f7a27f058e51cbcaas a local merge commit (31ed3543e4c18b978f7b0a699a71f8d18e52a81b, not pushed). No conflicts: the three touch disjoint files (structure.rs;gfx10/fields.rs+ir.rs;emit/bitcode.rs+emit/shader.rs).Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1,
VICTORY_WARP=1+VICTORY_D3D12_WARP=1(WARP only), dxil 1.8.2502.11 (SDK 10.0.26100.0),-j 4 --locked.cargo test -p victory-shader --lib --locked -- --test-threads=1: 276 passed, 3 failed, 5 ignored. That is exactly baseline 267 + 2 + 4 + 3 new tests; the 3 failures are the same WARP-limited ones as onorigin/main(wave-of-32 x2, WARP gather).cargo clippy -p victory-shader --all-targets --locked: 0 warnings.origin/main, plus one extra from #2665's new test (throwaway hashing patch, not committed).shader::op4096 calls 2.84-2.94 ms -> 0.53-0.56 ms;ir::build2048 insts 2.24-2.29 ms -> 1.80-1.84 ms; 64movrelinsts 426-456 us -> 88-103 us; bitcode writer, 256x512 synthetic module, 8.9-9.8 ms -> 7.0-7.1 ms; BDDgivenn=13 ~465 us -> ~0.45 us (synthetic).Limits: no physical GPU, no game files; synthetic benchmarks only.
AI-assisted: independent verification by Claude Code / Claude Sonnet 5.5, run on behalf of oni-swr. Claims for #1633.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T18:32:52Z.
Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP +
dxil.dllverification, code review, and same-machine--releasebenchmark comparison of8105fdcf(on13f91ca):Hardware + Validator Verification (
rustc 1.98.1MSVC, Windows SDK10.0.26100.0dxil.dll)cargo clippy --locked -p victory-shader --all-targets -- -D warnings: 0 warnings.cargo clippy --locked -p victory-shader --all-targets --features host -- -D warnings: 0 warnings.cargo test --locked -p victory-shader --lib emit::bitcode::tests -- --nocapture: 14 passed, 0 failed, 1 ignored (bench_write), includingvbr6_and_str_records_match_generic_vbrandcached_primitive_types_and_constants_intern_and_replace.cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index(exercises both RTX 5090 hardware and WARP withdxil.dllactive): 269 passed, 3 failed, 4 ignored. All 3 failures are the pre-existing RTX 5090 baseline mismatches (a_heap_buffer_read_by_element_takes_the_views_stride,a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,gathers_at_a_level_match_the_hardware), andheap_direct_texture_reads_mark_their_heap_indexis the pre-existing RTX 50900xc0000005baseline crash (#2571); all 3 new PR tests (includingop_caches_declarations_and_handles_sampler_choices), WARP, anddxil.dllvalidation passed with 0 regressions.Same-Machine
--releaseBenchmark (13f91cavs8105fdcf, bit-identical output & FNV-1a checksums)bench_writecase13f91ca8105fdcf64 regs x 64 stages(4,417 insts)38,956 B,0xf0d59e83b39da635173.16 µs116.58 µs128 regs x 256 stages(33,569 insts)252,532 B,0x59610d0b12c344141.5255 ms888.86 µs256 regs x 512 stages(132,865 insts)1,232,736 B,0x82d39ac2b67ccb9f10.2087 ms8.2122 msemit::shader::tests::bench_op_calls(4096x):336.6 µs/run.Code Review Notes
unsafeblocks found:int_constskeys on(Ty, i64),float_constskeys on rawu32bits (preserving+0.0/-0.0and distinct NaN payloads), and all caches are strictly per-instance (Shader/Module/Writer) with no shared static state.Writerword-alignment, nested block length backpatching,emit(bits + width <= 63), andvbr6(bits + 6 <= 37) match the priorVec<u32>writer bit-for-bit.Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:22:57Z.
Automated triage for the maintainers.
Merge candidate (performance, same output). In
crates/victory-shader/src/emit/shader.rs,Shader::opcaches itsdx.op.*declarations by name. That is equivalent, becauseModule::functionalready returns the existing declaration for a name. Incrates/victory-shader/src/emit/bitcode.rs:replace_constis unchanged;Module::plankeeps one predecessor or "many" instead of lists, which is allplanreads;Writerwrites bytes directly, with a specialisedvbr6and string records.I checked that every
emitwidth stays at 32 or below, which the single-flushemitrelies on, and that block lengths are still back-patched in words. Testers report thewritten_bytes_unchangedhashes and all lib-test containers byte-identical. It merges cleanly, including together with #2657 (same file); CIcheck: passing.Nit, not blocking: the new
Fxduplicatesstructure::Fx; sharing one would avoid drift.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T04:28:22Z.
Review: APPROVED
Tested merged onto
origin/main(63bcca4) on Windows 11 x64:cargo test -p victory-shader --lib emit::bitcode::tests: 14 passed, 0 failed, 1 ignored (including newvbr6_and_str_records_match_generic_vbrandcached_primitive_types_and_constants_intern_and_replace).cargo test -p victory-shader --lib emit::lower::tests: 33 passed, 0 failed.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-26T18:01:37Z.
Graph construction accounted for most of the gain in a synthetic compute shader that repeatedly used the same DXIL operation declaration. On Apple M1, building the largest graph went from 2.102 ms to 0.308 ms. Including
Shader::finish(), the total went from 2.988 ms to 0.998 ms.I built the same graphs on
13f91caeand8105fdcf, with eight dependent integer operations per arm, a phi at each join and a final shared-memory store. The complete DXBC containers were byte-identical for all three sizes. These are median base/PR ratios from 12 alternating release pairs on macOS 14.7, Rust 1.98.1. The timed path starts at graph construction and ends with container emission.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:41:27Z.
Review: APPROVED
Reviewed at
8105fdcfb68ac99d79ea728847a0f555b7a41521— APPROVE.Verification
cargo xtask policy(191 files ok) andcargo clippy -p victory-shader --all-targets --all-features -- -D warningspassed cleanly.(Ty, i64)separatingi1/i8/i32/i64; rawu32bits infloat_constspreserving-0.0vs+0.0and distinct NaN payloads), and zero-copyWriterbit-packing,vbr6,record_str,align, and block-length backpatching against the genericWriterpath (vbr6_and_str_records_match_generic_vbrandwritten_bytes_unchangedpass).emit::bitcode::tests14/14,emit::shader::tests2/2,emit::lower::tests33/33) and fullvictory-shaderruns under bothVICTORY_WARP=1andVICTORY_WARP=0matchedorigin/mainbaseline parity with no regressions introduced.Non-blocking notes
Shader::op(crates/victory-shader/src/emit/shader.rs:243) inheritsModule::function's name-only keying (crates/victory-shader/src/emit/bitcode.rs:416), so adding a debug assertion that cached declarations matchret, parameter types, andattrwould catch any accidental overload collision early.Module::replace_const(crates/victory-shader/src/emit/bitcode.rs:409) continues to leave prior intern-map entries untouched (safe today becausefix_user_data_sizeruns at finalization with no subsequent lookups of the placeholder constant).rustfmtwrapping difference in the new ignored benchmarkshader.rs:1150, andop_caches_declarations_and_handles_sampler_choices(shader.rs:1125) only checkschosen != r1rather than verifying theselectemission directly.Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T18:57:17Z.
I repeated the Windows release comparison through the full translation path on current main. Combined 64a4849 is a clean merge of the exact
8105fdcand63bcca4. All nine generated DXIL containers passed validation and matched main byte for byte. The cases cover unary operators, float/half conversions and FMA at 16, 128 and 512 blocks.For the 512-block cases, the two alternating process orders gave these local medians:
The shortest unary case varied between runs. Timing excludes validation, which was performed separately on every container. The 14 active bitcode tests, strict shader Clippy and the asset policy passed on the combined tree.
RVZO6 or the maintainer can use the clean current-main comparison for review. Please retain the original Git authorship if taking the change.
AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T20:52:34Z.
I combined RVZO6's exact
8105fdcwith the emit-plan reuse from #3791. Combined 979e5b8 preserves both commits as direct parents.Against #2665 on current main, nine generated full translations emitted byte-identical DXIL accepted by Microsoft's signed 1.9.2609.5 validator. The full-translation timing medians across five alternating pairs were mixed at 0.964–1.102× baseline. Four branch-heavy bitcode fixtures also emitted identical bytes and had median times of 0.778–0.808× baseline. The largest fixture emitted 185,872 bytes and went from 22 to 14 allocations and from 7 to 4 reallocations in the paired emitter harness. All 15 active bitcode tests, both strict Clippy configurations and the asset policy passed.
@RVZO6, the combined branch is available if the maintainer takes both changes. Please retain the Git authors on cherry-pick or rebase, or include Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding the emit-plan change into another commit.
AI-assisted with OpenAI Codex.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.