Skip to content

startup: draw the gfx2hlsl binary32 arithmetic operations - #1712

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

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

Conversation

@Asimmimx

Copy link
Copy Markdown
Contributor

Refs #1149.

Change

No check ran these thirteen binary32 operations of the VEC_F table in tools/startup/gfx2hlsl.py: v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32, v_mul_legacy_f32, v_min_f32, v_max_f32, v_fma_f32, v_mad_f32, v_mad_legacy_f32, v_med3_f32, v_min3_f32 and v_max3_f32.

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

tests/startup_gfx2hlsl_float_alu_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 and #1708. In each draw:

  • one operation runs per lane on flat inputs, alternating _e32 and _e64 for the VOP2 forms;
  • the four result words are compared exactly;
  • the recorded llvm-mc bodies are checked by a binary mode;
  • the controls cover accepted modifiers and refused neg_lo and neg_hi.

References (AMD RDNA 2 ISA reference):

  • v_subrev is S1 - S0.
  • The legacy multiply reads 0 when either factor is 0, even against +inf or a NaN. The inputs cover 0 * inf, inf * 0, 0 * NaN and NaN * 0.
  • v_med3_f32 is the middle of three values.

Every input set has an exact result: dyadic values whose sums and products fit in 24 bits. The check therefore does not depend on rounding or on whether a compiler fuses a multiply and an add.

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

  • add as subtract, both subtract orders reversed, multiply as add;
  • the legacy product doubled;
  • min and max exchanged;
  • a * b - c for the multiply-adds;
  • max3 for med3.

Results

Commit 0389c74a 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 52/52 match 26/26 read the mutated words
AMD Radeon RX 6600 (RDNA 2) 32.0.21045.29003 52/52 match 26/26 read the mutated words

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

Observation: v_fma_f32 and v_mad_f32 on an inexact product. The translation writes both as a * b + c. v_fma_f32 rounds once. v_mad_f32 rounds the product and then the sum.

I drew them with a = b = 1 + 2^-12 and c = -(1 + 2^-11). A single rounding gives 2^-24 (0x33800000), and two roundings give 0.

WARP RX 6600
v_fma_f32, FXC and DXC 00000000 33800000
v_mad_f32, FXC and DXC 00000000 33800000

So the same translated text rounds twice on WARP and is fused on the RX 6600. v_fma_f32 differs from the ISA on WARP, and v_mad_f32 differs on the RX 6600. As far as I know, binary32 HLSL has no form that guarantees either behaviour, which is also why compute_store_aot refuses fused multiply-add. I recorded this under "Not covered" in docs/experiments.md and did not change the lowering.

Also found. v_add_f32 v1, v2 (one source short) raises IndexError in Stage.one instead of Unsupported. A VEC_F form with an extra source, such as v_med3_f32 v1, v2, v3, v4, v5, translates with the extra source silently dropped. The VEC_U table already checks the operand count. I'll send that fix as a separate PR.

AI-assisted (Claude Code).

The VEC_F table lowers v_add_f32, v_sub_f32, v_subrev_f32, v_mul_f32,
v_mul_legacy_f32, v_min_f32, v_max_f32, v_fma_f32, v_mad_f32,
v_mad_legacy_f32, v_med3_f32, v_min3_f32 and v_max3_f32, and no check ran
any of them.

tests/startup_gfx2hlsl_float_alu_check.py draws them with the shared
per-lane runner (byte-identical copy of tests/startup_gfx2hlsl_lane_alu.py)
on inputs with exact results, against references written from the RDNA 2
ISA (S1 - S0 for subrev, the legacy multiply's 0 against inf and NaN, the
middle of three). Each operation has one mutated lowering whose first draw
must read its own mutated words.

All 52 draws match on WARP and an RX 6600 through FXC ps_5_1 and DXC
ps_6_0, and the 26 mutated draws read their mutated words. Not covered and
recorded in docs/experiments.md: the translation writes v_fma_f32 and
v_mad_f32 as a * b + c, which WARP rounds twice and the RX 6600 fuses, so
an inexact product reads 0 on one and 2^-24 on the other.

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

Peer review at head_sha: 0389c74aa5df30e68390b29a96cdef1de97bb3d5

Verdict: 0 P1, 0 P2. The translator is not changed; this PR only adds checks.

Scope (full diff read)

  • New: tests/startup_gfx2hlsl_float_alu_check.py and the shared runner tests/startup_gfx2hlsl_lane_alu.py. Changed: CMakeLists.txt (a controls test plus warp/hardware draw tests with SKIP_RETURN_CODE 77), docs/experiments.md, and .github/workflows/windows.yml (two lines: the binary check with llvm-mc-15 in the Python check list, and the file in the Linux check list). Nothing under src/ or tools/.
  • The runner is byte-identical to the copies in #1703, #1706 and #1708 (SHA-256 prefix d8801e44cfe611cd for all four heads), so the add/add on that path merges cleanly whichever lands first. git merge-tree against current main (eaa2024b): no conflicts; it also merges cleanly with #1716, and with both applied the controls, binary mode and my evaluation below still pass.

Semantics against the ISA

  • v_subrev_f32 = S1 - S0; v_mul_legacy_f32 and v_mad_legacy_f32 use a multiply that reads 0 when either factor is 0, even against an infinity or NaN (the fixture sets include 0 * inf, inf * 0, 0 * NaN, NaN * 0); med3 is the middle value; min3/max3 are the nested min/max. These match the VEC_F entries and the mul_legacy/med3 helpers ((a == 0.0 || b == 0.0) ? 0.0 : a * b and max(min(a, b), min(max(a, b), c))).
  • The input sets are dyadic values whose sums and products fit in 24 bits, so the references are exact and do not depend on fusing. The PR says so itself, and it documents what it does not cover: inexact products (v_fma_f32 and v_mad_f32 are both lowered as a * b + c, which gives adapter-dependent results on an inexact product), NaN and signed-zero ordering in min/max/med3, and denormals.

Independent check on Linux

The draw is Windows-only, so I evaluated the translated text myself: for each of the 13 operations I translated the fixture, evaluated the four per-lane expressions in Python (binary32 conversion for F/U, the two helpers as defined above), and compared with the reference words. I used the PR's draw sets plus 4,000 random exact sets per operation (multiples of 1/8, all with exact results), and did the same for each mutated lowering against its mutated reference: 11,026 four-lane chunks, 0 differences. llvm-mc-19 -mcpu=gfx1013 words for all 13 bodies match the recorded words (binary mode).

Findings

  • nit: the check relies on the Windows draw (startup_gfx2hlsl_float_alu_warp/_hardware) to prove the semantics. The Linux-run controls only check shape (four lanes translated, mutation pattern matches four times, mutation changes set 0). For example, flipping v_subrev_f32 to a - b in the translator still passes the Linux controls and binary mode (see below). That is by design, and the PR says the evaluator-style results come from the draw, but it means the numbers in the docs (52 draws on WARP and on an RX 6600, 26 mutated draws) are not something I could reproduce here.

Verified

Limits: Linux x86-64 only. No D3D12, no DXC/FXC, so draw (and the WARP and RX 6600 results in the docs) was not run. llvm-mc-15, which CI uses, is not installed here; I used llvm-mc-19.

  • python3 -B tests/startup_gfx2hlsl_float_alu_check.py -> exit 0 (41 checks ok). ... binary /usr/bin/llvm-mc-19 -> "fixture words match llvm-mc", exit 0. python3 -B tests/docs_links_check.py -> OK.
  • Mutation 1 (translator): v_subrev_f32 {1} - {0} -> {0} - {1}: Linux controls exit 0 and binary exits 0 (escape; only the draw, or my evaluator, sees it: 0xbf400000 vs 0x3f400000).
  • Mutation 2 (translator): v_mad_legacy_f32 mul_legacy({0}, {1}) + {2} -> {0} * {1} + {2}: Linux controls exit 1 (AssertionError: ('v_mad_legacy_f32', 0), the mutation pattern no longer matches).
  • Mutation 3 (translator): v_min3_f32 reshaped to min(min({0}, {1}), {2}): Linux controls exit 1 (same pattern assertion).
  • Mutation 4 (test): the recorded v_med3_f32 body word changed: binary exits 1 (assembled ..., recorded ...).
  • All reverted with git checkout -- .; git status --short clean.

Also found (not for this PR)

  • v_fma_f32 is lowered to a * b + c, which is not a single-rounding fused operation on every adapter (the docs show 0x33800000 on RX 6600 and 0 on WARP). That is a translator question for a later change, not a defect of this check.

AI-assisted; commands listed were executed locally.

@thepatrickworld

thepatrickworld commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Green on its base, red on main

I merged this branch into main at df40e4bc on a physical RX 9070 XT (RDNA 4, driver 32.0.32029.2010) to run the draw mode. All three registered tests fail before any draw executes. Three runs, identical:

$ ctest --preset dev -R "^startup_gfx2hlsl_float_alu" -V      # excerpt
457:   File "tests/startup_gfx2hlsl_lane_alu.py", line 54, in translate
457:     assert count == 4 and mutated != text, (op, count)
457: AssertionError: ('v_mad_f32', 0)
0% tests passed, 3 tests failed out of 3

That is one local merge on my machine, not a CI run. df40e4bc was the head of main when I merged; the only conflict was docs/experiments.md, resolved by keeping both experiment sections; the tree committed clean and git diff --check passes.

The mutation for v_mad_f32 finds nothing left to replace, because main changed the lowering under you. df40e4bc contains f7b7297d ("shader: preserve non-fused gfx1013 multiply-add rounding"). Excerpt of its hunk on tools/startup/gfx2hlsl.py:

@@ -1048,10 +1048,11 @@ VEC_F = {
     'v_add_f32': (2, '{0} + {1}'), 'v_sub_f32': (2, '{0} - {1}'), 'v_subrev_f32': (2, '{1} - {0}'),
     'v_mul_f32': (2, '{0} * {1}'), 'v_mul_legacy_f32': (2, 'mul_legacy({0}, {1})'),
     'v_min_f32': (2, 'min({0}, {1})'), 'v_max_f32': (2, 'max({0}, {1})'),
-    'v_fmac_f32': (2, '{0} * {1} + {2}'), 'v_mac_f32': (2, '{0} * {1} + {2}'),
-    'v_fma_f32': (3, '{0} * {1} + {2}'), 'v_mad_f32': (3, '{0} * {1} + {2}'), 'v_mad_legacy_f32': (3, 'mul_legacy({0}, {1}) + {2}'),
+    'v_fmac_f32': (2, '{0} * {1} + {2}'), 'v_mac_f32': (2, 'mad32({0}, {1}, {2}, false)'),
+    'v_fma_f32': (3, '{0} * {1} + {2}'), 'v_mad_f32': (3, 'mad32({0}, {1}, {2}, false)'),
+    'v_mad_legacy_f32': (3, 'mad32({0}, {1}, {2}, true)'),
     'v_fmamk_f32': (3, '{0} * {1} + {2}'), 'v_fmaak_f32': (3, '{0} * {1} + {2}'),
-    'v_madmk_f32': (3, '{0} * {1} + {2}'), 'v_madak_f32': (3, '{0} * {1} + {2}'),
+    'v_madmk_f32': (3, 'mad32({0}, {1}, {2}, false)'), 'v_madak_f32': (3, 'mad32({0}, {1}, {2}, false)'),
     'v_rcp_f32': (1, '1.0 / {0}'), 'v_rcp_iflag_f32': (1, '1.0 / {0}'), 'v_rsq_f32': (1, 'rsqrt({0})'), 'v_sqrt_f32': (1, 'sqrt({0})'),
     'v_exp_f32': (1, 'exp2({0})'), 'v_log_f32': (1, 'log2({0})'), 'v_floor_f32': (1, 'floor({0})'), 'v_ceil_f32': (1, 'ceil({0})'),
     'v_trunc_f32': (1, 'trunc({0})'), 'v_fract_f32': (1, 'frac({0})'), 'v_rndne_f32': (1, 'round({0})'),

v_fma_f32 is the same string on both sides of the hunk; v_mad_f32 and v_mad_legacy_f32 are not. VEC_F is a single table with no gfx1013 or per-adapter branch, and translating the PR's v_mad_f32 body against df40e4bc prints v12 = U(mad32(F(v0), F(v4), F(v8), false)); — which is the output the mutation is looking at.

I checked the other side on the same code, at the base this PR was written against (bfe9158d, before f7b7297d), where git show bfe9158d:tools/startup/gfx2hlsl.py | grep -c 'mad32(' returns 0. There, both opcodes emitted the same text:

bfe9158d   v_fma_f32  ->  v12 = U(F(v0) * F(v4) + F(v8));
bfe9158d   v_mad_f32  ->  v12 = U(F(v0) * F(v4) + F(v8));      <- identical
df40e4bc   v_mad_f32  ->  v12 = U(mad32(F(v0), F(v4), F(v8), false));

For the v_mad_f32 mutation on this one shader, that pattern matches 4 times at bfe9158d and 0 times at df40e4bc, per run. So the sentence in the PR body — that the translator writes both of them as a * b + c — is accurate at its base and stale at today's main. That is one stale claim in the last paragraph, not a defect in the test: the test is doing its job by catching the table change.

Why the checks were green

The PR's merge ref is 16979ce0, "Merge 0389c74 into bfe9158d" — a base that predates f7b7297d, so the tree built there contains no mad32(. The green checks are attached to the head commit 0389c74a; no check run is attached to the merge ref. Whatever the green run built, it was not the tree that merging this into today's main creates — I recreated that tree twice, and both times the same assertion fired.

What I would change

  1. Rebase on main, so the tests see the current table.
  2. Re-derive the expected match count against the new table rather than swapping the pattern — and decide what this test is: a check that the translator still follows VEC_F, or a check that a draw reads the words the operation's semantics call for. Those are different tests, and the second is the one I would write: it should catch a table change like f7b7297d on purpose instead of by accident.
  3. Rewrite the "Not covered: inexact products" paragraph. It becomes the most valuable part of the PR: with mad32(..., false) and mad32(..., true) in place, fused versus twice-rounded is now a per-operation question the draw can be asked, instead of a shared a * b + c that could not distinguish them.

I have made no hardware claim here: the fixture never reached a draw, so this says nothing about RDNA 4 arithmetic. Once the controls pass I can run the 52 draws and the 26 mutants on the same machine that produced this report.

AI-assisted.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Hardware run of head 0389c74. 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_alu_controls Passed
startup_gfx2hlsl_float_alu_warp Passed
startup_gfx2hlsl_float_alu_hardware on the Quadro M1000M Passed
startup_gfx2hlsl_float_alu_hardware on the HD 530 Passed

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 0389c74a as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (cf97bee1):

  • CMakeLists.txt
  • docs/experiments.md
  • tests/startup_gfx2hlsl_lane_alu.py

Commands run locally:

git fetch origin main pull/1712/head
git merge-tree --write-tree --name-only origin/main FETCH_HEAD
gh pr view 1712 --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