Shaders: trace multi-term, scaled, masked, and phi-joined addresses in resource tracking #645

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

@⁠nathanbzrr on GitHub · 2026-11-24 06:06 UTC · original
nathanbzrr:shader/resource-address-tracing → main · +2388 −117 · 3 files · 1 commits · closed without merging

Summary

Fixes five resource-address tracking failures in crates/victory-shader/src/resource.rs and adds SSA-dominance-safe relative offset lowering in crates/victory-shader/src/emit/lower.rs blocking compute, raytracing/BVH, and multi-term buffer shaders:

  1. Defect A: RootType::Constants dword 0 was treated as a pointer base whenever param_of saw dword 0, colliding with real base pointers ("an address not from a root parameter", "an offset from a root parameter", or "an address from a root parameter that is no address").
  2. Defect B: Adding more than one dynamic term ((base + row) + col, or base + dyn plus soffset/vaddr) discarded base.
  3. Defect C: Standard GFX10 address instructions (s_addk_i32, s_lshl{1..4}_add_u32, v_lshl_add_u32, v_add3_u32, v_add_nc_i32, subtractions on the minuend, aligned constant s_or_b32/v_or_b32, and address-local v_mov_b32 -> v_readfirstlane_b32/v_readlane_b32) were untraced.
  4. Defect D: High-half validation rejected v_add_co_ci_u32(want, 0, _), skipped table dynamic carries (high == None), and did not strip VA-preserving and masks (mask & 0xFF == 0xFF).
  5. Defect E: Control-flow joined (if/else) and loop-carried induction pointer Phis failed immediately with "an address from a phi".

Changes (crates/victory-shader/src/resource.rs, emit/lower.rs, emit/valu_tests.rs)

  • Add RelOffset { value, base, extra_dynamic } to Address (keeping Address: Copy and keeping dynamic populated for downstream readers such as translate.rs).
  • Restrict root-parameter pointer bases in sum_value to Table, ResourceHeap, SamplerHeap, and Descriptor.
  • Add memoized DAG sum tracing with cycle-safe Phi base validation (requiring at least one non-cyclic edge and a matching base across all non-cyclic edges), multi-term retention (multi: bool), and all listed GFX10 address ops without double-counting s.offset or extra_dynamic.
  • Update high_half, carried(Term, Term), and Source::Built mask stripping (set_bits) to accept v_add_co_ci_u32, Term::Const(0) table carries, VA masks, and high-half phis.
  • Implement address_offset and rel_offset in emit/lower.rs with per-call local caching for non-phi expressions and global addr_phis caching resolved in fill_phis to preserve DXIL SSA dominance across blocks, wired into flat(), code_smem(), and built_offset().
  • Add 5 unit tests in resource.rs and 3 end-to-end DXIL + D3D12 GPU/WARP compute dispatch tests (root_srv_*) in emit/valu_tests.rs.

Verification

Isolated verification on branch shader/resource-address-tracing (499bd98):

  • rustfmt --edition 2024 --config skip_children=true --check crates/victory-shader/src/resource.rs crates/victory-shader/src/emit/lower.rs crates/victory-shader/src/emit/valu_tests.rs (passed)
  • cargo test -p victory-shader -- --test-threads=1 (77 passed, 0 failed, 0 ignored)
  • VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-shader root_srv_ -- --test-threads=1 (3 passed, 0 failed, 0 ignored, 74 filtered out)
  • cargo clippy -p victory-shader --all-targets (0 warnings)
  • cargo xtask policy (151 files ok)

No game files. AI-assisted; reviewed and verified with the tests above.

**@⁠nathanbzrr** on GitHub · 2026-11-24 06:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/645) `nathanbzrr:shader/resource-address-tracing` → `main` · +2388 −117 · 3 files · 1 commits · closed without merging ## Summary Fixes five resource-address tracking failures in `crates/victory-shader/src/resource.rs` and adds SSA-dominance-safe relative offset lowering in `crates/victory-shader/src/emit/lower.rs` blocking compute, raytracing/BVH, and multi-term buffer shaders: 1. **Defect A:** `RootType::Constants` dword 0 was treated as a pointer base whenever `param_of` saw dword `0`, colliding with real base pointers (`"an address not from a root parameter"`, `"an offset from a root parameter"`, or `"an address from a root parameter that is no address"`). 2. **Defect B:** Adding more than one dynamic term (`(base + row) + col`, or `base + dyn` plus `soffset`/`vaddr`) discarded `base`. 3. **Defect C:** Standard GFX10 address instructions (`s_addk_i32`, `s_lshl{1..4}_add_u32`, `v_lshl_add_u32`, `v_add3_u32`, `v_add_nc_i32`, subtractions on the minuend, aligned constant `s_or_b32`/`v_or_b32`, and address-local `v_mov_b32` -> `v_readfirstlane_b32`/`v_readlane_b32`) were untraced. 4. **Defect D:** High-half validation rejected `v_add_co_ci_u32(want, 0, _)`, skipped table dynamic carries (`high == None`), and did not strip VA-preserving `and` masks (`mask & 0xFF == 0xFF`). 5. **Defect E:** Control-flow joined (`if/else`) and loop-carried induction pointer `Phi`s failed immediately with `"an address from a phi"`. ### Changes (`crates/victory-shader/src/resource.rs`, `emit/lower.rs`, `emit/valu_tests.rs`) - Add `RelOffset { value, base, extra_dynamic }` to `Address` (keeping `Address: Copy` and keeping `dynamic` populated for downstream readers such as `translate.rs`). - Restrict root-parameter pointer bases in `sum_value` to `Table`, `ResourceHeap`, `SamplerHeap`, and `Descriptor`. - Add memoized DAG sum tracing with cycle-safe `Phi` base validation (requiring at least one non-cyclic edge and a matching `base` across all non-cyclic edges), multi-term retention (`multi: bool`), and all listed GFX10 address ops without double-counting `s.offset` or `extra_dynamic`. - Update `high_half`, `carried(Term, Term)`, and `Source::Built` mask stripping (`set_bits`) to accept `v_add_co_ci_u32`, `Term::Const(0)` table carries, VA masks, and high-half phis. - Implement `address_offset` and `rel_offset` in `emit/lower.rs` with per-call local caching for non-phi expressions and global `addr_phis` caching resolved in `fill_phis` to preserve DXIL SSA dominance across blocks, wired into `flat()`, `code_smem()`, and `built_offset()`. - Add 5 unit tests in `resource.rs` and 3 end-to-end DXIL + D3D12 GPU/WARP compute dispatch tests (`root_srv_*`) in `emit/valu_tests.rs`. ## Verification Isolated verification on branch `shader/resource-address-tracing` (`499bd98`): - `rustfmt --edition 2024 --config skip_children=true --check crates/victory-shader/src/resource.rs crates/victory-shader/src/emit/lower.rs crates/victory-shader/src/emit/valu_tests.rs` (passed) - `cargo test -p victory-shader -- --test-threads=1` (77 passed, 0 failed, 0 ignored) - `VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-shader root_srv_ -- --test-threads=1` (3 passed, 0 failed, 0 ignored, 74 filtered out) - `cargo clippy -p victory-shader --all-targets` (0 warnings) - `cargo xtask policy` (151 files ok) No game files. AI-assisted; reviewed and verified with the tests above.
# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/valu_tests.rs
#	crates/victory-shader/src/resource.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:11:19 +00:00
github-bot closed this pull request 2026-11-26 04:11:19 +00:00
Author
Owner

@⁠ghost on GitHub · 2026-11-24 08:13 UTC · original

Offline translation and validation of 499bd98 against main at d2a4bae: +32 programs validate, 0 regressions.

Setup:

  • The title's shaders.rpf plus its executable and 16 DLLs: 26,148 programs.
  • victory translate --validate --list, validated with DXC 1.9 (1.9.2609.5).
  • Built on Linux. The base is main plus #235 for the Linux build, and a local hook that runs dxv off Windows; neither touches translation.
  • Offline only: no pipelines, no title run.
Stage main: emitted / validated #645: emitted / validated change
compute (of 2,581) 1,693 / 1,670 1,721 / 1,698 +28 / +28
mesh (of 8,506) 8,191 / 8,129 8,191 / 8,129 +0 / +0
vertex and pixel (of 14,013) 12,284 / 12,184 12,288 / 12,188 +4 / +4

What changed:

  • Regressions (validated on main, not with this PR): 0.
  • Outcome changes: 32 programs go from refused to validated, and no other program changes outcome.
    • 24 were refused with resources: an address from a phi (Defect E).
    • 8 were refused with resources: an address not from a root parameter.
  • Reason changes on programs still refused:
    • One program moves from an address not from a root parameter to emit: a descriptor read only as data. It was refused on main too, so this isn't a regression.
    • 4 compute programs are still refused with an address from a phi. All 4 are in the same shader group and fail at nearly the same offset (0x508/0x510), so they are likely one pattern the phi tracing doesn't yet accept.

Caveat: these numbers are from main at d2a4bae.

  • main has moved since (now dd273df).
  • This PR no longer merges cleanly: crates/victory-shader/src/emit/valu_tests.rs conflicts with #140 (v_cmp_class_f16), which first appears at merge 80c0d12.
  • I didn't resolve the conflict myself, so I haven't measured this PR on current main. I can rerun after a rebase.
**@⁠ghost** on GitHub · 2026-11-24 08:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/645#issuecomment-7125881448) **Offline translation and validation of `499bd98` against `main` at `d2a4bae`: +32 programs validate, 0 regressions.** **Setup:** - The title's `shaders.rpf` plus its executable and 16 DLLs: 26,148 programs. - `victory translate --validate --list`, validated with DXC 1.9 (`1.9.2609.5`). - Built on Linux. The base is `main` plus #235 for the Linux build, and a local hook that runs `dxv` off Windows; neither touches translation. - Offline only: no pipelines, no title run. | Stage | main: emitted / validated | #645: emitted / validated | change | |---|---:|---:|---:| | compute (of 2,581) | 1,693 / 1,670 | 1,721 / 1,698 | +28 / +28 | | mesh (of 8,506) | 8,191 / 8,129 | 8,191 / 8,129 | +0 / +0 | | vertex and pixel (of 14,013) | 12,284 / 12,184 | 12,288 / 12,188 | +4 / +4 | **What changed:** - **Regressions** (validated on `main`, not with this PR): **0**. - **Outcome changes:** 32 programs go from refused to validated, and no other program changes outcome. - 24 were refused with `resources: an address from a phi` (Defect E). - 8 were refused with `resources: an address not from a root parameter`. - **Reason changes on programs still refused:** - One program moves from `an address not from a root parameter` to `emit: a descriptor read only as data`. It was refused on `main` too, so this isn't a regression. - 4 compute programs are still refused with `an address from a phi`. All 4 are in the same shader group and fail at nearly the same offset (`0x508`/`0x510`), so they are likely one pattern the phi tracing doesn't yet accept. **Caveat: these numbers are from `main` at `d2a4bae`.** - `main` has moved since (now `dd273df`). - This PR no longer merges cleanly: `crates/victory-shader/src/emit/valu_tests.rs` conflicts with #140 (`v_cmp_class_f16`), which first appears at merge `80c0d12`. - I didn't resolve the conflict myself, so I haven't measured this PR on current `main`. I can rerun after a rebase.
Author
Owner

@⁠xiao47576-glitch on GitHub · 2026-11-24 07:49 UTC · original · edited 2026-11-24 08:31 UTC

I tested PR #645 (499bd98) against main (d2a4bae) on Windows Server 2025 using D3D12 WARP. Main rejected a root SRV plus a byte offset from the first root-constant dword. On this PR, both operand orders read the expected values at offsets 0, 4, 48, and 112 bytes, plus a fixed 12-byte load offset.

I found one carry case the tracker still accepts:

s_add_u32    s0, s4, s10
s_cmp_eq_u32 s10, 0
s_addc_u32   s1, s5, 0

Here s[4:5] holds the root SRV address and s10 is the byte offset. When the offset is zero, the comparison sets SCC to 1 even though the low add produced no carry. The tracker also accepts a high add with immediate 1 and a signed low add.

Download the tested source patch

With the patch applied, the tracker rejected all three cases. All 12 valid WARP reads returned the expected values, and all 78 shader tests passed. Only the valid reads were dispatched on WARP. The effect on the opening cutscene in #255 is still untested. AI-assisted.

**@⁠xiao47576-glitch** on GitHub · 2026-11-24 07:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/645#issuecomment-7125420146) · edited 2026-11-24 08:31 UTC I tested PR #645 (`499bd98`) against `main` (`d2a4bae`) on Windows Server 2025 using D3D12 WARP. Main rejected a root SRV plus a byte offset from the first root-constant dword. On this PR, both operand orders read the expected values at offsets 0, 4, 48, and 112 bytes, plus a fixed 12-byte load offset. I found one carry case the tracker still accepts: ```asm s_add_u32 s0, s4, s10 s_cmp_eq_u32 s10, 0 s_addc_u32 s1, s5, 0 ``` Here `s[4:5]` holds the root SRV address and `s10` is the byte offset. When the offset is zero, the comparison sets SCC to 1 even though the low add produced no carry. The tracker also accepts a high add with immediate 1 and a signed low add. [Download the tested source patch](https://github.com/user-attachments/files/35320599/VIctory-645-scalar-carry.patch.txt) With the patch applied, the tracker rejected all three cases. All 12 valid WARP reads returned the expected values, and all 78 shader tests passed. Only the valid reads were dispatched on WARP. The effect on the opening cutscene in #255 is still untested. AI-assisted.
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 09:24 UTC · original

Measured on the full retail shader pack (26,503 containers) with victory translate --validate --pipelines (pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR conflicts with current main (lower.rs), so it was measured on its own base, against main 1f858b0 before #612 (VS/PS 12272 / 12172, CS 1693 / 1670, MS 8191 / 8129).

CS 1721 / 1698 (+28 validated): "resources: an address from a phi" 28 → 4 and "an address not from a root parameter" 73 → 64. VS/PS +4 validated, MS unchanged.

**@⁠slint6614-dev** on GitHub · 2026-11-24 09:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/645#issuecomment-7127371931) Measured on the full retail shader pack (26,503 containers) with `victory translate --validate --pipelines` (pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR conflicts with current main (`lower.rs`), so it was measured on its own base, against main 1f858b0 before #612 (VS/PS 12272 / 12172, CS 1693 / 1670, MS 8191 / 8129). CS 1721 / **1698** (+28 validated): "resources: an address from a phi" 28 → 4 and "an address not from a root parameter" 73 → 64. VS/PS +4 validated, MS unchanged.
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-24 16:20 UTC · original

Measured on Linux (RX 9070 XT, RADV/Mesa 26.3.1, GE-Proton11-14, vkd3d-proton master), head 499bd98 merged onto a tree that already carries other merged fixes (its offline base is 692 of 751 of the world shader set, not main's number):

  • offline victory translate of the 751-program world set: 692 -> 700; all 700 pass dxil-spirv and spirv-val --target-env vulkan1.3 and build a RADV pipeline.
  • failure classes: "an address from a phi" 6 -> 0, "not from a root parameter" 3 -> 1; the other classes are untouched.
  • cargo test -p victory-shader --lib: 122 pass, 1 known pre-existing failure, 1 ignored; no new failures.
  • cargo xwin clippy --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.
    Not tested: in-game on this branch; native Windows.
**@⁠pablogonz12** on GitHub · 2026-11-24 16:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/645#issuecomment-7137514782) Measured on Linux (RX 9070 XT, RADV/Mesa 26.3.1, GE-Proton11-14, vkd3d-proton master), head 499bd98 merged onto a tree that already carries other merged fixes (its offline base is 692 of 751 of the world shader set, not main's number): - offline `victory translate` of the 751-program world set: 692 -> 700; all 700 pass dxil-spirv and `spirv-val --target-env vulkan1.3` and build a RADV pipeline. - failure classes: "an address from a phi" 6 -> 0, "not from a root parameter" 3 -> 1; the other classes are untouched. - `cargo test -p victory-shader --lib`: 122 pass, 1 known pre-existing failure, 1 ignored; no new failures. - `cargo xwin clippy --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. Not tested: in-game on this branch; native Windows.
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 21:55 UTC · original

Rebased onto origin/main (ffe310e) at 6817902 and addressed the scalar-carry review finding.

@⁠catminou4 @⁠xiao47576-glitch

Rebase integration (ffe310e)

  • Integrated #566 waterfall tracking/lowering in crates/victory-shader/src/resource.rs and crates/victory-shader/src/emit/lower.rs, preserving the 4-argument resource::track(..., st) signature (#801).
  • Merged #612's read_phis worklist in emit/lower.rs (fill_phis) alongside pending_addr_phis.
  • Kept #140 (v_cmp_class_f16_matches_rdna2) and wrapped run_root_srv_cases with ONE_AT_A_TIME.lock() (#433) in crates/victory-shader/src/emit/valu_tests.rs.

Soundness fix (s_addc_u32 / s_subb_u32 carry)

  • Updated high-half carry matching in crates/victory-shader/src/resource.rs so s_addc_u32 and s_subb_u32 require:
    1. a zero constant addend (Term::Const(c) if c == 0, rejecting non-zero immediates), and
    2. an scc value traceable to a matching s_add_u32 on the corresponding low-half value (including loop-carried phi induction chains), rejecting clobbered scc producers (s_cmp_eq_u32, s_add_i32, or unrelated s_add_u32).
  • Added negative test coverage in resource::tests::scalar_carry_requires_matching_add_u32_scc_and_zero_const.

Verification (6817902)

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/valu_tests.rs: clean (resource.rs and emit/lower.rs only retain pre-existing upstream formatting diffs on origin/main).
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.
  • cargo test --workspace --locked: 351 passed; 0 failed; 6 ignored (victory-shader: 129 passed; 0 failed; 1 ignored; xtask: 4 passed).
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- root_srv: 3 passed; 0 failed.
**@⁠nathanbzrr** on GitHub · 2026-11-24 21:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/645#issuecomment-7145567276) Rebased onto `origin/main` (`ffe310e`) at `6817902` and addressed the scalar-carry review finding. @⁠catminou4 @⁠xiao47576-glitch ### Rebase integration (`ffe310e`) - Integrated `#566` waterfall tracking/lowering in `crates/victory-shader/src/resource.rs` and `crates/victory-shader/src/emit/lower.rs`, preserving the 4-argument `resource::track(..., st)` signature (`#801`). - Merged `#612`'s `read_phis` worklist in `emit/lower.rs` (`fill_phis`) alongside `pending_addr_phis`. - Kept `#140` (`v_cmp_class_f16_matches_rdna2`) and wrapped `run_root_srv_cases` with `ONE_AT_A_TIME.lock()` (`#433`) in `crates/victory-shader/src/emit/valu_tests.rs`. ### Soundness fix (`s_addc_u32` / `s_subb_u32` carry) - Updated high-half carry matching in `crates/victory-shader/src/resource.rs` so `s_addc_u32` and `s_subb_u32` require: 1. a zero constant addend (`Term::Const(c) if c == 0`, rejecting non-zero immediates), and 2. an `scc` value traceable to a matching `s_add_u32` on the corresponding low-half value (including loop-carried `phi` induction chains), rejecting clobbered `scc` producers (`s_cmp_eq_u32`, `s_add_i32`, or unrelated `s_add_u32`). - Added negative test coverage in `resource::tests::scalar_carry_requires_matching_add_u32_scc_and_zero_const`. ### Verification (`6817902`) - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/valu_tests.rs`: clean (`resource.rs` and `emit/lower.rs` only retain pre-existing upstream formatting diffs on `origin/main`). - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean. - `cargo test --workspace --locked`: `351 passed; 0 failed; 6 ignored` (`victory-shader`: `129 passed; 0 failed; 1 ignored`; `xtask`: `4 passed`). - `VICTORY_WARP=1 cargo test -p victory-shader --locked -- root_srv`: `3 passed; 0 failed`.
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 22:03 UTC · original

Closing this PR as superseded by recent main (70564ae):

  • 27c3dc9 (Shaders and GPU: title memory at raw addresses through a page table, decision 0005) and 0f63757 (GPU: root views of programs that use title memory go through the page table) now intercept the data-computed address failure classes in Tracer::address() (including "an address from a phi", "an address with two computed terms", "an address with an untracked high half", "an address not from a root parameter", and "an address from a root parameter that is no address") and route them through the MEMORY page table, alongside USER_DATA handling for spilled compute root parameters.
  • Maintaining the pre-27c3dc9 ~2,388-line tracer rework would conflict with decision 0005 and 0f63757's root-view page-table aliasing model; any remaining descriptor-heap address edge cases can be targeted cleanly in a focused follow-up PR on main.

Thanks @⁠catminou4 and @⁠xiao47576-glitch for the detailed reviews!

**@⁠nathanbzrr** on GitHub · 2026-11-24 22:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/645#issuecomment-7145742469) Closing this PR as superseded by recent `main` (`70564ae`): - `27c3dc9` (*Shaders and GPU: title memory at raw addresses through a page table*, decision 0005) and `0f63757` (*GPU: root views of programs that use title memory go through the page table*) now intercept the data-computed address failure classes in `Tracer::address()` (including `"an address from a phi"`, `"an address with two computed terms"`, `"an address with an untracked high half"`, `"an address not from a root parameter"`, and `"an address from a root parameter that is no address"`) and route them through the `MEMORY` page table, alongside `USER_DATA` handling for spilled compute root parameters. - Maintaining the pre-`27c3dc9` ~2,388-line tracer rework would conflict with decision 0005 and `0f63757`'s root-view page-table aliasing model; any remaining descriptor-heap address edge cases can be targeted cleanly in a focused follow-up PR on `main`. Thanks @⁠catminou4 and @⁠xiao47576-glitch for the detailed reviews!

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