Skip to content

Shaders: every loop has one back edge (vkd3d-proton's converter recursed without end on four compute pipelines) - #119

Open
Impre-visible wants to merge 1 commit into
Zettrox:mainfrom
Impre-visible:fix/one-back-edge-per-loop
Open

Impre-visible wants to merge 1 commit into
Zettrox:mainfrom
Impre-visible:fix/one-back-edge-per-loop

Conversation

@Impre-visible

Copy link
Copy Markdown

What

Under Wine + vkd3d-proton, four of the title's compute pipelines are never made on this tree: CreateComputePipelineState does not return, the victory-pipelines-N worker that called it is lost, and the frame line shows (4 being made) for the whole run. This change makes the bitcode writer emit every loop with a single back edge, which avoids the converter bug behind it. Windows is not affected by the bug (no vkd3d there); the change is in the writer, so it applies everywhere.

Reported and discussed in #62.

Cause

It is unbounded recursion in dxil-spirv, the DXIL to SPIR-V converter inside vkd3d-proton, on valid input. Built natively at its HEAD (061225d) and run on the translated shaders saved with VICTORY_SHADER_SAVE:

Converter::Impl::convert_entry_point
 CFGStructurizer::flatten_subgroup_shuffles
  CFGStructurizer::recompute_cfg
   CFGNode::recompute_immediate_dominator  <->  CFGNode::find_common_dominator   (about 174,000 frames)

flatten_subgroup_shuffles runs when the module uses WaveReadLaneAt, and its recompute_cfg sees the raw CFG before rewrite_multiple_back_edges. A loop header with two or more back edges, or one block that branches back to two loop headers, then makes the two dominator routines call each other without end. Under Wine each lost thread shows as one virtual_setup_exception stack overflow line; a larger stack does not help (tried 64 MiB).

I am reporting the converter bug upstream separately.

Change

  • Module::one_back_edge_each(), called just before write(): a block that branches back to several headers keeps the branch to the innermost one and reaches the others through new empty blocks; a header with several latches gets one new latch block that takes the header's phis from them.
  • The empty-latch fold in write() no longer creates a second back edge.
  • Test every_loop_has_one_branch_back, built from a synthetic program and checked with the existing interpreter.

Measured

Translator (Linux, cargo test -p victory-shader --lib): 171 passed, 0 failed, 2 ignored on this branch.

Native dxil-spirv --validate over all 2,568 translations saved from one run of the title (counts only):

convert and validate crash
before 2,564 4
with this change 2,568 0

27 modules change; the other 2,541 are byte-identical. The 27 were also executed before and after the rewrite in a bitcode interpreter on random inputs (400 to 4,000 seeds from the entry, 300 from each rewritten loop header, every rewritten back edge taken): 0 differences in calls, loads, stores and atomics.

In the title (GTX 1660, Wine from GE-Proton11-6, vkd3d-proton 2576c0b, Linux), first playable scene:

before with this change
stack overflow lines per run 4 0
frame line (4 being made) all run (0 being made)
runs counted every run before (more than 20) 18 runs of 5 to 15 minutes, 0 lines in each

Not tested / limits

  • AMD. A comment in write() says the AMD driver hangs on divergent loops whose exiting block is not the latch. In the merged-latch case the exiting blocks now branch to a new latch, so they are no longer the latch. That is 27 modules. I have no AMD card; this needs a run there before merging.
  • Windows on any card.
  • No speed gain: the 500 ms pipeline wait those four cost was on a recording thread, not on the critical path (frame time in the scene is the same before and after).
  • The four pipelines that now exist do run, so the scene does somewhat more GPU work; I could not separate that from run-to-run variation (500 to 700 ms per frame on this card either way).

No game files, shader bytes or hashes in this PR.

🤖 Generated with Claude Code

…does not recurse without end

Under Wine + vkd3d-proton, four of the title's compute pipelines were never
made: dxil-spirv recursed until the stack overflowed (flatten_subgroup_shuffles
-> recompute_cfg -> recompute_immediate_dominator <-> find_common_dominator)
on a module that uses WaveReadLaneAt and has a loop header with two back
edges, or a block that branches back to two headers. The worker thread was
lost with its stack and the pipeline stayed "being made" for the whole run.

The writer now gives every loop header one latch and every block at most one
branch back (Module::one_back_edge_each, before write()), and the empty-latch
fold never creates a second back edge. The program does the same.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TsTmwWj9LVy4o3qDH52Jas
Signed-off-by: Impre-visible <69050895+Impre-visible@users.noreply.github.com>
@Impre-visible

Copy link
Copy Markdown
Author

Update on "I am reporting the converter bug upstream separately": I did not need to. Before filing I rebuilt dxil-spirv at today's master, 694cc67 ("Only run fixup passes after structurizer has run.", 2026-11-25), and the bug is already fixed there:

dxil-spirv synthetic two-latch + WaveReadLaneAt reproducer the 4 translations this tree currently emits
061225d (2026-11-24) exit 139, stack overflow 4 of 4 crash
694cc67 (2026-11-25) exit 0, --validate passes 4 of 4 convert and validate

A sample of 400 other saved translations also converts on 694cc67.

So this PR is a workaround for the vkd3d-proton builds people actually run today (I hit it with 2576c0b from Proton Experimental experimental-11.0-20261112; the tester in #62 saw the same stack-overflow lines with GE-Proton11-14), not for something that will stay broken. Once Proton ships a vkd3d-proton with dxil-spirv at or after 694cc67, the rewrite is no longer needed for correctness. Whether to carry it until then is your call; the AMD caveat in the description still applies.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant