Recompiler: fold zext(i1) boolean comparisons and trunc in direct DXIL opt #2278

Open
github-bot wants to merge 1 commit from github-bot/gh-2278 into main AGit
Owner

@⁠ramon-victor on GitHub · 2026-11-25 12:06 UTC · original
ramon-victor:opt/dxil-fold-zext-bool-cmp → main · +135 −0 · 1 files · 1 commits · open

Folds icmp comparisons against integer constants and trunc ... to i1 on zext i1 %c to iN inside Module::simplify_op (gpu::dxil::module::opt).

In direct DXIL lowering, scalar condition codes (SCC) and boolean results stored in integer registers are widened with zext i1 %c to i32 and subsequently tested with icmp ne %scc, 0 (e.g. for s_cbranch_scc1, s_cselect_b32, s_cmov_b32). Evaluating the comparison against {0, 1} directly collapses identity tests (ne 0, eq 1, ugt 0, uge 1, sgt 0, sge 1, and their commuted forms) back to %c and always-true/always-false range tests (eq 2, ne 2, ult 2, slt 0, etc.) to i1 constants without creating new instructions, letting DCE remove the dead zext and icmp.

Part of #1269.

What is in it

  • crates/xcl/src/gpu/dxil/module/opt.rs:
    • Add Module::zext_i1_inner to match CastOp::ZExt from i1.
    • In Module::simplify_op, fold Op::Cmp when one operand is zext i1 %c to iW (2 <= W <= 32) and the other is an integer constant, returning %c or a constant i1 whenever the {0, 1} truth table requires no new instruction (leaving negated compares such as zext(c) == 0 intact).
    • In Module::simplify_op, fold trunc (zext i1 %c to iW) to i1 directly to %c.
    • Add simplify_op_folds_zext_i1_comparisons_and_trunc and direct_scc_branch_drops_zext_icmp_roundtrip unit + DXIL validation/disassembly tests.

On a seeded 600-program sample (xcl shaders <archive> --direct --sample 600 --seed 3 --max-bytes 40000 --validate --threads 8, 573 programs within the cap), all 573/573 validate cleanly (0 validator failures):

  • Total emitted DXIL: 41,323,736 B -> 41,249,464 B (-74,272 B, -0.18%)
  • Translation time p95: 263.12 ms -> 260.53 ms

Tested

  • Hardware and toolchain: NVIDIA GeForce RTX 3070 (8 GiB, driver 621.24, wave32), AMD Ryzen 7 5700X, Windows 11 Pro 26300, rustc 1.99.0 (x86_64-pc-windows-msvc), DXC/DXIL 1.8.2502.11 (Windows SDK 10.0.26100.0).
  • cargo test -p xcl --lib gpu::dxil::module::opt::tests (debug): 8 passed; 0 failed (including both new tests).
  • .\scripts\test.ps1 (debug, full workspace): 1252 passed; 0 failed; 46 ignored (25 GPU sub-checks skipped on this wave32 adapter: 21 no wave64, 1 no heap windows, 3 per-lane #1426 refusals).
  • cargo build --release -p xcl + direct DXIL validation sweep on 573 sampled retail shader programs: 573/573 translated and dxil.dll-validated (0 failures).

Regression run

xcl-smoke v1
commit: f087b998d0d51a9f11bbfa0761967a63e778edd9
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14467 MB
xcl-smoke v1
commit: cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1620 (PresentX 1620)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14449 MB

Not tested

  • Native wave64 GPU hardware (AMD/Intel): RTX 3070 only supports wave32, so native wave64 tests skip on this adapter.
  • Sample-gated tests (XCL_SAMPLE_* env vars unset).

AI use

  • Antigravity (Gemini 4 Neon): implemented zext(i1) comparison and trunc folding in module/opt.rs, added IR and DXIL disassembly/validation tests, and ran the workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.
**@⁠ramon-victor** on GitHub · 2026-11-25 12:06 UTC · [original](https://github.com/xcl-dev/xcl/pull/2278) `ramon-victor:opt/dxil-fold-zext-bool-cmp` → `main` · +135 −0 · 1 files · 1 commits · open Folds `icmp` comparisons against integer constants and `trunc ... to i1` on `zext i1 %c to iN` inside `Module::simplify_op` (`gpu::dxil::module::opt`). In direct DXIL lowering, scalar condition codes (`SCC`) and boolean results stored in integer registers are widened with `zext i1 %c to i32` and subsequently tested with `icmp ne %scc, 0` (e.g. for `s_cbranch_scc1`, `s_cselect_b32`, `s_cmov_b32`). Evaluating the comparison against `{0, 1}` directly collapses identity tests (`ne 0`, `eq 1`, `ugt 0`, `uge 1`, `sgt 0`, `sge 1`, and their commuted forms) back to `%c` and always-true/always-false range tests (`eq 2`, `ne 2`, `ult 2`, `slt 0`, etc.) to `i1` constants without creating new instructions, letting DCE remove the dead `zext` and `icmp`. Part of #1269. **What is in it** - `crates/xcl/src/gpu/dxil/module/opt.rs`: - Add `Module::zext_i1_inner` to match `CastOp::ZExt` from `i1`. - In `Module::simplify_op`, fold `Op::Cmp` when one operand is `zext i1 %c to iW` (`2 <= W <= 32`) and the other is an integer constant, returning `%c` or a constant `i1` whenever the `{0, 1}` truth table requires no new instruction (leaving negated compares such as `zext(c) == 0` intact). - In `Module::simplify_op`, fold `trunc (zext i1 %c to iW) to i1` directly to `%c`. - Add `simplify_op_folds_zext_i1_comparisons_and_trunc` and `direct_scc_branch_drops_zext_icmp_roundtrip` unit + DXIL validation/disassembly tests. On a seeded 600-program sample (`xcl shaders <archive> --direct --sample 600 --seed 3 --max-bytes 40000 --validate --threads 8`, 573 programs within the cap), all 573/573 validate cleanly (`0` validator failures): - Total emitted DXIL: `41,323,736 B -> 41,249,464 B` (`-74,272 B`, `-0.18%`) - Translation time p95: `263.12 ms -> 260.53 ms` **Tested** - Hardware and toolchain: NVIDIA GeForce RTX 3070 (8 GiB, driver `621.24`, wave32), AMD Ryzen 7 5700X, Windows 11 Pro `26300`, `rustc 1.99.0` (`x86_64-pc-windows-msvc`), DXC/DXIL `1.8.2502.11` (Windows SDK `10.0.26100.0`). - `cargo test -p xcl --lib gpu::dxil::module::opt::tests` (debug): `8 passed; 0 failed` (including both new tests). - `.\scripts\test.ps1` (debug, full workspace): `1252 passed; 0 failed; 46 ignored` (25 GPU sub-checks skipped on this wave32 adapter: 21 `no wave64`, 1 `no heap windows`, 3 per-lane `#1426` refusals). - `cargo build --release -p xcl` + direct DXIL validation sweep on 573 sampled retail shader programs: `573/573` translated and `dxil.dll`-validated (`0` failures). **Regression run** ``` xcl-smoke v1 commit: f087b998d0d51a9f11bbfa0761967a63e778edd9 gpu: NVIDIA GeForce RTX 3070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14467 MB ``` ``` xcl-smoke v1 commit: cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8 gpu: NVIDIA GeForce RTX 3070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1620 (PresentX 1620) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14449 MB ``` **Not tested** - Native wave64 GPU hardware (AMD/Intel): RTX 3070 only supports wave32, so native wave64 tests skip on this adapter. - Sample-gated tests (`XCL_SAMPLE_*` env vars unset). **AI use** - Antigravity (Gemini 4 Neon): implemented `zext(i1)` comparison and `trunc` folding in `module/opt.rs`, added IR and DXIL disassembly/validation tests, and ran the workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:25:23 +00:00
Author
Owner

@⁠itsSio on GitHub · 2026-11-25 12:34 UTC · original

Tested

Clean PR head cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8, Windows 11 Pro build 26200, x64 MSVC, unoptimized dev/test profiles with debug info off. cargo build --workspace --locked passed on both PCs.

cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture:

GPU / driver CPU, RAM, toolchain Passed / failed / ignored Runtime SKIPPED Exit
RTX 5080 / 32.0.16.1088 Ryzen 7 9800X3D, 32 GiB; Rust 1.97.0, DXC/DXIL 1.9.2609.5 1252 / 0 / 46 25 0
RTX 2080 SUPER / 32.0.16.1088 Ryzen 5 5600X, 16 GiB; Rust 1.99.1, DXC/DXIL 1.8.2502.11 1251 / 1 / 46 27 101

cargo test -p xcl --lib --locked gpu::dxil::module::opt::tests -- --test-threads=1 --nocapture: 8 / 0 / 0, no runtime skips, on both PCs. Both new tests passed: boolean folding and direct SCC branch lowering. They check the optimizer and generated DXIL, not GPU readback.

The RTX 2080 SUPER workspace failed at vfs_hooks::tests::move_with_locked_overlay_source_preserves_destination: “move should succeed after the source handle closes.” Running that test alone on the same head (cargo test -p xcl --lib --locked vfs_hooks::tests::move_with_locked_overlay_source_preserves_destination -- --exact --test-threads=1 --nocapture) passed on both PCs (1 / 0 / 0, no skips). The original workspace failure stands; the isolated passes do not establish its cause.

Not tested

Exact parent, AMD, native wave64, retail samples, and performance.

AI use

Ran via Samaritan:

  • OpenCode with OpenAI GPT-6.2 Sol (openai/gpt-6.2-sol): ran both PCs' checks, audited logs, and wrote this report. No subagents contributed.
**@⁠itsSio** on GitHub · 2026-11-25 12:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/2278#issuecomment-7162550629) **Tested** Clean PR head `cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8`, Windows 11 Pro build 26200, x64 MSVC, unoptimized dev/test profiles with debug info off. `cargo build --workspace --locked` passed on both PCs. `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture`: | GPU / driver | CPU, RAM, toolchain | Passed / failed / ignored | Runtime `SKIPPED` | Exit | | --- | --- | ---: | ---: | ---: | | RTX 5080 / `32.0.16.1088` | Ryzen 7 9800X3D, 32 GiB; Rust 1.97.0, DXC/DXIL 1.9.2609.5 | 1252 / 0 / 46 | 25 | 0 | | RTX 2080 SUPER / `32.0.16.1088` | Ryzen 5 5600X, 16 GiB; Rust 1.99.1, DXC/DXIL 1.8.2502.11 | 1251 / 1 / 46 | 27 | 101 | `cargo test -p xcl --lib --locked gpu::dxil::module::opt::tests -- --test-threads=1 --nocapture`: **8 / 0 / 0**, no runtime skips, on both PCs. Both new tests passed: boolean folding and direct SCC branch lowering. They check the optimizer and generated DXIL, not GPU readback. The RTX 2080 SUPER workspace failed at `vfs_hooks::tests::move_with_locked_overlay_source_preserves_destination`: “move should succeed after the source handle closes.” Running that test alone on the same head (`cargo test -p xcl --lib --locked vfs_hooks::tests::move_with_locked_overlay_source_preserves_destination -- --exact --test-threads=1 --nocapture`) passed on both PCs (1 / 0 / 0, no skips). The original workspace failure stands; the isolated passes do not establish its cause. **Not tested** Exact parent, AMD, native wave64, retail samples, and performance. **AI use** Ran via Samaritan: - OpenCode with OpenAI GPT-6.2 Sol (`openai/gpt-6.2-sol`): ran both PCs' checks, audited logs, and wrote this report. No subagents contributed.
Author
Owner

@⁠nnonno91 on GitHub · 2026-11-25 15:10 UTC · original

Independent Windows test of cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8. The optimized comparison selected the expected value for true and false inputs on both adapters.

Tested

  • Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Radeon integrated graphics (32.0.21042.62), RTX 5090 (32.0.16.1742), Rust 1.100.0 x64 MSVC, Windows SDK DXC/DXIL 1.8.2502.11. Unoptimized test profile.
  • Ran cargo test --locked -p xcl --lib gpu::dxil::module::opt::tests -- --nocapture --test-threads=1 once per explicitly selected adapter. Each run reported 8 passed, 0 failed, 0 ignored, and no logged skips.
  • Extended the existing SCC fixture locally to execute its DXIL on the GPU after the existing validation and disassembly checks. The scalar select chose the true value for input 1 and the false value for 0, 2, and u32::MAX. The logs confirmed all four results on Radeon integrated graphics and RTX 5090.

Not tested

A separate GPU control-flow branch fixture, the parent revision, retail titles, or the full workspace suite. The fixture extension was temporary.

AI use

OpenAI Codex (GPT-6.2 Sol) prepared and ran the tests, verified the results, and wrote this report.

**@⁠nnonno91** on GitHub · 2026-11-25 15:10 UTC · [original](https://github.com/xcl-dev/xcl/pull/2278#issuecomment-7166424265) Independent Windows test of `cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8`. The optimized comparison selected the expected value for true and false inputs on both adapters. **Tested** - Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Radeon integrated graphics (32.0.21042.62), RTX 5090 (32.0.16.1742), Rust 1.100.0 x64 MSVC, Windows SDK DXC/DXIL 1.8.2502.11. Unoptimized test profile. - Ran `cargo test --locked -p xcl --lib gpu::dxil::module::opt::tests -- --nocapture --test-threads=1` once per explicitly selected adapter. Each run reported 8 passed, 0 failed, 0 ignored, and no logged skips. - Extended the existing SCC fixture locally to execute its DXIL on the GPU after the existing validation and disassembly checks. The scalar select chose the true value for input 1 and the false value for 0, 2, and `u32::MAX`. The logs confirmed all four results on Radeon integrated graphics and RTX 5090. **Not tested** A separate GPU control-flow branch fixture, the parent revision, retail titles, or the full workspace suite. The fixture extension was temporary. **AI use** OpenAI Codex (GPT-6.2 Sol) prepared and ran the tests, verified the results, and wrote this report.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 17:05 UTC · original

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

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 17:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/2278#issuecomment-7168981278) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
Author
Owner

@⁠raylitebull on GitHub · 2026-11-25 18:41 UTC · original

Diff review, CI, and both AMD and NVIDIA checks passed on cbfb4ee, but recent origin/main merges turned this branch CONFLICTING/DIRTY. Please rebase onto origin/main so it becomes mergeable again.

**@⁠raylitebull** on GitHub · 2026-11-25 18:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/2278#issuecomment-7171248799) Diff review, CI, and both AMD and NVIDIA checks passed on `cbfb4ee`, but recent `origin/main` merges turned this branch `CONFLICTING/DIRTY`. Please rebase onto `origin/main` so it becomes mergeable again.
Author
Owner

@⁠emirduran707 on GitHub · 2026-11-25 18:46 UTC · original

Independent AMD RDNA4 (native wave64) test report on exact head cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1250 passed, 2 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • Both PR tests passed: gpu::dxil::module::opt::tests::simplify_op_folds_zext_i1_comparisons_and_trunc and direct_scc_branch_drops_zext_icmp_roundtrip.
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 seed 722 and fuzz_validate_and_gpu_3 seed 1599) are pre-existing on unmodified main on this GPU.

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1574 (PresentX 1574)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14932 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.
**@⁠emirduran707** on GitHub · 2026-11-25 18:46 UTC · [original](https://github.com/xcl-dev/xcl/pull/2278#issuecomment-7171388807) Independent AMD RDNA4 (native wave64) test report on exact head `cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8` (clean detached checkout, no local changes). **Tested** - AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2602.17; tests debug, smoke release. - `cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4`: **1250 passed, 2 failed, 46 ignored; 8 SKIPPED** (6 in `lower_dxil::tests::{f16,f32}_programs_on_the_gpu` refused by per-lane form; `heap_window_out_of_memory_falls_back_to_the_switch_form`; `pso_measurement_compiles_an_exact_synthetic_record`). All native wave64 GPU tests ran and passed. - Both PR tests passed: `gpu::dxil::module::opt::tests::simplify_op_folds_zext_i1_comparisons_and_trunc` and `direct_scc_branch_drops_zext_icmp_roundtrip`. - The 2 failures (`gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1` seed 722 and `fuzz_validate_and_gpu_3` seed 1599) are pre-existing on unmodified `main` on this GPU. **Regression run** (60 s retail-title smoke, `XCL_NETWORK=0`, release) ``` xcl-smoke v1 commit: cbfb4ee11d758687e5a8b0d842a8e5a53d9621f8 gpu: AMD Radeon RX 9060 XT title: 21222967 seconds: 60 exit: 0x0 frames shown: 1574 (PresentX 1574) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14932 MB ``` (Matched `main` `68a284a` baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.) **Not tested** - D3D12 debug layer; Intel GPUs. **AI use** - Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/gpu/dxil/module/opt.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2278/head:github-bot/gh-2278'
git switch 'github-bot/gh-2278'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2278'
git switch 'github-bot/gh-2278'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2278'
git switch 'github-bot/gh-2278'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2278'
git switch 'main'
git merge --squash 'github-bot/gh-2278'
git switch 'main'
git merge --ff-only 'github-bot/gh-2278'
git switch 'main'
git merge 'github-bot/gh-2278'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.