Recompiler: translate v_trig_preop_f64 in both backends #2315

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

@⁠NWProgramming00 on GitHub · 2026-11-25 13:39 UTC · original
NWProgramming00:recomp/trig-preop → main · +260 −2 · 4 files · 1 commits · open

Both backends refused v_trig_preop_f64. This translates it as RDNA2 hardware runs it. I measured the hardware first, then checked xcl against it bit for bit.

What the hardware does (gfx1036, 2,072 measured rows, MODE with f64 denormals kept)

  • The result is the 53 bits of 2/π that start shift bits below b1200, times 2^(−53 − shift).
    • shift = S1[4:0] × 53, plus exp(S0) − 1077 when the S0 exponent is above 1077.
    • When the S0 exponent is 1968 or more, the scale gets an extra 2^128.
    • This is the ISA pseudocode.
  • The hardware's table ends at b17. Windows that reach below it read zeros. The full 1,201-bit constant gives different results on 104 of the measured rows; the 1,184-bit table matches all of them.
  • Rounding cannot change the result.
    • Table bit b_j is worth 2^(j − 1201), or 2^(j − 1073) with the 2^128 scale, for every input.
    • With the 2^128 scale, every bit down to b17 is at or above 2^−1074, so the result is exact.
    • Without it, the bits below 2^−1074 always start at b126, and b126 is 0. So rounding to nearest and truncating give the same double for every input.
    • Of 570 rows whose window straddles 2^−1074, 471 are inexact; the hardware matches both forms on all of them.
    • So the double is built from integers.
  • Only the exponent of S0 is read: NaN, inf, negative and denormal S0 follow the same rule. Bits above S1[4:0] are ignored.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs:
    • a trig_preop() helper and the 37-word table (TRIG_PREOP_2_OVER_PI);
    • both go into the HLSL only when a program uses the instruction, so no other shader's text changes.
  • crates/xcl/src/gpu/dxil/lower_wide.rs: the same computation in the direct DXIL translator. It reads the table with a chain of selects, because that translator has no constant arrays.
  • crates/xcl/src/gpu/dxil/lower.rs and emit.rs: v_trig_preop_f64 leaves the list of f64 ops that follow the f64 round mode (it does not round).
  • An output modifier (OMOD / CLAMP) is refused, because I did not measure it.
  • crates/xcl/src/gpu/recomp/tests.rs:
    • gpu_trig_preop_f64_matches_rdna2 checks both backends against 64 rows measured on the gfx1036.
    • trig_preop_f64_with_omod_is_refused checks the OMOD refusal in both backends.

Tested

  • Windows 11, NVIDIA GeForce RTX 5070 Ti 16 GB (wave64 as two halves) and an AMD Radeon RDNA2 iGPU (gfx1036, native wave64, XCL_ADAPTER=radeon). Rust 1.100 MSVC, release.
  • All 2,072 measured rows through xcl's kernel path, compared with the gfx1036's results: 2072/2072 equal on both adapters, in both backends.
  • The new tests pass on both adapters. The table test fails with each of six deliberate mistakes:
    • the 2^128 threshold moved to 1969 (HLSL);
    • the exponent threshold moved to 1078 (HLSL);
    • one bit of the table's last word changed;
    • 52 bits per S1 step instead of 53 (direct DXIL);
    • the denormal shift off by one (direct DXIL);
    • S1 masked to 6 bits (direct DXIL).
  • The whole suite: cargo build --release --workspace, then cargo test --release --workspace --no-fail-fast -- --test-threads=4, with and without XCL_DIRECT_DXIL=all, against base 1a0bbe3 on the same machine:
Adapter Mode base 1a0bbe3 this PR (44455fe)
RTX 5070 Ti default 1246 passed, 4 failed, 46 ignored 1248 passed, 4 failed, 46 ignored
RTX 5070 Ti XCL_DIRECT_DXIL=all 1245 passed, 5 failed, 46 ignored 1247 passed, 5 failed, 46 ignored
Radeon gfx1036 default 1249 passed, 1 failed, 46 ignored 1251 passed, 1 failed, 46 ignored
Radeon gfx1036 XCL_DIRECT_DXIL=all 1248 passed, 2 failed, 46 ignored 1250 passed, 2 failed, 46 ignored

The +2 are the new tests. The failing tests are the same on base and PR, and all are tracked: fuzz_validate_and_gpu_0..3 on NVIDIA (#1905), fuzz_validate_and_gpu_1 on RDNA2 (#2115, #2170) and offline_compute_pipeline_is_reused_by_a_second_process with direct DXIL (#1674).

Regression run
Base (main 06c9f55), NVIDIA GeForce RTX 5070 Ti:

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: NVIDIA GeForce RTX 5070 Ti
title: title-a
seconds: 60
exit: 0x0
frames shown: 819 (PresentX 820)
presents per 2 s (last 3): 13 28 28
device removals: 0
crashes: 0
peak commit: 16053 MB

This PR (44455fe), same card:

xcl-smoke v1
commit: 44455fe9109fed47598714ecc1a464c96d1012c8
gpu: NVIDIA GeForce RTX 5070 Ti
title: title-a
seconds: 60
exit: 0x0
frames shown: 929 (PresentX 929)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15797 MB

Earlier runs here ended at about 44 s on main and on this PR alike. The cause was #695: this PC had no active playback device. With one active, both runs reach their end.

Not tested

  • OMOD / CLAMP on this instruction (refused).
  • A program that flushes f64 denormals. With MODE's double / half denormal field set to flush output denormals, the gfx1036 returns 0 for all 223 measured denormal results.
    • This keeps them, like xcl's other f64 instructions, which do not read that field.
    • Denormals kept is the mode the measurements and the test use.
  • RDNA3 / RDNA4, Intel, and whether a retail program uses the instruction.

AI use

  • Claude Code (Claude Opus 5.5) did the work: it measured the hardware, wrote both lowerings and the tests, ran the comparisons, the mutation checks and the suites, and wrote this description.

🤖 Generated with Claude Code

**@⁠NWProgramming00** on GitHub · 2026-11-25 13:39 UTC · [original](https://github.com/xcl-dev/xcl/pull/2315) `NWProgramming00:recomp/trig-preop` → `main` · +260 −2 · 4 files · 1 commits · open Both backends refused `v_trig_preop_f64`. This translates it as RDNA2 hardware runs it. I measured the hardware first, then checked xcl against it bit for bit. **What the hardware does** (gfx1036, 2,072 measured rows, MODE with f64 denormals kept) - The result is the 53 bits of 2/π that start `shift` bits below b1200, times 2^(−53 − shift). - `shift = S1[4:0] × 53`, plus `exp(S0) − 1077` when the S0 exponent is above 1077. - When the S0 exponent is 1968 or more, the scale gets an extra 2^128. - This is the ISA pseudocode. - **The hardware's table ends at b17.** Windows that reach below it read zeros. The full 1,201-bit constant gives different results on 104 of the measured rows; the 1,184-bit table matches all of them. - **Rounding cannot change the result.** - Table bit b_j is worth 2^(j − 1201), or 2^(j − 1073) with the 2^128 scale, for every input. - With the 2^128 scale, every bit down to b17 is at or above 2^−1074, so the result is exact. - Without it, the bits below 2^−1074 always start at b126, and b126 is 0. So rounding to nearest and truncating give the same double for every input. - Of 570 rows whose window straddles 2^−1074, 471 are inexact; the hardware matches both forms on all of them. - So the double is built from integers. - Only the exponent of S0 is read: NaN, inf, negative and denormal S0 follow the same rule. Bits above S1[4:0] are ignored. **What is in it** - `crates/xcl/src/gpu/recomp/emit.rs`: - a `trig_preop()` helper and the 37-word table (`TRIG_PREOP_2_OVER_PI`); - both go into the HLSL only when a program uses the instruction, so no other shader's text changes. - `crates/xcl/src/gpu/dxil/lower_wide.rs`: the same computation in the direct DXIL translator. It reads the table with a chain of selects, because that translator has no constant arrays. - `crates/xcl/src/gpu/dxil/lower.rs` and `emit.rs`: `v_trig_preop_f64` leaves the list of f64 ops that follow the f64 round mode (it does not round). - An output modifier (`OMOD` / `CLAMP`) is refused, because I did not measure it. - `crates/xcl/src/gpu/recomp/tests.rs`: - `gpu_trig_preop_f64_matches_rdna2` checks both backends against 64 rows measured on the gfx1036. - `trig_preop_f64_with_omod_is_refused` checks the OMOD refusal in both backends. **Tested** - Windows 11, NVIDIA GeForce RTX 5070 Ti 16 GB (wave64 as two halves) and an AMD Radeon RDNA2 iGPU (gfx1036, native wave64, `XCL_ADAPTER=radeon`). Rust 1.100 MSVC, release. - **All 2,072 measured rows through xcl's kernel path, compared with the gfx1036's results:** 2072/2072 equal on both adapters, in both backends. - **The new tests pass on both adapters.** The table test fails with each of six deliberate mistakes: - the 2^128 threshold moved to 1969 (HLSL); - the exponent threshold moved to 1078 (HLSL); - one bit of the table's last word changed; - 52 bits per S1 step instead of 53 (direct DXIL); - the denormal shift off by one (direct DXIL); - S1 masked to 6 bits (direct DXIL). - **The whole suite:** `cargo build --release --workspace`, then `cargo test --release --workspace --no-fail-fast -- --test-threads=4`, with and without `XCL_DIRECT_DXIL=all`, against base `1a0bbe3` on the same machine: | Adapter | Mode | base `1a0bbe3` | this PR (`44455fe`) | |---|---|---|---| | RTX 5070 Ti | default | 1246 passed, 4 failed, 46 ignored | 1248 passed, 4 failed, 46 ignored | | RTX 5070 Ti | `XCL_DIRECT_DXIL=all` | 1245 passed, 5 failed, 46 ignored | 1247 passed, 5 failed, 46 ignored | | Radeon gfx1036 | default | 1249 passed, 1 failed, 46 ignored | 1251 passed, 1 failed, 46 ignored | | Radeon gfx1036 | `XCL_DIRECT_DXIL=all` | 1248 passed, 2 failed, 46 ignored | 1250 passed, 2 failed, 46 ignored | The +2 are the new tests. The failing tests are the same on base and PR, and all are tracked: `fuzz_validate_and_gpu_0..3` on NVIDIA (#1905), `fuzz_validate_and_gpu_1` on RDNA2 (#2115, #2170) and `offline_compute_pipeline_is_reused_by_a_second_process` with direct DXIL (#1674). **Regression run** Base (`main` `06c9f55`), NVIDIA GeForce RTX 5070 Ti: ``` xcl-smoke v1 commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324 gpu: NVIDIA GeForce RTX 5070 Ti title: title-a seconds: 60 exit: 0x0 frames shown: 819 (PresentX 820) presents per 2 s (last 3): 13 28 28 device removals: 0 crashes: 0 peak commit: 16053 MB ``` This PR (`44455fe`), same card: ``` xcl-smoke v1 commit: 44455fe9109fed47598714ecc1a464c96d1012c8 gpu: NVIDIA GeForce RTX 5070 Ti title: title-a seconds: 60 exit: 0x0 frames shown: 929 (PresentX 929) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15797 MB ``` Earlier runs here ended at about 44 s on `main` and on this PR alike. The cause was #695: this PC had no active playback device. With one active, both runs reach their end. **Not tested** - `OMOD` / `CLAMP` on this instruction (refused). - **A program that flushes f64 denormals.** With MODE's double / half denormal field set to flush output denormals, the gfx1036 returns 0 for all 223 measured denormal results. - This keeps them, like xcl's other f64 instructions, which do not read that field. - Denormals kept is the mode the measurements and the test use. - RDNA3 / RDNA4, Intel, and whether a retail program uses the instruction. **AI use** - Claude Code (Claude Opus 5.5) did the work: it measured the hardware, wrote both lowerings and the tests, ran the comparisons, the mutation checks and the suites, and wrote this description. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Both backends refused v_trig_preop_f64. This follows the RDNA2 ISA
pseudocode with the table measured on an RDNA2 gfx1036: the 53 bits of
2/pi that start S1[4:0] * 53 bits (plus exp(S0) - 1077 past 1077) below
b1200, times 2^(-53 - shift), plus 2^128 when exp(S0) >= 1968. The
hardware's table ends at b17. The product is then always exact, so the
double is built from integers.

The HLSL backend reads a 37-word table that it declares only when the
instruction is used. Direct DXIL selects the words. An output modifier
is refused; it was not measured.

gpu_trig_preop_f64_matches_rdna2 checks both backends against 64 rows
measured on the gfx1036.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:27:04 +00:00
Author
Owner

@⁠nnonno91 on GitHub · 2026-11-25 14:04 UTC · original

Independent focused check of 44455fe9109fed47598714ecc1a464c96d1012c8 on Radeon integrated graphics and RTX 5090.

Tested

  • Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC, Windows SDK DXC/DXIL 1.8.2502.11, unoptimized profile. AMD Radeon(TM) Graphics driver 32.0.21042.62 and GeForce RTX 5090 driver 32.0.16.1742.
  • Ran cargo test --locked -p xcl --lib trig_preop_f64 -- --nocapture --test-threads=1 in separate processes with XCL_ADAPTER=AMD Radeon or XCL_ADAPTER=NVIDIA GeForce RTX 5090 (XCL_REQUIRE_GPU=wave64 on Radeon). Each run gave 2 passed, 0 failed, 0 ignored and no skips.
  • On each GPU, all 64 checked-in hardware reference values matched bit for bit through HLSL and direct DXIL. The separate OMOD refusal test passed for both translators.

Not tested

The larger hardware capture, retail smoke, and the full workspace suite.

AI use

OpenAI Codex (GPT-6.2 Sol) ran the tests, checked the logs and wrote this report.

**@⁠nnonno91** on GitHub · 2026-11-25 14:04 UTC · [original](https://github.com/xcl-dev/xcl/pull/2315#issuecomment-7164716332) Independent focused check of `44455fe9109fed47598714ecc1a464c96d1012c8` on Radeon integrated graphics and RTX 5090. **Tested** - Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC, Windows SDK DXC/DXIL 1.8.2502.11, unoptimized profile. AMD Radeon(TM) Graphics driver 32.0.21042.62 and GeForce RTX 5090 driver 32.0.16.1742. - Ran `cargo test --locked -p xcl --lib trig_preop_f64 -- --nocapture --test-threads=1` in separate processes with `XCL_ADAPTER=AMD Radeon` or `XCL_ADAPTER=NVIDIA GeForce RTX 5090` (`XCL_REQUIRE_GPU=wave64` on Radeon). Each run gave 2 passed, 0 failed, 0 ignored and no skips. - On each GPU, all 64 checked-in hardware reference values matched bit for bit through HLSL and direct DXIL. The separate OMOD refusal test passed for both translators. **Not tested** The larger hardware capture, retail smoke, and the full workspace suite. **AI use** OpenAI Codex (GPT-6.2 Sol) ran the tests, checked the logs and wrote this report.
Author
Owner

@⁠SlopTart on GitHub · 2026-11-25 14:10 UTC · original

RDNA3 confirmation: the new f64 trig translation is bit-exact on gfx1100 too.

The fix is calibrated against gfx1036 hardware measurements; here it is on RDNA3 (RX 7900 XT, NAVI31) under RADV, which the PR does not list as tested.

Tested: origin/main 379725ae + PR head 44455fe9 (merge c7785944, no conflicts, mergeable clean onto current main). Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT, cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, official MS DXC (binaries 2026-09-29).

  • gpu_ suite (--release --locked, --test-threads=2): 143 passed / 3 failed / 1 ignored, versus 142 / 3 on plain origin/main on this rig. The pass delta is exactly this PR's added test, and the 3 failures are the rig's known baseline, unchanged: fuzz seed 1003 (posted on #2115), gpu_nonfused_mad_and_mac_prevent_fma_contraction, gpu_depth_buffer_created_for_unordered_access_is_placed.
  • So on RDNA3 + Mesa 26.2.5, the translated v_trig_preop_f64 matches what the test expects bit for bit, same as on the gfx1036 measurements.

Not tested: Windows on this card; gfx103x native Linux (native target, not Wine).

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

**@⁠SlopTart** on GitHub · 2026-11-25 14:10 UTC · [original](https://github.com/xcl-dev/xcl/pull/2315#issuecomment-7164883151) **RDNA3 confirmation: the new f64 trig translation is bit-exact on gfx1100 too.** The fix is calibrated against gfx1036 hardware measurements; here it is on RDNA3 (RX 7900 XT, NAVI31) under RADV, which the PR does not list as tested. **Tested**: `origin/main` `379725ae` + PR head `44455fe9` (merge `c7785944`, no conflicts, mergeable clean onto current main). Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT, cargo target `x86_64-pc-windows-gnu` run under wine-11.0 (Proton), rustc 1.94.0, official MS DXC (binaries 2026-09-29). - `gpu_` suite (`--release --locked`, `--test-threads=2`): 143 passed / 3 failed / 1 ignored, versus 142 / 3 on plain `origin/main` on this rig. The pass delta is exactly this PR's added test, and the 3 failures are the rig's known baseline, unchanged: fuzz seed 1003 (posted on #2115), `gpu_nonfused_mad_and_mac_prevent_fma_contraction`, `gpu_depth_buffer_created_for_unordered_access_is_placed`. - So on RDNA3 + Mesa 26.2.5, the translated `v_trig_preop_f64` matches what the test expects bit for bit, same as on the gfx1036 measurements. **Not tested**: Windows on this card; gfx103x native Linux (native target, not Wine). **AI use**: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 17:52 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. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 17:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/2315#issuecomment-7170128835) 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!
Author
Owner

@⁠raylitebull on GitHub · 2026-11-25 18:41 UTC · original

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

**@⁠raylitebull** on GitHub · 2026-11-25 18:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/2315#issuecomment-7171251072) Diff review, CI, and both AMD and NVIDIA checks passed on `44455fe`, but recent `origin/main` merges turned this branch `CONFLICTING/DIRTY`. Please rebase onto `origin/main` so it becomes mergeable again.
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/gpu/dxil/lower_wide.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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