shader: add opt-in whole-function DXIL exec-mask, known-bits, and bitfield dataflow pass #281

Closed
Vladimir-Human wants to merge 639 commits from upstream-pr/281 into main
Vladimir-Human commented 2026-11-27 13:33:11 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by Vladimir-Human on 2026-11-27T13:33:11Z.


Current switch values use bit 4096 for DATAFLOW. Set VICTORY_SHADER_OPT=4159 with the default passes, or 4223 to include WIDE. The test notes below use the previous switch values.

Summary

Adds an opt-in whole-function DXIL bitcode dataflow optimizer (bitcode::opt::DATAFLOW = 512, enabled via VICTORY_SHADER_OPT=575 alongside the default passes or 639 with WIDE) in crates/victory-shader/src/emit/bitcode/dataflow.rs.

Default VICTORY_SHADER_OPT remains 63 (opt::DEFAULT = ALL & !(WIDE | DATAFLOW)) and TRANSLATOR_VERSION remains at main's 9, preserving a matched baseline for A/B testing via the existing shader_opt() shader-cache key (crates/victory-gpu/src/shader_cache.rs:139). Pass bits 128 and 256 remain free for #193.

Optimization Phases

  1. Edge-Sensitive Sparse Conditional Known-Bits Propagation (SCKP):
    • Worklist fixed-point over executable CFG blocks/edges and bit-level KnownBits { width, zero, one } across PHIs, loops, exact ripple-carry Add/Sub, exact small-mask shifts, Mul, guarded UDiv/URem/SDiv, ZExt/SExt/Trunc/Bitcast, Select, exact (1 << w) - 1 bitfield masks (v_bfe_*/s_bfm), integer comparisons, and pure DXIL bit intrinsics (UMin/UMax/IMin/IMax, Bfrev, Countbits, FirstbitLo, FirstbitHi, legacyF32ToF16).
    • Registers transitive 2-hop SSA dependency edges for sub(shl(..), 1) and bitcast(bitcast(..)) so loop backedge widening always re-enqueues dependent instructions.
    • Precomputes dominator-tree branch refinements feeding PHI incoming edges and prunes unreachable loop backedges/PHI cycles.
  2. Correlated Dominator-Tree & Multi-Predecessor PathEnv Forward Rewrite:
    • Snapshots pre-mutation block terminators and propagates dominating branch predicates, ranges, and recursive bitmask/shift/cast constraints (with multi-predecessor edge intersection).
    • Collapses nested exec-guard / v_cndmask select chains via eval_assuming (select(g, a, select(g & h, b, c)) -> select(g, a, c)).
    • Collapses shift pairs (lshr(shl) / ashr(shl) / shl(lshr) from v_mul_u32_u24, SDWA, and v_bfe_*), v_bfi_b32 same-value idioms, zext/sext/trunc roundtrips, redundant UMin/UMax/IMin/IMax, and sequential per-use bit-demand bypass (strip_unused_bits).
  3. Whole-Function Backward Structural Demanded-Bits Pass:
    • Propagates constant-only structural bit demands across all blocks and PHIs paired with fresh SCKP KnownBits, eliminating dead upper-bit packs and narrowing ashr/sext to lshr/zext.

Soundness Guarantees

  • Zero groupshared (addrspace(3)) LDS store-to-load forwarding: wave-ordered barrierless LDS reads remain untouched (space3_lds_store_is_never_forwarded_to_load + HW wave_ordered_barrierless_lds_soundness_hw_parity).
  • Zero dx.op.getDimensions* or Tag::Clamp coalescing/reuse (crediting Belobug/VIctory-Belobug-AMD#324): Module::tag records Tag::Clamp unconditionally, opt.rs::key excludes both Tag::Clamp and dx.op.getDimensions*, and dataflow.rs never folds a Tag::Clamp onto another clamp/UMin (requiring strictly smaller non-clamp provenance).
  • Zero fcmp one (pred::ONE) emission, zero float arithmetic folding, undef-safe checks.

Verification (VICTORY_D3D12_DEBUG=1, --test-threads=1)

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default & --all-features): 0 warnings
  • cargo run -p xtask --locked -- policy (315 files ok) & cargo test -p xtask --locked: 11/11 passed
  • emit::bitcode::dataflow:: 12/12 passed:
    • Exhaustive 4-bit KnownBits transfer proof across all 81 x 81 = 6,561 abstract pairs x all concrete values
    • 500-program randomized multi-block CFG/loop/bitfield/exec-guard equivalence oracle (DATAFLOW, DATAFLOW | DEAD, FOLD | DATAFLOW | DEAD, DEFAULT | DATAFLOW, ALL)
    • Regression tests for 2-hop SCKP loop widening (sub(shl) & bitcast(bitcast)), symmetric and/or mask stripping, space-3 LDS non-forwarding, getDimensions non-coalescing, RDNA4 Tag::Clamp non-merging, and 64-bit zext/trunc INT64 elimination
  • emit::dataflow_tests:: 4/4 passed (HW RTX 4070 SUPER + WARP + dxil.dll validation, 0 D3D12 debug errors)
  • emit::bitcode::opt::tests:: 11/11, the_clean_up_switch_parses_or_runs_every_pass 1/1, VICTORY_SHADER_OPT=575 emit::lower::tests:: 19/19 (1 ignored), VICTORY_SHADER_OPT=639 wide-load 1/1
  • Full victory-shader suite: 338/338 passed (5 ignored, 0 D3D12 debug errors)

Hardware GPU Benchmark (NVIDIA GeForce RTX 4070 SUPER, ID3D12QueryHeap timestamps + bit-exact readback parity)

Command: cargo test --release -p victory-shader --locked bench_dataflow_hw_gpu -- --ignored --nocapture --test-threads=1

Compute Kernel (4096 x 64, 512 iters) Base (opt=63) Insts Dataflow (opt=575) Insts Inst Delta DXIL Bytes GPU Median (us) GPU Delta
RDNA2 bitfield + u24 loop (v_bfe/v_mul_u24) 68 (92 unopt) 53 -22.1% 2356 -> 2276 375.81 -> 360.45 -4.1%
RDNA2 exec + v_cndmask loop 52 (64 unopt) 40 -23.1% 2296 -> 2236 239.62 -> 239.62 0.0%
Whole-fn bitfield + clamp + i64 pack + exec 55 (55 unopt) 33 (INT64 cleared) -40.0% 2332 -> 2168 218.11 -> 201.73 -7.5%
<!-- greyfinch-sync:TaynanGT/Vice-Tay:issue:5527508864 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/281). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T13:33:11Z. --- Current switch values use bit `4096` for DATAFLOW. Set `VICTORY_SHADER_OPT=4159` with the default passes, or `4223` to include WIDE. The test notes below use the previous switch values. ## Summary Adds an opt-in whole-function DXIL bitcode dataflow optimizer (`bitcode::opt::DATAFLOW = 512`, enabled via `VICTORY_SHADER_OPT=575` alongside the default passes or `639` with `WIDE`) in `crates/victory-shader/src/emit/bitcode/dataflow.rs`. Default `VICTORY_SHADER_OPT` remains `63` (`opt::DEFAULT = ALL & !(WIDE | DATAFLOW)`) and `TRANSLATOR_VERSION` remains at `main`'s `9`, preserving a matched baseline for A/B testing via the existing `shader_opt()` shader-cache key (`crates/victory-gpu/src/shader_cache.rs:139`). Pass bits `128` and `256` remain free for `#193`. ### Optimization Phases 1. **Edge-Sensitive Sparse Conditional Known-Bits Propagation (SCKP):** - Worklist fixed-point over executable CFG blocks/edges and bit-level `KnownBits { width, zero, one }` across PHIs, loops, exact ripple-carry `Add`/`Sub`, exact small-mask shifts, `Mul`, guarded `UDiv`/`URem`/`SDiv`, `ZExt`/`SExt`/`Trunc`/`Bitcast`, `Select`, exact `(1 << w) - 1` bitfield masks (`v_bfe_*`/`s_bfm`), integer comparisons, and pure DXIL bit intrinsics (`UMin`/`UMax`/`IMin`/`IMax`, `Bfrev`, `Countbits`, `FirstbitLo`, `FirstbitHi`, `legacyF32ToF16`). - Registers transitive 2-hop SSA dependency edges for `sub(shl(..), 1)` and `bitcast(bitcast(..))` so loop backedge widening always re-enqueues dependent instructions. - Precomputes dominator-tree branch refinements feeding PHI incoming edges and prunes unreachable loop backedges/PHI cycles. 2. **Correlated Dominator-Tree & Multi-Predecessor `PathEnv` Forward Rewrite:** - Snapshots pre-mutation block terminators and propagates dominating branch predicates, ranges, and recursive bitmask/shift/cast constraints (with multi-predecessor edge intersection). - Collapses nested `exec`-guard / `v_cndmask` `select` chains via `eval_assuming` (`select(g, a, select(g & h, b, c)) -> select(g, a, c)`). - Collapses shift pairs (`lshr(shl)` / `ashr(shl)` / `shl(lshr)` from `v_mul_u32_u24`, SDWA, and `v_bfe_*`), `v_bfi_b32` same-value idioms, `zext`/`sext`/`trunc` roundtrips, redundant `UMin`/`UMax`/`IMin`/`IMax`, and sequential per-use bit-demand bypass (`strip_unused_bits`). 3. **Whole-Function Backward Structural Demanded-Bits Pass:** - Propagates constant-only structural bit demands across all blocks and PHIs paired with fresh SCKP `KnownBits`, eliminating dead upper-bit packs and narrowing `ashr`/`sext` to `lshr`/`zext`. ### Soundness Guarantees - **Zero groupshared (`addrspace(3)`) LDS store-to-load forwarding:** wave-ordered barrierless LDS reads remain untouched (`space3_lds_store_is_never_forwarded_to_load` + HW `wave_ordered_barrierless_lds_soundness_hw_parity`). - **Zero `dx.op.getDimensions*` or `Tag::Clamp` coalescing/reuse** (crediting [Belobug/VIctory-Belobug-AMD#324](https://github.com/Belobug/VIctory-Belobug-AMD/issues/324)): `Module::tag` records `Tag::Clamp` unconditionally, `opt.rs::key` excludes both `Tag::Clamp` and `dx.op.getDimensions*`, and `dataflow.rs` never folds a `Tag::Clamp` onto another clamp/`UMin` (requiring strictly smaller non-clamp provenance). - **Zero `fcmp one` (`pred::ONE`) emission, zero float arithmetic folding, undef-safe checks.** ### Verification (`VICTORY_D3D12_DEBUG=1`, `--test-threads=1`) - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default & `--all-features`): **0 warnings** - `cargo run -p xtask --locked -- policy` (`315 files ok`) & `cargo test -p xtask --locked`: **11/11 passed** - `emit::bitcode::dataflow::` **12/12 passed**: - Exhaustive 4-bit `KnownBits` transfer proof across all `81 x 81 = 6,561` abstract pairs x all concrete values - 500-program randomized multi-block CFG/loop/bitfield/exec-guard equivalence oracle (`DATAFLOW`, `DATAFLOW | DEAD`, `FOLD | DATAFLOW | DEAD`, `DEFAULT | DATAFLOW`, `ALL`) - Regression tests for 2-hop SCKP loop widening (`sub(shl)` & `bitcast(bitcast)`), symmetric `and`/`or` mask stripping, space-3 LDS non-forwarding, `getDimensions` non-coalescing, RDNA4 `Tag::Clamp` non-merging, and 64-bit `zext`/`trunc` `INT64` elimination - `emit::dataflow_tests::` **4/4 passed** (HW RTX 4070 SUPER + WARP + `dxil.dll` validation, `0` D3D12 debug errors) - `emit::bitcode::opt::tests::` **11/11**, `the_clean_up_switch_parses_or_runs_every_pass` **1/1**, `VICTORY_SHADER_OPT=575` `emit::lower::tests::` **19/19** (1 ignored), `VICTORY_SHADER_OPT=639` wide-load **1/1** - Full `victory-shader` suite: **338/338 passed** (5 ignored, `0` D3D12 debug errors) ### Hardware GPU Benchmark (`NVIDIA GeForce RTX 4070 SUPER`, `ID3D12QueryHeap` timestamps + bit-exact readback parity) Command: `cargo test --release -p victory-shader --locked bench_dataflow_hw_gpu -- --ignored --nocapture --test-threads=1` | Compute Kernel (`4096 x 64`, 512 iters) | Base (`opt=63`) Insts | Dataflow (`opt=575`) Insts | Inst Delta | DXIL Bytes | GPU Median (`us`) | GPU Delta | |---|---:|---:|---:|---:|---:|---:| | RDNA2 bitfield + `u24` loop (`v_bfe`/`v_mul_u24`) | 68 (92 unopt) | 53 | **-22.1%** | 2356 -> 2276 | 375.81 -> 360.45 | **-4.1%** | | RDNA2 `exec` + `v_cndmask` loop | 52 (64 unopt) | 40 | **-23.1%** | 2296 -> 2236 | 239.62 -> 239.62 | 0.0% | | Whole-fn bitfield + clamp + `i64` pack + `exec` | 55 (55 unopt) | 33 (`INT64` cleared) | **-40.0%** | 2332 -> 2168 | 218.11 -> 201.73 | **-7.5%** |
TaynanGT commented 2026-11-27 13:37:44 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T13:37:44Z.


@Vladimir-Human The opt-in bit gives us a baseline for the dataflow pass. Please reconcile with main’s translator version 9 and keep the emitted output and cache key paired. The useful correctness checks are loop backedges, inactive lanes and joins where information becomes less precise.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7219734324 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/281#issuecomment-7219734324). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T13:37:44Z. --- @Vladimir-Human The opt-in bit gives us a baseline for the dataflow pass. Please reconcile with main’s translator version 9 and keep the emitted output and cache key paired. The useful correctness checks are loop backedges, inactive lanes and joins where information becomes less precise.
Vladimir-Human commented 2026-11-27 13:57:22 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by Vladimir-Human on 2026-11-27T13:57:22Z.


@TaynanGT Rebased cleanly onto main (2532474, TRANSLATOR_VERSION = 9 in crates/victory-shader/src/lib.rs:29) and updated to 54a9348 (all 4 CI checks green):

  • Cache-key & baseline pairing: lib.rs is untouched (TRANSLATOR_VERSION stays 9) and opt::DEFAULT remains 63 (ALL & !(WIDE | DATAFLOW)), so the default output stays byte-identical. Opting in via VICTORY_SHADER_OPT=575 (or 639) flows directly through emit::shader_opt() into both emit::options_hash() (crates/victory-shader/src/emit/mod.rs:338) and the disk shader cache key (crates/victory-gpu/src/shader_cache.rs:139), keeping 63 and 575 entries paired with their emitted code.
  • Loop backedges & PHI widening: sckp_2hop_sub_shl_loop_widening_does_not_miscompile (crates/victory-shader/src/emit/bitcode/dataflow.rs:4347), sckp_2hop_bitcast_roundtrip_loop_widening_does_not_miscompile (dataflow.rs:4403, covering 2-hop SSA dependency edges when loop PHIs widen across backedges), sccp_kills_unreachable_loop_backedge_phi_cycle (dataflow.rs:4283), and HW+WARP loop parity tests rdna2_bitfield_and_u24_loop_hw_parity (crates/victory-shader/src/emit/dataflow_tests.rs:741) and dxil_whole_function_bitfield_clamp_exec_hw_parity (dataflow_tests.rs:895).
  • Inactive lanes (exec / v_cndmask): exec_mask_nested_select_and_cross_block_bitfield_collapse (dataflow.rs:4141, exercising eval_assuming at dataflow.rs:2234) and HW+WARP divergent-EXEC + v_cndmask loop parity rdna2_exec_mask_and_cndmask_loop_hw_parity (dataflow_tests.rs:789).
  • Lossy joins: KnownBits::join keeps only bits agreed across all incoming edges, learn_multi_pred (dataflow.rs:2144) intersects facts across every executable predecessor only for strictly dominating values, and randomized_equivalence_oracle_across_500_programs_and_pass_combos (dataflow.rs:5065) checks 500 multi-block CFG/diamond/loop/PHI programs bit-exact across 6 input vectors and 5 pass combinations (DATAFLOW, DATAFLOW | DEAD, FOLD | DATAFLOW | DEAD, DEFAULT | DATAFLOW, ALL).
<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7220151577 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/281#issuecomment-7220151577). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T13:57:22Z. --- @TaynanGT Rebased cleanly onto `main` (`2532474`, `TRANSLATOR_VERSION = 9` in `crates/victory-shader/src/lib.rs:29`) and updated to `54a9348` (all 4 CI checks green): - **Cache-key & baseline pairing:** `lib.rs` is untouched (`TRANSLATOR_VERSION` stays `9`) and `opt::DEFAULT` remains `63` (`ALL & !(WIDE | DATAFLOW)`), so the default output stays byte-identical. Opting in via `VICTORY_SHADER_OPT=575` (or `639`) flows directly through `emit::shader_opt()` into both `emit::options_hash()` (`crates/victory-shader/src/emit/mod.rs:338`) and the disk shader cache key (`crates/victory-gpu/src/shader_cache.rs:139`), keeping `63` and `575` entries paired with their emitted code. - **Loop backedges & PHI widening:** `sckp_2hop_sub_shl_loop_widening_does_not_miscompile` (`crates/victory-shader/src/emit/bitcode/dataflow.rs:4347`), `sckp_2hop_bitcast_roundtrip_loop_widening_does_not_miscompile` (`dataflow.rs:4403`, covering 2-hop SSA dependency edges when loop PHIs widen across backedges), `sccp_kills_unreachable_loop_backedge_phi_cycle` (`dataflow.rs:4283`), and HW+WARP loop parity tests `rdna2_bitfield_and_u24_loop_hw_parity` (`crates/victory-shader/src/emit/dataflow_tests.rs:741`) and `dxil_whole_function_bitfield_clamp_exec_hw_parity` (`dataflow_tests.rs:895`). - **Inactive lanes (`exec` / `v_cndmask`):** `exec_mask_nested_select_and_cross_block_bitfield_collapse` (`dataflow.rs:4141`, exercising `eval_assuming` at `dataflow.rs:2234`) and HW+WARP divergent-`EXEC` + `v_cndmask` loop parity `rdna2_exec_mask_and_cndmask_loop_hw_parity` (`dataflow_tests.rs:789`). - **Lossy joins:** `KnownBits::join` keeps only bits agreed across all incoming edges, `learn_multi_pred` (`dataflow.rs:2144`) intersects facts across every executable predecessor only for strictly dominating values, and `randomized_equivalence_oracle_across_500_programs_and_pass_combos` (`dataflow.rs:5065`) checks 500 multi-block CFG/diamond/loop/PHI programs bit-exact across 6 input vectors and 5 pass combinations (`DATAFLOW`, `DATAFLOW | DEAD`, `FOLD | DATAFLOW | DEAD`, `DEFAULT | DATAFLOW`, `ALL`).
TaynanGT commented 2026-11-27 14:12:15 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T14:12:15Z.


@Vladimir-Human The backedge, inactive-lane and join tests cover the cases I was concerned about. I moved DATAFLOW to bit 4096 in c5ab09b because #193 uses bit 512 for LICM. Use VICTORY_SHADER_OPT=4159 for the comparison against 63, or 4223 with WIDE. This gives dataflow its own cache entry.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7220483274 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/281#issuecomment-7220483274). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T14:12:15Z. --- @Vladimir-Human The backedge, inactive-lane and join tests cover the cases I was concerned about. I moved DATAFLOW to bit 4096 in `c5ab09b` because #193 uses bit 512 for LICM. Use `VICTORY_SHADER_OPT=4159` for the comparison against `63`, or `4223` with WIDE. This gives dataflow its own cache entry.
TaynanGT commented 2026-11-30 17:13:23 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:13:23Z.


main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7285914473 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/281#issuecomment-7285914473). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-30T17:13:23Z. --- `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
TaynanGT/Vice-Tay!281
No description provided.