Shaders: evaluate non-active mask_number chains iteratively with cycle guard (#815) #2407
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2407
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2407/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 mergingSummary
In
crates/victory-shader/src/emit/lower.rs,Lower::mask_dwordguarded the active-mask ballot path (self.boolean(m, at)) withself.balloting, while delegating non-active masks (!self.x.st.active.contains(&lo)) directly to recursiveLower::mask_numbercalls without tracking in-flight frames or bounding call-stack depth:0xC00000FD) on deep non-active scalar mask chains: Each recursivemask_dword -> mask_number -> word -> i32_of -> get -> mask_dwordstep 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+ chaineds_xor_b32) deterministically overflows the stack (STATUS_STACK_OVERFLOW).self.ballotingguard on non-active masks: Any cyclic dependency reaching a non-active mask bypassedself.ballotinguntil hitting an active mask or exhausting the stack.Changes
mask_number/mask_number_walk/unballoted_mask/mask_number_inst/mask_number_one) in the exact same(dword_half, src_idx)order as before.self.ballotingbefore descending into its dependencies, returningErr("a lane mask whose lanes depend on its own value")on any cycle and cleaning up all pushed frames fromself.ballotingon both normal completion and error return.emit::lower::tests:deep_scalar_mask_chain_does_not_overflow_the_stack: lowers a 4,000-instruction non-actives_xor_b32chain on an explicit 1 MiB thread stack.non_active_mask_cycle_returns_error: verifies direct and nested cycle rejection andself.ballotingcleanup 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 warningscargo clippy -p victory-shader --all-targets --all-features --locked -- -D warningscargo xtask policy@oni-swr on GitHub · 2026-11-25 13:34 UTC · original
WARP verification
Tested: PR head
fcbf76bec21456a0fe25a714c09b0801d350c7b4, onorigin/main940d32f69b22265690a0b5a15d6ded572ba33baf(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 onPATH.cargo test -p victory-shader --lib --lockeddefect_test, all ignored on main too)cargo clippy -p victory-shader --all-targets --locked -- -D warningscargo xtask policyasset policy: 182 files ok, exit 0New tests
emit::lower::tests::deep_scalar_mask_chain_does_not_overflow_the_stack(4000-link chain on a 1 MiB-stack thread) andemit::lower::tests::non_active_mask_cycle_returns_errorboth ran and passed (not skipped).Diff review (
emit/lower.rsonly):mask_numbernow walks the chain iteratively with an explicit stack and aballotingset, 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)
@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 ownballotingframes. Three concerns:mask_numberfollows the operands of the scalar instruction defining a value; a phi is not such an instruction and returnsELSEWHERE, so in SSA a chain of non-active masks cannot loop back on itself. Any loop passes through an active mask, whichmask_dwordalready guards withballoting.non_active_mask_cycle_returns_errorreaches the error only by puttinglo0intoballotingby hand, a state real lowering never produces.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.What would change the verdict: evidence of a real overflow, and dropping the cycle part. CI: passing.
Generated by Claude Code
@fuzeofficlal on GitHub · 2026-11-25 15:40 UTC · original
Closing per triage feedback:
mask_numberstops at phis (ELSEWHERE), so non-active scalar chains cannot form a cycle in SSA without crossing an active mask thatmask_dwordalready guards, and we have no title shader reproducing a stack overflow here.@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,ballotingset as the recursion-guard equivalent, deps bounded at(1 + wave64) * count ≤ 4. A cycle now returns a proper error instead of recursing forever, anddeep_scalar_mask_chain+non_active_mask_cyclepin both failure modes the recursion had. CI green. Correct approach for #815.Pull request closed