Skip to content

Recompiler: translate v_trig_preop_f64 in both backends - #2315

Open
NWProgramming00 wants to merge 1 commit into
xcl-dev:mainfrom
NWProgramming00:recomp/trig-preop
Open

NWProgramming00 wants to merge 1 commit into
xcl-dev:mainfrom
NWProgramming00:recomp/trig-preop

Conversation

@NWProgramming00

@NWProgramming00 NWProgramming00 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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

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 <noreply@anthropic.com>
@SlopTart

Copy link
Copy Markdown

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 DXIL fuzz tests: fuzz_validate_and_gpu_1 fails on AMD (seed 722 and others, RDNA2-4), so the default suite is red there #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.

@NWProgramming00
NWProgramming00 marked this pull request as ready for review November 25, 2026 14:19
@throny throny added the off-goal Does not bear on the current goal (world loading); waits until it is reached label Nov 25, 2026
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@raylitebull

Copy link
Copy Markdown
Member

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.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…cl-dev#2315)

LATER shared semantics: the measured 2/pi fraction table
(TRIG_PREOP_2_OVER_PI, b1200..b17) lands verbatim in recomp/emit.rs
+ the 64-row gfx1036 TRIG_PREOP_RDNA2 vector set in recomp/tests.rs,
for the future SPIR-V port (exact integer construction from the
table + shift formula). HLSL/DXIL lowering + GPU/omod tests stay out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

off-goal Does not bear on the current goal (world loading); waits until it is reached

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants