Recompiler: simplify integer min/max (dx.op.binary.i32) via range bounds and absorption in direct DXIL opt #2568

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

@⁠ramon-victor on GitHub · 2026-11-25 23:16 UTC · original
ramon-victor:opt/dxil-fold-int-minmax → main · +354 −0 · 1 files · 1 commits · open

Part of #1269.

Problem

Module::simplify_op in crates/xcl/src/gpu/dxil/module/opt.rs did not simplify dx.op.binary.i32 integer min/max calls (IMax=37, IMin=38, UMax=39, UMin=40).

In translated RDNA2 shaders, integer clamps (v_med3_i32 / v_med3_u32 / v_med3_i16 / v_med3_u16, each lowered as 4 dx.op.binary.i32 calls) and byte/half range clamps (v_cvt_pk_*, v_sat_pk_*) frequently receive constant bounds via SGPRs that only become constants after mem2reg promotes them to SSA, or clamp values whose range is already constrained by and, lshr, ashr, zext, sext, or an outer min/max. Because dx.op.binary.i32 was opaque to simplify_op, all 4 calls stayed live in the emitted DXIL.

Fix

  • Add conservative bounded [lo, hi] interval helpers Module::unsigned_range_i32 and Module::signed_range_i32 (depth 4) in crates/xcl/src/gpu/dxil/module/opt.rs, tracking ranges across i32 constants, constant-masked And (requiring bit 31 clear for signed), constant LShr (shift >= 1 for signed), constant AShr, ZExt/SExt from narrower widths, Select, and nested dx.op.binary.i32 min/max of matching signedness.
  • Add Module::simplify_int_minmax called from Module::simplify_op on dx.op.binary.i32 (37..=40), folding:
    • Identical operands (min(x, x) -> x, max(x, x) -> x).
    • Lattice idempotence and absorption (min(min(x, y), x) -> min(x, y), min(max(x, y), x) -> x, max(min(x, y), x) -> x, max(max(x, y), x) -> max(x, y)).
    • Non-overlapping interval bounds (ha <= lb / hb <= la for min; la >= hb / lb >= ha for max), which subsumes constant folding, identity/absorbing bounds (0, u32::MAX, i32::MIN, i32::MAX), redundant byte/half clamps, and collapses every permutation of constant-bound v_med3_i32/v_med3_u32 from 4 calls down to 2 (or 1 when lo == 0/i32::MIN or hi == u32::MAX/i32::MAX) automatically after mem2reg.
  • Add unit, RDNA2 IR-collapse, and RTX 3070 GPU parity tests in opt::int_minmax_tests (including soundness check that imax(x & 0x8000_00FF, 0) is not folded).

Measured impact

573-program retail direct-DXIL validation sweep (dxil.dll 1.8.2502.11):

  • Baseline (6d12f58): 573/573 validated (val_fail=0), 51,117,488 total DXIL bytes (p50=22,865 us, p95=408,132 us).
  • This PR (4094ff9): 573/573 validated (val_fail=0), 50,984,004 total DXIL bytes (-133,484 B / -0.261%; p50=21,870 us, p95=419,075 us).

Tested

  • Targeted unit + RDNA2 IR-collapse + RTX 3070 GPU parity tests (cargo test --lib -p xcl int_minmax): 2 passed, 0 failed (simplify_int_minmax_folds_bounds_absorption_and_clamps, med3_via_sgpr_bounds_collapses_and_matches_gpu).
  • Full workspace (.\scripts\test.ps1, NVIDIA GeForce RTX 3070, Windows 11): 1425 passed, 0 failed, 46 ignored.
  • 573-shader direct DXIL dxil.dll validation sweep: 573/573 passed, 0 failures.
  • Release build (cargo build --release -p xcl) and 60 s retail smoke run (see below).

Not tested

  • Wave64-only GPU paths (the RTX 3070 runs wave32; wave64 GPU tests skipped automatically on this adapter).

Regression run

Main (f087b99):

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

This PR (4094ff9):

xcl-smoke v1
commit: 4094ff9cb9c3eea2af33f2556dae05a28987bb05
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: 14002 MB

AI use

  • Antigravity (Gemini 4 Neon): implementation, soundness & GPU parity tests, 573-shader DXIL sweep, and PR drafting.
**@⁠ramon-victor** on GitHub · 2026-11-25 23:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/2568) `ramon-victor:opt/dxil-fold-int-minmax` → `main` · +354 −0 · 1 files · 1 commits · open Part of #1269. **Problem** `Module::simplify_op` in `crates/xcl/src/gpu/dxil/module/opt.rs` did not simplify `dx.op.binary.i32` integer min/max calls (`IMax=37`, `IMin=38`, `UMax=39`, `UMin=40`). In translated RDNA2 shaders, integer clamps (`v_med3_i32` / `v_med3_u32` / `v_med3_i16` / `v_med3_u16`, each lowered as 4 `dx.op.binary.i32` calls) and byte/half range clamps (`v_cvt_pk_*`, `v_sat_pk_*`) frequently receive constant bounds via SGPRs that only become constants after `mem2reg` promotes them to SSA, or clamp values whose range is already constrained by `and`, `lshr`, `ashr`, `zext`, `sext`, or an outer `min`/`max`. Because `dx.op.binary.i32` was opaque to `simplify_op`, all 4 calls stayed live in the emitted DXIL. **Fix** - Add conservative bounded `[lo, hi]` interval helpers `Module::unsigned_range_i32` and `Module::signed_range_i32` (depth 4) in `crates/xcl/src/gpu/dxil/module/opt.rs`, tracking ranges across `i32` constants, constant-masked `And` (requiring bit 31 clear for signed), constant `LShr` (shift `>= 1` for signed), constant `AShr`, `ZExt`/`SExt` from narrower widths, `Select`, and nested `dx.op.binary.i32` min/max of matching signedness. - Add `Module::simplify_int_minmax` called from `Module::simplify_op` on `dx.op.binary.i32` (`37..=40`), folding: - Identical operands (`min(x, x) -> x`, `max(x, x) -> x`). - Lattice idempotence and absorption (`min(min(x, y), x) -> min(x, y)`, `min(max(x, y), x) -> x`, `max(min(x, y), x) -> x`, `max(max(x, y), x) -> max(x, y)`). - Non-overlapping interval bounds (`ha <= lb` / `hb <= la` for min; `la >= hb` / `lb >= ha` for max), which subsumes constant folding, identity/absorbing bounds (`0`, `u32::MAX`, `i32::MIN`, `i32::MAX`), redundant byte/half clamps, and collapses every permutation of constant-bound `v_med3_i32`/`v_med3_u32` from 4 calls down to 2 (or 1 when `lo == 0`/`i32::MIN` or `hi == u32::MAX`/`i32::MAX`) automatically after `mem2reg`. - Add unit, RDNA2 IR-collapse, and RTX 3070 GPU parity tests in `opt::int_minmax_tests` (including soundness check that `imax(x & 0x8000_00FF, 0)` is *not* folded). **Measured impact** 573-program retail direct-DXIL validation sweep (`dxil.dll` 1.8.2502.11): - Baseline (`6d12f58`): `573/573` validated (`val_fail=0`), `51,117,488` total DXIL bytes (`p50=22,865 us`, `p95=408,132 us`). - This PR (`4094ff9`): `573/573` validated (`val_fail=0`), `50,984,004` total DXIL bytes (`-133,484 B` / `-0.261%`; `p50=21,870 us`, `p95=419,075 us`). **Tested** - Targeted unit + RDNA2 IR-collapse + RTX 3070 GPU parity tests (`cargo test --lib -p xcl int_minmax`): 2 passed, 0 failed (`simplify_int_minmax_folds_bounds_absorption_and_clamps`, `med3_via_sgpr_bounds_collapses_and_matches_gpu`). - Full workspace (`.\scripts\test.ps1`, NVIDIA GeForce RTX 3070, Windows 11): `1425 passed, 0 failed, 46 ignored`. - 573-shader direct DXIL `dxil.dll` validation sweep: `573/573` passed, `0` failures. - Release build (`cargo build --release -p xcl`) and 60 s retail smoke run (see below). **Not tested** - Wave64-only GPU paths (the RTX 3070 runs wave32; wave64 GPU tests skipped automatically on this adapter). **Regression run** Main (`f087b99`): ``` 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 ``` This PR (`4094ff9`): ``` xcl-smoke v1 commit: 4094ff9cb9c3eea2af33f2556dae05a28987bb05 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: 14002 MB ``` **AI use** - Antigravity (Gemini 4 Neon): implementation, soundness & GPU parity tests, 573-shader DXIL sweep, and PR drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:33:58 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 23:28 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.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-25 23:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/2568#issuecomment-7177615700) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 26 ignored. Thanks! — Devin
Author
Owner

@⁠DarkPanda69 on GitHub · 2026-11-25 23:37 UTC · original

Wave64 run (listed as untested; the wave64 GPU tests skipped on the RTX 3070) on an AMD RX 9070 XT, plus an RTX 3060: both new tests pass, and nothing else changes in the direct-DXIL GPU suite.

Head 4094ff9 applied on main 08db802, debug, Windows 11 Pro 26200, XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4 on main and with the PR, card chosen with XCL_ADAPTER:

main this PR
RX 9070 XT (RDNA4, 32.0.32029.2010) 704 passed, 2 failed, 6 SKIPPED 706 passed, 2 failed, 6 SKIPPED
RTX 3060 (32.0.16.1088) 706 passed, 0 failed, 24 SKIPPED 708 passed, 0 failed, 24 SKIPPED

On both cards, the only result changes against main are the new simplify_int_minmax_folds_bounds_absorption_and_clamps and med3_via_sgpr_bounds_collapses_and_matches_gpu, both ok, with no new skips. med3_via_sgpr_bounds_collapses_and_matches_gpu translates with wave64: false, so its own GPU run is wave32. The wave64 coverage comes from the rest of the suite, which runs through this optimizer pass: on the RTX 3060, 21 wave64 cases skip. On the RX 9070 XT, 18 of them ran with this PR applied and still pass; the other 3 are per-lane forms refused on both cards. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on main.

Not tested: Intel, a title.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

**@⁠DarkPanda69** on GitHub · 2026-11-25 23:37 UTC · [original](https://github.com/xcl-dev/xcl/pull/2568#issuecomment-7177829736) Wave64 run (listed as untested; the wave64 GPU tests skipped on the RTX 3070) on an AMD RX 9070 XT, plus an RTX 3060: both new tests pass, and nothing else changes in the direct-DXIL GPU suite. Head `4094ff9` applied on `main` `08db802`, debug, Windows 11 Pro 26200, `XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4` on `main` and with the PR, card chosen with `XCL_ADAPTER`: | | `main` | this PR | |---|---|---| | RX 9070 XT (RDNA4, 32.0.32029.2010) | 704 passed, 2 failed, 6 SKIPPED | 706 passed, 2 failed, 6 SKIPPED | | RTX 3060 (32.0.16.1088) | 706 passed, 0 failed, 24 SKIPPED | 708 passed, 0 failed, 24 SKIPPED | On both cards, the only result changes against `main` are the new `simplify_int_minmax_folds_bounds_absorption_and_clamps` and `med3_via_sgpr_bounds_collapses_and_matches_gpu`, both ok, with no new skips. `med3_via_sgpr_bounds_collapses_and_matches_gpu` translates with `wave64: false`, so its own GPU run is wave32. The wave64 coverage comes from the rest of the suite, which runs through this optimizer pass: on the RTX 3060, 21 wave64 cases skip. On the RX 9070 XT, 18 of them ran with this PR applied and still pass; the other 3 are per-lane forms refused on both cards. The 2 RX 9070 XT failures are the AMD fuzz seeds tracked in #2115, the same on `main`. Not tested: Intel, a title. AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-26 00:11 UTC · original

Independent RX 6800M focused report for exact PR head 4094ff9cb9c3eea2af33f2556dae05a28987bb05 (clean detached worktree).

Tested

  • AMD Radeon RX 6800M, driver 30.0.13002.13003; Ryzen 9 5980HX; Windows 11 Home build 26200; Rust 1.99.0 MSVC; DXC 1.9.2609.5.
  • XCL_ADAPTER=6800M XCL_REQUIRE_GPU=1 XCL_DIRECT_DXIL=1 cargo test --locked -p xcl --lib -j 1 med3_via_sgpr_bounds_collapses_and_matches_gpu -- --test-threads=1 --show-output: 1 passed, 0 failed, 0 ignored, 0 skipped (1325 filtered out). No explicit SKIPPED records. The fixture’s kernel uses the wave32 form and checks the RDNA2 SGPR-bound med3 clamp against GPU results; it does not test the native wave64 form. Resource guard: none. Log: filter-med3-via-sgpr-bounds-gpu-retry.log.

Not tested

  • Full suite, native wave64 form, retail-title smoke, other GPUs.

AI use

  • OpenAI Codex (exact model variant unavailable in this session) ran this focused test and drafted this report.
**@⁠luckyteagirl** on GitHub · 2026-11-26 00:11 UTC · [original](https://github.com/xcl-dev/xcl/pull/2568#issuecomment-7178498210) Independent RX 6800M focused report for exact PR head `4094ff9cb9c3eea2af33f2556dae05a28987bb05` (clean detached worktree). **Tested** - AMD Radeon RX 6800M, driver `30.0.13002.13003`; Ryzen 9 5980HX; Windows 11 Home build 26200; Rust `1.99.0` MSVC; DXC `1.9.2609.5`. - `XCL_ADAPTER=6800M XCL_REQUIRE_GPU=1 XCL_DIRECT_DXIL=1 cargo test --locked -p xcl --lib -j 1 med3_via_sgpr_bounds_collapses_and_matches_gpu -- --test-threads=1 --show-output`: **1 passed, 0 failed, 0 ignored, 0 skipped** (`1325` filtered out). No explicit `SKIPPED` records. The fixture’s kernel uses the wave32 form and checks the RDNA2 SGPR-bound med3 clamp against GPU results; it does not test the native wave64 form. Resource guard: none. Log: `filter-med3-via-sgpr-bounds-gpu-retry.log`. **Not tested** - Full suite, native wave64 form, retail-title smoke, other GPUs. **AI use** - OpenAI Codex (exact model variant unavailable in this session) ran this focused test and drafted this report.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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