Skip to content

Shaders: O(|known|) BDD forced() sweep, read-only implies(), per-block reuse in finish(), and FxHash in meet_in() - #2662

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/structure-bdd-fast
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/structure-bdd-fast

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 25, 2026

Copy link
Copy Markdown

Claimed in #1633: #1633 (comment)

Summary

Eliminates quadratic BDD node creation and repeated scans during CFG structurisation (crates/victory-shader/src/structure.rs, +225/-60), with identical reduced OBDD output:

  1. O(|known|) read-only Bools::forced() sweep: replaces Bools::given()'s O(|b| * |vars(b)|) mutating implies loop with a single bottom-up pass over reachable edges of the reduced OBDD.
  2. Read-only Bools::implies(): checks entailment directly without allocating temporary BDD nodes or evicting memo entries.
  3. Per-block reuse in finish(): computes forced(cond[n]) once per block and reuses it across guards and exits.
  4. FxHash in meet_in(): switches the refuted set from default SipHash to the existing Fx hasher.

Measured (--release structure::tests::bench_bools)

Workload Before (main) After Speedup
given() (n = 8) 20.43 µs 0.22 µs 93×
given() (n = 13) 655.25 µs 0.54 µs 1,213×

Not a duplicate of

Verification

  • Verified on windows-latest MSVC + WARP SM 6.6 CI (both clippy -D warnings profiles, xtask policy 6/6, victory-shader 281/281 including equivalence tests comparing forced()/implies() against the previous BDD implementations): https://github.com/RVZO6/VIctory/actions/runs/45666985023
  • Not run: the title; any hardware GPU.

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

…k reuse in finish(), and FxHash in meet_in()

Replace Bools::given()'s O(|b| * |vars(b)|) mutating implies loop with an O(|known|) read-only forced() sweep over reduced OBDD edges, make Bools::implies() read-only without allocating BDD nodes or evicting memo entries, reuse forced(cond[n]) across guards and exits per block in finish(), and switch meet_in()'s refuted set from SipHash to FxHash.

Measured on bench_bools (--release): given() drops from 20.43 us -> 0.22 us (93x) at n=8 and 655.25 us -> 0.54 us (1,213x) at n=13.

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

Copy link
Copy Markdown

Reviewed: the read-only implies/implies_in (local proved set, no memo or node allocation) is sound — BDD implication is decidable by simultaneous top-variable descent. overflow returning true preserves the conservative direction. FxHash swap in the hot BDD maps is a straight win. CI green.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent verification of #2662 (BDD forced() sweep, read-only implies(), given_with)

PR head 95f56a53f8ff90f700d74d102ad79538bad68771, merges cleanly onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca (git merge-tree).

Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1, VICTORY_WARP=1 + VICTORY_D3D12_WARP=1 on every run (WARP adapter only, no physical GPU created), DXC/dxil 1.8.2502.11 (Windows SDK 10.0.26100.0, via VICTORY_DXIL), cargo ... -j 4 --locked.

Tests/clippy vs baseline. origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca: 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_number and permutes_read_the_lane_named_and_zero_from_lanes_left_out ("no pipeline for a wave of 32"), and gathers_at_a_level_match_the_hardware (WARP gather result differs). Clippy: cargo clippy -p victory-shader --all-targets --locked = 0 warnings.
With #2662: 269 passed, 3 failed (same 3), 3 ignored (+2 new tests: forced_matches_implies_across_disjunctive_skips_and_random_bdds, read_only_implies_matches_and_not_without_allocating_nodes). Clippy: 0 warnings.

Equivalence. I read the full diff. forced() (variable v is forced iff no reachable non-FALSE edge skips v and every reachable node testing v takes one polarity) is correct for reduced OBDDs; the TERMINAL far-edge correctly stops forcing past an edge into TRUE. implies() returns true on overflow like the old ite-based form (which yielded FALSE on overflow). given_with(known = FALSE) follows hi edges, matching the old "every atom implied true". Output check: a throwaway, uncommitted patch hashed every container Shader::finish emits during the lib test run: 650 containers (476 distinct), byte-identical to origin/main.

Perf (release, bench_bools, synthetic BDDs). given at n=8/10/12/13 on base: 15 / 55 / 240 / 465 us (its own fast path); with the three shader PRs combined: 0.25 / 0.44 / 0.44 / 0.45 us. Small n (2, 4) unchanged within noise. Caveat: this isolates given; the benchmark's known is cheap to walk, and the effect on real shaders was not measured.

Findings: no bugs. Observation: since implies/forced no longer allocate BDD nodes, the MAX_NODES overflow (TOO_LARGE) should be hit less often than before; that can only turn a TOO_LARGE into a success, not the reverse. Not measured on real shaders.

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.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Composition check: #2662 + #2664 + #2665 together

Heads: #2662 95f56a53f8ff90f700d74d102ad79538bad68771, #2664 e6ab345e7b73be37a0741a54767e061aad35239c, #2665 8105fdcfb68ac99d79ea728847a0f555b7a41521, merged in that order onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca as 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 on origin/main (wave-of-32 x2, WARP gather).
  • cargo clippy -p victory-shader --all-targets --locked: 0 warnings.
  • Emitted containers over the lib test run: 650 byte-identical to origin/main, plus one extra from Shaders/Bitcode: cache Shader::op declarations, primitive types, scalar constants, and zero-copy bitcode Writer #2665's new test (throwaway hashing patch, not committed).
  • Perf (release, combined vs base, 2 rounds each): shader::op 4096 calls 2.84-2.94 ms -> 0.53-0.56 ms; ir::build 2048 insts 2.24-2.29 ms -> 1.80-1.84 ms; 64 movrel insts 426-456 us -> 88-103 us; bitcode writer, 256x512 synthetic module, 8.9-9.8 ms -> 7.0-7.1 ms; BDD given n=13 ~465 us -> ~0.45 us (synthetic).
  • No interaction bugs found. The merge order does not matter (disjoint files).

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.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP + dxil.dll verification, code review, and same-machine --release benchmark comparison of 95f56a53 (on 13f91ca):

Verification (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0 dxil.dll)

Same-Machine --release Benchmark (structure::tests::bench_bools)

Case 13f91ca 95f56a53 Speedup
n=2, nodes=9 0.37 µs 0.12 µs 3.1x
n=4, nodes=45 1.05 µs 0.13 µs 8.1x
n=8, nodes=765 8.91 µs 0.14 µs 63.6x
n=10, nodes=3069 29.49 µs 0.24 µs 122.9x
n=12, nodes=12285 142.57 µs 0.25 µs 570.3x
n=13, nodes=24573 335.56 µs 0.26 µs 1,290.6x

Code Review Notes

  • Read-only implies_in (no Bools::nodes growth or memo churn), O(|known|) forced() sweep (TRUE/FALSE edge handling, TERMINAL = u32::MAX ordering, append-only given_with safety with cached_forced), and bounded FxHash refuted set in meet_in are strictly equivalent with zero unsafe. Clean and ready to merge.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (performance, same output). In crates/victory-shader/src/structure.rs:

  • Bools::implies becomes a read-only simultaneous descent with no node allocation.
  • The new Bools::forced finds the variables known decides in one walk of known.
  • Lanes::finish computes that once per block and reuses it for guards and exits (given_with).
  • meet_in uses the existing Fx hasher.

I checked forced against the reduced-OBDD argument: a variable is forced only if no reachable non-FALSE edge skips it and all its nodes take one polarity, with an edge into TRUE skipping everything below it. Restricting by forced variables that b does not contain is a no-op, so the result equals the old per-variable loop. The known == FALSE path follows hi edges like before, and the memo in implies_in is sound because a false result short-circuits the whole walk. The new tests compare forced/implies with the old formulations on random BDDs, and a tester reports byte-identical DXIL for all lib-test shaders. It merges cleanly; CI check: passing.


Generated by Claude Code

@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 merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --lib structure::tests: 15 passed, 0 failed, 1 ignored (including new forced_matches_implies_across_disjunctive_skips_and_random_bdds and read_only_implies_matches_and_not_without_allocating_nodes).
  • 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).

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…ep, read-only implies(), per-block reuse in finish(), and FxHash in meet_in()

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

Reviewed at 95f56a53f8ff90f700d74d102ad79538bad68771 — APPROVE.

Verification

  • cargo xtask policy (191 files ok) and cargo clippy -p victory-shader --all-targets --all-features -- -D warnings passed cleanly.
  • Verified forced(), read-only implies() / implies_in(), and given_with() semantics against the prior implementation: negations are ordinary OBDD nodes, skipped variables are suppressed via skip_until <= v (with Bool::TRUE edges yielding TERMINAL), implies() uses a fresh per-query Seen<(Bool, Bool)> without mutating self.memo or allocating nodes, and finish() resets last_guard per block and keys cached_forced on known with no cross-block bleed. meet_in() only performs membership/insertion on its FxHash Set, remaining order-independent.
  • Passed structure:: (15/15, 1 benchmark ignored) and emit::lower::tests (33/33).

Non-blocking notes

  • forced() calls entries.sort_unstable_by_key (crates/victory-shader/src/structure.rs:406), making its worst-case complexity O(|known| log |known|) rather than strictly O(|known|).
  • implies_in's local Seen set (structure.rs:366) has no cap (unlike MAX_MEMO on ite), and finish() (structure.rs:940, 954) can invoke forced_for even when the guard/branch target e or when is a terminal constant (Bool::TRUE/FALSE).
  • Minor rustfmt formatting differences in the new unit test (structure.rs:1994, 2008, 2024, 2049).

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.

6 participants