Repository navigation
startup: return the minimum of the three on v_med3_f32 with a NaN source - #2095
Conversation
ea21601 to
6883eeb
Compare
Rebuilt
|
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:
- the NaN test is on the bit pattern, because a float
isnan()may be folded by the compiler — your fixture already rejects that variant; - the NaN path is
minof the three, because D3Dmin()returns the other operand, somin(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 whatfminsemantics 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.
|
Thank you for rebuilding the binary and running it on the RTX 5080. The draw needs I took the description note: the body now says the helper is the same body as On 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
left a comment
There was a problem hiding this comment.
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 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 /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 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. - nit: the PR states that the hardware
v_med3NaN branch was not exercised. That is honest and matches what the lowering does.
AI-assisted; commands listed were executed locally.
|
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 Windows 11 26200.9974, AMD Radeon RX 9070 XT, driver 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 Your other nit is right and stays as written: the hardware AI-assisted; runs executed on the machine above. |
|
@thepatrickworld could you rebase this branch onto current GitHub reports head Files that conflict with current
Commands run locally: Once it is rebased and CI is green it will be re-checked right away. AI-assisted; commands listed were executed locally. |
…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.
|
Merged On the RX 9070 XT (
Tests after the bump: the f32 med3 draw and the integer med3 fixture exit AI-assisted. |
|
CI caught a fixture I had missed: On AI-assisted. |
MasterDomino
left a comment
There was a problem hiding this comment.
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
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. - 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.
|
@thepatrickworld, one item remains before this can merge, and it isn't changed at the new head
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. |
|
Heads-up on the startup cache revision: #2467 merged as AI-assisted. |
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.
|
Rebased onto Two things the rebase surfaced, both fixed here:
The framed compute key at CI green on AI-assisted. |
MasterDomino
left a comment
There was a problem hiding this comment.
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.
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.