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

Closed
ESAT586 wants to merge 1 commit from upstream-pr/1674 into main
ESAT586 commented 2026-11-24 20:40:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-24T20:40:52Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6590987783 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1674). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-24T20:40:52Z. --- > **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.
catminou4 commented 2026-11-24 22:01:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T21:40:13Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests.

Speculative and EXEC-predicated heap descriptor loads are guarded (#1595): a lazy SSA use index (uses/UseSite), pure compares hoisted ahead of the load (hoisted set), GuardCond Bool/Scc conditions, plus a null 1D texture SRV view matching a title's null descriptor in heap slot 0. 1897-line diff; macOS compile is clean but the guard logic is exercised by Windows hardware tests — please make sure those ran before merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6047849963 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1674#pullrequestreview-6047849963). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T21:40:13Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests. Speculative and EXEC-predicated heap descriptor loads are guarded (#1595): a lazy SSA use index (uses/UseSite), pure compares hoisted ahead of the load (hoisted set), GuardCond Bool/Scc conditions, plus a null 1D texture SRV view matching a title's null descriptor in heap slot 0. 1897-line diff; macOS compile is clean but the guard logic is exercised by Windows hardware tests — please make sure those ran before merge.
upstream-sync closed this pull request 2026-11-25 02:04:53 +00:00
Marvisak commented 2026-11-25 02:05:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:24:12Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149508537 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1674#issuecomment-7149508537). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:24:12Z. --- 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 commented 2026-11-25 02:06:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149698388 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1674#issuecomment-7149698388). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:34:11Z. --- <!-- 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

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
Marvisak/VIctory!1673
No description provided.