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, on this change rebased onto main 8b957be (9a608a4), against that base on the same machine:
Adapter Mode base 8b957be this PR (9a608a4)
RTX 5070 Ti default 1442 passed, 4 failed, 47 ignored 1444 passed, 4 failed, 47 ignored
RTX 5070 Ti XCL_DIRECT_DXIL=all 1442 passed, 4 failed, 47 ignored 1444 passed, 4 failed, 47 ignored
Radeon gfx1036 default 1445 passed, 1 failed, 47 ignored 1447 passed, 1 failed, 47 ignored
Radeon gfx1036 XCL_DIRECT_DXIL=all 1445 passed, 1 failed, 47 ignored 1447 passed, 1 failed, 47 ignored

The +2 are the new tests, which pass in all four rows. The failing tests are the same on base and PR, and both are tracked: fuzz_validate_and_gpu_0..3 on NVIDIA (#1905) and fuzz_validate_and_gpu_1 on RDNA2 (#2115, #2170).

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

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 5070 Ti
title: title-a
seconds: 60
exit: 0x0
frames shown: 649 (PresentX 650)
presents per 2 s (last 3): 32 32 33
device removals: 0
crashes: 0
peak commit: 16853 MB

This PR (a2d9c47), same card:

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

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.
  • RDNA4 and Intel by me. RDNA3 (gfx1100, RADV) was checked by @SlopTart in a comment below, bit-exact.
  • 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

@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.
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>
@NWProgramming00

Copy link
Copy Markdown
Contributor Author

@raylitebull rebased as asked. The new head is a2d9c47, on e120925, the main commit just before #2555. It merges cleanly into current main 8b957be, and the merge gives the same tree as the commit the suite ran on.

The description has the numbers.

@Decodere

Copy link
Copy Markdown
Contributor

Independent desktop NVIDIA verification of PR #2315 at rebased head a2d9c470554cf43ad412af89a5b7c53812ebe4dc (1 commit; exact parent & merge-base: e120925e3abc9956a8b9bae631969d32e8ccd43c, pre-#2555 ancestor of main).

Environment

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12, Wave32-only Ada), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6C / 12T), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200 (Developer Mode off)
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC 1.9.2609.5, --release

Targeted release tests & build (a2d9c470)

Command Result
cargo test --release -p xcl --lib trig_preop_f64_with_omod_is_refused -- --nocapture 1 passed; 0 failed; 0 ignored; 1343 filtered out (0.00s)
cargo test --release -p xcl --lib gpu_trig_preop_f64_matches_rdna2 -- --nocapture (3 consecutive runs, on-GPU RTX 4060 across both HLSL and direct DXIL backends) 1 passed; 0 failed; 0 ignored; 1343 filtered out × 3 (0.26s, 0.22s, 0.22s)
cargo test --release -p xcl --lib gpu::dxil:: 184 passed; 0 failed; 3 ignored; 1157 filtered out (3.79s; dispatch_chain_native_wave64_builds_whole self-skipped on Wave32-only RTX 4060)
cargo test --release -p xcl --lib gpu::recomp:: 403 passed; 0 failed; 20 ignored; 921 filtered out (5.27s; Wave64-only and #1426-refused subcases self-skipped as expected)
cargo build --release -p xcl --bin xcl Clean build (1m 14s, exit 0)

Paired quiet 60 s retail smoke (title: 29640639)

Exact parent & merge-base (e120925e3abc9956a8b9bae631969d32e8ccd43c, cached baseline on the same machine):

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 4060
title: 29640639
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: 13979 MB

PR #2315 (a2d9c470554cf43ad412af89a5b7c53812ebe4dc):

xcl-smoke v1
commit: a2d9c470554cf43ad412af89a5b7c53812ebe4dc
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1579 (PresentX 1579)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14021 MB

Runtime log observations (xcl.log)

  • 9222 log lines, 0 [ERROR] / device-removal / panic lines.
  • Final draw/memory telemetry in log: 19474 draws with translated pipelines (0 skipped, 0 with PC pipelines), 1024 dispatches (0 skipped); last memory sample 14020 MB committed by the process; video memory 6114 MB local, 4683 MB non-local.

Limits of coverage

  • gpu_trig_preop_f64_matches_rdna2 executed directly on the RTX 4060 GPU across both the HLSL and direct DXIL backends (3/3 passes against all 64 RDNA2 gfx1036 reference rows), and trig_preop_f64_with_omod_is_refused verified refusal of output modifiers.
  • Wave64-only GPU subcases in gpu::dxil:: and gpu::recomp:: self-skipped on the Wave32-only RTX 4060, and the existing #[ignore] dump/fuzz/benchmark tests remained ignored.
  • The 60 s retail smoke on title: 29640639 serves as an end-to-end non-regression check and was not confirmed to execute a retail shader containing v_trig_preop_f64.

AI disclosure: test execution and report drafting assisted by Google Antigravity (Gemini 4 Neon).

@artor410

Copy link
Copy Markdown
Contributor

Test report, head a2d9c47, NVIDIA GeForce RTX 3070 Ti (driver 610.88), Windows 11, rustc 1.100.0, debug build, no game data (no XCL_SAMPLE_* set).

cargo test --workspace -- --test-threads=4: 1396 passed, 1 failed, 31 ignored.

The one failure is e2e::test_title_runs_under_the_debug_layer_with_dred (test title exits 0xC0000005 under the debug layer). It fails the same way on main e429936 on this machine, so it is not from this PR. Everything else passes.

AI use: Claude Code (Claude Opus 5.5) ran the suite and wrote this.

@thehollowedone

Copy link
Copy Markdown
Contributor

Supplemental arithmetic check for a2d9c470554cf43ad412af89a5b7c53812ebe4dc. I independently recomputed the first 1,184 bits of 2/π and matched all 37 words in the proposed table. In an isolated Python model, I enumerated all 65,536 pairs of the 11-bit exponent and 5-bit segment. The three-word window agreed with arbitrary-precision bit extraction, and the bit packing agreed with binary64 scaling: 32,025 normal, 2,012 subnormal, and 31,499 zero cases, with no mismatches.

That checks the arithmetic for the proposed table with subnormals preserved. It does not establish RDNA2’s hardware table length, emitted DXIL or HLSL behavior, or a Windows GPU result. No XCL code was executed. AI assistance: OpenAI ChatGPT wrote and ran the independent arithmetic model.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head a2d9c47

No open or merged PR translates v_trig_preop_f64; on main it is only in the generated spec table (#2169 merely lists TRIG_PREOP in its fuzzer's skip list).
Read both forms (recomp/emit.rs trig_preop, dxil/lower_wide.rs Lw::trig_preop): same shift, window and denormal handling, and the test runs both backends against the 64 measured gfx1036 rows. Minor: in the direct form the 64-bit shifts by k / -k can get a count >= 64 on the branch the select drops; the HLSL form masks it with & 63, the direct one does not.
Recommendation: keep open.

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

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.

9 participants