Repository navigation
shader: take MIN3 of the operands when a v_med3_f32 operand is NaN - #1822
Merged
tedin7 merged 1 commit intoNov 24, 2026
Merged
Conversation
pixel_aot lowered v_med3_f32 as max(min(a, b), min(max(a, b), c)). HLSL min and max skip a NaN operand, so with one NaN operand the result was the larger of the other two, or the first operand. The RDNA 2 ISA gives V_MIN3_F32 of the three operands when any of them is NaN, which with IEEE mode off is the smallest operand that is not NaN. The new med3_32 preamble helper tests the operand encodings for NaN, as f32_comparison and num16 do, because desktop compilers may fold isnan. Finite operands keep the previous form. The template check now evaluates the helper with NaN operands, and the three-way draw adds a case with a NaN bound and a NaN first operand, plus a negative control that ignores the NaN operand. Committed-by: Conclave (cliproxy/claude-opus-5-5)
tedin7
reviewed
Nov 24, 2026
tedin7
left a comment
Collaborator
There was a problem hiding this comment.
Verified PR #1822 at commit 91fb38a47c16040c4c4ac03cb8802bf266e47af3 (shader: take MIN3 of the operands when a v_med3_f32 operand is NaN).
Review Summary
- ISA semantics:
med3_32(a, b, c)checks operand encodings viamax(max(asuint(a) & 0x7fffffffu, asuint(b) & 0x7fffffffu), asuint(c) & 0x7fffffffu) > 0x7f800000u(avoiding float NaN-test folding while keeping±Infat0x7f800000uon the finite branch). When any operand is NaN, it returnsmin(min(a, b), c), matching RDNA2V_MED3_F32->V_MIN3_F32(smallest non-NaN operand for 1 or 2 NaNs, NaN for 3 NaNs, IEEE mode off). - Tests & docs:
tests/pixel_aot_check.pyextendscheck_three_way_templateswith NaN combinations across all three operands, updatesthree_way_caseandcheck_three_way_drawwith a NaN constant-buffer case plus a negative control verifying that ignoring NaN triggerspixel_mismatch, anddocs/experiments.mdaccurately documents the behavior and coverage. - Findings: 0 P1, 0 P2. Clean-room verified.
Verification Commands Executed Locally
- Linux checks:
git diff --check origin/main...HEAD-> PASS (rc=0)python -B tests/docs_links_check.py-> PASS (17 Markdown files and 368 links)python -B tests/bidi_controls_check.py-> PASS (1313 tracked files)python -B tests/form_coverage_check.py-> PASS (form coverage checks passed)python -B tests/rdna2_tools_check.py-> PASS (148 tests OK, skipped=18)pixel_aot_check.check_three_way_templates()&_check_controls_inline()-> PASS
- Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
- Debug (
--preset dev):ctest --preset dev -R '^(pixel_aot_three_way_warp|pixel_aot_region_body_warp|pixel_aot_e64_warp|pixel_aot_warp)$' --parallel 6 --no-tests=error --output-on-failure-> 4/4 passed (22.68s) - Release
/O2(--preset release):ctest --preset release -R '^(pixel_aot_three_way_warp|pixel_aot_region_body_warp|pixel_aot_e64_warp|pixel_aot_warp)$' --parallel 6 --no-tests=error --output-on-failure-> 4/4 passed (23.10s) - Windows Python checks:
tests/docs_links_check.py(rc=0),tests/bidi_controls_check.py(rc=0)
- Debug (
- Mutation checks:
- Reverted NaN branch in
med3_32(tools/pixel_aot.py) tomax(min(a, b), min(max(a, b), c)): failed locally (check_three_way_templatesrc=1,AssertionError: ('med3', -1.5, 0.0, nan, 0.0)) and on Win2025 (rc=1,tests/form_coverage_check.pyrc=1). - Swapped NaN branch in
med3_32tomax(max(a, b), c): failed locally (check_three_way_templatesrc=1,AssertionError: ('med3', -1.5, 0.0, nan, 0.0)).
- Reverted NaN branch in
AI-assisted; commands listed were executed locally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
tools/pixel_aot.pyloweredv_med3_f32asmax(min(a, b), min(max(a, b), c)). HLSLminandmaxskip a NaN operand, so with one NaN operand the result was the larger of the other two operands, or the first one. The RDNA 2 ISA reference givesV_MIN3_F32of the three operands when any operand is NaN. With IEEE mode off, that is the smallest operand that is not NaN. For example,v_med3_f32 v12, v12, s44, s45withs44= NaN ands45= 0.75 gavev12onmain. The ISA rule givesmin(v12, 0.75).The ALU entry for
med3now calls a new preamble helper,med3_32. It tests the three encodings for NaN ((asuint(x) & 0x7fffffff) > 0x7f800000), asf32_comparisonandnum16already do, because desktop compilers may foldisnan. If any operand is NaN, the helper returnsmin(min(a, b), c). Otherwise it returns the previous expression, so finite results do not change. The integer and 16-bit med3 forms are not changed.The rule is written from the public ISA pseudocode (AMD RDNA 2 Instruction Set Architecture reference,
V_MED3_F32; the CLRX GCN VOP3 page gives the same NaN rule). Thedocs/experiments.mdparagraph that said NaN operands were not measured now describes the rule and what is checked.Verification
Commit
91fb38a4onorigin/mainecc6e6ce. Windows 11 build 26300, NVIDIA GeForce RTX 5070 (driver 32.0.16.1088) as the hardware adapter, and WARP. Debug layer status was not checked. DXC from Windows SDK 10.0.26100. No capability skips.Test changes in
tests/pixel_aot_check.py:check_three_way_templatesnow adds NaN to the operand values. It evaluates themin3,max3andmed3templates, and themed3_32helper taken from the preamble text, with a Python model of HLSLmin/max, where a NaN operand gives the other operand. It compares the results with a reference for the ISA rule (med3_f32).three_way_casecomputes both medians with that reference.check_three_way_drawadds a case with constants(NaN, 0.75, 0.375, NaN): the bounds ofv_med3_f32 v12, v12, s44, s45and the first operand ofv_med3_f32 v15, s47, v13, v12are NaN. As a negative control, a shader whose helper ignores the NaN operand must reportpixel_mismatchon that case.check_controlsexpectsmed3_32(...)in the translated source.Before the fix, with the new tests (on the earlier base
d6cbd5d7):Calling
check_three_way_drawalone (controls skipped) on the unfixed translator gaveExpected ['pass', 'pass', 'pass'], got ['pass', 'mismatch', 'pass']with first mismatchpixel_mismatch x=48 y=0, on WARP and on the RTX 5070. Only the NaN case failed.After the fix:
Evidence and follow-up
The fixture is the existing source-built
three_way()program. Its new case uses only constant-buffer values. No new files.Not tested:
v_min3_f32andv_max3_f32on a GPU. They are checked only against the HLSLmin/maxmodel in Python.tools/startup/gfx2hlsl.pyandmedian_fintools/ffx_dispatch_aot.py. They are separate translators and are left for a follow-up. startup: draw the gfx2hlsl binary32 arithmetic operations #1712 adds a draw check forgfx2hlsl.py'sv_med3_f32but leaves NaN out; shader: lower the 16-bit three-operand float VOP3 ops in pixel_aot #1466 lowersv_med3_f16inpixel_aotand refuses non-finite operands.Checklist
AI use
This change was written with AI assistance: Conclave, model
cliproxy/claude-opus-5-5. Conclave wrote the change and ran the commands above on the machine described in Verification.