Skip to content

startup: return the minimum of the three on v_med3_f32 with a NaN source - #2095

Merged
MasterDomino merged 8 commits into
R0Y00:mainfrom
thepatrickworld:panam/med3-nan
Nov 25, 2026
Merged

MasterDomino merged 8 commits into
R0Y00:mainfrom
thepatrickworld:panam/med3-nan

Conversation

@thepatrickworld

@thepatrickworld thepatrickworld commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

The gfx2hlsl preamble lowered v_med3_f32 with a branchless helper:

float med3(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }

The RDNA 2 guide gives a different rule for a NaN source (V_MED3_F32, opcode 343):

if (isNan(S0.f) || isNan(S1.f) || isNan(S2.f))
   D.f = V_MIN3_F32(S0.f, S1.f, S2.f);

tools/pixel_aot.py already models that in med3_32, and tests the encoding rather than calling isnan()
because desktop compilers fold float NaN tests. This patch gives the gfx2hlsl preamble the same body and points
v_med3_f32 at it. v_med3_u32 and v_med3_i32 are integers, have no NaN source and are untouched.

Measured. One pixel, four lanes, med3(a, 2.0, 1.0) with a read from the entry constants, RX 9070 XT
(driver 32.0.32029.2010) and WARP (10.0.26100.9818), through fxc (ps_5_1) and dxc (ps_6_0):

a this patch before ISA
NaN 1.0 2.0 1.0
3.0 2.0 2.0 2.0
1.5 1.5 1.5 1.5
0.5 1.0 1.0 1.0

The only disagreement is the NaN lane, and it is the one the ISA decides.

What this does not establish. The card never executes v_med3_f32 here: the lowering is min and max, and no
compiler recognises the median pattern well enough to issue the instruction, so the hardware's own NaN branch was
not interrogated. What is measured is the value the translation produces, which is exactly what this patch
changes. An experiment that could issue v_med3 directly should confirm the V_MIN3 branch; this one cannot.

The control that makes the draw worth something. Restoring the branchless helper in the same pair must
mismatch on the NaN lane and match the other three. It does, on both adapters and both compilers —
40000000,40000000,3fc00000,3f800000 against 3f800000,40000000,3fc00000,3f800000.

Three med3 tests and the whole ^startup_gfx2hlsl suite (48 tests) are green on the machine above.

Two translators, one body. med3_32 here is the same body as med3_32 in tools/pixel_aot.py, down to
the encoding test and the min(min(a, b), c) NaN path. The two translators now agree on v_med3_f32, and that
agreement is the thing a later change has to keep: neither half should be "simplified" back to a branchless
max(min(a, b), min(max(a, b), c)), and the NaN test should stay on the bit pattern, because a float isnan()
can be folded by the compiler.

AI-assisted; runs executed on the machine above.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Rebuilt offscreen_check from this branch and ran the draw half on both adapters — your negative control holds

The draw path did not run against my pre-existing binary (it has no --startup-entry-words, which this PR adds to offscreen_check), so I rebuilt it
from the branch:

cmake --preset dev
cmake --build --preset dev --target offscreen_check

then:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_f32_med3_check.py draw \
    "<Windows Kits>/10/bin/10.0.26100.0/x64/dxc.exe" out/build/dev/offscreen_check.exe warp
driver=10.0.26100.9818
startup_entry_words words=40000000,40000000,3fc00000,3f800000
debug_errors=0 debug_warnings=0
dxc branchless lowering on warp mismatched on the NaN lane (40000000,40000000,3fc00000,3f800000)
and matched the other three: ok                                          exit 0

and hardware:

driver=32.0.15.8142
startup_entry_words words=40000000,40000000,3fc00000,3f800000
dxc branchless lowering on hardware mismatched on the NaN lane and matched the other three: ok   exit 0

Windows 11 build 26300.9974, DXC 10.0.26100.0, debug layer enabled, RTX 5080 Laptop.

That is the strongest form of evidence this fixture can produce for the claim: the old branchless helper disagrees with the new one on exactly the one
lane that carries a NaN
, and agrees on the three that do not — on the card, not only in WARP. A fixture that only compared final colours would have
had to guess which lane was wrong.

The controls, run too

python3 -B tests/startup_gfx2hlsl_f32_med3_check.py
an isnan() test instead of the encoding test is rejected: ok
the table pointing back at a helper without the branch is rejected: ok      exit 0

and one mutation of mine, on the production side rather than the fixture's own controls:

mutation in tools/startup/gfx2hlsl.py controls
med3_32 reduced to the branchless max(min(a,b), min(max(a,b), c)) exit 1

so the helper's NaN branch is pinned by a check that does not depend on a device.

The reasoning in the comment is the part I would protect in review

uint top = max(max(asuint(a) & 0x7fffffffu, ...));   // NaN test on the encoding
return top > 0x7f800000u ? min(min(a, b), c) : max(min(a, b), min(max(a, b), c));

Two things here are easy to "simplify" later and both are wrong:

  1. the NaN test is on the bit pattern, because a float isnan() may be folded by the compiler — your fixture already rejects that variant;
  2. the NaN path is min of the three, because D3D min() returns the other operand, so min(min(a,b),c) over a NaN source is the smallest of the
    non-NaN ones — which is what the ISA's "minimum of the three" means in practice, and is not what fmin semantics would give on a strict IEEE
    target.

Worth saying in the PR description as well: this is the same body as med3_32 in tools/pixel_aot.py, so the two translators now agree, and that
agreement is the thing a future change has to keep.

What is not covered

v_min3_f32 / v_max3_f32 still use plain min/max nesting, and with a NaN operand they inherit whatever D3D does rather than the ISA's rule. If
that is deliberate (they may be v_min3_f32 without the NaN clause), fine; if not, it is the next slice — but it is a separate behaviour and I would
not fold it into this PR.

AI-assisted; both adapters and the rebuild were done locally.

@thepatrickworld

thepatrickworld commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you for rebuilding the binary and running it on the RTX 5080. The draw needs --startup-entry-words, so it could not have run against a build older than this branch, and having the control hold on a device this branch never touched is the part that matters most.

I took the description note: the body now says the helper is the same body as med3_32 in tools/pixel_aot.py, both translators agree, and that agreement is what a later change has to keep.

On v_min3_f32 / v_max3_f32: I checked the guide before answering. Unlike V_MED3_F32, they carry no NaN clause. They are defined as nested operations — D.f = V_MIN_F32(V_MIN_F32(S0.f, S1.f), S2.f), and the maximum equivalent — which is the shape the lowering already has. The only difference is the association: the ISA nests to the left, the emitted HLSL to the right. Since D3D min/max return the other operand when one side is NaN, both orders return the minimum (or maximum) of the non-NaN sources for one, two or three NaN inputs, so I do not see a divergence to fix there rather than a separate behaviour worth its own slice.

The one case that reading does not cover is a signed-zero pair, where the order could matter and the guide says nothing. That one needs a draw, not an argument, and it is not in this PR.

AI-assisted.

@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 6883eeb3e78c8b4f927a4efd1417c80829d8931d (comment only, no verdict).

Summary. v_med3_f32 now lowers to med3_32, which tests NaN on the encoding and returns min(min(a,b),c) for a NaN source. The body matches med3_32 in tools/pixel_aot.py. The integer forms are untouched. I found no P1 or P2 issues.

Checked against the ISA. The NaN test is max(|a|,|b|,|c|) bits > 0x7f800000, so it catches quiet and signaling NaNs and ignores infinities. The NaN branch follows the RDNA 2 V_MED3_F32 rule (NaN source gives V_MIN3_F32). D3D min returns the non-NaN operand, so min(min(a,b),c) gives the smallest non-NaN value, which matches the 1.0 in the PR table. No other caller of the old med3( helper remains (rg over tools/ and tests/).

Commands run locally on the PR head (Linux worktree):

  • python3 tests/startup_gfx2hlsl_f32_med3_check.py: self-test passed, including the four negative controls (branchless helper, >= threshold, isnan(), table repointed).
  • python3 tests/docs_links_check.py: passed, 18 Markdown files and 394 links.
  • python3 tests/bidi_controls_check.py: passed, 1421 files.
  • git diff --check origin/main...HEAD: clean.
  • Mutation: in tools/startup/gfx2hlsl.py, I changed the NaN branch from min(min(a, b), c) to max(max(a, b), c). The check failed with AssertionError: med3_32 missing or changed, so the mutation is killed. I then restored the file with git checkout -- ..

CI at this head. Linux Python checks, gfx1013 fixtures and the Windows build and test jobs pass. The /O2 job and the coverage-chart job were skipped. The PR reports mergeable: UNKNOWN, so please re-check it once main settles.

Notes (nit, no action required):

  • nit: the Linux mutation above is killed by the text comparison (MED3_32 in hlsl). The value-level kill (a NaN lane giving 2.0 instead of 1.0) only happens in the startup_gfx2hlsl_f32_med3_warp and _hardware draw tests. I did not run those on a Windows 2025 host here. I am relying on the passing Windows CI job and did not re-verify that the WARP test ran, not skipped. Exit code 77 means skip. Please confirm in the job log that the WARP variant was not skipped.
  • nit: the PR states that the hardware v_med3 NaN branch was not exercised. That is honest and matches what the lowering does.

AI-assisted; commands listed were executed locally.

@thepatrickworld

Copy link
Copy Markdown
Contributor Author

Thank you for the ISA reading of the NaN test and for killing the mutation.

On the nit you asked about — I cannot answer it from the CI log. The Actions log endpoint answers 401 to a token that can comment on this repository but does not administer it, so I cannot confirm from the job log whether the WARP variant ran. What I can do is run it, and I did, on head 6883eeb3:

3/6 Test  #448: startup_gfx2hlsl_f32_med3_warp ........   Passed    1.13 sec
4/6 Test  #449: startup_gfx2hlsl_f32_med3_hardware ....   Passed    1.44 sec
100% tests passed out of 6

Windows 11 26200.9974, AMD Radeon RX 9070 XT, driver 32.0.32029.2010, ctest --preset dev -R med3, exit 0. Neither is a skip: a skip in this harness is exit 77 and ctest reports it as ***Skipped, which is what the /O2 and coverage jobs show in the CI run.

That run also covers the value-level kill you separated from the text one. The two draw tests are the ones that carry the branchless mutant, and on both adapters it comes back with 40000000 on the NaN lane where the corrected lowering returns 3f800000 — the mismatch the test asserts. So the value-level check is exercised; it just is not exercised by the Linux mutation, exactly as you said.

Your other nit is right and stays as written: the hardware v_med3_f32 instruction is never executed by these draws — what is measured is the value the translation produces, not the instruction.

AI-assisted; runs executed on the machine above.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

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

GitHub reports head 6883eeb3 as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (cf97bee1):

  • CMakeLists.txt
  • tests/desktop_gpu.h
  • tests/offscreen_check.cpp
  • tools/startup/gfx2hlsl.py

Commands run locally:

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

root and others added 2 commits November 25, 2026 16:55
…n conflicts

The preamble keeps main's mul_hi_u32 next to this branch's med3_32, the draw
operation table keeps main's pixel-words validator and raster-reference row with
the new --startup-entry-words entry, and CMakeLists keeps both test blocks.
main's readfirstlane expectation follows the helper rename to med3_32.
@thepatrickworld

Copy link
Copy Markdown
Contributor Author

Merged origin/main (7092683a) into the branch, and added the cache-revision bump this lowering needs. v_med3_f32 now emits med3_32(...) with a different body, so a translation cached before the fix would be reused as it is — NaN lane included. The revision goes from 14 to 16 (15 is taken by #2010, also in flight) in tools/startup/gfx2hlsl.py, src/startup_stubs/d3d12_draw.inl, tests/startup_indirect_draw_indexed.cpp, tests/startup_gfx2hlsl_buffer_store_check.py and docs/startup-runtime.md. The compute revision stays at 14cs.

On the RX 9070 XT (1002:7550, driver 32.0.32029.2010), head fa9b8cde, backend hardware, both compilers:

  • the lowering reads startup_entry_words words=3f800000,40000000,3fc00000,3f800000, then fxc hardware: 3f800000,40000000,3fc00000,3f800000 and dxc hardware: 3f800000,40000000,3fc00000,3f800000, matching the expected words
  • the branchless variant reads 40000000,40000000,3fc00000,3f800000 and prints branchless lowering on hardware mismatched on the NaN lane (40000000,40000000,3fc00000,3f800000) and matched the other three: ok, once for fxc and once for dxc
  • the translator checks reject the four wrong helpers: the branchless one restored, the threshold changed to >=, an isnan() test instead of the encoding test, and the table pointing back at a helper without the branch

Tests after the bump: the f32 med3 draw and the integer med3 fixture exit 0. Buffer store, compute request offline, readfirstlane recompute and the translator check exit 0 as well, run in the merge trees before the bump.

AI-assisted.

@thepatrickworld

Copy link
Copy Markdown
Contributor Author

CI caught a fixture I had missed: tests/startup_gfx2hlsl_readfirstlane_uniform_check.py still expected the old helper name med3( in two cases, so the platform-independent check failed on fa9b8cde. Fixed in c93f0285: both expectations read med3_32(...) and that fixture reports 0 failed.

On c93f0285 the three checks that run are green — the Python checks, the Windows build and test, and the gfx1013 assembly; the /O2 and coverage-chart jobs are skipped, as they are on #2010.

AI-assisted.

@MasterDomino MasterDomino 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 c93f0285, merged locally with current main e06aca8b. The merge is clean.

Result: one P2 (the compute cache revision) and one optional coverage note. The lowering itself checks out.

P2: compute translations change too, but the compute revision stays at 14cs

ComputePipeline.common() inherits Pipeline.common(), so the new med3_32 body is in every startup compute program. Compute programs also accept v_med3_f32 through the same VEC_F table. compute_key hashes x2w-startup-shader-14cs plus the program words, root blob and state, not the emitted HLSL. A compute translation cached before this change is therefore found under the same key and reused with the branchless helper, NaN lane included. That is the case the graphics bump to 16 exists for.

I translated the same compute words (v_med3_f32 v1, v0, v0, v0, then s_endpgm, with case I's RSRC2 and root) through both trees:

Tree Helper and call emitted Key literal
main e06aca8b float med3(...) { return max(min(a, b), min(max(a, b), c)); }, v1 = U(med3(F(v0), F(v0), F(v0))) x2w-startup-shader-14cs
This PR merged with main float med3_32(...) with the encoding test, v1 = U(med3_32(F(v0), F(v0), F(v0))) x2w-startup-shader-14cs

There is a precedent for moving both: 565f9d59 (#2059) moved the graphics revision 13 to 14 and the compute revision 13cs to 14cs together. The fix would be 16cs, or whatever the graphics number ends up as, in three places: compute_key in tools/startup/gfx2hlsl.py, compute_revision in src/startup_stubs/d3d12_draw.inl, and the literal in tests/startup_compute_request_offline_check.py.

What I checked

  • The helper: the NaN test is an integer max over asuint(x) & 0x7fffffff against 0x7f800000. That catches quiet, signalling and negative NaNs and lets infinities through. Neither FXC nor DXC can fold it the way they fold float unordered compares (#1936). The NaN branch matches V_MED3_F32, and the body is identical to med3_32 in tools/pixel_aot.py.
  • No old med3( caller remains in the graphics or compute output.

Commands executed (Windows 11 build 26300)

offscreen_check and the tests were built from this PR merged with main e06aca8b, using the dev preset.

ctest --preset dev --no-tests=error -R 'f32_med3|int_med3' -V
python -B tests/startup_gfx2hlsl_{f32_med3,readfirstlane_uniform,readfirstlane_recompute,buffer_store,operand_count,handler_arity,f32_nan}_check.py
Check Result
f32_med3 and int_med3 controls, including the four mutations pass
Draw on WARP 10.0.26100.9818, debug layer on, FXC ps_5_1 and DXC ps_6_0 pass, 3f800000,40000000,3fc00000,3f800000
Draw on AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010), debug layer on, FXC and DXC pass, same words, debug_errors=0
Branchless control on both adapters and both compilers diverges on the NaN lane only (40000000,...)
Neighbouring checks: readfirstlane ×2, buffer store, operand count, handler arity, f32 NaN pass

No skips.

Optional

The draw puts the NaN in S0 only. A lane with the NaN in S1 or S2 would pin that the test covers every source. So would one lane with two NaN sources, where min(min(a, b), c) has to return the remaining number. The helper handles all of these by inspection, so this is coverage, not a defect.

Title and body are neutral. The change is original in-repo work.

AI-assisted; commands listed were executed locally.

@MasterDomino

Copy link
Copy Markdown
Collaborator

@thepatrickworld, one item remains before this can merge, and it isn't changed at the new head 059624dd: the compute cache revision (P2 in my review above).

  • Finding: ComputePipeline inherits the same preamble, so startup compute programs get the new med3_32 body. Compute programs accept v_med3_f32, but compute_key still hashes x2w-startup-shader-14cs. A compute translation cached before this change would be reused with the branchless helper.
  • Evidence: I translated the same compute words (v_med3_f32 v1, v0, v0, v0, then s_endpgm) on main and on this branch. The emitted helper and call change (med3(...) becomes med3_32(...)), and the key literal stays x2w-startup-shader-14cs in both.
  • What would resolve it: move the compute revision with the graphics one, to 16cs, as shader: lower buffer_store_dwordx3 in gfx2hlsl (Part of #1723) #2059 did when 13 and 13cs became 14 and 14cs. It's a three-line change:
    • compute_key in tools/startup/gfx2hlsl.py;
    • compute_revision in src/startup_stubs/d3d12_draw.inl;
    • the literal in tests/startup_compute_request_offline_check.py.

Everything else checked out, including the WARP and RX 9070 XT draws with both compilers, so I can merge once that's in and CI is green.

AI-assisted.

@MasterDomino

Copy link
Copy Markdown
Collaborator

Heads-up on the startup cache revision: #2467 merged as ae5d482a and moved main to x2w-startup-shader-15 / 15cs. This PR's 16 is still free, but #2010 also needs a number above 15, so whichever lands later needs 17. The compute-revision request from my earlier comment still stands: move 14cs (now 15cs on main) to the same number as the graphics string.

AI-assisted.

panam added 2 commits November 25, 2026 22:40
main moved to 16 through R0Y00#2010, so this branch takes 17 and moves the
compute key with it (17cs), as MasterDomino asked in R0Y00#2095.
@thepatrickworld

Copy link
Copy Markdown
Contributor Author

Rebased onto main 8f045aee; the revision moved to x2w-startup-shader-17 with the compute key (17cs) in the same patch, as you asked.

Two things the rebase surfaced, both fixed here:

  • main had reintroduced med3 in the preamble through startup: cap v_fract_f32 at 0x3f7fffff like pixel_aot already does #2010. Nothing calls it after this branch's switch to med3_32, and tests/startup_gfx2hlsl_f32_med3_check.py rejects a leftover med3(, so it is dropped.
  • The revision literal is spelled in more places than the three you listed: d3d12_draw.inl, tools/startup/gfx2hlsl.py (both keys), tests/startup_compute_request_offline_check.py, tests/startup_gfx2hlsl_buffer_store_check.py, tests/startup_gfx2hlsl_fract_check.py, tests/startup_indirect_draw_indexed.cpp, tests/startup_resource_retire_title.cpp, and docs/startup-runtime.md. All eleven moved, so nothing keeps asserting 16.

The framed compute key at 17cs is 0x8421b8080fd0a5e6, updated in the offline check; the fract control now compares 16 against 17 instead of 15 against 16.

CI green on 135c11bc: platform-independent Python checks, Windows build and tests, gfx1013 fixtures. The two skipped checks are path-filtered, not failures.

AI-assisted.

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

Approving 135c11bc. The compute-revision request from my earlier review is resolved: the graphics and compute strings move together to x2w-startup-shader-17 / 17cs, above main's 16 from #2010. They're in gfx2hlsl.py (pipeline_key, compute_key), in d3d12_draw.inl (revision, compute_revision), in the offline compute-request check with its recomputed key, and in docs/startup-runtime.md.

Since 059624dd, the head adds a merge of main (8f045aee) and the bump commit. The bump also drops the old med3 helper line that the merge brought back. Nothing on the branch still calls med3(; v_med3_f32 emits med3_32.

Run at 135c11bc (Windows 11 build 26300; AMD Radeon RX 9070 XT, driver 32.0.32029.2010; WARP 10.0.26100.9818):

ctest --preset dev --no-tests=error -j4 -R 'med3|startup_compute_request_offline|startup_gfx2hlsl_fract|startup_gfx2hlsl_buffer_store|startup_gfx2hlsl_readfirstlane|startup_indirect_draw_indexed|startup_resource_retire|startup_gfx2hlsl_compute|docs_links'

58/58 passed, 0 skipped. That includes startup_gfx2hlsl_f32_med3_{controls,warp,hardware}, startup_gfx2hlsl_int_med3_controls and compute_store_aot_alu_med3_{warp,hardware}. CI at this head is green.

No title start-up progress is claimed. The revision bump keeps pre-fix cached translations from being reused.

AI-assisted; commands listed were executed locally.

@MasterDomino
MasterDomino merged commit bd53c9b into R0Y00:main Nov 25, 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.

4 participants