Skip to content

Recompiler: fix trivial phi collapse, 0-pred phis, and chains in recomp::ssa - #2148

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1269-recomp-ssa-braun-bugs
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1269-recomp-ssa-braun-bugs

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

What is in it

Fixes four Braun et al. SSA construction bugs in crates/xcl/src/gpu/recomp/ssa.rs (used by recomp/lower.rs and recomp/lower_dxil.rs, where uncollapsed trivial phis break resolve_value(v) checks against num_marker / bit_marker):

  1. Missing users transfer on trivial phi collapse (remove_trivial / replace_phi): Previously, self.users.remove(&phi) dropped every user of a collapsed phi that remained non-trivial at that moment (for example, phi3 = phi(phi2, a) when phi2 collapsed to an unsealed outer phi1). When phi1 later collapsed to a (or collapsed later in the same users loop when users[phi2] ordered [phi3, phi1]), phi3 was never revisited and remained live as a trivial phi(a, a). Live users (user != phi && user != replacement && self.operands.contains_key(&user)) are now transferred into self.users[replacement] before cascading remove_trivial().
  2. Stale return value from remove_trivial (replace_phi): When a mutual phi cycle (phi1 = phi(a, phi2) and phi2 = phi(phi1, phi2)) collapsed phi2 -> phi1 and cascaded phi1 -> a, remove_trivial returned the already-replaced same (phi1) instead of self.resolve(same) (a). Now returns self.resolve(replacement).
  3. Malformed 0-operand phis on unsealed 0-predecessor blocks and complete operand registration (add_operands): Sealing an unsealed block with preds[block].is_empty() previously left a 0-operand phi live (same == None) instead of replacing it with sink.undef(var). add_operands() now replaces 0-predecessor incomplete phis with sink.undef(var) via replace_phi() (transferring and cascading any users), and for non-empty predecessors collects all incoming (pred, value) pairs and resolves each value before populating operands/users so a half-populated phi is never exposed to a recursive remove_trivial() cascade.
  4. Iterative single-predecessor chain walk with cycle detection (read): read() previously recursed unconditionally on preds[block].len() == 1, overflowing the stack on deep straight-line chains and on unreachable sealed 1-predecessor cycles (preds[b] = [b] or a <-> b). Sealed single-predecessor chains are now walked iteratively with HashSet cycle detection, returning and caching sink.undef(var) on unreachable 1-predecessor cycles.

Adds four deterministic pure-CPU regression tests in gpu::recomp::ssa::tests covering all four bugs.

Part of #1269

Tested

Hardware/toolchain: Windows 11 (26200), NVIDIA GeForce RTX 4060 (32-lane waves, driver 621.14), DXC 1.9.2609.5 (01b62ad4), Rust 1.100.0.

  • Verified pre-fix failures on all 4 new regression tests before applying the fixes (delayed_trivial_phi_collapse_transfers_users: live phis left: [4]; mutual_phi_cycle_collapse_returns_resolved_value: returned stale 3 instead of 1; unsealed_zero_pred_block_replaces_incomplete_phi_with_undef: 0-pred phi stayed live: [1, 2]; sealed_single_pred_cycle_and_deep_chain_do_not_overflow: STATUS_STACK_OVERFLOW 0xc00000fd).
  • cargo test --release -p xcl --lib gpu::recomp::ssa::tests::: 10 passed, 0 failed, 0 ignored (pure CPU, CI WARP-safe).
  • cargo test --release -p xcl --lib gpu::recomp::lower::tests:: -- --test-threads=1: 35 passed, 0 failed, 0 ignored.
  • cargo test --release -p xcl --lib gpu::recomp::lower_dxil::tests:: -- --test-threads=1: 18 passed, 0 failed, 0 ignored.
  • cargo build --release --workspace: exit 0.
  • cargo test --release --workspace -- --test-threads=4: 1158 passed, 0 failed, 46 ignored across 21 test suites, with 25 pre-existing runtime SKIPPED lines (23 native wave64 / unsupported per-lane lowering skips on 32-lane RTX 4060, 1 Windows driver-cache skip, 1 heap-window skip; 0 new skips).

Regression run

Paired 60-second scripts/smoke.ps1 runs on the same retail title build (29640639) from clean origin/main (6ea813a) and this branch (a7f059b):

Base (origin/main):

xcl-smoke v1
commit: 6ea813a61f7d324e2fcbeb0c6a068edcd37e818b
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1588 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14476 MB

PR (fix-1269-recomp-ssa-braun-bugs):

xcl-smoke v1
commit: a7f059b9972bda022a825b6bf3e079b143082b50
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1584 (PresentX 1585)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14476 MB

Not tested

  • Native 64-lane wave64 GPU hardware (the test GPU is a 32-lane NVIDIA RTX 4060; recomp::ssa is wave-size-independent SSA construction and all new tests are deterministic CPU unit tests).
  • Non-Windows host platforms.

AI use

Drafted with Google Antigravity (Gemini 4 Neon) and verified locally by reproducing all four failures before the fix (including stack overflow on the 1-predecessor cycle/chain test), running recomp::ssa, recomp::lower, recomp::lower_dxil, and the full release workspace test suite, and completing the paired 60-second smoke run above.

…mp::ssa

Fix four Braun et al. SSA construction bugs in crates/xcl/src/gpu/recomp/ssa.rs:

- User transfer on trivial phi collapse: transfer still-live users of a
  collapsed phi into users[replacement] before cascading remove_trivial(), so
  phis that stay live temporarily (or sit earlier in users[phi] when
  replacement collapses during the same cascade) are revisited when
  replacement later collapses.
- Resolved return value: return self.resolve(replacement) from replace_phi()
  / remove_trivial() so mutual phi-cycle collapses never return an already
  replaced phi handle.
- Unsealed 0-predecessor blocks and complete operand registration: in
  add_operands(), replace an incomplete phi at a 0-predecessor block with
  sink.undef(var) (and cascade its users) instead of leaving a malformed
  0-operand phi; otherwise collect all incoming (pred, value) pairs before
  populating operands/users (resolving each value) so a half-populated phi is
  never exposed to a recursive remove_trivial() cascade.
- Iterative single-predecessor chains: walk sealed 1-predecessor chains
  iteratively with cycle detection in read(), terminating unreachable 1-pred
  cycles with sink.undef(var) and avoiding stack overflow on deep chains.

Part of xcl-dev#1269

Assisted-by: Google Antigravity (Gemini 4 Neon)
@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head a7f059b (on its base 6ea813a).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::recomp::lower_dxil::tests::mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 1146 passed, 0 failed, 44 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: delayed_trivial_phi_collapse_transfers_users, mutual_phi_cycle_collapse_returns_resolved_value, unsealed_zero_pred_block_replaces_incomplete_phi_with_undef, sealed_single_pred_cycle_and_deep_chain_do_not_overflow.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 1157 passed, 0 failed, 46 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main 568e7db too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests, const_and_null_mask_operands_agree_with_hlsl_on_the_gpu and mask_ff0_and_flbit_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 25, 2026
… in ssa

Braun-SSA soundness fixes in recomp/ssa.rs, verbatim from upstream head
a7f059b: transfer live users into users[replacement] in replace_phi
before cascading; return resolve(replacement) instead of the stale phi;
0-predecessor incomplete phis become sink.undef via replace_phi and
incoming values are collected/resolved before operands/users register;
sealed 1-predecessor chains walked iteratively with cycle detection
(unreachable cycles get undef). Adds the four upstream regression
tests verbatim (all fail pre-fix; cycle/chain case overflows pre-fix).
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.

3 participants