Recompiler: direct DXIL decides compares of two float constants #1943

Closed
github-bot wants to merge 2 commits from gh-pr/1943/head into gh-pr/1943/base
Owner

@⁠nFaktorial on GitHub · 2026-11-24 23:46 UTC · original
dxil-fold-fcmp → main · +435 −37 · 5 files · 2 commits · closed without merging

Stacked on #1906; only the top commit (bbd6dd0) belongs here. Part of #1269.

The direct translator keeps registers as i32, so a float literal reaches an fcmp as a bitcast of an integer constant. #1906 folds that bitcast, but the compare of two constants stays in the code, for example the "is the literal zero" test of every legacy multiply by a literal. This PR decides fcmp of two 32-bit float constants at build time, for all ordered and unordered predicates, except where an operand is a denormal (a driver may flush it to zero). It replaces the float part of my #1923, which duplicated #1906.

On a retail shader pack, against #1906's head 9ea9ad1 (xcl shaders --direct --sample 3000 --seed 7 --split 1 --windows 4): DXIL 0.31 % smaller in total over 2 990 programs (2 335 smaller, none larger, 655 the same). In the first 400 of them (dxc -dumpbin): 142 469 fcmp instead of 148 570 (−4.1 %), 4 340 854 instructions instead of 4 359 046.

What is in it

  • Recompiler: direct DXIL decides compares of two float constants
    • crates/xcl/src/gpu/dxil/module/opt.rs: an fcmp arm in simplify_op and a const_f32 helper (a null float is +0).
    • crates/xcl/src/gpu/dxil/ops_tests.rs: float_constant_compares_fold_away checks the listing (only the compare of a variable and the denormal's stay), the validator and the GPU results of a legacy multiply by 1.5 and five predicates on constants (−0 against +0 among them). It fails without the opt.rs change.
    • crates/xcl/src/gpu/dxil/kernel.rs: TRANSLATOR_VERSION 8 → 9.
    • Size: runtime +40/−1 (39 in opt.rs, the version line), test +48.

Tested

  • NVIDIA GeForce RTX 3070 8 GB (driver 610.88, 32-lane waves), AMD Ryzen 7 5800X, 16 GB RAM, Windows 11 26200, Rust 1.97.1 (MSVC 14.41), debug tests, release smoke and sweeps. Head bbd6dd0 (on #1906's 9ea9ad1, base main 689f2db).
  • cargo test --workspace -- --test-threads=4 with the SDK's DXC 1.7.2308.16: 977 passed, 0 failed, 45 ignored; 89 SKIPPED, 66 of them the validation step (validator 1.7 < 1.8), 18 no wave64.
  • The same with XCL_DXC_DIR set to DXC 1.9.2602.17: 977 passed, 0 failed, 45 ignored; 23 SKIPPED: 18 no wave64, 3 per-lane f16 / V_CVT_F16_F32 refusals, no heap windows, and the PSO measurement (Developer Mode off).
  • xcl shaders --direct ... --validate over the 3 000-program sample: 0 validator failures (DXC 1.9.2602.17), on this branch and on #1906.

Regression run

main:

xcl-smoke v1
commit: 689f2db477c3c062cf162649781640ef8a977c18
gpu: NVIDIA GeForce RTX 3070
title: 6ca5cab7
seconds: 120
exit: 0x0
frames shown: 2064 (PresentX 2064)
presents per 2 s (last 3): 19 21 21
device removals: 0
crashes: 0
peak commit: 15517 MB

This PR:

xcl-smoke v1
commit: bbd6dd08133071838708cd5d0e37461d271fe2af
gpu: NVIDIA GeForce RTX 3070
title: 6ca5cab7
seconds: 120
exit: 0x0
frames shown: 1812 (PresentX 1812)
presents per 2 s (last 3): 19 19 21
device removals: 0
crashes: 0
peak commit: 15553 MB

Both runs: XCL_DIRECT_DXIL=1 XCL_PSO_PRELOAD=0, A pressed every 4 s from 20 s, 120 s. Both built the same three compute pipelines through the direct backend: 80 520, 27 500 and 38 888 B of DXIL on main, 74 008, 26 112 and 36 824 B with #1906 and this commit.

Not tested

  • Driver compile time (CreateComputePipelineState) of the smaller DXIL.
  • AMD and wave64 hardware: the RTX 3070 skips the native-wave64 tests (no wave64).
  • The title runs stay in its menus (no Xbox Live sign-in), so the world load is not covered.

AI use

  • Claude Code (Claude Opus 5.5) wrote the code and the test, ran the measurements, tests and smoke runs, and wrote this description.

🤖 Generated with Claude Code

**@⁠nFaktorial** on GitHub · 2026-11-24 23:46 UTC · [original](https://github.com/xcl-dev/xcl/pull/1943) `dxil-fold-fcmp` → `main` · +435 −37 · 5 files · 2 commits · closed without merging Stacked on #1906; only the top commit (`bbd6dd0`) belongs here. Part of #1269. The direct translator keeps registers as i32, so a float literal reaches an `fcmp` as a bitcast of an integer constant. #1906 folds that bitcast, but the compare of two constants stays in the code, for example the "is the literal zero" test of every legacy multiply by a literal. This PR decides `fcmp` of two 32-bit float constants at build time, for all ordered and unordered predicates, except where an operand is a denormal (a driver may flush it to zero). It replaces the float part of my #1923, which duplicated #1906. On a retail shader pack, against #1906's head `9ea9ad1` (`xcl shaders --direct --sample 3000 --seed 7 --split 1 --windows 4`): DXIL **0.31 % smaller** in total over 2 990 programs (2 335 smaller, none larger, 655 the same). In the first 400 of them (`dxc -dumpbin`): 142 469 `fcmp` instead of 148 570 (−4.1 %), 4 340 854 instructions instead of 4 359 046. **What is in it** - `Recompiler: direct DXIL decides compares of two float constants` - `crates/xcl/src/gpu/dxil/module/opt.rs`: an `fcmp` arm in `simplify_op` and a `const_f32` helper (a null float is +0). - `crates/xcl/src/gpu/dxil/ops_tests.rs`: `float_constant_compares_fold_away` checks the listing (only the compare of a variable and the denormal's stay), the validator and the GPU results of a legacy multiply by 1.5 and five predicates on constants (−0 against +0 among them). It fails without the `opt.rs` change. - `crates/xcl/src/gpu/dxil/kernel.rs`: `TRANSLATOR_VERSION` 8 → 9. - Size: runtime +40/−1 (39 in `opt.rs`, the version line), test +48. **Tested** - NVIDIA GeForce RTX 3070 8 GB (driver 610.88, 32-lane waves), AMD Ryzen 7 5800X, 16 GB RAM, Windows 11 26200, Rust 1.97.1 (MSVC 14.41), debug tests, release smoke and sweeps. Head `bbd6dd0` (on #1906's `9ea9ad1`, base `main` `689f2db`). - `cargo test --workspace -- --test-threads=4` with the SDK's DXC 1.7.2308.16: 977 passed, 0 failed, 45 ignored; 89 `SKIPPED`, 66 of them the validation step (validator 1.7 < 1.8), 18 `no wave64`. - The same with `XCL_DXC_DIR` set to DXC 1.9.2602.17: 977 passed, 0 failed, 45 ignored; 23 `SKIPPED`: 18 `no wave64`, 3 per-lane f16 / `V_CVT_F16_F32` refusals, `no heap windows`, and the PSO measurement (Developer Mode off). - `xcl shaders --direct ... --validate` over the 3 000-program sample: 0 validator failures (DXC 1.9.2602.17), on this branch and on #1906. **Regression run** `main`: ``` xcl-smoke v1 commit: 689f2db477c3c062cf162649781640ef8a977c18 gpu: NVIDIA GeForce RTX 3070 title: 6ca5cab7 seconds: 120 exit: 0x0 frames shown: 2064 (PresentX 2064) presents per 2 s (last 3): 19 21 21 device removals: 0 crashes: 0 peak commit: 15517 MB ``` This PR: ``` xcl-smoke v1 commit: bbd6dd08133071838708cd5d0e37461d271fe2af gpu: NVIDIA GeForce RTX 3070 title: 6ca5cab7 seconds: 120 exit: 0x0 frames shown: 1812 (PresentX 1812) presents per 2 s (last 3): 19 19 21 device removals: 0 crashes: 0 peak commit: 15553 MB ``` Both runs: `XCL_DIRECT_DXIL=1 XCL_PSO_PRELOAD=0`, A pressed every 4 s from 20 s, 120 s. Both built the same three compute pipelines through the direct backend: 80 520, 27 500 and 38 888 B of DXIL on `main`, 74 008, 26 112 and 36 824 B with #1906 and this commit. **Not tested** - Driver compile time (`CreateComputePipelineState`) of the smaller DXIL. - AMD and wave64 hardware: the RTX 3070 skips the native-wave64 tests (`no wave64`). - The title runs stay in its menus (no Xbox Live sign-in), so the world load is not covered. **AI use** - Claude Code (Claude Opus 5.5) wrote the code and the test, ran the measurements, tests and smoke runs, and wrote this description. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Extend `gpu::dxil::module::opt` to fold remaining post-phi redundancies in
direct-DXIL shaders without colliding with CFG-level folding or violating DXC
validator shader-flag invariants:

- Enable 64-bit integer constant/identity folding and DCE (`i64` arithmetic,
  shifts, `LShr(ZExt)` / `LShr(Or(ZExt, Shl(ZExt)))` window-high patterns,
  `Trunc(ZExt|SExt)`, and `i1` / `ZExt(i1)` compares) and clear
  `Feature::Int64Ops` in `Shader::infer_type_features` when no surviving
  instruction uses `i64`.
- Fold finite constant `BitCast` (`i32 <-> f32`, `i16 <-> f16`, excluding NaN
  and Inf in both directions), reuse dominating `BitCast`s from CSE, collapse
  nested same-condition `Select` chains, and push `BitCast` through `Select`
  only when the `Select` has no surviving non-bitcast uses (preventing select
  duplication).
- Canonicalize commutative integer binops (`Add`/`Mul`/`And`/`Or`/`Xor`) and
  integer `Cmp` predicates before dominator CSE, separate `XCL_DXIL_NO_OPT=fold`
  from `cse`, and expand flag-neutral `ReadNone` intrinsic DCE.
- Bump `dxil::kernel::TRANSLATOR_VERSION` (7 -> 8) and add validator + RTX 4060
  GPU execution parity tests.

On a 252-program validated direct-DXIL corpus sample (100% across CS/GS/MS/PS/VS):
-3.25% DXIL bytes (-397 KB, 252/252 shrank), -6.33% instructions, -28.74%
bitcasts, -2.30% selects, -38.32% i64 ops, 0 validator errors (and 0 errors
across 977 programs in a 1,000-record sample).

Assisted-by: Google Antigravity (Gemini 4 Neon)
The direct translator keeps registers as i32, so a float literal reaches
an fcmp as a bitcast of an integer constant. Once the optimizer folds
that bitcast (#1906), the compare has two constants but stayed in the
code. A legacy multiply by a literal kept its "is the literal zero"
test that way.

Module::optimize now decides fcmp of two 32-bit float constants at
build time, for all ordered and unordered predicates, except where an
operand is a denormal: a driver may flush it to zero.

TRANSLATOR_VERSION goes to 9: the emitted containers change.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:47 +00:00
github-bot closed this pull request 2026-11-26 05:10:47 +00:00
Author
Owner

@⁠anes66 on GitHub · 2026-11-25 00:58 UTC · original

CPU-only predicate matrix on exact head bbd6dd08133071838708cd5d0e37461d271fe2af passed. Optional test companion 859243e is directly on that head and adds tests only.

Tested

  • Windows 10 Pro 19045, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, debug; no GPU, DXC or retail input.
  • All 16 predicates over 18 raw f32 patterns: signed zero, +/-1 and2, minimum normal, maximum finite, signed infinities, signed quiet/signaling NaNs and signed minimum subnormals. A null-float case is added. Reference outcomes use integer IEEE bit ordering and the predicate truth mask, not host f32 comparisons or the optimizer's match arms.
  • Live sink operands after optimization: 4097 constant outcomes matched, and 1088 denormal comparisons remained unfurled, matching this PR's conservative denormal policy.
  • cargo test --locked -p xcl --lib cpu_float_tests:: -- --nocapture --test-threads=1: 1 passed / 0 failed / 0 ignored, no skips.
  • Temporary controls both failed: omitting the ordered-NaN guard gave FONe(+0,qNaN)=1 instead of0; removing the denormal filter violated the preservation check. The latter checks policy, not a wrong arithmetic result for every denormal predicate. Both edits were restored before the final passing run and commit.

Not tested
Serialized bitcode/validator, GPU or driver denormal behavior, release, full workspace suite, retail execution, performance or newer-main integration. This checks integer-IR constant outcomes and the documented conservative guard, not complete shader correctness. No competing upstream PR.

AI use
Codex (GPT-6; exact variant unavailable) wrote the test and temporary controls, ran CPU validation and prepared this report.

**@⁠anes66** on GitHub · 2026-11-25 00:58 UTC · [original](https://github.com/xcl-dev/xcl/pull/1943#issuecomment-7149077157) CPU-only predicate matrix on exact head `bbd6dd08133071838708cd5d0e37461d271fe2af` passed. Optional test companion [859243e](https://github.com/anes66/xcl/commit/859243ecbf2346130386c5524591cd7ab33d57f9) is directly on that head and adds tests only. **Tested** - Windows 10 Pro 19045, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, debug; no GPU, DXC or retail input. - All 16 predicates over 18 raw f32 patterns: signed zero, +/-1 and2, minimum normal, maximum finite, signed infinities, signed quiet/signaling NaNs and signed minimum subnormals. A null-float case is added. Reference outcomes use integer IEEE bit ordering and the predicate truth mask, not host f32 comparisons or the optimizer's match arms. - Live sink operands after optimization: **4097 constant outcomes matched**, and **1088 denormal comparisons remained unfurled**, matching this PR's conservative denormal policy. - `cargo test --locked -p xcl --lib cpu_float_tests:: -- --nocapture --test-threads=1`: **1 passed / 0 failed / 0 ignored**, no skips. - Temporary controls both failed: omitting the ordered-NaN guard gave `FONe(+0,qNaN)=1` instead of0; removing the denormal filter violated the preservation check. The latter checks policy, not a wrong arithmetic result for every denormal predicate. Both edits were restored before the final passing run and commit. **Not tested** Serialized bitcode/validator, GPU or driver denormal behavior, release, full workspace suite, retail execution, performance or newer-main integration. This checks integer-IR constant outcomes and the documented conservative guard, not complete shader correctness. No competing upstream PR. **AI use** Codex (GPT-6; exact variant unavailable) wrote the test and temporary controls, ran CPU validation and prepared this report.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 02:50 UTC · original

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

**@⁠DavidM1911** on GitHub · 2026-11-25 02:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/1943#issuecomment-7151259068) Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** — needs a rebase before it can be tested.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.