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

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

@⁠fuzeofficlal on GitHub · 2026-11-25 13:05 UTC · original
fuzeofficlal:fix/shader-mask-number-cycle-guard-815 → main · +170 −7 · 1 files · 1 commits · closed without merging

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
**@⁠fuzeofficlal** on GitHub · 2026-11-25 13:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2407) `fuzeofficlal:fix/shader-mask-number-cycle-guard-815` → `main` · +170 −7 · 1 files · 1 commits · closed without merging ## 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`
Lower::mask_dword previously evaluated !active console masks via recursive
Lower::mask_number calls without inserting into self.balloting. A deep
chain of non-active scalar mask ops (such as 1,000+ chained s_xor_b32)
exceeded Windows's 1 MiB thread stack (0xC00000FD STATUS_STACK_OVERFLOW),
and any cycle reaching a non-active mask lacked a cycle guard.

Walk unballoted non-active mask operands iteratively in post-order on a
heap stack while tracking all active frames in self.balloting (and
removing them on both completion and error return).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:04:55 +00:00
github-bot closed this pull request 2026-11-26 05:04:56 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 13:34 UTC · original

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)

**@⁠oni-swr** on GitHub · 2026-11-25 13:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7163977135) **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)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:31 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 15:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7166978942) <!-- 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)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 15:40 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7167200156) 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.
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 16:54 UTC · original

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 16:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2407#issuecomment-7168718452) 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

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