Recompiler: fold shift-masks, nested masks, double-xor, and all-ones or in direct DXIL opt #2269

Merged
ramon-victor merged 1 commit from upstream-pr/2279 into main 2026-11-25 18:37:38 +00:00
ramon-victor commented 2026-11-25 12:11:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ramon-victor on 2026-11-25T12:11:36Z.


Adds zero-allocation algebraic bitmask and identity simplifications to Module::simplify_op (gpu::dxil::module::opt) for <= 32-bit integer Op::Bin instructions:

  • All-ones or absorption (x | ~0 -> ~0, ~0 | x -> ~0) and 0 >>a x -> 0.
  • Double-xor cancellation ((x ^ k) ^ k -> x in all operand orders, collapsing double lnot / bitwise not).
  • Redundant and disjoint masks after constant shifts:
    • (x >>u s) & m -> x >>u s when (mask >> s) & m == (mask >> s) (e.g. (x >> 24) & 0xFF in v_cvt_f32_ubyte3, (x >> 16) & 0xFFFF, (x >> 31) & 1), or 0 when disjoint.
    • (x << s) & m -> x << s when ((mask << s) & mask) & m == ((mask << s) & mask) (e.g. (x << 2) & ~3 in dword-aligned buffer addressing), or 0 when disjoint.
  • Nested constant masks: ((x & c1) & c2) -> x & c1 when c1 & c2 == c1 (or 0 when c1 & c2 == 0), and ((x | c1) | c2) -> x | c1 when c1 | c2 == c1 (or ~0 when c1 | c2 == ~0).
  • Masking zext(i1): zext(i1) & m -> zext(i1) when m & 1 != 0, or 0 when m & 1 == 0.

All rules return existing dominating SSA values or constants without creating instructions and keep w > 32 untouched so Int64Ops flags stay consistent.

Part of #1269.

What is in it

  • crates/xcl/src/gpu/dxil/module/opt.rs: add the above algebraic bitmask and identity folds inside Module::simplify_op, plus simplify_op_folds_bitmasks_double_xor_and_all_ones_or testing every rule and verifying SSA well-formedness.

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,273,276 B (-50,460 B, -0.12%)

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): 7 passed; 0 failed (including simplify_op_folds_bitmasks_double_xor_and_all_ones_or).
  • .\scripts\test.ps1 (debug, full workspace): 1251 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: 38f47bfaf1d00332560535c8f202482cf77d630e
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1613 (PresentX 1613)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14476 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 the bitmask, shift-mask, double-xor, and all-ones or folds in module/opt.rs, added unit tests covering each rule, and ran the workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.
<!-- greyfinch-sync:ivall/xcl:issue:5491074160 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2279). Originally posted by [ramon-victor](https://github.com/ramon-victor) on 2026-11-25T12:11:36Z. --- Adds zero-allocation algebraic bitmask and identity simplifications to `Module::simplify_op` (`gpu::dxil::module::opt`) for `<= 32`-bit integer `Op::Bin` instructions: - All-ones `or` absorption (`x | ~0 -> ~0`, `~0 | x -> ~0`) and `0 >>a x -> 0`. - Double-`xor` cancellation (`(x ^ k) ^ k -> x` in all operand orders, collapsing double `lnot` / bitwise `not`). - Redundant and disjoint masks after constant shifts: - `(x >>u s) & m -> x >>u s` when `(mask >> s) & m == (mask >> s)` (e.g. `(x >> 24) & 0xFF` in `v_cvt_f32_ubyte3`, `(x >> 16) & 0xFFFF`, `(x >> 31) & 1`), or `0` when disjoint. - `(x << s) & m -> x << s` when `((mask << s) & mask) & m == ((mask << s) & mask)` (e.g. `(x << 2) & ~3` in dword-aligned buffer addressing), or `0` when disjoint. - Nested constant masks: `((x & c1) & c2) -> x & c1` when `c1 & c2 == c1` (or `0` when `c1 & c2 == 0`), and `((x | c1) | c2) -> x | c1` when `c1 | c2 == c1` (or `~0` when `c1 | c2 == ~0`). - Masking `zext(i1)`: `zext(i1) & m -> zext(i1)` when `m & 1 != 0`, or `0` when `m & 1 == 0`. All rules return existing dominating SSA values or constants without creating instructions and keep `w > 32` untouched so `Int64Ops` flags stay consistent. Part of #1269. **What is in it** - `crates/xcl/src/gpu/dxil/module/opt.rs`: add the above algebraic bitmask and identity folds inside `Module::simplify_op`, plus `simplify_op_folds_bitmasks_double_xor_and_all_ones_or` testing every rule and verifying SSA well-formedness. 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,273,276 B` (`-50,460 B`, `-0.12%`) **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): `7 passed; 0 failed` (including `simplify_op_folds_bitmasks_double_xor_and_all_ones_or`). - `.\scripts\test.ps1` (debug, full workspace): `1251 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: 38f47bfaf1d00332560535c8f202482cf77d630e gpu: NVIDIA GeForce RTX 3070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1613 (PresentX 1613) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14476 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 the bitmask, shift-mask, double-xor, and all-ones `or` folds in `module/opt.rs`, added unit tests covering each rule, and ran the workspace test suite, 573-shader validation benchmark, and 60 s smoke regression run.
itsSio commented 2026-11-25 13:05:12 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by itsSio on 2026-11-25T12:41:20Z.


Tested

Independent verification on clean 38f47bfaf1d00332560535c8f202482cf77d630e, Windows 11 Pro build 26200, x64 MSVC, unoptimized dev/test profiles with debug info off. cargo build --workspace --locked and cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture exited 0 on both PCs.

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

cargo test -p xcl --lib --locked gpu::dxil::module::opt::tests -- --test-threads=1 --nocapture: 7 passed / 0 failed / 0 ignored, zero runtime skips on both PCs. This includes the synthetic test for the bitmask, double-XOR and all-ones OR folds and SSA well-formedness. That test checks optimizer behavior, not GPU output.

Not tested

Exact parent, AMD or Intel, native wave64, title samples, or performance.

AI use

Ran via Samaritan:

  • OpenCode with OpenAI GPT-6.2 Sol (openai/gpt-6.2-sol): scoped and ran the two-PC verification, audited the logs, and wrote this report. No subagents contributed.
<!-- greyfinch-sync:ivall/xcl:comment:7162718234 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2279#issuecomment-7162718234). Originally posted by [itsSio](https://github.com/itsSio) on 2026-11-25T12:41:20Z. --- **Tested** Independent verification on clean `38f47bfaf1d00332560535c8f202482cf77d630e`, Windows 11 Pro build 26200, x64 MSVC, unoptimized dev/test profiles with debug info off. `cargo build --workspace --locked` and `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture` exited 0 on both PCs. | GPU / driver | CPU, RAM, toolchain | Passed / failed / ignored | Runtime `SKIPPED` | | --- | --- | ---: | ---: | | RTX 5080 / `32.0.16.1088` | Ryzen 7 9800X3D, 32 GiB; Rust 1.97.0, DXC/DXIL 1.9.2609.5 | 1251 / 0 / 46 | 25 | | RTX 2080 SUPER / `32.0.16.1088` | Ryzen 5 5600X, 16 GiB; Rust 1.99.1, DXC/DXIL 1.8.2502.11 | 1251 / 0 / 46 | 27 | `cargo test -p xcl --lib --locked gpu::dxil::module::opt::tests -- --test-threads=1 --nocapture`: **7 passed / 0 failed / 0 ignored**, zero runtime skips on both PCs. This includes the synthetic test for the bitmask, double-XOR and all-ones OR folds and SSA well-formedness. That test checks optimizer behavior, not GPU output. **Not tested** Exact parent, AMD or Intel, native wave64, title samples, or performance. **AI use** Ran via Samaritan: - OpenCode with OpenAI GPT-6.2 Sol (`openai/gpt-6.2-sol`): scoped and ran the two-PC verification, audited the logs, and wrote this report. No subagents contributed.
nnonno91 commented 2026-11-25 16:12:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by nnonno91 on 2026-11-25T15:13:57Z.


Independent Windows test of 38f47bfaf1d00332560535c8f202482cf77d630e. The optimizer tests passed on the PR head, and the new test caught the old behavior when run with the parent optimizer.

Tested

  • Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Rust 1.100.0 x64 MSVC, unoptimized test profile. These are CPU-side optimizer tests.
  • On the clean head, cargo test --locked -p xcl --lib gpu::dxil::module::opt::tests -- --nocapture --test-threads=1 reported 7 passed, 0 failed, 0 ignored, and no logged skips.
  • Temporarily substituted the optimizer implementation from parent 1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6, keeping the head's test module. cargo test --locked -p xcl --lib simplify_op_folds_bitmasks_double_xor_and_all_ones_or -- --nocapture --test-threads=1 reported 0 passed, 1 failed, 0 ignored, and no logged skips. The first assertion caught that or(x, ~0) still returned an instruction instead of the constant 0xffffffff. Restored the clean head afterward.

Not tested

GPU shader execution, performance, retail titles, or the full workspace suite.

AI use

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

<!-- greyfinch-sync:ivall/xcl:comment:7166511355 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2279#issuecomment-7166511355). Originally posted by [nnonno91](https://github.com/nnonno91) on 2026-11-25T15:13:57Z. --- Independent Windows test of `38f47bfaf1d00332560535c8f202482cf77d630e`. The optimizer tests passed on the PR head, and the new test caught the old behavior when run with the parent optimizer. **Tested** - Windows 11 25H2 (26200.9974), Ryzen 7 9800X3D, Rust 1.100.0 x64 MSVC, unoptimized test profile. These are CPU-side optimizer tests. - On the clean head, `cargo test --locked -p xcl --lib gpu::dxil::module::opt::tests -- --nocapture --test-threads=1` reported 7 passed, 0 failed, 0 ignored, and no logged skips. - Temporarily substituted the optimizer implementation from parent `1a0bbe3a2ba91ea8f5b6fc29130a053868d3c9e6`, keeping the head's test module. `cargo test --locked -p xcl --lib simplify_op_folds_bitmasks_double_xor_and_all_ones_or -- --nocapture --test-threads=1` reported 0 passed, 1 failed, 0 ignored, and no logged skips. The first assertion caught that `or(x, ~0)` still returned an instruction instead of the constant `0xffffffff`. Restored the clean head afterward. **Not tested** GPU shader execution, performance, retail titles, or the full workspace suite. **AI use** OpenAI Codex (GPT-6.2 Sol) prepared and ran the tests, verified the results, and wrote this report.
DavidM1911 commented 2026-11-25 17:07:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T17:05:13Z.


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!

<!-- greyfinch-sync:ivall/xcl:comment:7168981910 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2279#issuecomment-7168981910). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T17:05:13Z. --- 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!
emirduran707 commented 2026-11-25 18:04:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by emirduran707 on 2026-11-25T18:00:40Z.


Independent AMD RDNA4 (native wave64) test report on exact head 38f47bfaf1d00332560535c8f202482cf77d630e (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: 1249 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.
    • The PR's test passed: gpu::dxil::module::opt::tests::simplify_op_folds_bitmasks_double_xor_and_all_ones_or.
    • 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: 38f47bfaf1d00332560535c8f202482cf77d630e
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1566 (PresentX 1566)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16478 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.
<!-- greyfinch-sync:ivall/xcl:comment:7170312204 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2279#issuecomment-7170312204). Originally posted by [emirduran707](https://github.com/emirduran707) on 2026-11-25T18:00:40Z. --- Independent AMD RDNA4 (native wave64) test report on exact head `38f47bfaf1d00332560535c8f202482cf77d630e` (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`: **1249 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. - The PR's test passed: `gpu::dxil::module::opt::tests::simplify_op_folds_bitmasks_double_xor_and_all_ones_or`. - 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: 38f47bfaf1d00332560535c8f202482cf77d630e gpu: AMD Radeon RX 9060 XT title: 21222967 seconds: 60 exit: 0x0 frames shown: 1566 (PresentX 1566) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 16478 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.
upstream-sync closed this pull request 2026-11-25 19:08:02 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!2269
No description provided.