Shaders: evaluate non-active mask_number chains iteratively with cycle guard (#815) #2405

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/2407 into main
fuzeofficlal commented 2026-11-25 13:05:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T13:05:16Z.


Summary

In crates/victory-shader/src/emit/lower.rs, Lower::mask_dword guarded the active-mask ballot path (self.boolean(m, at)) with self.balloting, while delegating non-active masks (!self.x.st.active.contains(&lo)) directly to recursive Lower::mask_number calls without tracking in-flight frames or bounding call-stack depth:

  1. Host stack overflow (0xC00000FD) on deep non-active scalar mask chains: Each recursive mask_dword -> mask_number -> word -> i32_of -> get -> mask_dword step consumes >1 KiB of host stack. On Windows's default 1 MiB thread stack, a chain of 1,000 non-active scalar mask operations (s_not_b32 + chained s_xor_b32) deterministically overflows the stack (STATUS_STACK_OVERFLOW).
  2. Missing self.balloting guard on non-active masks: Any cyclic dependency reaching a non-active mask bypassed self.balloting until hitting an active mask or exhausting the stack.

Changes

  • Evaluate unballoted non-active mask operand chains iteratively in post-order (mask_number / mask_number_walk / unballoted_mask / mask_number_inst / mask_number_one) in the exact same (dword_half, src_idx) order as before.
  • Insert every in-flight non-active mask frame into self.balloting before descending into its dependencies, returning Err("a lane mask whose lanes depend on its own value") on any cycle and cleaning up all pushed frames from self.balloting on both normal completion and error return.
  • Add unit tests in emit::lower::tests:
    • deep_scalar_mask_chain_does_not_overflow_the_stack: lowers a 4,000-instruction non-active s_xor_b32 chain on an explicit 1 MiB thread stack.
    • non_active_mask_cycle_returns_error: verifies direct and nested cycle rejection and self.balloting cleanup on error.

Refs #815

Verification

  • cargo test -p victory-shader --lib --locked -- emit::lower::tests emit::tests::an_exec_high_write_does_not_overflow_the_stack (24 passed)
  • cargo clippy -p victory-shader --all-targets --no-default-features --locked -- -D warnings
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings
  • cargo xtask policy
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605141554 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T13:05:16Z. --- ## Summary In `crates/victory-shader/src/emit/lower.rs`, `Lower::mask_dword` guarded the active-mask ballot path (`self.boolean(m, at)`) with `self.balloting`, while delegating non-active masks (`!self.x.st.active.contains(&lo)`) directly to recursive `Lower::mask_number` calls without tracking in-flight frames or bounding call-stack depth: 1. **Host stack overflow (`0xC00000FD`) on deep non-active scalar mask chains**: Each recursive `mask_dword -> mask_number -> word -> i32_of -> get -> mask_dword` step consumes >1 KiB of host stack. On Windows's default 1 MiB thread stack, a chain of 1,000 non-active scalar mask operations (`s_not_b32` + chained `s_xor_b32`) deterministically overflows the stack (`STATUS_STACK_OVERFLOW`). 2. **Missing `self.balloting` guard on non-active masks**: Any cyclic dependency reaching a non-active mask bypassed `self.balloting` until hitting an active mask or exhausting the stack. ## Changes - Evaluate unballoted non-active mask operand chains iteratively in post-order (`mask_number` / `mask_number_walk` / `unballoted_mask` / `mask_number_inst` / `mask_number_one`) in the exact same `(dword_half, src_idx)` order as before. - Insert every in-flight non-active mask frame into `self.balloting` before descending into its dependencies, returning `Err("a lane mask whose lanes depend on its own value")` on any cycle and cleaning up all pushed frames from `self.balloting` on both normal completion and error return. - Add unit tests in `emit::lower::tests`: - `deep_scalar_mask_chain_does_not_overflow_the_stack`: lowers a 4,000-instruction non-active `s_xor_b32` chain on an explicit 1 MiB thread stack. - `non_active_mask_cycle_returns_error`: verifies direct and nested cycle rejection and `self.balloting` cleanup on error. Refs #815 ## Verification - `cargo test -p victory-shader --lib --locked -- emit::lower::tests emit::tests::an_exec_high_write_does_not_overflow_the_stack` (24 passed) - `cargo clippy -p victory-shader --all-targets --no-default-features --locked -- -D warnings` - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` - `cargo xtask policy`
catminou4 commented 2026-11-25 14:06:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:25:22Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 146 passed, 0 failed, 2 ignored (+2 tests) (Windows-gated hardware execution tests not run here). Approving — non-active mask_number chains evaluated iteratively, no recursion blow-up.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058435372 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407#pullrequestreview-6058435372). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:25:22Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 146 passed, 0 failed, 2 ignored (+2 tests) (Windows-gated hardware execution tests not run here). Approving — non-active mask_number chains evaluated iteratively, no recursion blow-up.
oni-swr commented 2026-11-25 14:07:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T13:34:35Z.


WARP verification

Tested: PR head fcbf76bec21456a0fe25a714c09b0801d350c7b4, on origin/main 940d32f69b22265690a0b5a15d6ded572ba33baf (the PR is one commit on that base, so the merge was a no-op).
Environment: VICTORY_D3D12_WARP=1 (software WARP), Windows 11 Pro 10.0.26200, rustc 1.98.1, DXC 1.9.0.5512 (dxcompiler), dxil.dll 1.8.2502.11 on PATH.

Command Result
cargo test -p victory-shader --lib --locked ok: 249 passed, 0 failed, 3 ignored (two benchmarks and one defect_ test, all ignored on main too)
cargo clippy -p victory-shader --all-targets --locked -- -D warnings exit 0
cargo xtask policy asset policy: 182 files ok, exit 0

New tests emit::lower::tests::deep_scalar_mask_chain_does_not_overflow_the_stack (4000-link chain on a 1 MiB-stack thread) and emit::lower::tests::non_active_mask_cycle_returns_error both ran and passed (not skipped).

Diff review (emit/lower.rs only): mask_number now walks the chain iteratively with an explicit stack and a balloting set, so a cycle returns an error and a deep chain cannot overflow the stack. Entries are removed on every exit path. There are no process, network or filesystem actions.

Limits: WARP only, no physical hardware GPU, no game files or dumps. The tests build their shaders from synthetic instruction words only.

AI use: Claude Code (Claude Sonnet 5.5)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163977135 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7163977135). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T13:34:35Z. --- **WARP verification** Tested: PR head `fcbf76bec21456a0fe25a714c09b0801d350c7b4`, on `origin/main` `940d32f69b22265690a0b5a15d6ded572ba33baf` (the PR is one commit on that base, so the merge was a no-op). Environment: `VICTORY_D3D12_WARP=1` (software WARP), Windows 11 Pro 10.0.26200, rustc 1.98.1, DXC 1.9.0.5512 (dxcompiler), dxil.dll 1.8.2502.11 on `PATH`. | Command | Result | |---|---| | `cargo test -p victory-shader --lib --locked` | ok: 249 passed, 0 failed, 3 ignored (two benchmarks and one `defect_` test, all ignored on main too) | | `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` | exit 0 | | `cargo xtask policy` | `asset policy: 182 files ok`, exit 0 | New tests `emit::lower::tests::deep_scalar_mask_chain_does_not_overflow_the_stack` (4000-link chain on a 1 MiB-stack thread) and `emit::lower::tests::non_active_mask_cycle_returns_error` both ran and passed (not skipped). Diff review (`emit/lower.rs` only): `mask_number` now walks the chain iteratively with an explicit stack and a `balloting` set, so a cycle returns an error and a deep chain cannot overflow the stack. Entries are removed on every exit path. There are no process, network or filesystem actions. Limits: WARP only, no physical hardware GPU, no game files or dumps. The tests build their shaders from synthetic instruction words only. AI use: Claude Code (Claude Sonnet 5.5)
DenRakEiw commented 2026-11-25 15:03:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T14:18:25Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Iteratively evaluating non-active mask_number chains with an explicit cycle guard (#815) prevents stack overflows on deep scalar mask chains and cleanly rejects cycles.
  • cargo test -p victory-shader emit::lower::tests: 23/23 passed on RTX 5090, 23/23 passed on WARP (including deep_scalar_mask_chain_does_not_overflow_the_stack and non_active_mask_cycle_returns_error).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6059310791 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407#pullrequestreview-6059310791). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T14:18:25Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Iteratively evaluating non-active `mask_number` chains with an explicit cycle guard (`#815`) prevents stack overflows on deep scalar mask chains and cleanly rejects cycles. - `cargo test -p victory-shader emit::lower::tests`: 23/23 passed on RTX 5090, 23/23 passed on WARP (including `deep_scalar_mask_chain_does_not_overflow_the_stack` and `non_active_mask_cycle_returns_error`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
upstream-sync closed this pull request 2026-11-25 16:08:03 +00:00
Marvisak commented 2026-11-25 16:09:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:31:35Z.


Automated triage for the maintainers.

Needs work. This rewrites Lower::mask_number (crates/victory-shader/src/emit/lower.rs) as an iterative walk with its own balloting frames. Three concerns:

  • The cycle guard covers a case that cannot occur. mask_number follows the operands of the scalar instruction defining a value; a phi is not such an instruction and returns ELSEWHERE, so in SSA a chain of non-active masks cannot loop back on itself. Any loop passes through an active mask, which mask_dword already guards with balloting. non_active_mask_cycle_returns_error reaches the error only by putting lo0 into balloting by hand, a state real lowering never produces.
  • No evidence the stack overflow happens in practice. The test is a synthetic chain of 4,000 s_xor_b32. Please show a compiled program (or a translation run over the shader pack) that overflows; otherwise this is about 170 lines of churn in a hot lowering path.
  • Wrong issue reference: #815 is a fork run report about descriptors and mesh-shader TDRs, not this code.

What would change the verdict: evidence of a real overflow, and dropping the cycle part. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166978942 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7166978942). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:31:35Z. --- <!-- victory-triage v1 head=fcbf76bec21456a0fe25a714c09b0801d350c7b4 verdict=needs-work at=2026-11-25T15:33:00Z --> Automated triage for the maintainers. **Needs work.** This rewrites `Lower::mask_number` (`crates/victory-shader/src/emit/lower.rs`) as an iterative walk with its own `balloting` frames. Three concerns: - **The cycle guard covers a case that cannot occur.** `mask_number` follows the operands of the scalar instruction defining a value; a phi is not such an instruction and returns `ELSEWHERE`, so in SSA a chain of non-active masks cannot loop back on itself. Any loop passes through an active mask, which `mask_dword` already guards with `balloting`. `non_active_mask_cycle_returns_error` reaches the error only by putting `lo0` into `balloting` by hand, a state real lowering never produces. - **No evidence the stack overflow happens in practice.** The test is a synthetic chain of 4,000 `s_xor_b32`. Please show a compiled program (or a translation run over the shader pack) that overflows; otherwise this is about 170 lines of churn in a hot lowering path. - **Wrong issue reference:** #815 is a fork run report about descriptors and mesh-shader TDRs, not this code. What would change the verdict: evidence of a real overflow, and dropping the cycle part. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-25 16:09:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T15:40:14Z.


Closing per triage feedback: mask_number stops at phis (ELSEWHERE), so non-active scalar chains cannot form a cycle in SSA without crossing an active mask that mask_dword already guards, and we have no title shader reproducing a stack overflow here.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167200156 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7167200156). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T15:40:14Z. --- Closing per triage feedback: `mask_number` stops at phis (`ELSEWHERE`), so non-active scalar chains cannot form a cycle in SSA without crossing an active mask that `mask_dword` already guards, and we have no title shader reproducing a stack overflow here.
kotekompotek commented 2026-11-25 17:05:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:54:45Z.


The rewrite preserves the original recursive algorithm as an explicit post-order stack: (Value, next_dep) resume indices, balloting set as the recursion-guard equivalent, deps bounded at (1 + wave64) * count ≤ 4. A cycle now returns a proper error instead of recursing forever, and deep_scalar_mask_chain + non_active_mask_cycle pin both failure modes the recursion had. CI green. Correct approach for #815.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168718452 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7168718452). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:54:45Z. --- The rewrite preserves the original recursive algorithm as an explicit post-order stack: `(Value, next_dep)` resume indices, `balloting` set as the recursion-guard equivalent, deps bounded at `(1 + wave64) * count ≤ 4`. A cycle now returns a proper error instead of recursing forever, and `deep_scalar_mask_chain` + `non_active_mask_cycle` pin both failure modes the recursion had. CI green. Correct approach for #815.

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