startup: return the minimum of the three on v_med3_f32 with a NaN source #2088
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2095"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T00:13:49Z.
The gfx2hlsl preamble lowered
v_med3_f32with a branchless helper:The RDNA 2 guide gives a different rule for a NaN source (V_MED3_F32, opcode 343):
tools/pixel_aot.pyalready models that inmed3_32, and tests the encoding rather than callingisnan()because desktop compilers fold float NaN tests. This patch gives the gfx2hlsl preamble the same body and points
v_med3_f32at it.v_med3_u32andv_med3_i32are integers, have no NaN source and are untouched.Measured. One pixel, four lanes,
med3(a, 2.0, 1.0)witharead 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):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_f32here: the lowering is min and max, and nocompiler 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_med3directly should confirm theV_MIN3branch; 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,3f800000against3f800000,40000000,3fc00000,3f800000.Three med3 tests and the whole
^startup_gfx2hlslsuite (48 tests) are green on the machine above.Two translators, one body.
med3_32here is the same body asmed3_32intools/pixel_aot.py, down tothe encoding test and the
min(min(a, b), c)NaN path. The two translators now agree onv_med3_f32, and thatagreement 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 floatisnan()can be folded by the compiler.
AI-assisted; runs executed on the machine above.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:22:03Z.
Rebuilt
offscreen_checkfrom this branch and ran the draw half on both adapters — your negative control holdsThe draw path did not run against my pre-existing binary (it has no
--startup-entry-words, which this PR adds tooffscreen_check), so I rebuilt itfrom the branch:
then:
and
hardware: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
and one mutation of mine, on the production side rather than the fixture's own controls:
tools/startup/gfx2hlsl.pymed3_32reduced to the branchlessmax(min(a,b), min(max(a,b), c))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
Two things here are easy to "simplify" later and both are wrong:
isnan()may be folded by the compiler — your fixture already rejects that variant;minof the three, because D3Dmin()returns the other operand, somin(min(a,b),c)over a NaN source is the smallest of thenon-NaN ones — which is what the ISA's "minimum of the three" means in practice, and is not what
fminsemantics would give on a strict IEEEtarget.
Worth saying in the PR description as well: this is the same body as
med3_32intools/pixel_aot.py, so the two translators now agree, and thatagreement is the thing a future change has to keep.
What is not covered
v_min3_f32/v_max3_f32still use plainmin/maxnesting, and with a NaN operand they inherit whatever D3D does rather than the ISA's rule. Ifthat is deliberate (they may be
v_min3_f32without the NaN clause), fine; if not, it is the next slice — but it is a separate behaviour and I wouldnot fold it into this PR.
AI-assisted; both adapters and the rebuild were done locally.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T00:40:55Z.
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_32intools/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. UnlikeV_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 D3Dmin/maxreturn 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.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:56:01Z.
Review: COMMENTED
Review of head
6883eeb3e78c8b4f927a4efd1417c80829d8931d(comment only, no verdict).Summary.
v_med3_f32now lowers tomed3_32, which tests NaN on the encoding and returnsmin(min(a,b),c)for a NaN source. The body matchesmed3_32intools/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 2V_MED3_F32rule (NaN source givesV_MIN3_F32). D3Dminreturns the non-NaN operand, somin(min(a,b),c)gives the smallest non-NaN value, which matches the 1.0 in the PR table. No other caller of the oldmed3(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.tools/startup/gfx2hlsl.py, I changed the NaN branch frommin(min(a, b), c)tomax(max(a, b), c). The check failed withAssertionError: med3_32 missing or changed, so the mutation is killed. I then restored the file withgit checkout -- ..CI at this head. Linux Python checks, gfx1013 fixtures and the Windows build and test jobs pass. The
/O2job and the coverage-chart job were skipped. The PR reportsmergeable: UNKNOWN, so please re-check it once main settles.Notes (nit, no action required):
MED3_32 in hlsl). The value-level kill (a NaN lane giving 2.0 instead of 1.0) only happens in thestartup_gfx2hlsl_f32_med3_warpand_hardwaredraw 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.v_med3NaN branch was not exercised. That is honest and matches what the lowering does.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T02:05:19Z.
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: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 andctestreports it as***Skipped, which is what the/O2and 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
40000000on the NaN lane where the corrected lowering returns3f800000— 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_f32instruction 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.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:53:20Z.
@thepatrickworld could you rebase this branch onto current
mainand force-push?GitHub reports head
6883eeb3as conflicting withmain, so it cannot be merged as is.Files that conflict with current
main(cf97bee1):CMakeLists.txttests/desktop_gpu.htests/offscreen_check.cpptools/startup/gfx2hlsl.pyCommands run locally:
Once it is rebased and CI is green it will be re-checked right away.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T16:57:10Z.
Review: COMMENTED
Review of head
c93f0285, merged locally with currentmaine06aca8b. 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
14csComputePipeline.common()inheritsPipeline.common(), so the newmed3_32body is in every startup compute program. Compute programs also acceptv_med3_f32through the sameVEC_Ftable.compute_keyhashesx2w-startup-shader-14csplus 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 to16exists for.I translated the same compute words (
v_med3_f32 v1, v0, v0, v0, thens_endpgm, with caseI's RSRC2 and root) through both trees:maine06aca8bfloat med3(...) { return max(min(a, b), min(max(a, b), c)); },v1 = U(med3(F(v0), F(v0), F(v0)))x2w-startup-shader-14csmainfloat med3_32(...)with the encoding test,v1 = U(med3_32(F(v0), F(v0), F(v0)))x2w-startup-shader-14csThere is a precedent for moving both:
565f9d59(#2059) moved the graphics revision13to14and the compute revision13csto14cstogether. The fix would be16cs, or whatever the graphics number ends up as, in three places:compute_keyintools/startup/gfx2hlsl.py,compute_revisioninsrc/startup_stubs/d3d12_draw.inl, and the literal intests/startup_compute_request_offline_check.py.What I checked
maxoverasuint(x) & 0x7fffffffagainst0x7f800000. 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 matchesV_MED3_F32, and the body is identical tomed3_32intools/pixel_aot.py.med3(caller remains in the graphics or compute output.Commands executed (Windows 11 build 26300)
offscreen_checkand the tests were built from this PR merged withmaine06aca8b, using thedevpreset.f32_med3andint_med3controls, including the four mutationsps_5_1and DXCps_6_03f800000,40000000,3fc00000,3f800000debug_errors=040000000,...)No skips.
Optional
The draw puts the NaN in
S0only. A lane with the NaN inS1orS2would pin that the test covers every source. So would one lane with two NaN sources, wheremin(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.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T16:08:43Z.
Merged
origin/main(7092683a) into the branch, and added the cache-revision bump this lowering needs.v_med3_f32now emitsmed3_32(...)with a different body, so a translation cached before the fix would be reused as it is — NaN lane included. The revision goes from14to16(15 is taken by #2010, also in flight) intools/startup/gfx2hlsl.py,src/startup_stubs/d3d12_draw.inl,tests/startup_indirect_draw_indexed.cpp,tests/startup_gfx2hlsl_buffer_store_check.pyanddocs/startup-runtime.md. The compute revision stays at14cs.On the RX 9070 XT (
1002:7550, driver32.0.32029.2010), headfa9b8cde, backendhardware, both compilers:startup_entry_words words=3f800000,40000000,3fc00000,3f800000, thenfxc hardware: 3f800000,40000000,3fc00000,3f800000anddxc hardware: 3f800000,40000000,3fc00000,3f800000, matching the expected words40000000,40000000,3fc00000,3f800000and printsbranchless 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>=, anisnan()test instead of the encoding test, and the table pointing back at a helper without the branchTests 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 exit0as well, run in the merge trees before the bump.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T16:24:58Z.
CI caught a fixture I had missed:
tests/startup_gfx2hlsl_readfirstlane_uniform_check.pystill expected the old helper namemed3(in two cases, so the platform-independent check failed onfa9b8cde. Fixed inc93f0285: both expectations readmed3_32(...)and that fixture reports0 failed.On
c93f0285the three checks that run are green — the Python checks, the Windows build and test, and the gfx1013 assembly; the/O2and coverage-chart jobs are skipped, as they are on #2010.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T18:24:24Z.
@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).ComputePipelineinherits the same preamble, so startup compute programs get the newmed3_32body. Compute programs acceptv_med3_f32, butcompute_keystill hashesx2w-startup-shader-14cs. A compute translation cached before this change would be reused with the branchless helper.v_med3_f32 v1, v0, v0, v0, thens_endpgm) onmainand on this branch. The emitted helper and call change (med3(...)becomesmed3_32(...)), and the key literal staysx2w-startup-shader-14csin both.16cs, as #2059 did when13and13csbecame14and14cs. It's a three-line change:compute_keyintools/startup/gfx2hlsl.py;compute_revisioninsrc/startup_stubs/d3d12_draw.inl;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.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T20:19:19Z.
Heads-up on the startup cache revision: #2467 merged as
ae5d482aand movedmaintox2w-startup-shader-15/15cs. This PR's16is still free, but #2010 also needs a number above15, so whichever lands later needs17. The compute-revision request from my earlier comment still stands: move14cs(now15csonmain) to the same number as the graphics string.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-25T21:48:48Z.
Review: APPROVED
Approving
135c11bc. The compute-revision request from my earlier review is resolved: the graphics and compute strings move together tox2w-startup-shader-17/17cs, abovemain's 16 from #2010. They're ingfx2hlsl.py(pipeline_key,compute_key), ind3d12_draw.inl(revision,compute_revision), in the offline compute-request check with its recomputed key, and indocs/startup-runtime.md.Since
059624dd, the head adds a merge ofmain(8f045aee) and the bump commit. The bump also drops the oldmed3helper line that the merge brought back. Nothing on the branch still callsmed3(;v_med3_f32emitsmed3_32.Run at
135c11bc(Windows 11 build 26300; AMD Radeon RX 9070 XT, driver 32.0.32029.2010; WARP 10.0.26100.9818):58/58 passed, 0 skipped. That includes
startup_gfx2hlsl_f32_med3_{controls,warp,hardware},startup_gfx2hlsl_int_med3_controlsandcompute_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.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T21:46:58Z.
Rebased onto
main8f045aee; the revision moved tox2w-startup-shader-17with the compute key (17cs) in the same patch, as you asked.Two things the rebase surfaced, both fixed here:
mainhad reintroducedmed3in the preamble through #2010. Nothing calls it after this branch's switch tomed3_32, andtests/startup_gfx2hlsl_f32_med3_check.pyrejects a leftovermed3(, so it is dropped.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, anddocs/startup-runtime.md. All eleven moved, so nothing keeps asserting16.The framed compute key at
17csis0x8421b8080fd0a5e6, updated in the offline check; thefractcontrol now compares16against17instead of15against16.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.