shader: hoist flat load/store runs within overhang, sibling handles, and single-dword ds_write guard #174

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

@⁠Mhyster on GitHub · 2026-12-02 00:04 UTC · original
Mhyster:opt/flat-load-run-and-shader-ir → main · +405 −33 · 5 files · 1 commits · open

What

Overlap note: Overlaps open PRs #169 (crates/victory-shader/src/emit/lower.rs) and #146 (crates/victory-shader/src/emit/lower.rs and crates/victory-shader/src/lib.rs TRANSLATOR_VERSION bump).

Reduces redundant NonUniform title-window descriptor lookups and conditional guard branches in victory-shader without changing console semantics, and bumps TRANSLATOR_VERSION from 13 to 14:

  1. flat_load_run + memory_loads (crates/victory-shader/src/emit/memory_flat.rs):
    • Derives OVERHANG = (FLAT_VIEW_BYTES - (1u64 << FLAT_SLICE_BITS)) as i32 directly from emit::shader::FLAT_VIEW_BYTES (rule 3, single source of truth).
    • Anchors memory_loads at min(offset) and omits the off-slice fix-up branch and extra self.memory(...) lookup for every part whose [off - off0, off - off0 + 4*n] lies within the 64 KiB FLAT_VIEW_BYTES overhang of the anchor's 2 GiB slice.
    • Extends flat_load_run across intervening non-memory-writing instructions (is_load_hoist_safe: s_nop, s_waitcnt*, pure v_* / s_* ALU, and read-only loads) in the same block while staying under the same guard, outside whole-wave sections, within OVERHANG, and verifying when guard.is_some() that no skipped intervening instruction defines an old destination VGPR SSA value read by the hoisted load's guarded write.
  2. flat_store_run + memory_store_run (crates/victory-shader/src/emit/memory_flat.rs, lower.rs):
    • Groups global_store_dword* to the same SSA address VGPR pair (across s_nop, s_waitcnt*, and pure ALU whose defs do not feed a store's data VGPRs, within OVERHANG, under the same guard) to share one writable title-window view and one begin_guarded block while preserving exact store order.
  3. Sibling-branch descriptor handle hoisting (crates/victory-shader/src/emit/bitcode/opt.rs):
    • Adds Module::hoist_sibling_handles under REUSE (up to 6 fixed-point rounds in Module::optimise): when both arms of CondBr(_, t, f) (each with single predecessor b) construct the identical pure dx.op.createHandleFromHeap or dx.op.annotateHandle from operands dominating b, hoists one copy into b so the next merge deduplicates both arms.
  4. Single-dword ds_write guard merge (crates/victory-shader/src/emit/lower.rs):
    • Combines guard and LDS bounds check inside via both(self, guard, inside) into one begin_guarded block when stores.len() == 1 (matching ds_write_b8/b16).

The reference

No direct vkd3d-proton or crates/victory-gpu counterpart (this is our native GFX10 → DXIL lowering and IR pass in victory-shader). The 64 KiB slice overhang relies on emit::shader::FLAT_VIEW_BYTES (crates/victory-shader/src/emit/shader.rs) and greenfield/vkback/src/flatviews.rs (view_range), which are kept in sync by flatviews::tests::flat_views_match_the_translator.

Evidence

  • Machine: Linux (CachyOS), Ryzen 5 9600X + AMD RX 9070 XT (Mesa RADV 26.3.1).
  • TRANSLATOR_VERSION bumped 13 → 14 (crates/victory-shader/src/lib.rs).
  • Whole-store DXIL diff (vktory6/main vs this branch, all 19,161 containers in source.sqlite via translate-dir):
    • 19,161 / 19,161 translated, 0 failures on both base and branch.
    • 219 / 19,161 containers changed (all 219 in CS).
    • dxil-probe + spirv-val --target-env vulkan1.3 passed 20/20 on the 20 largest changed CS shaders (OpBranchConditional: 31,359 → 31,094, −265).
    • Ray-tracing compute walkers:
      • CS/f22a76fc: DXIL 107,080 → 94,892 B (−11.4%); SPIR-V NonUniform 732 → 624 (−108), OpLoad 566 → 526 (−40), OpBranchConditional 419 → 346 (−73), spirv-val OK.
      • CS/3592426e: DXIL 200,652 → 179,884 B (−20,768 B).
      • CS/b58eb3ca: DXIL 194,824 → 174,036 B (−20,788 B).
      • CS/6d7fe774: DXIL 220,180 → 204,028 B (−16,152 B).
      • CS/a1b20bc4: DXIL 126,588 → 126,568 B (OpBranchConditional 341 → 337), spirv-val OK.
  • Offline tests and CI checks run and passing:
    • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked (all passed, including new consecutive_stores_share_one_view, updated a_load_that_writes_the_address_ends_the_run, and sibling_handles_hoist_to_branch_block).
    • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked) (all passed).
    • Clippy with --locked -- -D warnings: native root crates, greenfield, greenfield/wine, and greenfield/vkback/translate all clean.
  • Checks NOT run in this PR (GPU / live game runs were paused because another agent was actively using the game/GPU on this machine):
    • Windows-only on-card victory-shader tests under Proton (tests-shwin.sh).
    • Boat scene A B B A performance measurement (VKB_LAT + greenfield/tools/frec/frames.py).
    • House scene ~60 s Vulkan validation run (VK_LAYER_KHRONOS_validation).
    • PE-side cargo xwin clippy (CI will verify).

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
**@⁠Mhyster** on GitHub · 2026-12-02 00:04 UTC · [original](https://github.com/vktory6/VKtory/pull/174) `Mhyster:opt/flat-load-run-and-shader-ir` → `main` · +405 −33 · 5 files · 1 commits · open ## What > **Overlap note:** Overlaps open PRs #169 (`crates/victory-shader/src/emit/lower.rs`) and #146 (`crates/victory-shader/src/emit/lower.rs` and `crates/victory-shader/src/lib.rs` `TRANSLATOR_VERSION` bump). Reduces redundant `NonUniform` title-window descriptor lookups and conditional guard branches in `victory-shader` without changing console semantics, and bumps `TRANSLATOR_VERSION` from `13` to `14`: 1. **`flat_load_run` + `memory_loads` (`crates/victory-shader/src/emit/memory_flat.rs`)**: - Derives `OVERHANG = (FLAT_VIEW_BYTES - (1u64 << FLAT_SLICE_BITS)) as i32` directly from `emit::shader::FLAT_VIEW_BYTES` (rule 3, single source of truth). - Anchors `memory_loads` at `min(offset)` and omits the off-slice fix-up branch and extra `self.memory(...)` lookup for every part whose `[off - off0, off - off0 + 4*n]` lies within the 64 KiB `FLAT_VIEW_BYTES` overhang of the anchor's 2 GiB slice. - Extends `flat_load_run` across intervening non-memory-writing instructions (`is_load_hoist_safe`: `s_nop`, `s_waitcnt*`, pure `v_*` / `s_*` ALU, and read-only loads) in the same block while staying under the same guard, outside whole-wave sections, within `OVERHANG`, and verifying when `guard.is_some()` that no skipped intervening instruction defines an old destination VGPR SSA value read by the hoisted load's guarded `write`. 2. **`flat_store_run` + `memory_store_run` (`crates/victory-shader/src/emit/memory_flat.rs`, `lower.rs`)**: - Groups `global_store_dword*` to the same SSA address VGPR pair (across `s_nop`, `s_waitcnt*`, and pure ALU whose defs do not feed a store's data VGPRs, within `OVERHANG`, under the same guard) to share one writable title-window view and one `begin_guarded` block while preserving exact store order. 3. **Sibling-branch descriptor handle hoisting (`crates/victory-shader/src/emit/bitcode/opt.rs`)**: - Adds `Module::hoist_sibling_handles` under `REUSE` (up to 6 fixed-point rounds in `Module::optimise`): when both arms of `CondBr(_, t, f)` (each with single predecessor `b`) construct the identical pure `dx.op.createHandleFromHeap` or `dx.op.annotateHandle` from operands dominating `b`, hoists one copy into `b` so the next `merge` deduplicates both arms. 4. **Single-dword `ds_write` guard merge (`crates/victory-shader/src/emit/lower.rs`)**: - Combines `guard` and LDS bounds check `inside` via `both(self, guard, inside)` into one `begin_guarded` block when `stores.len() == 1` (matching `ds_write_b8`/`b16`). ## The reference No direct vkd3d-proton or `crates/victory-gpu` counterpart (this is our native GFX10 → DXIL lowering and IR pass in `victory-shader`). The 64 KiB slice overhang relies on `emit::shader::FLAT_VIEW_BYTES` (`crates/victory-shader/src/emit/shader.rs`) and `greenfield/vkback/src/flatviews.rs` (`view_range`), which are kept in sync by `flatviews::tests::flat_views_match_the_translator`. ## Evidence - Machine: Linux (CachyOS), Ryzen 5 9600X + AMD RX 9070 XT (Mesa RADV 26.3.1). - `TRANSLATOR_VERSION` bumped `13` → `14` (`crates/victory-shader/src/lib.rs`). - **Whole-store DXIL diff (`vktory6/main` vs this branch, all 19,161 containers in `source.sqlite` via `translate-dir`)**: - `19,161 / 19,161` translated, **0 failures** on both base and branch. - **219 / 19,161** containers changed (all 219 in `CS`). - `dxil-probe` + `spirv-val --target-env vulkan1.3` passed **20/20** on the 20 largest changed CS shaders (`OpBranchConditional`: `31,359 → 31,094`, `−265`). - Ray-tracing compute walkers: - `CS/f22a76fc`: DXIL `107,080 → 94,892 B` (`−11.4%`); SPIR-V `NonUniform` `732 → 624` (`−108`), `OpLoad` `566 → 526` (`−40`), `OpBranchConditional` `419 → 346` (`−73`), `spirv-val` OK. - `CS/3592426e`: DXIL `200,652 → 179,884 B` (`−20,768 B`). - `CS/b58eb3ca`: DXIL `194,824 → 174,036 B` (`−20,788 B`). - `CS/6d7fe774`: DXIL `220,180 → 204,028 B` (`−16,152 B`). - `CS/a1b20bc4`: DXIL `126,588 → 126,568 B` (`OpBranchConditional` `341 → 337`), `spirv-val` OK. - Offline tests and CI checks run and passing: - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked` (all passed, including new `consecutive_stores_share_one_view`, updated `a_load_that_writes_the_address_ends_the_run`, and `sibling_handles_hoist_to_branch_block`). - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)` (all passed). - Clippy with `--locked -- -D warnings`: native root crates, `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate` all clean. - **Checks NOT run in this PR** (GPU / live game runs were paused because another agent was actively using the game/GPU on this machine): - Windows-only on-card `victory-shader` tests under Proton (`tests-shwin.sh`). - Boat scene A B B A performance measurement (`VKB_LAT` + `greenfield/tools/frec/frames.py`). - House scene ~60 s Vulkan validation run (`VK_LAYER_KHRONOS_validation`). - PE-side `cargo xwin clippy` (CI will verify). ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 00:10:15 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 15:28 UTC · original

Thanks, the logic looks sound where we traced it: the overhang reasoning matches flatviews::view_range, and the hoist safety excludes stores, atomics, barriers and branches. But it is four translator topics in one PR (longer flat-load runs, store runs, sibling-handle hoisting, the single-dword ds_write merge), and translator changes have the highest bar we have. Please send one PR per topic, each with:

  • the on-card tests (tests-shwin.sh);
  • a validation run;
  • an A B B A boat run, since it's a speed change;
  • tests for the rule-22 class (every instruction that writes EXEC or memory).

Heads-up: #198 and #206 also bump TRANSLATOR_VERSION, so whichever lands later bumps it again.

**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/174#issuecomment-7350385052) Thanks, the logic looks sound where we traced it: the overhang reasoning matches `flatviews::view_range`, and the hoist safety excludes stores, atomics, barriers and branches. But it is four translator topics in one PR (longer flat-load runs, store runs, sibling-handle hoisting, the single-dword `ds_write` merge), and translator changes have the highest bar we have. Please send one PR per topic, each with: - the on-card tests (`tests-shwin.sh`); - a validation run; - an A B B A boat run, since it's a speed change; - tests for the rule-22 class (every instruction that writes EXEC or memory). Heads-up: #198 and #206 also bump `TRANSLATOR_VERSION`, so whichever lands later bumps it again.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/lib.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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