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 from upstream-pr/119 into main
Impre-visible commented 2026-11-25 13:06:11 +00:00 (Migrated from github.com)
First-time contributor

Imported from Zettrox/ztrx. Originally posted by Impre-visible on 2026-11-25T12:40:12Z.


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

<!-- greyfinch-sync:Zettrox/ztrx:issue:6604688268 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/119). Originally posted by [Impre-visible](https://github.com/Impre-visible) on 2026-11-25T12:40:12Z. --- ## 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](https://claude.com/claude-code)
Impre-visible commented 2026-11-25 13:06:13 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by Impre-visible on 2026-11-25T12:51:04Z.


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

<!-- greyfinch-sync:Zettrox/ztrx:comment:7162944088 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/119#issuecomment-7162944088). Originally posted by [Impre-visible](https://github.com/Impre-visible) on 2026-11-25T12:51:04Z. --- 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](https://claude.com/claude-code)
Impre-visible commented 2026-11-25 14:11:05 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by Impre-visible on 2026-11-25T13:37:31Z.


A caveat I have to add after more runs, before anyone merges this.

Since this change the four pipelines exist and their programs run. On this card (GTX 1660), default settings, the same tree:

runs Xid 109 (context switch timeout) multi-second pause in the scene
before the change (the 4 pipelines never made) about 15 0 0
with the change about 33 1 (13 s after world entry) 1 (12.4 s, then the run went on)

One event of each kind in 33 runs is not proof, and I have not yet identified which submission was on the card in those two cases. But the plausible reading is that one of the four programs that now run can occasionally take very long (they read structures that the ray-tracing structure builder, off by default, never built). I am checking that now and will report here either way.

So please treat this PR as "fixes the converter recursion, may expose a rare long GPU job" until that is settled.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Zettrox/ztrx:comment:7164050890 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/119#issuecomment-7164050890). Originally posted by [Impre-visible](https://github.com/Impre-visible) on 2026-11-25T13:37:31Z. --- A caveat I have to add after more runs, before anyone merges this. Since this change the four pipelines exist and their programs run. On this card (GTX 1660), default settings, the same tree: | | runs | `Xid 109` (context switch timeout) | multi-second pause in the scene | |---|---|---|---| | before the change (the 4 pipelines never made) | about 15 | 0 | 0 | | with the change | about 33 | 1 (13 s after world entry) | 1 (12.4 s, then the run went on) | One event of each kind in 33 runs is not proof, and I have not yet identified which submission was on the card in those two cases. But the plausible reading is that one of the four programs that now run can occasionally take very long (they read structures that the ray-tracing structure builder, off by default, never built). I am checking that now and will report here either way. So please treat this PR as "fixes the converter recursion, may expose a rare long GPU job" until that is settled. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Impre-visible commented 2026-11-25 14:11:05 +00:00 (Migrated from github.com)
Author
First-time contributor

Imported from Zettrox/ztrx. Originally posted by Impre-visible on 2026-11-25T13:46:05Z.


Follow-up on the caveat above, from reading the two logs and the four programs:

  • The 12 s pause was not the card. That frame took 16.4 s on the CPU side (5.4 s preparing, of which 3.6 s copies, and 6.9 s copying back) with 0.8 s on the GPU and a longest card wait of 207 ms. So it does not count against this change.
  • The Xid 109 run is still open. The log cannot name the submission that was on the card (a slow submission is only logged when it ends). In that run the four pipelines had already been running for about 90 s in submissions of 7 to 50 ms.
  • What the four programs are. Three of them bind title memory by raw address (slots 254 and 253), which is how memory.rs defines the programs that walk the ray-tracing structures. Each has loops nested two deep with the writer's 4096-iteration guard per loop, so the worst case is about 4096 × 4096 inner iterations: seconds on a GTX 1660, against a few milliseconds when only the outer limit is reached. With the structure builder off by default they walk structures nobody built. That fits a rare context-switch timeout, but it is an inference; I have one event.

The comment on Builder in pipeline.rs says nothing reads the structures because the programs that walk them do not translate. With this change three of them get a host pipeline, so that assumption no longer holds on vkd3d-proton (and never held on Windows, where these pipelines were always made).

I am not proposing anything further in this PR. Options I see, for the maintainer to judge: (a) leave it, since Windows already runs these programs; (b) run the walkers only while the builder's structures are valid, extending the builder's own all-or-none policy to its readers; (c) wait for a working builder. I am working on (c) locally and will report.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Zettrox/ztrx:comment:7164260220 --> Imported from [Zettrox/ztrx](https://github.com/Zettrox/ztrx/pull/119#issuecomment-7164260220). Originally posted by [Impre-visible](https://github.com/Impre-visible) on 2026-11-25T13:46:05Z. --- Follow-up on the caveat above, from reading the two logs and the four programs: - **The 12 s pause was not the card.** That frame took 16.4 s on the CPU side (5.4 s preparing, of which 3.6 s copies, and 6.9 s copying back) with 0.8 s on the GPU and a longest card wait of 207 ms. So it does not count against this change. - **The `Xid 109` run is still open.** The log cannot name the submission that was on the card (a slow submission is only logged when it ends). In that run the four pipelines had already been running for about 90 s in submissions of 7 to 50 ms. - **What the four programs are.** Three of them bind title memory by raw address (slots 254 and 253), which is how `memory.rs` defines the programs that walk the ray-tracing structures. Each has loops nested two deep with the writer's 4096-iteration guard per loop, so the worst case is about 4096 × 4096 inner iterations: seconds on a GTX 1660, against a few milliseconds when only the outer limit is reached. With the structure builder off by default they walk structures nobody built. That fits a rare context-switch timeout, but it is an inference; I have one event. The comment on `Builder` in `pipeline.rs` says nothing reads the structures because the programs that walk them do not translate. With this change three of them get a host pipeline, so that assumption no longer holds on vkd3d-proton (and never held on Windows, where these pipelines were always made). I am not proposing anything further in this PR. Options I see, for the maintainer to judge: (a) leave it, since Windows already runs these programs; (b) run the walkers only while the builder's structures are valid, extending the builder's own all-or-none policy to its readers; (c) wait for a working builder. I am working on (c) locally and will report. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/119:upstream-pr/119'
git switch 'upstream-pr/119'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/119'
git switch 'upstream-pr/119'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/119'
git switch 'upstream-pr/119'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/119'
git switch 'main'
git merge --squash 'upstream-pr/119'
git switch 'main'
git merge --ff-only 'upstream-pr/119'
git switch 'main'
git merge 'upstream-pr/119'
git push origin 'main'
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
Zettrox/ztrx!119
No description provided.