Skip to content

Shaders/Lower: dense Value tables, FxHash maps, lazy get() mask checks, O(1) valu16 reject, and zero-alloc buffer/mask helpers - #2942

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/lower-hot-fast
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/lower-hot-fast

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown

Summary

Optimizes hot lowering paths in crates/victory-shader/src/emit/lower.rs across all shaders while emitting 100% bit-identical DXIL (and keeping lower.rs below the 512 KiB xtask policy limit by placing helper tables and tests in crates/victory-shader/src/emit/lower/tables.rs):

  1. Dense Value tables & FxMap/FxSet (emit/lower/tables.rs): replaces SipHash HashMaps for vals, floats, and phi_at with DenseMap<T> (Vec<Option<T>> indexed by Value.0), and uses FxMap/FxSet for internal pure-lookup maps/sets (leaving def_block/Shaders: replace HashMap/linear scans in Lower with O(1) dense Vec tables #2210, user_at/Shaders: cache USER_DATA CBV rows and extracted dwords in Lower::user_at #2207, sub-dword stores/Shaders: use atomicCompareExchange CAS loop for buffer_store_byte and buffer_store_short #2208, memory/Shaders: guard memory_dirty atomics with test-before-or and coalesce page-table loads (FIND-12) #2167, counters, and user_reads untouched).
  2. Lazy is_mask check in Lower::get(): defers the two st.masks/st.highs SipHash lookups into the Missing/None mask_ok fallback instead of executing them unconditionally on every operand read.
  3. O(1) fast-reject in Lower::valu16(): skips scanning all 44 16-bit opcode names on every 32-bit VALU op when !name.ends_with("16") && name != "v_cvt_f16_f32".
  4. Deferred effect scan & encoding-gated checks in Lower::inst(): moves contains("store")/contains("atomic") into the Err(why) arm only, and gates mask_ok string checks on Encoding.
  5. Hoisted round16, discards, and sections checks: skips round16 RPO/dataflow when no s_round_mode/s_setreg* exists, gates round16_after on Sopp | Sopk, and hoists !discards.is_empty() and sections.contains_key(&b) outside per-instruction loops.
  6. Zero-alloc reads_mask_as_data() & Lower::buffer(): borrows &[Operand] directly instead of collecting Vec<&Operand>, and matches format_d16_* suffixes directly instead of allocating name.replace("_format_d16_", "_format_") per buffer instruction.

Measured (--release bench_lower_hot_paths, median of 3 runs, 2,821-inst CS on 63bcca4)

  • emit::lower: 2,053.6 → 1,115.0 µs/run (1.84×, -45.7%)
  • Bit-identical DXIL: 15,632 B, FNV-1a 0x8b22c9b628d798d0 before and after.

Verification

Not run: the title; any hardware GPU.
AI-assisted: T3 Code (Antigravity, gemini-4-neon).

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Code review and RTX 5090 + WARP + DXIL + release benchmark verification at head 1569277a4533 merged cleanly onto main (63bcca43c6a0; emit/lower.rs = 522,163 B, 2,125 B under the 512 KiB cap thanks to emit/lower/tables.rs):

  • Code & equivalence audit:
    • DenseMap<T> (emit/lower/tables.rs) pre-sizes from value_count(p) and safely resizes on demand in insert/entry, with no stale-slot reuse across vals/floats/phi_at.
    • Cross-checked every fast-path gate against gfx10/opcodes.rs: round16 (Sopk/Sopp covers s_setreg_b32, s_setreg_imm32_b32, s_round_mode), mask_ok (Sop1 | Sopc | Vop3), valu16 (!ends_with("16") && != "v_cvt_f16_f32"), deferred effect on Err, and _format_d16_ allocation removal are all behaviorally equivalent.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, dxil.dll 10.0.26100.0, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 192 files ok; cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default + --features diagnostics): 0 warnings
    • tables::tests: 1/1 passed (dense_map_and_round16_fast_path_behave_identically, 1 ignored bench)
    • emit::tests (17/17) and emit::semantics_tests (34/34, 1 ignored): passed on both RTX 5090 and WARP
    • --release bench_lower_hot_paths: lower_hot (2821 insts): 699.0 us/run, dxil=15632 B, fnv1a=0x8b22c9b628d798d0.

Clean and well-tested; LGTM.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Both requested changes are in 08efa8e:

  • emit/lower/tables.rs uses the shared structure::Fx.
  • The fast path in round16 now calls round16_after(i, Some(0)), so the list of MODE writers is in one place.

The fast path gives Some(0) exactly for the blocks in reverse_postorder and None for the others, which matches the slow fixpoint when nothing changes MODE. The Sopp/Sopk gate in round16_after covers s_round_mode and both s_setreg forms. lower.rs is 522,251 bytes, under the policy limit. The gain is in translation time.

CI: passing.


Generated by Claude Code

…s, O(1) valu16 reject, and zero-alloc buffer/mask helpers

Speed up emit::lower hot paths across all shaders with 100% bit-identical DXIL:
- emit/lower/tables.rs: DenseMap<T> (Vec<Option<T>> indexed by Value.0)
  for vals, floats, and phi_at, plus FxMap/FxSet for internal lookup maps.
- Lower::get: defer st.masks/st.highs SipHash lookups into the Missing/None
  mask_ok fallback instead of running them on every operand read.
- Lower::valu16: fast-reject !name.ends_with("16") && name != "v_cvt_f16_f32"
  before scanning the 44 16-bit opcode names on every 32-bit VALU op.
- Lower::inst: defer effect substring scans (contains("store"/"atomic"))
  to Err(why); gate mask_ok checks on Encoding.
- Lower::run / round16: skip round16 RPO/dataflow when no s_round_mode or
  s_setreg* exists; gate round16_after on Sopp|Sopk; hoist !discards.is_empty()
  and sections.contains_key(&b) outside instruction loops.
- reads_mask_as_data / Lower::buffer: eliminate per-call Vec<&Operand> and
  String replace("_format_d16_") heap allocations.

Measured (--release bench_lower_hot_paths, median of 3 runs, 2,821-inst CS):
- emit::lower: 2,053.6 -> 1,115.0 us/run (1.84x, -45.7%)
- Identical DXIL: 15,632 B, FNV-1a 0x8b22c9b628d798d0 before and after.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
@RVZO6

RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown
Author

Addressed both triage items in 08efa8e:

  • Shared the crate's existing Fx hasher (pub(crate) struct Fx in structure.rs) instead of duplicating it in emit/lower/tables.rs.
  • Removed the duplicated MODE-writer name list in round16(): the fast path now calls round16_after(i, Some(0)) != Some(0) directly as the single source of truth (and preserves None for unreachable blocks via reverse_postorder).

Verified green on windows-latest MSVC + WARP SM 6.6 CI (clippy default + --all-features, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45762903919

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + dxil.dll):

  • cargo test -p victory-shader emit::lower::tests: 33 passed, 0 failed.
  • cargo test -p victory-shader emit::tests -- --test-threads=1: 17 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dense Value tables, lazy get() mask checks, valu16 fast-reject, deferred effect scan, round16 fast path, and zero-alloc reads_mask_as_data/buffer helpers preserve the local semantics I checked, and emit/lower.rs stays under the 512 KiB policy limit (522,251 bytes).

One gap remains in the "100% bit-identical DXIL across all shaders" claim: read_phis switched to FxSet, and live_edges() seeds its LIFO work stack from self.read_phis.iter(). Because Known caps tracked conditions at MAX_KNOWN = 4 (dropping the oldest condition on overflow) while seen prunes subsets (d.get().iter().all(|c| known.get().contains(c))), changing read_phis iteration order can alter traversal order and potentially yield a different conservative live-edge set in fill_phis(). The 2,821-instruction benchmark is straight-line and does not exercise this path. Could you compare base and head DXIL on a branch-heavy phi case with a missing incoming value (or qualify the claim accordingly)?

Verified locally on Windows 11 x64 (RTX 5090 + WARP + dxil.dll, 08efa8e0):

  • cargo run --locked -p xtask -- policy: passed (192 files ok)
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean
  • emit::lower::tables::tests: 1 passed, 0 failed (1 ignored bench)
  • emit::lower::tests: 33 passed, 0 failed
  • emit::tests -- --test-threads=1 (RTX 5090 and VICTORY_WARP=1): 17 passed, 0 failed on both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants