Skip to content

Recompiler: fold constant CFG terminators and recompute live DXIL flags - #3268

Merged
raylitebull merged 2 commits into
xcl-dev:mainfrom
Foniass:dxil-cfg-fold-live-flags
Nov 29, 2026
Merged

raylitebull merged 2 commits into
xcl-dev:mainfrom
Foniass:dxil-cfg-fold-live-flags

Conversation

@Foniass

@Foniass Foniass commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #3266: the first commit here is #3266's own commit (4a20b256), and the CFG commit (04df5904) is on top. Once #3266 merges, only the CFG commit remains in this PR. The v_movrel* change in this PR's diff is #3266's, not this PR's. The CFG folding needs #3266 first: folding removes f64 instructions but left DoubleExtensions declared, and the validator rejected the module (fuzz_validate_and_gpu_0..2, seeds 33 / 641 / 1448, found by CI). #3269 builds on these two commits.

Review fixes (throny), in the CFG commit:

  • The live feature scan kept AtomicInt64GroupShared for a cmpxchg only when the instruction's type was i64, but a cmpxchg is typed as its { T, i1 } pair, so a 64-bit group-shared compare-exchange lost its flag. T now comes from the compared value (live_64_bit_group_shared_atomics_keep_their_shader_flag).
  • A folded CondBr(c, t, t), or a constant switch with several cases to one block, kept one phi entry per case in the target although only one edge remains. Main keeps one entry per edge (simplify_cfg's drop_phi_edges), and the fold now does too (a_constant_switch_with_two_cases_to_one_block_leaves_one_phi_entry).

Both tests fail without their fix and pass with it.

Overlaps #1818 (mokkamokacaffe, the original constant-CFG folding, conflicting with main): this is amd-dev's port of it (3c54e558, raylitebull), which adds the live shader-flag fix below. Maintainers, keep one. The IR optimizations of amd-dev's f089a9df (next PR) build on this one.

The direct DXIL optimizer did not fold a conditional branch or switch whose condition is constant (directly, or because a dominating branch already decided it), so dead branches survived. Their instructions also left shader flags set (Int64Ops, Doubles, ResourceHeapIndexing, ...) that the surviving code no longer needs.

What is in it

  • crates/xcl/src/gpu/dxil/module/opt.rs: CSE / trivial-phi replacements resolved before CFG folding; dominating branch / switch facts (bounded to 16 RPO passes); constant CondBr / Switch folded (phi entries dropped per removed edge); unreachable blocks and obsolete phi edges dropped (loop back-edges kept); trivial phis simplified again. XCL_DXIL_NO_OPT=cfg turns it off.
  • crates/xcl/src/gpu/dxil/shader.rs, module.rs: infer_type_features recomputes Int64Ops, Doubles, DoubleExtensions (f64 FMA too), resource / sampler heap indexing, 64-bit atomics and typed UAV load formats from the instructions that survived optimize()
  • tests: constant branch / switch, dominated branch, reconvergence, loop phi back-edge, pruned i64 / heap flags, live 64-bit group-shared atomics, one phi entry per edge after a fold
  • port to main (in the commit message): the shared simplify_phis keeps main's back-edge check from 5a984e02 (a phi is not replaced by a value defined after it), with positions recomputed after the CFG fold; main's select pass and tests are kept

Tested

  • RTX 5070 Ti (driver 32.0.16.2114), AMD Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug tests, release smoke
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 04df590: 2613 passed, 4 failed, 49 ignored. The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (E_OUTOFMEMORY from CreateComputePipelineState, seeds 70 / 545 / 1019 / 1507). They fail the same way on this branch's base, main fa20459, on this machine, and pass here on newer main (Recompiler: early-exit-safe multi-wave s_barrier in direct DXIL #3264, rebased onto 0fe06eb, has 0 failures). SKIPPED GPU tests: 30 distinct notices, almost all no wave64.
  • Retail title: 60 s smoke run below. The PR's run used -StartupTimeoutSeconds 3600 for the cold pipeline rebuild new DXIL needs.

Regression run

main:

xcl-smoke v1
commit: 3b6a91ed096d6d6e0fee52cfb86dc0a7d7c8c1b0
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1193 (PresentX 1193)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15684 MB

this PR:

xcl-smoke v1
commit: 04df590403858a99d8c9d5f562ac0f53ea1085b0
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1596 (PresentX 1596)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 17214 MB

Not tested

  • AMD GPUs; whether the image looks the same; titles other than this one

AI use

  • Claude Code with Claude Opus 5.5: ported the commit to main, merged it with main's back-edge fix, made the review fixes and their tests, ran the tests and the smoke run above and wrote this description.
  • Original commit: amd-dev 3c54e558 (raylitebull, from f4b07062, written with Antigravity (Gemini 4 Neon High)), itself a port of dxil: fold constant CFG terminators after CSE #1818.

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Nov 29, 2026
Fixes two Direct DXIL correctness bugs (xcl-dev#1894, xcl-dev#1890):
- Module::uses_double_extensions now iterates live instructions in self.blocks
  (matching insts_use_type) instead of all self.insts, preventing DCE-removed
  f64 ops from spuriously setting DoubleExtensions shader flags.
- Lw::top_vgpr now incorporates Lw::max_vgpr precomputed across all decoded
  instructions (init.touched is empty outside Entry::Kernel), and v_movrels /
  v_movreld / v_movrelsd mask M0 to M0[7:0] (& 0xFF) per RDNA2 ISA.

Assisted-by: Antigravity (Gemini 4 Neon High)

(cherry picked from commit 2c33f9f)

Port onto nvidia-dev: only `max_vgpr` is added to Lw; the
`barrier_uav_fence` field next to it upstream belongs to 85a9480,
which this branch does not have.

(cherry picked from commit aac4343)

Ported to main without the neighbouring test of fcbd0d1, which is a PR of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kalmarv

Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: fa20459
head: 527a8b2
merge: clean onto main and with the 7-PR stack.
build: cargo build --workspace -> ok; cargo build --release -p xcl -> ok.
tests: tested in the clean 7-PR stack (#3250+#3261+#3263+#3264+#3265+#3266+#3268) on main fa204595: cargo test --workspace --no-fail-fast -- --test-threads=4 (DXC 1.9.2609.5; -p xcl --lib rerun skipping the 2 known main #3187 uma_driver mixed-window tests a_refused_gpu_upload_segment_becomes_custom and mixed_window_keeps_its_bytes_and_the_gpu_sees_every_segment) -> 2620 passed, 0 failed, 49 ignored (2481 lib + 139 integration, 25 SKIPPED notices; all 4 fuzz_validate_and_gpu_0..3 passed); failing: none. 150 s xcl-smoke (ea3b5e4d, XCL_PREPARE=0, RTX 5070 Ti) on the combined 8-PR stack: exit: 0x0, 2096 frames shown, 0 device removals, 0 crashes, 0 display System events, 32049 MB peak commit.
review: none (diff reviewed: bounded 16-pass RPO constant-CFG terminator/path-fact folding preserving loop backedges and main's simplify_phis layout-order check, plus Shader::infer_type_features() reconciliation against surviving reachable instructions after optimize()).
performance: positive: prunes dead branches and stale shader flags at translation time; no runtime regression in 150 s smoke.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 5070 Ti 16 GB (wave32 only), Windows 11, DXC 1.9.2609.5 via XCL_DXC_DIR; Antigravity (Gemini 3.5 Pro) ran the merges, builds, workspace test suites, 150 s title smokes, diff reviews, and drafted this check.

@Foniass
Foniass marked this pull request as ready for review November 29, 2026 03:27
@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 29, 2026
@Kalmarv

Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: fa20459
head: a343eaf
verdict: safe to merge

Re-verified updated head a343eafc (fast-forward onto origin/main fa204595, tested together with #3269 4a8217a7 on NVIDIA GeForce RTX 5070 Ti with DXC 1.9.2609.5):

  • gpu::dxil::: 241 passed, 0 failed, 3 ignored (including fuzz_validate_and_gpu_0..3).
  • xcl --lib: 2477 passed, 0 failed, 29 ignored (5 parallel-contention tests re-verified 5/5 with --test-threads=1; 2 known main #3187 uma_driver mixed-window tests skipped).
  • GitHub CI (Build and test (Windows, no GPU)): green.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 29, 2026
@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: a343eaf

@throny

throny commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

merge-check
base: d8bd9a3
head: a343eaf
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2625 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at d8bd9a3: 2618 passed, 0 failed, 49 ignored.
review: two findings. (1) Scope: besides the CFG folding and the flag recomputation of the title, the diff changes v_movrel* (the index is masked with 0xFF and a program-wide highest VGPR is used). That is a separate behaviour change, not in the title, and it makes direct DXIL differ from the HLSL path, which does not mask. (2) In the scan for live features, the 64-bit atomic flag is kept for CmpXchg only when the instruction's type is i64, but a compare-exchange's type is the pair of value and flag, so a 64-bit groupshared compare-exchange would lose its flag. Not reached today as far as I found (only 32-bit ones are emitted on main), but it is wrong as written. A third point from reading, not confirmed: the new fold turns a conditional branch with two equal targets into a plain branch and prunes phi entries per predecessor rather than per edge. The dominating-branch facts look sound and deterministic. This PR overlaps the older open #1818 (listed on #1607).
performance: not measured.
verdict: not yet: move the v_movrel* change into its own PR and fix the compare-exchange type test in the feature scan.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

@github-actions github-actions Bot added checks-disagree Merge-checks disagree (safe vs not yet); needs maintainer/3rd look and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 29, 2026
Port xcl-dev#1818's constant-CFG terminator and path-fact folding onto Direct DXIL
and fix the stale shader-flag validation gap that blocked it:
1. Resolve CSE and trivial-PHI replacements before CFG folding, propagate
   dominating branch/switch path facts (bounded to 16 RPO passes so large
   shaders stay linear), fold constant CondBr/Switch terminators, drop newly
   unreachable blocks and obsolete PHI edges (preserving loop backedges), and
   rerun trivial-PHI simplification (opt-out via XCL_DXIL_NO_OPT=cfg).
2. Reconcile Int64Ops, Doubles, DoubleExtensions (including f64 FMA),
   ResourceHeapIndexing, SamplerHeapIndexing, AtomicInt64*, and
   TypedUavLoadAdditionalFormats in Shader::infer_type_features() against
   surviving reachable instructions after optimize(), preventing stale shader
   flags when dead branches are pruned.

Adds unit tests covering constant branches/switches, dominated branch facts,
reconvergence, loop PHI backedges, and pruned i64/heap shader flags.

Assisted-by: Antigravity (Gemini 4 Neon High)
(cherry picked from commit f4b0706)
(cherry picked from commit 3c54e55)

Ported to main: simplify_phis keeps main's back-edge check (5a984e0: a phi is not replaced by a value defined after it in the layout), with positions recomputed after the CFG fold; XCL_DXIL_NO_OPT lists both main's select and this cfg pass; main's tests stay next to the new ones.

Review fixes (xcl-dev#3268): a cmpxchg is typed as its { T, i1 } pair, so the
live scan now takes T from the compared value and keeps
AtomicInt64GroupShared for a 64-bit group-shared cmpxchg; and a folded
CondBr(c, t, t) or a switch with several cases to one block keeps one
phi entry per remaining edge (drop_phi_edges, as simplify_cfg does), not
one per case. Both have a test that fails without the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Foniass
Foniass force-pushed the dxil-cfg-fold-live-flags branch from a343eaf to 04df590 Compare November 29, 2026 17:15
@github-actions github-actions Bot removed checks-disagree Merge-checks disagree (safe vs not yet); needs maintainer/3rd look needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 29, 2026
@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks throny. Changes in 04df590:

The new suite and smoke runs are in the description. #3269 is rebased onto this.

(Written with Claude Code, Claude Opus 5.5.)

Foniass pushed a commit to Foniass/xcl that referenced this pull request Nov 29, 2026
Port xcl-dev#1818's constant-CFG terminator and path-fact folding onto Direct DXIL
and fix the stale shader-flag validation gap that blocked it:
1. Resolve CSE and trivial-PHI replacements before CFG folding, propagate
   dominating branch/switch path facts (bounded to 16 RPO passes so large
   shaders stay linear), fold constant CondBr/Switch terminators, drop newly
   unreachable blocks and obsolete PHI edges (preserving loop backedges), and
   rerun trivial-PHI simplification (opt-out via XCL_DXIL_NO_OPT=cfg).
2. Reconcile Int64Ops, Doubles, DoubleExtensions (including f64 FMA),
   ResourceHeapIndexing, SamplerHeapIndexing, AtomicInt64*, and
   TypedUavLoadAdditionalFormats in Shader::infer_type_features() against
   surviving reachable instructions after optimize(), preventing stale shader
   flags when dead branches are pruned.

Adds unit tests covering constant branches/switches, dominated branch facts,
reconvergence, loop PHI backedges, and pruned i64/heap shader flags.

Assisted-by: Antigravity (Gemini 4 Neon High)
(cherry picked from commit f4b0706)
(cherry picked from commit 3c54e55)

Ported to main: simplify_phis keeps main's back-edge check (5a984e0: a phi is not replaced by a value defined after it in the layout), with positions recomputed after the CFG fold; XCL_DXIL_NO_OPT lists both main's select and this cfg pass; main's tests stay next to the new ones.

Review fixes (xcl-dev#3268): a cmpxchg is typed as its { T, i1 } pair, so the
live scan now takes T from the compared value and keeps
AtomicInt64GroupShared for a 64-bit group-shared cmpxchg; and a folded
CondBr(c, t, t) or a switch with several cases to one block keeps one
phi entry per remaining edge (drop_phi_edges, as simplify_cfg does), not
one per case. Both have a test that fails without the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@raylitebull
raylitebull merged commit 5b0d5fa into xcl-dev:main Nov 29, 2026
4 of 5 checks passed

@KOLESNiii KOLESNiii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review of 527a8b26 for the stack in the #3041 comparison.

For readers of the #3041 comparison, the measured CFG commit had two issues already addressed by the later revision. The feature scan could clear AtomicInt64GroupShared on a live 64-bit cmpxchg because it checked the {T, i1} result type. The fold could also retain too many PHI inputs when several switch edges to one block collapsed into one. The PR documents both fixes at 04df5904. I verified the fixes in the rebased copy under #3269. Historical feature scan ?? Historical PHI pruning

Review assisted by Codex (GPT-6, exact variant unavailable). No new build or GPU run for this review.

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

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants