Skip to content

shader: lower the SDWA forms of ten unary f32 operations in pixel_aot - #756

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
rasptum:shader/sdwa-f32-unary
Nov 23, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
rasptum:shader/sdwa-f32-unary

Conversation

@rasptum

@rasptum rasptum commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Change

pixel_aot lowers the SDWA forms of the exact binary32 arithmetic (add, sub, subrev, mul, mul_legacy, max, min, floor, ceil, fract, trunc) through one path, SDWA_ALU. The SDWA forms of the other unary binary32 operations it already lowers in e32 and e64 were unrecognised mnemonics.

This adds those ten to the same path: v_exp_f32_sdwa, v_log_f32_sdwa, v_rcp_f32_sdwa, v_rcp_iflag_f32_sdwa, v_rsq_f32_sdwa, v_sqrt_f32_sdwa, v_sin_f32_sdwa, v_cos_f32_sdwa, v_rndne_f32_sdwa and v_frexp_mant_f32_sdwa. The translator change is one tuple. Each operation reuses its existing entry in ALU unchanged, so the whole-dword SDWA spelling and the e32 spelling produce the same shader (the controls assert this for all ten), and the result is placed with the SDWA move rules already documented.

The limit recorded in docs/experiments.md for SDWA float arithmetic is kept: only src0_sel:DWORD is accepted, because a byte or word source would reach the arithmetic as a zero-extended, usually subnormal binary32. For each of the ten forms the controls check that these stay refused: a byte or word source (BYTE_0, BYTE_3, WORD_0, WORD_1), a src1_sel, a second operand, an output modifier (mul:2), UNUSED_SEXT on a partial destination, and UNUSED_PRESERVE over an untracked destination.

Three programs are drawn, two constant rows each. A, B, C and D are constants s44 to s47.

D3D allows exp2, log2, a reciprocal, rsqrt, sqrt, sin and cos an error, so a raw result has no single binary32 reference. As in #729, the first two programs choose inputs whose true result is an integer, then add 0.5 and take the floor, which returns that integer for any result within 0.5 of it. This checks which operation ran and its source modifiers, not last-place accuracy.

Instruction (all src0_sel:DWORD) Row 1 Row 2
v_exp_f32_sdwa v12, -s44 8 16
v_log_f32_sdwa v13, |s45| 5 3
v_rcp_f32_sdwa v14, s46 8 -2
v_rcp_iflag_f32_sdwa v15, -|s47| -4 -16
v_rsq_f32_sdwa v12, |s44| 4 2
v_sqrt_f32_sdwa v13, s45 7 3
v_sin_f32_sdwa v14, s46 (revolutions) 1 -1
v_cos_f32_sdwa v15, -s47 (revolutions) -1 1

The third program is exact and is compared directly:

Instruction Row 1 Row 2
v_rndne_f32_sdwa v12, s44 (ties 2.5 and -3.5) 2 -4
v_frexp_mant_f32_sdwa v13, -s45 clamp 0.75 0 (clamped from -0.75)
v_rndne_f32_sdwa v14, s46 dst_sel:BYTE_2 dst_unused:UNUSED_PRESERVE over 1.0 0.51953125 0.515625
v_frexp_mant_f32_sdwa v15, s47 dst_sel:BYTE_3 dst_unused:UNUSED_PAD 2 -2

Negative controls: 26 wrong references, each replacing one channel, must report pixel_mismatch.

Wrong reference Value in the wrong reference Drawn
exp with the negation dropped 0 8
natural exponential for exp2 20 8
natural logarithm for log2 3 5
source copied for log 32 5
rsq for rcp 3 8
source copied for rcp 0 8
rcp_iflag with the negation dropped 4 -4
source copied for rcp_iflag 0 -4
sqrt for rsq 0 4
rcp for rsq 16 4
rsq for sqrt 0 7
source copied for sqrt 49 7
cos for sin 0 1
radians for revolutions in sin 0 1
sin for cos 0 -1
radians for revolutions in cos 1 -1
rndne with ties rounded up 3 2
rndne with ties rounded toward zero (row 2) -3 -4
frexp_mant with the negation dropped 0 0.75
source copied for frexp_mant, then clamped 1 0.75
clamp dropped (row 2) -0.75 0
padded instead of preserved 0 0.51953125
placed in the neighbouring byte 1 0.51953125
ties rounded up, then placed (row 2) 0.5234375 0.515625
result not placed 0.75 2
wrong byte value placed 4 2

That is 32 draws from three compiles and three renderer launches. They are registered as their own CTest entries, pixel_aot_sdwa_unary_hardware and pixel_aot_sdwa_unary_warp (tests/pixel_aot_check.py draw-sdwa-unary), each with its own FORM_COVERAGE_RECORD, so nothing is added to pixel_aot_hardware. The three programs are added to the LLVM 15 round trip, tests/form_coverage_check.py asserts the ten forms, and docs/experiments.md has a paragraph.

tools/scan_unsupported_shaders.py gains one pattern built from pixel_aot.SDWA_ALU, with a test in tests/rdna2_tools_check.py. The scanner had no pattern for SDWA float arithmetic, so this also makes it report the eleven forms accepted before this change as supported. It requires whole-dword sources, as the translator does.

This advances instruction-form coverage of the pixel translator by ten forms on gfx1013 and ten on gfx1030.

Verification

Update before opening: rebased onto main at 356a7be as c13268a. On that commit I reran the LLVM 15 round trip, tests/form_coverage_check.py, the static controls, and this PR's own draw test (draw-sdwa-unary) on WARP and on the RTX 3060: all pass. Other results below are from the commits named there.

Known red check, not from this PR: main is currently broken by a NameError in check_scalar_carry_chain_draw (since #695), which fails pixel_aot_predicate_rounding_* and the combined draw run for every branch. #752 is the one-line fix. I will rebase this PR once main is green.

Rebased onto main at cfb2600 as f3dcbaf. The four commits that landed in between (#627, #740) touch no shader file. On the rebased commit I reran the two new tests on WARP and on hardware, the controls, the LLVM 15 round trip, tests/form_coverage_check.py, tests/rdna2_tools_check.py and tests/docs_links_check.py (all pass). The full ctest run and the coverage count below are from e47be05, the same change on main at fd2486f.

Run at e47be05, on main at fd2486f. Windows 10 Pro 22H2 (10.0.19045.6466), Windows SDK 10.0.26100.0 DXC, Debug build, debug layer enabled in every run.

cmake --preset dev
cmake --build --preset dev --target offscreen_check
ctest --preset dev --no-tests=error -R '^pixel_aot_'

28 tests ran. 27 passed, 1 failed, 0 skipped.

WARP (Microsoft Basic Render Driver, 10.0.19041.5794):

Test Result
pixel_aot_sdwa_unary_warp Passed, 0.9 s
the other 13 pixel_aot_*_warp tests, including pixel_aot_warp Passed

Hardware (NVIDIA GeForce RTX 3060, 10de:2503, driver 32.0.15.9636):

Test Result
pixel_aot_sdwa_unary_hardware Passed, 2.5 s
12 of the other 13 pixel_aot_*_hardware tests Passed
pixel_aot_blend_scalar_hardware Failed. It also fails on untouched main on this machine and this change does not touch it.

In both new tests the six right references pass and the 26 wrong references each report mismatch.

Other checks, all passed:

  • python -B tests/pixel_aot_check.py binary <llvm-mc> <llvm-objcopy> <LLVM-C.dll> with LLVM 15.0.7. LLVM 15 assembles and prints the ten SDWA instructions as written for -mcpu=gfx1013.
  • python -B tests/pixel_aot_check.py (controls).
  • The 22 pure-Python checks listed in AGENTS.md.

Evidence and follow-up

Coverage, counted with tools/form_coverage.py's matching rule from the union of the record-writing shader tests run on WARP, once from a clean copy of fd2486f and once from this commit. tools/form_coverage.py on the JUnit file of the run above reports the same totals.

gfx1013 gfx1030 (RDNA2)
main at fd2486f 528 of 1968 522 of 1863
this change 538 of 1968 532 of 1863

The forms added are exactly the ten SDWA forms named above, in both lists. None is removed. docs/coverage/dashboard.svg is not regenerated here.

What this does not show:

  • Last-place accuracy of exp, log, rcp, rsq, sqrt, sin and cos on any adapter.
  • A partial destination for an approximate result. The translator places it by the same rule as the exact operations, but its low bits have no single reference, so it is not drawn.
  • Inputs outside these rows: zero, negative arguments to log, rsq and sqrt, infinities, NaN and denormals.
  • The integer-divide flag of v_rcp_iflag_f32, which has no desktop equivalent (as for the e32 form).
  • What the console computes. The draws show what the translated shader computes on a desktop adapter.

Overlap with open pull requests: the diffs of the open shader pull requests (34 read) were searched for the ten form names and for SDWA_ALU on added lines; none adds them. #423 covers the DPP forms of some of the same operations and does not touch the SDWA path. This pull request appends one line to the check_binary fixture list, as every open shader pull request does, so a textual conflict there with whichever lands first is expected and trivial.

AI-assisted (Claude Code). The Windows results were measured locally.

🤖 Generated with Claude Code

Extend SDWA_ALU to v_exp, v_log, v_rcp, v_rcp_iflag, v_rsq, v_sqrt, v_sin,
v_cos, v_rndne and v_frexp_mant f32 with whole-dword sources only. Each
reuses its e32/e64 lowering; byte and word sources stay refused.

Three programs are drawn on WARP and hardware against a CPU reference with
26 wrong references, as their own CTest entries with their own coverage
record, and are added to the LLVM 15 round trip.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@rasptum

rasptum commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main at aeb45da (after #752 repaired the NameError) as 407e355. Rerun on this commit, Windows 10 22H2, WARP 10.0.19041.5794 and RTX 3060: LLVM 15 round trip, tests/form_coverage_check.py, the full pixel_aot_check.py draw run on WARP, and this PR's own draw test on WARP and hardware. All pass.

@rasptum
rasptum force-pushed the shader/sdwa-f32-unary branch from c13268a to 407e355 Compare November 23, 2026 01:13
@R0Y00
R0Y00 merged commit a4ade30 into R0Y00:main Nov 23, 2026
5 checks passed
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.

2 participants