Skip to content

Recompiler: fold finite constant legacyF16ToF32 conversions in direct DXIL - #2552

Open
ramon-victor wants to merge 1 commit into
xcl-dev:mainfrom
ramon-victor:opt/dxil-const-f16-to-f32
Open

ramon-victor wants to merge 1 commit into
xcl-dev:mainfrom
ramon-victor:opt/dxil-const-f16-to-f32

Conversation

@ramon-victor

Copy link
Copy Markdown
Contributor

Part of #1269.

Problem

RDNA2 mixed-precision and half-conversion instructions (v_fma_mix_f32, v_fma_mixlo_f16, v_fma_mixhi_f16, v_cvt_f32_f16, packed f16 helpers) frequently convert inline or constant-folded 16-bit half operands to f32 via Shader::f16_to_f32 (dx.op.legacyF16ToF32). Even after opt.rs folds the preceding and 0xFFFF / lshr 16 extraction to a constant integer, dx.op.legacyF16ToF32 was left as an intrinsic call and was also absent from opt::removable, preventing constant folding and dead-code elimination.

Fix

  • Add f16_bits_to_f32_bits(h: u16) -> Option<u32> in crates/xcl/src/gpu/dxil/ops.rs, performing exact IEEE-754 binary16 → binary32 conversion for all finite values (±0.0, all subnormals, all normals) while returning None for ±Inf and NaN so DXIL never materializes non-finite float constants.
  • Fold constant operands directly in Shader::f16_to_f32 (ops.rs) and post-SSA/constant-propagation dx.op.legacyF16ToF32 calls in Module::simplify_op (module/opt.rs).
  • Allow DCE of dead dx.op.legacyF16ToF32 calls in Module::removable (module/opt.rs); legacyF16ToF32 is ReadNone, uses no 64-bit types, and sets no DXIL shader feature flags.
  • Add unit, DXIL validator/disassembly, and GPU parity tests in ops::f16_fold_tests covering all 65,536 half bit patterns, finite direct & shift/mask-folded constants (verifying zero remaining legacyF16ToF32), non-finite preservation, and bit-exact RTX 3070 GPU parity with hardware dx.op.legacyF16ToF32.

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 (84f6bdc): 573/573 validated (val_fail=0), 51,074,004 total DXIL bytes (-43,484 B / -0.085%; p50=19,788 us, p95=389,471 us).

Tested

  • Targeted unit + DXIL validation + GPU parity tests (cargo test --lib -p xcl f16_fold): 2 passed, 0 failed (f16_bits_to_f32_bits_exhaustive, f16_to_f32_constant_folding_validates_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 (84f6bdc):

xcl-smoke v1
commit: 84f6bdc97a46acac6b875e4419facfcd23e650dd
gpu: NVIDIA GeForce RTX 3070
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1611 (PresentX 1611)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14044 MB

AI use

  • Antigravity (Gemini 4 Neon): implementation, exhaustive & GPU parity tests, 573-shader DXIL sweep, and PR drafting.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

@DarkPanda69

Copy link
Copy Markdown

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 84f6bdc applied on main e1d84b7, 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; runs the wave64 tests natively) 698 passed, 2 failed, 6 SKIPPED 700 passed, 2 failed, 6 SKIPPED
RTX 3060 (32.0.16.1088) 700 passed, 0 failed, 24 SKIPPED 702 passed, 0 failed, 24 SKIPPED

On both cards, the only result changes against main are the new f16_bits_to_f32_bits_exhaustive and f16_to_f32_constant_folding_validates_and_matches_gpu, both ok, with no new skips. So the wave64 tests that skip on a 32-lane GPU ran over the new folding here and still pass. 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.

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) verification of exact PR head 84f6bdc97a46acac6b875e4419facfcd23e650dd on Windows 11 Pro 26300 (AMD Radeon RX 9060 XT 16 GB, driver 32.0.31044.16, Ryzen 5 5600, Rust 1.100.0 MSVC, DXC 1.9.2602.17).

Tested

  • cargo build --workspace (debug): passed (0 errors).
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 1423 passed, 2 failed, 46 ignored (8 SKIPPED: 6 per-lane DXIL forms refused by host adapter/driver, 1 heap_window_out_of_memory_falls_back_to_the_switch_form no heap windows, 1 pso_measurement_compiles_an_exact_synthetic_record Windows Developer Mode off).
    • Both new tests passed on this GPU:
      • gpu::dxil::ops::f16_fold_tests::f16_bits_to_f32_bits_exhaustive
      • gpu::dxil::ops::f16_fold_tests::f16_to_f32_constant_folding_validates_and_matches_gpu
    • The 2 failures (gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 and fuzz_validate_and_gpu_3) are pre-existing on unmodified main on this RX 9060 XT / driver 32.0.31044.16.
  • cargo build --release -p xcl + 60 s retail-title smoke test (scripts/smoke.ps1, offline):
xcl-smoke v1
commit: 84f6bdc97a46acac6b875e4419facfcd23e650dd
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1602 (PresentX 1602)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13929 MB

(Compare against main 68a284a on this machine: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • NVIDIA / Intel GPUs, Linux / Wine.

AI use

  • Verification run and report prepared with Antigravity (Gemini 4 Neon); all build, test, and smoke results are from live runs on this machine.

@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR. This PR does not change the mad/mac red on RADV.
Targeted run of f16_bits_to_f32_bits_exhaustive (all 65536 f16 patterns) and f16_to_f32_constant_folding_validates_and_matches_gpu: 2 passed; the ..._matches_gpu name sits outside the gpu_ filter, so it ran targeted.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 26, 2026
Integrates upstream amd-dev 9f02a3b (BVH null/OOB guard), b67375b (scratch
SADDR 0x7F), 4ae5668/xcl-dev#2565 (32-bit VALU add/sub carry without i64), plus
open PRs xcl-dev#2559 (bfm/ffbh_i), xcl-dev#2557 (known_nonzero_bits folding), xcl-dev#2552
(f16-to-f32 fold), xcl-dev#2568 (integer min/max range opt), and xcl-dev#2580 (empty DXC
blob null slice).

Assisted-by: Antigravity (Gemini 4 Neon)
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 26, 2026
…l-dev#2557/xcl-dev#2568

- real.rs: require 8-byte alignment in UnadoptGuard::new before atomic vtable
  load (fixes debug misaligned-pointer panic on synthetic unaligned test handles).
- list_x.rs: add UnadoptGuard and create_raw_fence in Wine GPU tests.
- opt.rs: revert unmerged xcl-dev#2557/xcl-dev#2568 passes while keeping xcl-dev#2552.

Assisted-by: Antigravity (Gemini 4 Neon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants