Skip to content

Recompiler: preserve control flow across structured jumps - #765

Open
Abuudiii wants to merge 1 commit into
xcl-dev:mainfrom
Abuudiii:xcl-701-control-flow
Open

Abuudiii wants to merge 1 commit into
xcl-dev:mainfrom
Abuudiii:xcl-701-control-flow

Conversation

@Abuudiii

@Abuudiii Abuudiii commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #701 in the legacy structured emitter. Reopen a skip guard when a later branch can set it and preserve actual jumps into the else arm.

What is in it

  • Emitter fixes plus synthetic branch and register-readback regressions.

Tested

  • Both emitter tests fail on unmodified base f4ce27c and pass with this patch. Exact SGPR results passed on RX 9070 under GE-Proton11-7 / vkd3d-proton 3.1 / DXC 1.9.2609.5: 2 CPU tests and 1 GPU test, no skips.
  • Independent Windows 11 / RTX 3060 Ti suite on the exact head: cargo test --workspace --no-fail-fast -- --test-threads=1: 704 passed, 0 failed, 41 ignored; the two wave64 tests skipped (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). All three PR regressions ran and passed.
  • Independent Windows 11 / RX 9070 XT suite with the PR merged onto 64b3f35: cargo test --workspace -- --test-threads=4: 704 passed, 0 failed, 41 ignored, no GPU skips.

Regression run
Native Windows runs reported by @ryosen0.

xcl-smoke v1
commit: f4ce27ceb6d1b0576d0c659b713b674778bf1968
gpu: NVIDIA GeForce RTX 3060 Ti
title: ScarlettGame
seconds: 90
exit: 0x0
frames shown: 2336 (PresentX 2336)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14473 MB
xcl-smoke v1
commit: 4cfd95109706b8c95e80efe3a20469df9378bbd8
gpu: NVIDIA GeForce RTX 3060 Ti
title: ScarlettGame
seconds: 90
exit: 0x0
frames shown: 2340 (PresentX 2340)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14485 MB

Not tested

AI use

  • omp (GPT-6.2 Sol) wrote and locally tested the change and drafted this description. The linked Windows results were produced by the credited contributors.

Fix the legacy emitter’s repeated exit guard and jumps into an else arm. Verify both with small synthetic control-flow programs.
Refs: xcl-dev#701
Assisted-by: GPT-6.2 Sol
@JoaoPauluu

Copy link
Copy Markdown

Test report: #765 on NVIDIA under Wine + vkd3d-proton (Linux)

This complements the RX 9070 run in the description with a 32-lane NVIDIA GPU, through the same kind of stack (Wine, vkd3d-proton 3.1, DXC 1.9.2609.5).

Setup

Results (lib test binary under Wine, --test-threads=1)

Not tested

  • The two CPU tests on the base (the description covers that); Windows; the rest of the suite; a title run

AI use: Claude Code (Claude Opus 5.5) built the tests, ran them on the machine above and wrote this report. The counts are pasted from those runs. The account owner asked for test reports to be posted while they were away and has not reviewed this one.

@eizispark

Copy link
Copy Markdown

Hardware check of 4cfd95109706b8c95e80efe3a20469df9378bbd8 (verified clean merge with main 64b3f35): pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace -- --test-threads=4: 704 passed, 0 failed, 41 ignored (GPU tests skipped on this adapter: none)
  • gpu::recomp::tests (gpu_structured_branch_targets_match_rdna2, structured_jump_into_else_preserves_target, structured_repeated_loop_exit_rechecks_guard): 3 passed, 0 failed

Regression run
main (f4ce27c):

xcl-smoke v1
commit: f4ce27ceb6d1b0576d0c659b713b674778bf1968
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1667 (PresentX 1667)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14847 MB

PR (4cfd951):

xcl-smoke v1
commit: 4cfd95109706b8c95e80efe3a20469df9378bbd8
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1662 (PresentX 1662)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14855 MB

Not tested

  • wave32-only GPUs (NVIDIA / Intel); gameplay past the menus

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.

@ryosen0

ryosen0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Native Windows cross-check for 4cfd951 (RTX 3060 Ti 8 GB, wave32, Ryzen 7 9800X3D, driver 32.0.16.1742, Windows 11), covering the two gaps in Not tested:

Native Windows GPU suite — cargo test --workspace --no-fail-fast -- --test-threads=1: 704 passed, 0 failed, 41 ignored, exit 0. 2 GPU SKIPPED (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware — no wave64 on this adapter); your two emitter regressions and the register-readback test pass natively.

Paired retail smoke — same title, same duration, release builds, no uncommitted changes; both blocks verbatim.

Main (f4ce27c, this PR's merge-base):

xcl-smoke v1
commit: f4ce27ceb6d1b0576d0c659b713b674778bf1968
gpu: NVIDIA GeForce RTX 3060 Ti
title: ScarlettGame
seconds: 90
exit: 0x0
frames shown: 2336 (PresentX 2336)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14473 MB

Head (4cfd951):

xcl-smoke v1
commit: 4cfd95109706b8c95e80efe3a20469df9378bbd8
gpu: NVIDIA GeForce RTX 3060 Ti
title: ScarlettGame
seconds: 90
exit: 0x0
frames shown: 2340 (PresentX 2340)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14485 MB

Both steady 60 presents / 2 s, no device removal — a no-regression check on the retail boot/menus path, not a repro of #701's shader (that stays covered by your synthetic regressions).

AI use: GPT-6.2 Astra (API model selector) in DSH ran the suite and both smokes and drafted this comment; the human contributor reviewed the numbers before posting.

@mastrr9

mastrr9 commented Nov 24, 2026

Copy link
Copy Markdown

Independent native Windows NVIDIA RTX 5080 build/test run and code review at head 4cfd95109706b8c95e80efe3a20469df9378bbd8.

Code Review

  • Clean fix for both #701 cases in the legacy structured emitter:
    1. else_ok scans c.edges directly (binary-searched by instruction-ordered src) to reject any branch from [end, t) targeting [t, u), catching nested conditionals to t that structurable_to previously swallowed as an inner if.
    2. emit_structured closes guard whenever !g.is_disjoint(&raised) so a subsequent branch raising the same skip flag re-opens a fresh guard check before the following block.
  • Both CPU emitter checks and the GPU SGPR readback test cover the exact regressions in both structured = true and false modes.

Tested

  • Environment: NVIDIA GeForce RTX 5080 (16 GB, wave32, driver 617.42 / 32.0.16.1742), Windows 11 Pro 25H2 (26200.9974), rustc 1.100.0 (0ef1bc386 2026-11-09), DXC from Windows SDK 10.0.26100.0, debug build.
  • Command: cargo build --workspace && cargo test --workspace -- --test-threads=4
  • Result: 704 passed, 0 failed, 41 ignored (0 new warnings).
  • PR-specific tests (3/3 ok):
    • gpu::recomp::tests::structured_repeated_loop_exit_rechecks_guard
    • gpu::recomp::tests::structured_jump_into_else_preserves_target
    • gpu::recomp::tests::gpu_structured_branch_targets_match_rdna2 (executed on RTX 5080, not skipped)
  • SKIPPED (2):
    • gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask (no wave64)
    • gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64)

Not tested

  • D3D12 debug layer (d3d12SDKLayers.dll not installed on this host), AMD/wave64 hardware, release build, and retail titles.

AI use: Antigravity (Gemini 4 Neon) performed the code review, ran the RTX 5080 workspace tests, and drafted this comment.

@NathanTheophile

Copy link
Copy Markdown

A random-program check of 4cfd951 with @QemmHD's #701 harness (structure_fuzz.rs from 6e9c949). It's adapted to run without #172: on each tree it compares only main's two forms, the default path and the pc chain, against the harness's CPU reference. The same harness ran on main and on the other #701 PRs, each with its own build directory.

CPU: first-form programs that walk unlike their graph (seeds 1–5000) GPU: 300 seeds × wave32 / split wave64, differences (default / pc chain) the two #701 shapes
main beaa245 132 of 1177 8 / 6 both wrong
#765 4cfd951 0 of 1179 0 / 0 both right
#721 1b4a140 0 of 1179 0 / 0 both right
#729 4255402 (with #728) 3 of 1179 0 / 0 both right

On #729, seeds 431 and 3252 also give wrong SGPRs on the GPU with 16 inputs each, and identical ones on main, so they are real miscompiles. This matches @QemmHD's leftovers on #728. On #765 and #721, those seeds are right.

Tested: NVIDIA GeForce RTX 4060 Laptop GPU (driver 616.87, 32-lane waves, so wave64 runs split), Windows 11 26200, Rust 1.100.0 MSVC, DXC 1.8.2502.11, debug. Not tested: AMD / native wave64; the harness's general-form (#172) checks, which this adaptation drops.

AI-assisted: Claude Code, Claude Opus 5.5.

@upipibi upipibi mentioned this pull request Nov 24, 2026
wondercrash added a commit to wondercrash/xcl that referenced this pull request Nov 24, 2026
`if (a && b) { ... } else { E }` compiles to two conditional jumps to the
start of E. The first became the if/else; the second, inside the if
branch, became a nested plain `if` ending where the if branch ends, so
else_ok saw no jump into the else (a nested if raises no flag). When the
second jump was taken, HLSL's `else` did not run (upstream xcl-dev#701, shape 2).

else_ok now refuses an else whenever any branch of the if branch targets
[t, u), including t itself; the jump then raises the skip flag and the
code after it runs under the flag's guard. `edges` is in instruction
order, so the scan is a binary search plus the branches of the if branch.
The check in structurable_uncached for a branch to the else's start is
now unreachable and goes.

Retail shaders.rpf, all 2 682 compute programs (split wave64, 4 windows),
checked by a static walk of the HLSL against the machine successors:
390 wrong transitions in 363 programs before (301 CS_RaytracingResolve
variants, CS_GatherRaymarchedLocalLightsClassified*, CS_ResolveVBuffer,
CS_MergeQuads, ...; 14 of them skipped an early s_endpgm), 0 after. The
HLSL of exactly those 363 programs changes (+18 KB of 3.85 GB).

Tests from xcl-dev#765, with the text tests in the single `xcl_skip` spelling:
the GPU test gives [1, 1, 0, 0, 1] instead of [1, 1, 0, 1, 1] without
this change (RTX 5090, wave32 split form). xcl-dev#765's guard-reopening hunk
is not needed after c70c9f0 (a guard fails on any flag); its two-breaks
test passes without it.

Ported from xcl-dev#765 (Abuudiii).

Assisted-by: omp (Claude Opus 5.5)
@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent confirmation of this fix on current main, from a different shape of the same bug: two conditional branches whose targets share the start of an else arm.

Tested

  • RTX 5070 Ti (NVIDIA 610.74), Ryzen 9 9950X, Windows 11 26200, release, DXC 1.9.2609.5.
  • A synthetic 12-instruction wave32 compute shader, one wave, where both branches land on the first instruction of the else arm. The case was found by an equivalence check of XCL's output against the guest program, then confirmed on the GPU.
    • main 689f2db: all 32 words store 9. The structured emitter nests the two conditions and skips the else arm, so the expected 13 is never written.
    • This PR rebased onto 689f2db (a local rebase; the one conflict was in recomp/mod.rs, next to main's separate pending-flag fix): all three branch outcomes give the expected values (13, 13, 11).
  • The retail compute pack has 7 programs with exactly this shape, each with one such else start: 305bbb77ddc3ff2d, 6e3fd903765f4376, bf82bfb95451bef8, c3ae68197a76d57f, d3c6619783fb3e57, ee2e847c35bba72c, fbf59c380949b267.
    • Re-exported with only this PR's else_ok edge rejection, all 7 pass a per-lane control-trace comparison against the guest branches; without it, all 7 fail.
    • In our title runs, the title asked for all 7, and 6e3fd903 was built and bound. We have no per-dispatch trace, so whether the title takes the wrong arm with real inputs is not shown.

Not tested

  • The full workspace suite on the rebased head (it's queued), AMD, a smoke pair.
  • Visible output differences in the title.

The rebased head and a second regression test (a compute dispatch with two branches to the else arm) are ready on our side. We can't push them to a fork just now, because our token lacks the workflow scope that main's new stale.yml requires. @Abuudiii, if you rebase onto main, the conflict is a few lines in recomp/mod.rs.

AI use: Claude Code (Claude Opus 5.5) and Codex helpers ran the check and the GPU tests on our machine; Claude Code wrote this.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up to the check above: cargo test --workspace (debug) on 689f2db with this PR rebased plus the extra regression test: 979 passed, 0 failed, 45 ignored. The 22 skip notices are 18 native-wave64, 3 unsupported f16 variants and 1 heap windows. The two-branches-to-the-else GPU test ran without a skip. Still not tested: AMD and a smoke pair.

AI use: a Codex helper ran the suite on our machine; Claude Code wrote this.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@Abuudiii the rebase I mentioned above is now on a branch you can pull: Kalmarv:shared-else-765-rebased. It's ee80f924 (this PR rebased onto main 689f2db, with the one recomp/mod.rs conflict resolved) plus afc55fcd, the extra regression test (a compute dispatch with two branches into the else arm). The workspace run above was on that head. Take it, cherry-pick from it, or ignore it, whichever suits you.

AI use: Claude Code (Claude Opus 5.5) did the rebase and wrote this.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

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.

Recompiler: the first structured form runs code a taken jump skips, and skips an else it jumps into

8 participants