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

Open
github-bot wants to merge 1 commit from github-bot/gh-2942 into main AGit
Owner

@⁠RVZO6 on GitHub · 2026-11-26 00:57 UTC · original
RVZO6:rvzo/lower-hot-fast → main · +344 −82 · 3 files · 1 commits · open

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/#2210, user_at/#2207, sub-dword stores/#2208, memory/#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).

**@⁠RVZO6** on GitHub · 2026-11-26 00:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2942) `RVZO6:rvzo/lower-hot-fast` → `main` · +344 −82 · 3 files · 1 commits · open ### 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 `HashMap`s 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`/#2210, `user_at`/#2207, sub-dword stores/#2208, `memory`/#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 - Unit test `emit::lower::tables::tests::dense_map_and_round16_fast_path_behave_identically`. - Verified green on `windows-latest` MSVC + WARP SM 6.6 CI (clippy, `xtask policy` `192 files ok`, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45730729035 **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:18:06 +00:00
github-bot force-pushed github-bot/gh-2942 from 1569277a45 to 08efa8e07b 2026-11-26 06:30:36 +00:00 Compare
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:11 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/2942#issuecomment-7179521102) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:28 UTC · original · edited 2026-11-26 07:24 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-26 04:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2942#issuecomment-7182621995) · edited 2026-11-26 07:24 UTC <!-- victory-triage v1 head=08efa8e07b12950a606fe4af631f77fd6b95c9b1 verdict=merge-candidate at=2026-11-26T07:24:03Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-26 05:42 UTC · original

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)

**@⁠RVZO6** on GitHub · 2026-11-26 05:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/2942#issuecomment-7183739834) 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)
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 07:47 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 07:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2942#pullrequestreview-6070237629) · **approved these changes** 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).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 09:48 UTC · original · reviewed

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
**@⁠fuzeofficlal** on GitHub · 2026-11-26 09:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/2942#pullrequestreview-6071829543) · **reviewed** 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
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2942/head:github-bot/gh-2942'
git switch 'github-bot/gh-2942'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2942'
git switch 'github-bot/gh-2942'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2942'
git switch 'github-bot/gh-2942'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2942'
git switch 'main'
git merge --squash 'github-bot/gh-2942'
git switch 'main'
git merge --ff-only 'github-bot/gh-2942'
git switch 'main'
git merge 'github-bot/gh-2942'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2942
No description provided.