Recompiler: fold constant CFG terminators and recompute live DXIL flags #3268
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3268"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T02:23:48Z.
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. Thev_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 leftDoubleExtensionsdeclared, 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:
AtomicInt64GroupSharedfor acmpxchgonly when the instruction's type was i64, but acmpxchgis 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).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'sdrop_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'sf089a9df(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); constantCondBr/Switchfolded (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=cfgturns it off.crates/xcl/src/gpu/dxil/shader.rs,module.rs:infer_type_featuresrecomputes Int64Ops, Doubles, DoubleExtensions (f64 FMA too), resource / sampler heap indexing, 64-bit atomics and typed UAV load formats from the instructions that survivedoptimize()simplify_phiskeeps main's back-edge check from5a984e02(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 keptTested
cargo test --workspace --no-fail-fast -- --test-threads=4on04df5904: 2613 passed, 4 failed, 49 ignored. The 4 failures aregpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3(E_OUTOFMEMORYfromCreateComputePipelineState, seeds 70 / 545 / 1019 / 1507). They fail the same way on this branch's base, mainfa204595, on this machine, and pass here on newer main (#3264, rebased onto0fe06eb0, has 0 failures). SKIPPED GPU tests: 30 distinct notices, almost allno wave64.-StartupTimeoutSeconds 3600for the cold pipeline rebuild new DXIL needs.Regression run
main:
this PR:
Not tested
AI use
3c54e558(raylitebull, fromf4b07062, written with Antigravity (Gemini 4 Neon High)), itself a port of #1818.🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T03:23:08Z.
merge-check
base:
fa204595a4head:
527a8b2671merge: 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 --librerun skipping the 2 knownmain#3187uma_drivermixed-window testsa_refused_gpu_upload_segment_becomes_customandmixed_window_keeps_its_bytes_and_the_gpu_sees_every_segment) -> 2620 passed, 0 failed, 49 ignored (2481 lib + 139 integration, 25 SKIPPED notices; all 4fuzz_validate_and_gpu_0..3passed); failing: none. 150 sxcl-smoke(ea3b5e4d,XCL_PREPARE=0, RTX 5070 Ti) on the combined 8-PR stack:exit: 0x0,2096frames shown,0device removals,0crashes,0display System events,32049 MBpeak commit.review: none (diff reviewed: bounded 16-pass RPO constant-CFG terminator/path-fact folding preserving loop backedges and main's
simplify_phislayout-order check, plusShader::infer_type_features()reconciliation against surviving reachable instructions afteroptimize()).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.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T06:39:32Z.
merge-check
base:
fa204595head:
a343eafcverdict: safe to merge
Re-verified updated head
a343eafc(fast-forward ontoorigin/mainfa204595, tested together with#32694a8217a7on NVIDIA GeForce RTX 5070 Ti with DXC 1.9.2609.5):gpu::dxil::: 241 passed, 0 failed, 3 ignored (includingfuzz_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 knownmain#3187uma_drivermixed-window tests skipped).Build and test (Windows, no GPU)): green.Imported from ivall/xcl. Originally posted by throny on 2026-11-29T09:01:33Z.
merge-check claim
head:
a343eafc06Imported from ivall/xcl. Originally posted by throny on 2026-11-29T09:20:38Z.
merge-check
base:
d8bd9a38head:
a343eafc06merge: 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
d8bd9a38: 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 forCmpXchgonly 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).
Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T17:18:55Z.
Thanks throny. Changes in
04df5904:v_movrel*change is #3266's commit, which this PR has to carry. The stack is now in order: #3266's own4a20b256first, then the CFG commit on top. Once #3266 merges, only the CFG commit is left in this PR's diff.cmpxchg's type from its compared value, since the instruction itself is typed as the{ T, i1 }pair. The testlive_64_bit_group_shared_atomics_keep_their_shader_flagfails without the fix.CondBr(c, t, t), or a switch with two cases to one block, kept one phi entry per case after the fold. The fold now drops entries per removed edge, assimplify_cfgdoes. The testa_constant_switch_with_two_cases_to_one_block_leaves_one_phi_entryshows 3 entries for 2 edges without the fix.The new suite and smoke runs are in the description. #3269 is rebased onto this.
(Written with Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-29T22:39:25Z.
Review: COMMENTED
Static review of
527a8b26for 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
AtomicInt64GroupSharedon a live 64-bitcmpxchgbecause 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 at04df5904. I verified the fixes in the rebased copy under #3269. Historical feature scan ?? Historical PHI pruningReview assisted by Codex (GPT-6, exact variant unavailable). No new build or GPU run for this review.