test(shader): check the memory and barrier passes on thread groups (on top of #41) #89

Open
github-bot wants to merge 3 commits from github-bot/gh-89 into beta AGit
Owner

@⁠Kal0s-design on GitHub · 2026-11-26 16:33 UTC · original
Kal0s-design:test/opt-thread-group → beta · +5487 −89 · 2 files · 3 commits · open

Test-only, stacked on #41 as suggested there. Only the last commit (a8ce914) is new; the two before it are #41's. Once #41 lands this is one file, crates/victory-shader/src/emit/bitcode/opt.rs, all inside mod tests.

Why

every_pass_leaves_what_random_programs_store never makes a load, a store or a barrier, and its interpreter panics on them. So LOADS, the loads LICM hoists and BARRIERS (adjacent and read-after-read) were covered only by the hand-written cases.

What

mod thread_group, two tests:

  • passes_keep_what_a_thread_group_computes builds random structured programs in the style of Gen (branches, short loops, joins) with two private arrays, two LDS arrays and dx.op.barrier at the top level only. Half the phases are straight-line, so several barriers share a block. An interpreter runs a group of four threads that can stop after any instruction, under six schedules (three fixed orders, round robin, two random interleavings). The LDS accesses are race free by construction: between two barriers each LDS array is only written at the thread's own slots, only used at its own slots, or only read. So the plain program gives one result under every schedule (the test asserts that none is skipped), and each of 11 pass masks has to give the same per-thread output stream and final LDS contents under every schedule. It also asserts that ALL removed some barriers and memory operations, so it does not pass on programs the passes ignore.
  • a_barrier_between_reads_and_an_own_store_stays is the hand-built shape the read-after-read rule must leave alone: reads of other threads' slots, a barrier, a store to the thread's own slot, more reads, a barrier.

VICTORY_OPT_GROUP_SEEDS runs more programs than the default 60. The two tests take about 3.5 s in a debug build.

Checked

  • On 690b4b2, 30,000 programs with no difference.
  • Planted bugs, one at a time, each caught by the default 60 programs: BarrierState::reset keeping rar (both tests fail; the hand-built case goes from 3 barriers to 2), a dynamic private store not clearing constant-index values (LOADS, seed 25), can_hoist taking any load (LICM, seed 35).
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings, with and without --all-features: clean. cargo test -p victory-shader --lib --locked: 239 passed, 4 ignored. xtask policy on this branch reports the same four files as on #41's base (lower.rs size, two files under docs/klbf, tests/check_cold_cache.py), none touched here. Windows only.

Not covered

  • Fence bits on their own. A barrier with the group sync bit is taken as making every store visible, so a rule that keeps the sync but loses a TGSM or UAV fence bit would not fail this.
  • Forwarding an LDS value across a barrier. In race-free code without branching between the two loads every thread runs the same stores, and the dynamic own-slot store already clears the cache, so a missing clear at the barrier cannot be seen this way.
  • Barriers inside divergent control flow, float arithmetic, and anything after the bitcode.

AI-assisted: yes (Claude Code).

🤖 Generated with Claude Code

**@⁠Kal0s-design** on GitHub · 2026-11-26 16:33 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/89) `Kal0s-design:test/opt-thread-group` → `beta` · +5487 −89 · 2 files · 3 commits · open Test-only, stacked on #41 as suggested there. Only the last commit (a8ce914) is new; the two before it are #41's. Once #41 lands this is one file, `crates/victory-shader/src/emit/bitcode/opt.rs`, all inside `mod tests`. ### Why `every_pass_leaves_what_random_programs_store` never makes a load, a store or a barrier, and its interpreter panics on them. So `LOADS`, the loads `LICM` hoists and `BARRIERS` (adjacent and read-after-read) were covered only by the hand-written cases. ### What `mod thread_group`, two tests: - `passes_keep_what_a_thread_group_computes` builds random structured programs in the style of `Gen` (branches, short loops, joins) with two private arrays, two LDS arrays and `dx.op.barrier` at the top level only. Half the phases are straight-line, so several barriers share a block. An interpreter runs a group of four threads that can stop after any instruction, under six schedules (three fixed orders, round robin, two random interleavings). The LDS accesses are race free by construction: between two barriers each LDS array is only written at the thread's own slots, only used at its own slots, or only read. So the plain program gives one result under every schedule (the test asserts that none is skipped), and each of 11 pass masks has to give the same per-thread output stream and final LDS contents under every schedule. It also asserts that `ALL` removed some barriers and memory operations, so it does not pass on programs the passes ignore. - `a_barrier_between_reads_and_an_own_store_stays` is the hand-built shape the read-after-read rule must leave alone: reads of other threads' slots, a barrier, a store to the thread's own slot, more reads, a barrier. `VICTORY_OPT_GROUP_SEEDS` runs more programs than the default 60. The two tests take about 3.5 s in a debug build. ### Checked - On 690b4b2, 30,000 programs with no difference. - Planted bugs, one at a time, each caught by the default 60 programs: `BarrierState::reset` keeping `rar` (both tests fail; the hand-built case goes from 3 barriers to 2), a dynamic private store not clearing constant-index values (`LOADS`, seed 25), `can_hoist` taking any load (`LICM`, seed 35). - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`, with and without `--all-features`: clean. `cargo test -p victory-shader --lib --locked`: 239 passed, 4 ignored. `xtask policy` on this branch reports the same four files as on #41's base (`lower.rs` size, two files under `docs/klbf`, `tests/check_cold_cache.py`), none touched here. Windows only. ### Not covered - Fence bits on their own. A barrier with the group sync bit is taken as making every store visible, so a rule that keeps the sync but loses a TGSM or UAV fence bit would not fail this. - Forwarding an LDS value across a barrier. In race-free code without branching between the two loads every thread runs the same stores, and the dynamic own-slot store already clears the cache, so a missing clear at the barrier cannot be seen this way. - Barriers inside divergent control flow, float arithmetic, and anything after the bitcode. AI-assisted: yes (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Extend bitcode::opt with four exact, bit-preserving DXIL optimization passes:
- Extended FOLD & canonical Cmp/FACTS: integer algebraic/bitwise/shift identities
  and constant reassociation, power-of-two strength reduction, compare negation
  (guarding against AMD fcmp one), boolean select/zext/compare-of-select folding,
  and canonicalized Key::Cmp/FACTS lookups preserving IEEE-754 NaN semantics.
- Pure LICM (bit 64, in MODULE/ALL): natural loop detection via dominator tree,
  innermost-first fixed-point hoisting to existing or lazily split dominating
  preheaders, strict unconditional single-pred Term::Br guaranteed-execution
  prefix with WriteKind::All (dx.op.discard) tracking, pure value-only math
  allowlist for speculative calls, and trace_ptr-validated constant-global loads.
- Pointer-root & non-aliasing memory forwarding/DSE (enhanced LOADS, bit 16):
  canonical signed/in-bounds scalar-leaf trace_ptr GEP paths and escape analysis
  for constant tables, thread-private space-0 globals, shared space-3 LDS, and
  DXIL resource memory; store-to-load forwarding, non-aliasing preservation
  across unrelated stores/barriers, and intra-block overwritten DSE.
- Adjacent redundant dx.op.barrier(mode) elimination (bit 128, in MODULE/ALL):
  drops redundant barriers covered by an earlier barrier's mode bits in the same
  block with no intervening memory or lane operations.
- Add test-only thread-local shader_opt override (with_shader_opt) in emit/mod.rs
  while preserving translation_fingerprint, plus unit, soundness-regression, and
  DXIL validation / hardware GPU parity tests.
Drop intra-block middle dx.op.barrier(80, mode) calls between consecutive non-escaping LDS (addrspace 3) read phases when bounded on the left by a covering group+TGSM barrier and on the right by either a covering group+TGSM barrier or shader return, with no intervening stores, atomics, resource/wave calls, or branches.
every_pass_leaves_what_random_programs_store makes no load, store or
barrier, so LOADS, the loads LICM hoists, and BARRIERS (adjacent and
read-after-read) were checked only by hand-written cases.

thread_group adds random structured programs with two private arrays,
two LDS arrays and top-level barriers, and an interpreter for a group of
four threads that can stop after any instruction. Each program runs
under six schedules; its LDS accesses are race free by construction, so
the plain program gives one result under all of them and every pass mask
must give the same one. A barrier dropped where it was needed shows up
as two schedules that disagree.

a_barrier_between_reads_and_an_own_store_stays pins the shape the
read-after-read rule must leave alone.

Planted bugs it catches within the default 60 programs: BarrierState
reset keeping the read-after-read state (both tests), a dynamic private
store not invalidating constant-index values (LOADS), and can_hoist
taking any load (LICM). 30,000 programs on 690b4b2 found no difference.

Not covered: fence bits on their own, barriers in divergent control
flow, float arithmetic.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:33:49 +00:00
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 16:36 UTC · original

Thanks @⁠Kal0s-design, quick turnaround. Stacking it on #41 keeps the review simple; we will take it right after #41 lands, and if you rebase it then so it shows only opt.rs, that is all it needs.

**@⁠PastiiJay** on GitHub · 2026-11-26 16:36 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/89#issuecomment-7197873409) Thanks @⁠Kal0s-design, quick turnaround. Stacking it on #41 keeps the review simple; we will take it right after #41 lands, and if you rebase it then so it shows only `opt.rs`, that is all it needs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/89/head:github-bot/gh-89'
git switch 'github-bot/gh-89'

Merge

Merge the changes and update on Forgejo.
git switch 'beta'
git merge --no-ff 'github-bot/gh-89'
git switch 'github-bot/gh-89'
git rebase 'beta'
git switch 'beta'
git merge --ff-only 'github-bot/gh-89'
git switch 'github-bot/gh-89'
git rebase 'beta'
git switch 'beta'
git merge --no-ff 'github-bot/gh-89'
git switch 'beta'
git merge --squash 'github-bot/gh-89'
git switch 'beta'
git merge --ff-only 'github-bot/gh-89'
git switch 'beta'
git merge 'github-bot/gh-89'
git push origin 'beta'
This discussion has been locked. Commenting is limited to contributors.
No description provided.