Skip to content

startup: draw the gfx2hlsl reciprocal, root, exp2 and log2 operations - #1745

Open
Asimmimx wants to merge 1 commit into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-float-transcendental-check
Open

Asimmimx wants to merge 1 commit into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-float-transcendental-check

Conversation

@Asimmimx

Copy link
Copy Markdown
Contributor

Refs #1149.

Change

The VEC_F table of tools/startup/gfx2hlsl.py lowers six operations to HLSL, and no check ran any of them:

Operation Lowering
v_rcp_f32, v_rcp_iflag_f32 1.0 / x
v_sqrt_f32 sqrt(x)
v_rsq_f32 rsqrt(x)
v_exp_f32 exp2(x)
v_log_f32 log2(x)

This PR adds a check only. The translator does not change.

tests/startup_gfx2hlsl_float_transcendental_check.py draws them with the per-lane runner tests/startup_gfx2hlsl_lane_alu.py. The runner is byte-identical to the copies in #1703, #1706, #1708, #1712 and #1741. Each draw runs one operation per lane on flat inputs, alternating _e32 and _e64, and compares the four result words exactly.

Exact inputs only. Every input is a power of two, or 0 and 9 for the square root, so every result is exact:

  • reciprocals: 4, -0.5, 2^20, 2^-10;
  • roots: 16, 0.25, 4096, 2^-20;
  • exponents: -10..20;
  • logarithms: 2^-20..2^30.

The draw therefore checks which function and which base each operation is lowered to. v_exp_f32 and v_log_f32 are base 2 in the AMD RDNA 2 ISA reference. The draw does not check the precision of an adapter's approximation.

Mutations, one per operation. The first draw with the mutated lowering must read the mutation's own reference words:

  • 2.0 / x for the reciprocals;
  • sqrt and rsqrt exchanged;
  • exp2(x) * 2;
  • log2(x) + 1.

v_sin_f32 and v_cos_f32 are not covered. Their results at the few exact points depend on the approximation.

Results

Commit 627d66e8 on origin/main. Windows 11 26300.9974, DXC 1.8.2502.11, FXC 10.0.26100, debug layer on.

Adapter Driver Draws (FXC ps_5_1 + DXC ps_6_0) Mutated draws
WARP 10.0.26100.9818 24/24 match 12/12 read the mutated words
AMD Radeon RX 6600 (RDNA 2) 32.0.21045.29003 24/24 match 12/12 read the mutated words

There were no skips. binary with llvm-mc 19.1.1 gives fixture words match llvm-mc. docs_links_check.py passes.

AI-assisted (Claude Code).

The VEC_F table lowers v_rcp_f32, v_rcp_iflag_f32, v_sqrt_f32, v_rsq_f32,
v_exp_f32 and v_log_f32 to 1.0 / x, sqrt, rsqrt, exp2 and log2, and no
check ran any of them.

tests/startup_gfx2hlsl_float_transcendental_check.py draws them with the
shared per-lane runner (byte-identical copy of
tests/startup_gfx2hlsl_lane_alu.py) on powers of two (and 0 and 9 for the
square root), so every result is exact and the draw checks which function
and which base each operation is lowered to, not its precision. Each
operation has a mutated lowering whose first draw must read its own words.

All 24 draws match on WARP and an RX 6600 through FXC ps_5_1 and DXC
ps_6_0, and the 12 mutated draws read their mutated words.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 627d66e8d5c45b176e88ee53bb6037021b0d9fbe. Tests, CMake, CI list and docs only; tools/startup/gfx2hlsl.py is unchanged. Verdict: 0 P1, 0 P2, with coverage nits.

Linux limits. Linux x86_64, llvm-mc-19 (the new CI entry pins llvm-mc-15), Python 3.14. No Windows, no DXC/FXC, no WARP or GPU: I could NOT run the draw mode, so the claimed 24 matching and 12 mutated draws on WARP and RX 6600 are not verified by me.

What I ran

  • python3 -B tests/startup_gfx2hlsl_float_transcendental_check.py (controls): passes. ... binary llvm-mc-19: fixture words match llvm-mc.
  • git diff --check origin/main...HEAD, docs_links_check.py, bidi_controls_check.py: clean; no non-ASCII bytes in the diff.
  • Semantics: the lowerings are 1.0 / x (rcp and rcp_iflag), rsqrt, sqrt, exp2, log2; v_exp_f32 and v_log_f32 are base 2 in the ISA, and every draw input is a power of two (or 0 and 9 for sqrt), so every reference is exact and the test checks which function is used, not the approximation error (the docs say so). Reference values recomputed by hand for a few lanes (rcp 4 = 0.25, rsq 16 = 0.25, exp2(10) = 1024, log2(2^-20) = -20): consistent.
  • Mutations (restored with git checkout -- .):
    • v_rcp_iflag_f32 1.0 / {0} -> 2.0 / {0}: controls FAILED (('v_rcp_iflag_f32', 0), the mutation regex no longer matches).
    • v_exp_f32 exp2 -> exp: controls FAILED (('v_exp_f32', 0), same mechanism).
    • v_rsq_f32 rsqrt({0}) -> 1.0 / sqrt({0}): controls FAILED (same mechanism; the value is the same, the shape is not).
    • test body word of v_rsq_f32 0x5d -> 0x5c: FAILED in binary (assembled words differ).
    • v_sqrt_f32 sqrt({0}) -> sqrt(abs({0})): controls and binary PASSED. This one also survives the draws: every v_sqrt_f32 draw input is 0 or positive, so the negative-domain behavior is not observed anywhere.

Coverage nits (not blocking)

  • Specials are untested for all six operations: rcp(+/-0), rsq(0), sqrt(-x) and sqrt(-0), log2(0), log2(-x), exp2 overflow and underflow, NaN and infinity, and denormal inputs. These are where RDNA 2 and HLSL may differ (flush, sign of zero, NaN payload), so the "which function is lowered" check does not protect them.
  • v_rcp_f32 and v_rcp_iflag_f32 share one lowering and one input set; the flag behavior of _iflag is not (and cannot be) observed.
  • As on the sibling PR #1741, a wrong value that keeps the shape of the translated text is only caught by the Windows draws.

Not approving (comment only).

AI-assisted; commands listed were executed locally.

@thepatrickworld

Copy link
Copy Markdown
Contributor

Draw mode, run on Windows

tedin7's review says it plainly: I could NOT run the draw mode, so no draw-mode result was on record outside the author's machine. They run here.

2026-11-24, head 627d66e8 merged onto main (my merge 3939c6a3, tree a2b5369f, 1101 tests), ctest --preset dev -R "^startup_gfx2hlsl_float_transcendental" --no-tests=error --output-on-failure -V, three consecutive runs, identical each time:

377: warp: 24 draws matched, 12 mutated draws read their mutated words
378: hardware: 24 draws matched, 12 mutated draws read their mutated words
100% tests passed out of 3

Same totals as the table in the PR body. What differs, row by row: the hardware row is a different AMD architecture, AMD Radeon RX 9070 XT 1002:7550 driver 32.0.32029.2010 (RDNA 4, gfx1201) instead of the RX 6600 (RDNA 2); the WARP row is the same Microsoft software adapter, with a newer compiler build. Both rows run DXC 1.9.2602.17 rather than 1.8.2502.11.

One line of detail on the mutations, because it separates "reads a different number" from "reads the mutation's own reference": for v_rcp_f32 the mutated draw reads 3f000000,c0800000,40000000,41800000, which is 0.5, -4.0, 2.0, 16.0 — exactly 2.0 / x for the four inputs 4.0, -0.5, 1.0, 0.125, and not a rounding difference. I decoded the words by hand for that operation only; the log reports 12 mutated draws per adapter (six operations × fxc and dxc), each reading its own mutated words.

Environment: Windows 11 25H2 26200.9974, DXC 1.9.2602.17, FXC 10.0.26100, dev preset, debug_layer=unavailable.

Limits: one adapter, one driver, no debug layer. As the test says, the inputs are exact powers of two, plus the two sqrt cases (0 and 9) whose roots are integers, so this checks which function and base each operation is lowered to, not the precision of any adapter's approximation. Nothing here covers v_sin_f32 or v_cos_f32, which the PR leaves out.

AI-assisted; runs executed on the machine above.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Hardware run of head 627d66e. Windows 11 Pro 26100.9168, DXC/FXC from Windows SDK 10.0.26100, dev preset, debug layer unavailable here. Adapter selected with XBOX2WINDOWS_ADAPTER: WARP 10.0.26100.8972, NVIDIA Quadro M1000M (Maxwell, 10de:13b1) 32.0.15.7371, Intel HD Graphics 530 (Gen9, 8086:191b) 31.0.101.2111.

Test Result
startup_gfx2hlsl_float_transcendental_controls Passed
startup_gfx2hlsl_float_transcendental_warp Passed
startup_gfx2hlsl_float_transcendental_hardware on the HD 530 Passed
startup_gfx2hlsl_float_transcendental_hardware on the Quadro M1000M Failed at v_sqrt_f32 (FXC)

The rcp, rcp_iflag and rsq rows pass on the Quadro too. The failing draw:

AssertionError: fxc v_sqrt_f32 inputs [(897581056, 0, 0), (1115684864, 0, 0), (0, 0, 0), (1091567616, 0, 0)]
startup_flat_param mismatch expected=3a800000,41000000,00000000,40400000 actual=3a800000,41000000,00000000,40400001

sqrt(9.0) (0x41100000) returns 0x40400001 on Maxwell, one ulp above the exact 0x40400000; the other lanes are exact. D3D's sqrt is not required to be correctly rounded, so this looks like an adapter tolerance rather than a lowering error. Whether the fixture should accept one ulp here, or the lowering should be made exact, is your call; I have not tested either.

No changes made.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@Asimmimx could you rebase this branch onto current main and force-push?

GitHub reports head 627d66e8 as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (cf97bee1):

  • .github/workflows/windows.yml
  • CMakeLists.txt
  • docs/experiments.md
  • tests/startup_gfx2hlsl_lane_alu.py

Commands run locally:

git fetch origin main pull/1745/head
git merge-tree --write-tree --name-only origin/main FETCH_HEAD
gh pr view 1745 --json mergeable   # CONFLICTING

Once it is rebased and CI is green it will be re-checked right away.

AI-assisted; commands listed were executed locally.

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.

4 participants