Recompiler: simplify self-squared and constant factors in direct DXIL mul_legacy #2252

Open
ramon-victor wants to merge 1 commit from upstream-pr/2262 into main
ramon-victor commented 2026-11-25 11:46:09 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ramon-victor on 2026-11-25T11:46:09Z.


Specializes Lw::mul_legacy (lowering v_mul_legacy_f32, the most frequent instruction in retail console shaders) in the direct DXIL translator for self-squared operands (x * x), constant ±0.0 factors, and non-zero non-subnormal constant factors while preserving exact IEEE-754 / RDNA2 zero-override semantics.

  • x * x (including identical bitcast i32 -> f32 wrappers and (-x) * (-x)): emits plain fmul(a, a) (1 op instead of 5), bit-identical for all 2^{32} f32 values ((±0)^2 = +0.0, (±Inf)^2 = +Inf, NaN^2 = NaN).
  • Either operand is constant ±0.0: folds directly to +0.0 (0 ops instead of 5).
  • One operand is a non-zero non-subnormal constant (normal, Inf, or NaN; exponent bits non-zero so FTZ cannot turn it into zero): skips that operand's fcmp oeq 0.0 and or while keeping the other operand's zero check so e.g. 2.0 * -0.0 and +Inf * 0.0 still yield +0.0 (3 ops instead of 5).
  • Both operands are non-zero non-subnormal constants: emits plain fmul(a, b).

Part of #1269.

What is in it

  • crates/xcl/src/gpu/dxil/module.rs: add Module::const_f32_bits (resolving constant f32, bitcast i32 -> f32 of integer constants, and fsub -0.0, k on non-subnormal/zero constants) and Module::same_f32 (matching identical bitcast wrappers emitted per operand by Lw::fsrc).
  • crates/xcl/src/gpu/dxil/prelude.rs: add self-square, constant-zero, and non-zero-constant fast paths to Lw::mul_legacy, plus mul_legacy_fastpath_tests::gpu_direct_mul_legacy_fastpaths_validate_and_match_ieee checking DXIL validation, disassembly (0/1 fcmp oeq counts), and GPU bit-exactness across +0.0, -0.0, finite, ±Inf, qNaN, and sNaN inputs.

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,147,056 B (-176,680 B, -0.43%)
  • Translation time p95: 263.12 ms -> 250.88 ms (-4.7%)

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::prelude::mul_legacy_fastpath_tests (debug): 1 passed; 0 failed.
  • .\scripts\test.ps1 (debug, full workspace on head b0c2efd): 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: b0c2efde0a9c3ba6e8e5a9476398bc556e7ee9f7
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1623 (PresentX 1623)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14448 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): designed and implemented const_f32_bits, same_f32, and the mul_legacy fast paths, added validation/disassembly/GPU parity tests, rebased onto origin/main (1a0bbe3), and ran the workspace test suite, 573-shader benchmark, and 60 s smoke regression run.
<!-- greyfinch-sync:ivall/xcl:issue:5490704502 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2262). Originally posted by [ramon-victor](https://github.com/ramon-victor) on 2026-11-25T11:46:09Z. --- Specializes `Lw::mul_legacy` (lowering `v_mul_legacy_f32`, the most frequent instruction in retail console shaders) in the direct DXIL translator for self-squared operands (`x * x`), constant `±0.0` factors, and non-zero non-subnormal constant factors while preserving exact IEEE-754 / RDNA2 zero-override semantics. - `x * x` (including identical `bitcast i32 -> f32` wrappers and `(-x) * (-x)`): emits plain `fmul(a, a)` (1 op instead of 5), bit-identical for all $2^{32}$ f32 values (`(±0)^2 = +0.0`, `(±Inf)^2 = +Inf`, `NaN^2 = NaN`). - Either operand is constant `±0.0`: folds directly to `+0.0` (0 ops instead of 5). - One operand is a non-zero non-subnormal constant (normal, Inf, or NaN; exponent bits non-zero so FTZ cannot turn it into zero): skips that operand's `fcmp oeq 0.0` and `or` while keeping the other operand's zero check so e.g. `2.0 * -0.0` and `+Inf * 0.0` still yield `+0.0` (3 ops instead of 5). - Both operands are non-zero non-subnormal constants: emits plain `fmul(a, b)`. Part of #1269. **What is in it** - `crates/xcl/src/gpu/dxil/module.rs`: add `Module::const_f32_bits` (resolving constant `f32`, `bitcast i32 -> f32` of integer constants, and `fsub -0.0, k` on non-subnormal/zero constants) and `Module::same_f32` (matching identical `bitcast` wrappers emitted per operand by `Lw::fsrc`). - `crates/xcl/src/gpu/dxil/prelude.rs`: add self-square, constant-zero, and non-zero-constant fast paths to `Lw::mul_legacy`, plus `mul_legacy_fastpath_tests::gpu_direct_mul_legacy_fastpaths_validate_and_match_ieee` checking DXIL validation, disassembly (0/1 `fcmp oeq` counts), and GPU bit-exactness across `+0.0`, `-0.0`, finite, `±Inf`, `qNaN`, and `sNaN` inputs. 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,147,056 B` (`-176,680 B`, `-0.43%`) - Translation time p95: `263.12 ms -> 250.88 ms` (`-4.7%`) **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::prelude::mul_legacy_fastpath_tests` (debug): `1 passed; 0 failed`. - `.\scripts\test.ps1` (debug, full workspace on head `b0c2efd`): `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: b0c2efde0a9c3ba6e8e5a9476398bc556e7ee9f7 gpu: NVIDIA GeForce RTX 3070 title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 1623 (PresentX 1623) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14448 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): designed and implemented `const_f32_bits`, `same_f32`, and the `mul_legacy` fast paths, added validation/disassembly/GPU parity tests, rebased onto `origin/main` (`1a0bbe3`), and ran the workspace test suite, 573-shader benchmark, and 60 s smoke regression run.
DarkPanda69 commented 2026-11-25 13:05:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DarkPanda69 on 2026-11-25T12:35:58Z.


Update for head b0c2efd (rebased onto main 1a0bbe3, tested as is): same result. The new test passes on both cards; whole gpu:: filter: RX 9070 XT 630 passed, 2 failed (only the #2115 fuzz seeds now); RTX 3060 632 passed, 0 failed.


The AMD native wave64 run listed as untested, plus an RTX 3060: the new test passes on both.

Head 2ae104f tested as is (it no longer applies cleanly on current main; its base is 426a5a7), debug, Windows 11 Pro 26200, cargo test -p xcl --lib -- gpu:: --test-threads=4:

  • RX 9070 XT (RDNA4, driver 32.0.32029.2010, waves 32..64): gpu_direct_mul_legacy_fastpaths_validate_and_match_ieee ok. 627 passed, 3 failed, 22 ignored, 6 SKIPPED. All three failures also fail on the base without this PR: the AMD fuzz seeds tracked in #2115 and gpu_image_get_resinfo_past_last_level (fixed on main since).
  • RTX 3060 (driver 32.0.16.1088): the new test ok; 630 passed, 0 failed, 22 ignored.

Not tested: the head rebased onto current main, a title with XCL_DIRECT_DXIL=1.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

<!-- greyfinch-sync:ivall/xcl:comment:7162595873 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2262#issuecomment-7162595873). Originally posted by [DarkPanda69](https://github.com/DarkPanda69) on 2026-11-25T12:35:58Z. --- **Update for head `b0c2efd`** (rebased onto `main` `1a0bbe3`, tested as is): same result. The new test passes on both cards; whole `gpu::` filter: RX 9070 XT 630 passed, 2 failed (only the #2115 fuzz seeds now); RTX 3060 632 passed, 0 failed. --- The AMD native wave64 run listed as untested, plus an RTX 3060: the new test passes on both. Head `2ae104f` tested as is (it no longer applies cleanly on current `main`; its base is `426a5a7`), debug, Windows 11 Pro 26200, `cargo test -p xcl --lib -- gpu:: --test-threads=4`: - **RX 9070 XT** (RDNA4, driver 32.0.32029.2010, waves 32..64): `gpu_direct_mul_legacy_fastpaths_validate_and_match_ieee` ok. 627 passed, 3 failed, 22 ignored, 6 SKIPPED. All three failures also fail on the base without this PR: the AMD fuzz seeds tracked in #2115 and `gpu_image_get_resinfo_past_last_level` (fixed on `main` since). - **RTX 3060** (driver 32.0.16.1088): the new test ok; 630 passed, 0 failed, 22 ignored. Not tested: the head rebased onto current `main`, a title with `XCL_DIRECT_DXIL=1`. AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.
nnonno91 commented 2026-11-25 16:12:12 +00:00 (Migrated from github.com)

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


Independent Windows check of b0c2efde0a9c3ba6e8e5a9476398bc556e7ee9f7. The legacy multiply shortcut cases matched the reference on Radeon integrated graphics and RTX 5090, including the upper half of a logical wave64.

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.
  • Clean head, cargo test --locked -p xcl --lib mul_legacy_fastpath_tests -- --nocapture --test-threads=1 on each selected adapter. The wave32 fixture reported 1 passed, 0 failed, 0 ignored, no logged skips per adapter.
  • Same command with a temporary fixture-only wave64 variant. It initialized and checked all 64 lanes with the existing inputs and five operations, including positive and negative zero, infinities, and NaNs. Logs confirmed native wave64 on Radeon and split wave64 on RTX 5090. Both reported 1 passed, 0 failed, 0 ignored, no logged skips. DXIL validation and disassembly assertions also passed, and the clean worktree was restored.

Not tested

Parent revision, 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:7166891931 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2262#issuecomment-7166891931). Originally posted by [nnonno91](https://github.com/nnonno91) on 2026-11-25T15:28:14Z. --- Independent Windows check of `b0c2efde0a9c3ba6e8e5a9476398bc556e7ee9f7`. The legacy multiply shortcut cases matched the reference on Radeon integrated graphics and RTX 5090, including the upper half of a logical wave64. **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. - Clean head, `cargo test --locked -p xcl --lib mul_legacy_fastpath_tests -- --nocapture --test-threads=1` on each selected adapter. The wave32 fixture reported 1 passed, 0 failed, 0 ignored, no logged skips per adapter. - Same command with a temporary fixture-only wave64 variant. It initialized and checked all 64 lanes with the existing inputs and five operations, including positive and negative zero, infinities, and NaNs. Logs confirmed native wave64 on Radeon and split wave64 on RTX 5090. Both reported 1 passed, 0 failed, 0 ignored, no logged skips. DXIL validation and disassembly assertions also passed, and the clean worktree was restored. **Not tested** Parent revision, 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:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T16:53:33Z.


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:7168689272 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2262#issuecomment-7168689272). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T16:53:33Z. --- 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!
raylitebull commented 2026-11-25 19:09:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by raylitebull on 2026-11-25T18:41:06Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7171250258 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2262#issuecomment-7171250258). Originally posted by [raylitebull](https://github.com/raylitebull) on 2026-11-25T18:41:06Z. --- Diff review, CI, and both AMD and NVIDIA checks passed on `b0c2efd`, but recent `origin/main` merges turned this branch `CONFLICTING/DIRTY`. Please rebase onto `origin/main` so it becomes mergeable again.
Foniass commented 2026-12-01 07:41:53 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:41:53Z.


Triage (#1607) – head b0c2efd

No open PR makes this change. Main's prelude.rs::mul_legacy (#3269, behind rdna_float_enabled, on by default) already folds a constant zero factor. With one finite non-zero constant, main uses a plain fmul and skips the other operand's zero check. That gives -0.0 for 2.0 * -0.0, where mul_legacy gives +0.0. This PR keeps that check. The self-square (same_f32) fast path is not on main. When you rebase, reconcile the constant branches with main's instead of adding a second set.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7304372193 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2262#issuecomment-7304372193). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T07:41:53Z. --- **Triage (#1607)** – head b0c2efd No open PR makes this change. Main's `prelude.rs::mul_legacy` (#3269, behind `rdna_float_enabled`, on by default) already folds a constant zero factor. With one finite non-zero constant, main uses a plain `fmul` and skips the other operand's zero check. That gives -0.0 for `2.0 * -0.0`, where `mul_legacy` gives +0.0. This PR keeps that check. The self-square (`same_f32`) fast path is not on main. When you rebase, reconcile the constant branches with main's instead of adding a second set. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2262:upstream-pr/2262'
git switch 'upstream-pr/2262'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2262'
git switch 'upstream-pr/2262'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2262'
git switch 'upstream-pr/2262'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2262'
git switch 'main'
git merge --squash 'upstream-pr/2262'
git switch 'main'
git merge --ff-only 'upstream-pr/2262'
git switch 'main'
git merge 'upstream-pr/2262'
git push origin 'main'
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!2252
No description provided.