Shaders: guard speculative and EXEC-predicated heap descriptor loads (#1595) #1674

Closed
github-bot wants to merge 1 commit from gh-pr/1674/head into gh-pr/1674/base
Owner

@⁠ESAT586 on GitHub · 2026-11-24 20:40 UTC · original
ESAT586:shader/guard-speculative-heap-loads → main · +1517 −191 · 4 files · 1 commits · closed without merging

AI Authorship & Clean-Room Disclosure: This PR was authored and hardware-verified by Gemini 4 Neon (Extra High Mode) as an AI coding agent, developed strictly clean-room with zero game files or external dumps.

Summary

Fixes #1595 without hardcoding any slot-0 convention in the translator (so a title or test with a valid CBV in heap slot 0 remains 100% intact).

Root Cause

On Xbox Series (RDNA2), an all-zero/null descriptor in heap slot 0 returns zeros for any buffer/texture read without faulting, so the console compiler regularly hoists heap descriptor reads (s_load_dwordx4 + s_buffer_load_dword*, or buffer_load_*) ahead of the branch, s_cselect, v_cndmask_b32, or EXEC-masked consumer that checks whether the index is valid (e.g. idx != 0). On PC D3D12, executing createHandleFromHeap + cbufferLoadLegacy over an SRV slot (such as [heap 0: Shader(0, Some([42, 2, 5768, ...])) -> null], a null Texture1D SRV) triggers GPU-based validation (Descriptor Type in Heap: SRV, Register Type: CBV) and removes the device on WARP (0x887A0005).

Similarly, EXEC-guarded vector buffer_load* / image_load* (and store/atomic handle creation) previously emitted createHandleFromHeap and loads unconditionally before selecting on guard, allowing inactive lanes (including Source::LanePhi's synthesized index 0) to hit an incompatible descriptor.

Changes

  1. Exact speculative heap-load guarding (crates/victory-shader/src/emit/lower.rs):

    • Added Lower::speculative_guard + Lower::dead_when_false for heap-indexed loads (smem, buffer, and explicit image_load*).
    • Discovers candidate guard conditions from:
      • the current block's exit branch (LaneExit::Branch),
      • downstream s_cselect_b32 / s_cselect_b64 / v_cndmask_b32,
      • downstream EXEC guards (st.guards[u][j]) reachable through pure lane-local ALU instructions and loop-invariant data phis.
    • Verifies via dead_when_false that whenever a condition is false, replacing the load outputs with zero cannot affect any observable instruction, cross-lane op, preserved VGPR old value, branch, or side effect.
    • Hoists late same-block pure compares (s_cmp*, s_cmpk*, s_bitcmp*, v_cmp*) once when all of their inputs are already available before the load.
    • Conjoins all proven non-tautological guards (C1 && C2) so a weaker or waterfall-only EXEC guard cannot shadow a stronger downstream v_cndmask_b32 / s_cselect / branch guard.
    • Branches around createHandleFromHeap + the load via begin_guarded / end_guarded, closing guarded blocks cleanly on any lowering error.
  2. EXEC-guarded handle creation & loads (crates/victory-shader/src/emit/lower.rs):

    • Moved self.handle(...) and loads inside begin_guarded(guard)..end_guarded(...) for buffer loads (CBV, format, typed, raw, structured), image_load*, buffer_store, buffer_atomic, and image_store when guard.is_some() (implicit-derivative image_sample* is intentionally untouched to preserve pixel quad derivatives).
  3. Scalar s_buffer_load_dword* inside console waterfall loops (crates/victory-shader/src/resource.rs):

    • Fields::Smem loads do not list Reg::S(EXEC) in inst.reads; Tracer::waterfall now resolves their active EXEC mask from the most recent preceding EXEC write in the waterfall block, so per-lane CBV waterfalls resolve each lane's own index instead of reading lane 0's 0 index.
  4. End-to-end tests (crates/victory-shader/src/emit/host.rs, crates/victory-shader/src/emit/waterfall_tests.rs):

    • Added View::NullTexture1dSrv (DXGI_FORMAT_R32_UINT, D3D12_SRV_DIMENSION_TEXTURE1D, null resource — matching #1595's slot 0).
    • Added 5 clean-room end-to-end tests (validated with dxil.dll, executed on both hardware GPU and WARP):
      • speculative_heap_cbv_before_branch_skips_null_srv_at_slot_0 (#1595 direct reproducer: rawBufferLoad -> s_cmp_lg_u32 -> speculative s_buffer_load_dword -> s_cbranch_scc0)
      • speculative_heap_cbv_before_late_cmp_and_cselect_skips_null_srv_at_slot_0 (pure ALU + late s_cmp_lg_u32 after load + s_cselect_b32)
      • divergent_cndmask_guards_per_lane_heap_cbv_from_null_srv_at_slot_0 (32-lane mixed wave waterfall + v_cndmask_b32 where even lanes have idx = 0 pointing at null Texture1D SRV and odd lanes read CBV slot 1 — reproduced WARP 0x887A0005 removal before the fix and passes cleanly after)
      • live_heap_cbv_at_slot_0_is_not_skipped_even_when_index_compared_to_zero (soundness control: valid CBV at slot 0 is still read when live)
      • exec_guarded_buffer_load_skips_inactive_lanes_on_null_srv_at_slot_0 (straight-line s_and_saveexec_b32 without s_cbranch_execz)

Verification

All tests are synthetic clean-room tests (no game files):

  • NVIDIA GeForce RTX 3060 Ti + Microsoft WARP + dxil.dll (cargo test -p victory-shader --locked): 125 passed, 0 failed, 1 ignored
  • Workspace Clippy (cargo clippy --workspace --all-targets --locked -- -D warnings, default & --all-features): 0 warnings
  • Asset policy (cargo xtask policy): 154 files ok

Fixes #1595.

**@⁠ESAT586** on GitHub · 2026-11-24 20:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/1674) `ESAT586:shader/guard-speculative-heap-loads` → `main` · +1517 −191 · 4 files · 1 commits · closed without merging > **AI Authorship & Clean-Room Disclosure:** This PR was authored and hardware-verified by **Gemini 4 Neon (Extra High Mode)** as an AI coding agent, developed strictly clean-room with **zero game files or external dumps**. ## Summary Fixes #1595 without hardcoding any slot-0 convention in the translator (so a title or test with a valid CBV in heap slot 0 remains 100% intact). ### Root Cause On Xbox Series (RDNA2), an all-zero/null descriptor in heap slot 0 returns zeros for any buffer/texture read without faulting, so the console compiler regularly hoists heap descriptor reads (`s_load_dwordx4` + `s_buffer_load_dword*`, or `buffer_load_*`) ahead of the branch, `s_cselect`, `v_cndmask_b32`, or `EXEC`-masked consumer that checks whether the index is valid (e.g. `idx != 0`). On PC D3D12, executing `createHandleFromHeap` + `cbufferLoadLegacy` over an SRV slot (such as `[heap 0: Shader(0, Some([42, 2, 5768, ...])) -> null]`, a null Texture1D SRV) triggers GPU-based validation (`Descriptor Type in Heap: SRV, Register Type: CBV`) and removes the device on WARP (`0x887A0005`). Similarly, EXEC-guarded vector `buffer_load*` / `image_load*` (and store/atomic handle creation) previously emitted `createHandleFromHeap` and loads unconditionally before selecting on `guard`, allowing inactive lanes (including `Source::LanePhi`'s synthesized index `0`) to hit an incompatible descriptor. ### Changes 1. **Exact speculative heap-load guarding (`crates/victory-shader/src/emit/lower.rs`):** - Added `Lower::speculative_guard` + `Lower::dead_when_false` for heap-indexed loads (`smem`, `buffer`, and explicit `image_load*`). - Discovers candidate guard conditions from: - the current block's exit branch (`LaneExit::Branch`), - downstream `s_cselect_b32` / `s_cselect_b64` / `v_cndmask_b32`, - downstream `EXEC` guards (`st.guards[u][j]`) reachable through pure lane-local ALU instructions and loop-invariant data phis. - Verifies via `dead_when_false` that whenever a condition is false, replacing the load outputs with zero cannot affect any observable instruction, cross-lane op, preserved VGPR old value, branch, or side effect. - Hoists late same-block pure compares (`s_cmp*`, `s_cmpk*`, `s_bitcmp*`, `v_cmp*`) once when all of their inputs are already available before the load. - Conjoins all proven non-tautological guards (`C1 && C2`) so a weaker or waterfall-only EXEC guard cannot shadow a stronger downstream `v_cndmask_b32` / `s_cselect` / branch guard. - Branches around `createHandleFromHeap` + the load via `begin_guarded` / `end_guarded`, closing guarded blocks cleanly on any lowering error. 2. **EXEC-guarded handle creation & loads (`crates/victory-shader/src/emit/lower.rs`):** - Moved `self.handle(...)` and loads inside `begin_guarded(guard)`..`end_guarded(...)` for `buffer` loads (CBV, format, typed, raw, structured), `image_load*`, `buffer_store`, `buffer_atomic`, and `image_store` when `guard.is_some()` (implicit-derivative `image_sample*` is intentionally untouched to preserve pixel quad derivatives). 3. **Scalar `s_buffer_load_dword*` inside console waterfall loops (`crates/victory-shader/src/resource.rs`):** - `Fields::Smem` loads do not list `Reg::S(EXEC)` in `inst.reads`; `Tracer::waterfall` now resolves their active `EXEC` mask from the most recent preceding `EXEC` write in the waterfall block, so per-lane CBV waterfalls resolve each lane's own index instead of reading lane 0's `0` index. 4. **End-to-end tests (`crates/victory-shader/src/emit/host.rs`, `crates/victory-shader/src/emit/waterfall_tests.rs`):** - Added `View::NullTexture1dSrv` (`DXGI_FORMAT_R32_UINT`, `D3D12_SRV_DIMENSION_TEXTURE1D`, null resource — matching #1595's slot 0). - Added 5 clean-room end-to-end tests (validated with `dxil.dll`, executed on both hardware GPU and WARP): - `speculative_heap_cbv_before_branch_skips_null_srv_at_slot_0` (#1595 direct reproducer: `rawBufferLoad` -> `s_cmp_lg_u32` -> speculative `s_buffer_load_dword` -> `s_cbranch_scc0`) - `speculative_heap_cbv_before_late_cmp_and_cselect_skips_null_srv_at_slot_0` (pure ALU + late `s_cmp_lg_u32` after load + `s_cselect_b32`) - `divergent_cndmask_guards_per_lane_heap_cbv_from_null_srv_at_slot_0` (32-lane mixed wave waterfall + `v_cndmask_b32` where even lanes have `idx = 0` pointing at null Texture1D SRV and odd lanes read CBV slot 1 — reproduced WARP `0x887A0005` removal before the fix and passes cleanly after) - `live_heap_cbv_at_slot_0_is_not_skipped_even_when_index_compared_to_zero` (soundness control: valid CBV at slot 0 is still read when live) - `exec_guarded_buffer_load_skips_inactive_lanes_on_null_srv_at_slot_0` (straight-line `s_and_saveexec_b32` without `s_cbranch_execz`) ## Verification All tests are synthetic clean-room tests (no game files): - **NVIDIA GeForce RTX 3060 Ti + Microsoft WARP + `dxil.dll`** (`cargo test -p victory-shader --locked`): **125 passed, 0 failed, 1 ignored** - **Workspace Clippy** (`cargo clippy --workspace --all-targets --locked -- -D warnings`, default & `--all-features`): **0 warnings** - **Asset policy** (`cargo xtask policy`): **154 files ok** Fixes #1595.
Authored and verified by Gemini 4 Neon (Extra High Mode) as an AI coding
agent, strictly clean-room with zero game files or external dumps.

Fixes #1595 without assuming any convention about heap slot 0:

1. Speculative heap descriptor loads (`emit/lower.rs`):
   On RDNA2, reading a null descriptor (such as a title's null Texture1D
   SRV in heap slot 0) through `s_buffer_load_dword*` or `buffer_load_*`
   is fault-free and returns 0, so the console compiler frequently hoists
   `s_load_dwordx4` + `s_buffer_load_dword*` ahead of `s_cbranch_*`,
   `s_cselect_b32`, `v_cndmask_b32`, or an EXEC-guarded consumer. On PC
   D3D12, executing `createHandleFromHeap` + `cbufferLoadLegacy` over an
   SRV slot is undefined behaviour (GBV mismatch and WARP `0x887A0005`
   device removal in #1595).

   Add a sound, semantics-preserving `speculative_guard` analysis in
   `Lower` for heap-indexed loads (`smem`, `buffer`, and explicit
   `image_load*`):
   - Collects candidate conditions from the block's exit branch,
     downstream `s_cselect_b32`/`s_cselect_b64`/`v_cndmask_b32`, and
     downstream `EXEC` guards reachable through pure lane-local ops and
     loop-invariant data phis.
   - Proves via `dead_when_false` that whenever a candidate condition is
     false, replacing the load outputs with zero cannot affect any
     observable value, branch, cross-lane op, or side effect (so a real
     live CBV at slot 0 is never skipped even when `idx != 0` is tested
     for a separate expression).
   - Supports late same-block pure compares (`s_cmp*`/`s_cmpk*`/
     `s_bitcmp*`/`v_cmp*`) whose operands are already available before
     the load by hoisting them once into the guarding block.
   - Conjoins all proven non-tautological guards so a weaker or
     waterfall-only EXEC guard never shadows a stronger downstream
     `v_cndmask_b32`/`s_cselect`/branch guard.

2. EXEC-predicated loads and handle creation (`emit/lower.rs`):
   When `self.guard(b, i)` is `Some(g)` on `buffer` loads (CBV, format,
   typed, raw, structured), `image_load*`, `buffer_store`, `buffer_atomic`,
   and `image_store`, move `self.handle(...)` and the load inside
   `begin_guarded(guard)`..`end_guarded(...)` (leaving implicit-derivative
   `image_sample*` untouched so pixel quad derivatives are unaffected).

3. Waterfall tracking for scalar `s_buffer_load_dword*` (`resource.rs`):
   In `Tracer::waterfall`, `Fields::Smem` loads do not carry `Reg::S(EXEC)`
   in `inst.reads`; derive their active `EXEC` mask from the most recent
   `EXEC` write preceding the load in the waterfall block so per-lane
   CBV waterfalls resolve each lane's own index instead of lane 0's.

Verification (clean-room synthetic tests, zero game data):
- `cargo test -p victory-shader --locked`: 125 passed, 0 failed, 1 ignored
  (5 new end-to-end tests in `emit/waterfall_tests.rs` validated with
  `dxil.dll` and executed on NVIDIA GeForce RTX 3060 Ti AND Microsoft WARP
  with a null Texture1D SRV in heap slot 0).
- `cargo clippy --workspace --all-targets --locked -- -D warnings` (default
  and `--all-features`): 0 warnings.
- `cargo xtask policy`: 154 files ok.

Fixes #1595.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:41:04 +00:00
github-bot closed this pull request 2026-11-26 04:41:04 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:24 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

1,500 lines guarding speculative heap loads to avoid a CBV handle over a null SRV at slot 0 (#1595); main removed that mismatch at the source: heap-direct constant data is read through raw views and a zero view-stride entry selects a null buffer view (lower.rs heap_constants, d71c3889).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1674#issuecomment-7149508537) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. 1,500 lines guarding speculative heap loads to avoid a CBV handle over a null SRV at slot 0 (#1595); main removed that mismatch at the source: heap-direct constant data is read through raw views and a zero view-stride entry selects a null buffer view (lower.rs `heap_constants`, d71c3889). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:34 UTC · original

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/host.rs, crates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1674#issuecomment-7149698388) <!-- victory-triage v1 head=b9d3e8364e61f1a672930da52969d5b4931914b5 verdict=needs-rebase at=2026-11-25T01:34:11Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/host.rs`, `crates/victory-shader/src/emit/lower.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_

Pull request closed

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!1674
No description provided.