Skip to content

shader: lower v_min3_f16, v_max3_f16, and v_med3_f16 in direct Compute and Pixel DXIL - #3266

Open
legacy-code-archaeologist wants to merge 2 commits into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-minmax-med3-f16
Open

legacy-code-archaeologist wants to merge 2 commits into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-minmax-med3-f16

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Lowers VOP3 v_min3_f16 (0x351), v_max3_f16 (0x354), and v_med3_f16 (0x357) in direct Compute and Pixel DXIL.

Fixes #3264. Refs #2935.

Summary

  • src/gfx1013_direct_compute.inl:
    • Lowered VOP3 v_min3_f16, v_max3_f16, and v_med3_f16 in DirectCompute::float_alu() with abs[2:0], neg[2:0], clamp (NaN/negative -> +0.0, positive subnormals preserved, > 0x3bff -> 1.0), op_sel[3:0] (high-half source reads on VGPRs and destination high/low half placement preserving the other 16 bits of vdst), VGPR/SGPR/literal/inline sources, exact 16-bit signed-zero (-0.0 < +0.0) and NaN ordering (including v_med3_f16 returning min of numeric inputs when any input is NaN), and the binary16 denormal mode gate ((mode_bits & 0xc0u) == 0xc0u).
    • Refuses non-VOP3/DPP/SDWA encodings, omod, high-half op_sel on non-VGPR sources, special scalar sources (106..127/253), and non-default or unknown binary16 denormal modes (half_binary = true in DirectCompute::lower()).
  • tests/native_minmax_med3_f16_check.py, tests/cmake/native_minmax_med3_f16.cmake:
    • 24 cases across all 3 opcodes: all 6 distinct permutations of med3_f16, signed zeros (-0.0/+0.0), binary16 subnormals and ±Inf, 1/2/3-NaN inputs, abs/neg, clamp, op_sel[3:0] with 0xDEADCAFE destination half preservation, SGPR/literal/inline sources, and partial EXEC_LO (0xAAAAAAAA) lane preservation.
    • 7 refusals: omod != 0, high-half op_sel on SGPR and inline float constants, vcc_lo source, s_denorm_mode 0x3, and conditional MODE write.

Verification (dev and release /O2, Windows 10.0.26100)

  • ctest --preset dev --no-tests=error -V -R '^native_minmax_med3_f16_' & ctest --preset release --no-tests=error -V -R '^native_minmax_med3_f16_':
    • native_minmax_med3_f16_ir: Passed (24 cases matched exact model; 7 refusals matched).
    • native_minmax_med3_f16_controls: Passed (24 cases compiled to DXIL; 7 refusals verified).
    • native_minmax_med3_f16_pixel_warp: Passed (Microsoft Basic Render Driver 10.0.26100.9711, 12 pixel draws / 24 cases matched; debug_layer=unavailable).
    • native_minmax_med3_f16_pixel_hardware: Passed (12 pixel draws / 24 cases matched; NVIDIA Quadro M1000M, 32.0.15.7371, debug_layer=unavailable).
    • native_minmax_med3_f16_compute_hardware: Skipped (77) (Intel(R) HD Graphics 530, vendor 8086, device 191b, driver 31.0.101.2111 lacks SM 6.6 wave32 — needs testing on a wave32-capable GPU).
  • ctest --preset dev --no-tests=error -R '^native_.*_(controls|ir)$' --output-on-failure: 71/71 passed (0 skips).
  • Sensitivity mutants caught (rc=8): dropping any_nan in v_med3_f16 (med3_one_nan_uses_min), dropping high-half op_sel & 8u destination placement (max3_opsel_dst_hi), and removing v_med3_f16 from half_binary = true (cond_mode unhandled s_denorm_mode) all failed native_minmax_med3_f16_ir.

AI-assisted.

@Leyukaka

Copy link
Copy Markdown
Collaborator

I reviewed the diff (not run). I found nothing wrong in the lowering. There are three gaps in the fixture cases, though:

  • NaN clamp. med3_clamp_nan_to_zero uses (0x7E01, 0x8000, 0x0000). With one NaN, med3 takes the min of the two numbers, -0. So the clamp sees 0x8000, never a NaN, and the is_nan(result) term of the clamp is never true in any case.
    • Three NaNs with clamp would reach it: the pre-clamp result is 0x7E00, which must clamp to +0.
    • Without that term, 0x7E00 would clamp to 0x3c00 (1.0).
  • Negative clamp. min3_clamp_neg_subnormal uses (0x0001, 0x3800, 0x3C00), which are all positive. The only negative value that reaches the clamp is -0, in the case above, although the fixture comment lists negative values, -0 and NaN.
    • A negative nonzero input such as 0x8001 or 0xBC00 with clamp would check that its sign takes it to +0.
  • med3 NaN position. The med3 cases put a single NaN in src1, a single NaN in src0, or NaNs in all three sources. None has the NaN only in src2, and none has two NaNs with one number. Dropping is_nan(c) from any_nan would still pass every case.
    • (0x4200, 0x3800, 0x7E01) would catch it: the expected result is 0x3800; without the term, 0x4200.

AI-assisted.

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

I reviewed 8223b4b7 merged over main 240e0fb9. The merge is clean (tree ab12c114) and git diff --cached --check is clean. I first ran everything over dbb933d1, then reran the builds and tests over 240e0fb9, which adds v_pk_fmac_f16 and the f16 rounding ops to the same file.

I found nothing wrong with the lowering itself. Three of its terms have no case that tells them apart, though: two of Leyukaka's gaps (I reran both as mutants) and one more. I'm requesting the cases, not code changes.

What I traced. VOP3 0x351/0x354/0x357 are v_min3_f16/v_max3_f16/v_med3_f16, and decode already names them. The lowering is integer selects on the 16-bit patterns, using the same order key and NaN rule as main's v_pk_min_f16/v_pk_max_f16:

  • x ^ (x >= 0x8000 ? 0xffff : 0x8000) puts -0 below +0;
  • one NaN returns the other operand, and two NaNs return 0x7e00.

v_med3_f16 is max(min(a, b), min(max(a, b), c)). If any input is NaN, it is min3 instead, as the RDNA 2 ISA pseudo-code for V_MED3 defines.

The rest:

  • clamp sends NaN and every sign-set result to +0, and anything above 0x3bff to 1.0;
  • op_sel[3] places the result in the high half and keeps the other half of vdst;
  • high-half op_sel on a non-VGPR source, omod, DPP/SDWA and special scalar sources are refused;
  • all three ops join half_binary.

What I ran. I built compile_dxil native_store_replay offscreen_check native_compute_ir_dump native_global_srv_heap_replay native_compute_manifest_reasons in dev and release, then ran:

ctest --test-dir out/build/<preset> --no-tests=error -j 4 -R '^native_' -E '_hardware'
python .agentic\scripts\with_gpu_lock.py ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^native_(minmax|min|max|med|f16|sdwa_f16|packed|pk|fma_f16)[a-z0-9_]*_hardware$'
  • Non-hardware: 145 tests per preset. 128 pass and 17 _warp tests skip with 77, the same WARP capability skips as on main, in both Debug and Release /O2. native_minmax_med3_f16_ir, _controls and _pixel_warp pass.
  • Hardware: 25/25 pass in both presets. native_minmax_med3_f16_compute_hardware runs here (it skipped on your Intel adapter), and so do _pixel_hardware and the packed/f16 neighbours.
  • Negative control: tests/cmake/native_minmax_med3_f16.cmake is new, so main registers none of these tests.
  • What the GPU runs show: because the lowering is integer selects, the GPU runs confirm that the DXIL matches the Python model. The model's NaN and signed-zero conventions are main's v_pk_min/max_f16 conventions; gfx1013 itself is not measured here.

Mutants. Each was rebuilt in dev, then I ran native_minmax_med3_f16_(ir|controls|pixel_warp|pixel_hardware|compute_hardware). The first twelve ran over dbb933d1; N1 and N2 ran over 240e0fb9.

Mutant Result
order key ignores the sign (x ^ 0x8000) killed
two NaNs return the first operand killed
NaN not skipped (plain ordered select) killed
median computed as max(min(a, b), c) killed
clamp upper bound 2.0 killed
clamp condition nan or sign changed to nan and sign killed (by the -0 case)
abs ignored killed
low-half write doesn't keep the high half killed
op_sel source reads the low half killed
omod accepted killed
N1: clamp NaN term dropped (or i1 false, sign) survives
N2: any_nan ignores src2 (is_nan(a) or is_nan(b)) survives
negative inline integers (193..208) sign-flipped survives
in-op denormal gate (mode_bits & 0xc0) != 0xc0 removed survives (expected, see below)

Restored and rebuilt afterwards; git diff --quiet is clean.

Cases that would kill the survivors (expected values from the lowering as written):

  1. Clamp of a NaN result. No case sends a NaN into the clamp: med3_clamp_nan_to_zero hands it 0x8000. For example, med3_three_nans with clamp=1 is 0x7e00 before the clamp and 0x0000 after it. Without the NaN term it is 0x3c00.
  2. med3 with a NaN only in src2. med3(0x4200, 0x3800, 0x7E01) gives 0x3800. With N2 it gives 0x4200.
  3. Negative inline integer. half_bits maps 193..208 to (192 - r) & 0xffff, and no case reads that branch: the only 193 in the test is the EXEC s_mov. min3 with src0 = inline -1 (193), as the PR reads it 0xffff, a NaN pattern, and 0x3C00, 0x4000 gives 0x3c00. With the sign flipped, src0 becomes 0x0001, and the result is 0x0001.

Leyukaka's other point holds too, though no mutant isolates it. The only sign-set input that reaches the clamp is -0. A clamp case with a negative nonzero result, such as 0xBC00, would pin the sign term for real negatives.

Non-blocking: the in-op denormal gate can't be reached. mode_bits starts at 0xf0, and changes only through consume_half_mode. That function already refuses any MODE write that changes the binary16 bits while half_binary is set, so both refusal controls stop there. The same holds for the existing v_pk_min/max_f16 gate. I'd keep the line as defence in depth; this survivor needs no case.

Verdict: changes requested on 8223b4b7, for cases 1–3. I'll re-run the mutants on the new head.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and WARP, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Windows SDK 10.0.28000.0 DXC, Python 3.12.10, wave32; I did not check the debug-layer status.
CI: Build and test on Windows passes on 8223b4b7.

This reply was AI-assisted.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks @pigalot and @Leyukaka — added the 5 requested fixture cases to tests/native_minmax_med3_f16_check.py (bringing the suite from 24 to 29 cases total, 28 non-partial Pixel cases across 14 draws):

  • med3_nan_src2: med3(0x4200, 0x3800, 0x7E01) -> 0x3800
  • med3_two_nans: med3(0x7E01, 0x3800, 0x7D02) -> 0x3800
  • min3_clamp_negative: min3(0xBC00, 0x3800, 0x3C00) with clamp=1 (-1.0 -> 0x0000)
  • med3_three_nans_clamp: med3(0x7E01, 0x7D02, 0x7F03) with clamp=1 (0x7E00 -> 0x0000)
  • min3_inline_neg_int: min3(src0=193, 0x3C00, 0x4000) -> 0x3C00 (193 reads 0xFFFF NaN)

Verification (dev and release presets):

  • ctest --preset dev --no-tests=error --output-on-failure -j 1 -V -R '^native_minmax_med3_f16_': native_minmax_med3_f16_ir (29 cases, 7 refusals), native_minmax_med3_f16_controls (29 cases compiled, 7 refusals), native_minmax_med3_f16_pixel_warp (14 draws / 28 cases), and native_minmax_med3_f16_pixel_hardware passed; native_minmax_med3_f16_compute_hardware skipped (77 on local Intel HD Graphics 530 31.0.101.2111 lacking SM 6.6 wave32)
  • ctest --preset release --no-tests=error --output-on-failure -j 1 -V -R '^native_minmax_med3_f16_': 4 passed, 1 skipped (77 on Intel HD Graphics 530)

Sensitivity mutant checks on dev (native_minmax_med3_f16_ir):

  • Dropping the NaN check in clamp: killed at slot 23 (med3_three_nans_clamp, 0xdead3c00 != 0xdead0000)
  • Dropping c from any_nan in v_med3_f16: killed at slot 14 (med3_nan_src2, 0xdead4200 != 0xdead3800)
  • Flipping sign on inline 193..208 (192u - r -> r - 192u): killed at slot 26 (min3_inline_neg_int, 0xdead0001 != 0xdead3c00)

AI-assisted.

…0Y00#3264)

Lower VOP3 v_min3_f16 (0x351), v_max3_f16 (0x354), and v_med3_f16 (0x357) in DirectCompute::float_alu() with abs/neg/clamp/op_sel[3:0], exact binary16 NaN and signed-zero ordering, other-half preservation, binary16 denormal mode tracking, and IR/DXIL/Pixel/Compute tests.

Fixes R0Y00#3264, Refs R0Y00#2935. AI-assisted.
Add med3_nan_src2, med3_two_nans, min3_clamp_negative, med3_three_nans_clamp, and min3_inline_neg_int (src0=193) to tests/native_minmax_med3_f16_check.py (29 cases total). Refs R0Y00#3264, R0Y00#2935. AI-assisted.
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (d649e470) to resolve the src/gfx1013_direct_compute.inl conflict after #3269 merged, and re-verified native_minmax_med3_f16_* in both dev and release:

  • 4 passed (native_minmax_med3_f16_ir 29 cases / 7 refusals, controls, pixel_warp 14 draws / 28 cases, pixel_hardware), 1 skipped (native_minmax_med3_f16_compute_hardware 77 on local Intel HD Graphics 530 31.0.101.2111 lacking SM 6.6 wave32).

AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thank you for the added cases. I reviewed df6c0545 merged locally into current main (bbf4687b). The merge is clean, with no conflict files and git diff --check empty. This is a comment, not a review state.

Requested changes. All three cases from the review on 8223b4b7 are on this head (med3_three_nans_clamp, med3_nan_src2, min3_inline_neg_int), and so is the negative-clamp case from the earlier comment (min3_clamp_negative). I rebuilt each mutant and ran ctest -R '^native_minmax_med3_f16_(ir|controls)$'; every one now fails ir:

  • clamp NaN term dropped → (23, 'med3_three_nans_clamp', '0xdead3c00', '0xdead0000')
  • any_nan ignores src2 → (14, 'med3_nan_src2', '0xdead4200', '0xdead3800')
  • inline 193..208 sign flipped → (26, 'min3_inline_neg_int', '0xdead0001', '0xdead3c00')
  • clamp sign term only for -0 (cmp("eq", result, "32768")) → (20, 'min3_clamp_negative', '0xdead3c00', '0xdead0000')
  • your op_sel[3] placement mutant → (24, 'max3_opsel_dst_hi', '0xdead4400', '0x4400cafe')

On the unmodified merge, native_minmax_med3_f16_ir reports "29 cases matched exact model; 7 refusals matched" and _controls reports "compiled 29 cases to DXIL; verified 7 refusals". I ran this with a Linux cross build under Wine, not on Windows, and ran no WARP or GPU tests (pixel_warp, pixel_hardware, compute_hardware). CI is green, but its Windows job runs ctest --preset pr ("100% tests passed out of 70"), and none of the new tests is in it (minmax_med3 appears 0 times in the job log). So ir/controls are measured only by local runs. Small things, none blocking:

  1. No docs entry for the three ops: git grep -E 'v_(min3|max3|med3)_f16' -- '*.md' finds nothing, while the f32 forms are in docs/experiments.md. A short paragraph on the med3 NaN rule, the clamp and op_sel would help.
  2. The in-op denormal gate (mode_bits & 0xc0u) != 0xc0u can't be reached, because consume_half_mode refuses first. Fine to keep as defence in depth.
  3. The PR body still says 24 cases and 12 pixel draws; the head has 29 cases and 14 draws.

From our side this looks ready, pending the re-review on the new head.

This comment was AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the work here. This branch no longer merges with current main (c6f23813): git merge-tree --write-tree origin/main df6c0545 exits 1 with a conflict in src/gfx1013_direct_compute.inl.

Could you merge or rebase current main into the branch, resolve that conflict, and re-run the tests this PR adds, so reviewers can look at the refreshed head?

This comment was AI-assisted.

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

I retested df6c0545 merged over main c6f23813. This supersedes my changes-requested review on 8223b4b7.

What changed. git range-diff shows that the lowering commit (b82ba196) is the same as 8223b4b7, apart from the rebase context in the half_binary list. df6c0545 adds five cases to tests/native_minmax_med3_f16_check.py, and the model supplies their expected values. Each of my earlier findings:

  • Case 1, clamp of a NaN result: addressed. med3_three_nans_clamp sends 0x7e00 into the clamp.
  • Case 2, med3 with a NaN only in src2: addressed by med3_nan_src2. med3_two_nans adds the two-NaNs-one-number case.
  • Case 3, negative inline integer: addressed. min3_inline_neg_int reads inline -1 (193).
  • Leyukaka's negative clamp: addressed. min3_clamp_negative clamps 0xBC00 to +0.

Merge with main. The branch conflicts with c6f23813 in src/gfx1013_direct_compute.inl. #3292 added v_cvt_norm_i16_f16/v_cvt_norm_u16_f16 to the same half_binary list. Keeping both sides resolves it:

            op.name == "v_cvt_norm_i16_f16" ||
            op.name == "v_cvt_norm_u16_f16" ||
            op.name == "v_min3_f16" ||
            op.name == "v_max3_f16" ||
            op.name == "v_med3_f16")

With that resolution, the tree is 2eab42a7 and git diff --cached --check is clean. I didn't push it.

What I ran on that tree. I built compile_dxil native_store_replay offscreen_check native_compute_ir_dump native_global_srv_heap_replay native_compute_manifest_reasons in dev and release, then ran:

ctest --test-dir out/build/<preset> --no-tests=error -j 4 -R '^native_' -E '_hardware'
python .agentic\scripts\with_gpu_lock.py ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^native_(minmax|min|max|med|f16|sdwa_f16|packed|pk|fma_f16)[a-z0-9_]*_hardware$'
  • Non-hardware: 163 tests per preset. 145 pass and 18 skip with 77 (WARP capability skips), in both Debug and Release /O2.
  • Hardware: 27/27 pass in both presets, including native_minmax_med3_f16_compute_hardware and _pixel_hardware.

Mutants. Each was rebuilt in dev, then I ran native_minmax_med3_f16_*, hardware included. All 13 are killed now; the three marked "now killed" survived on 8223b4b7:

Mutant Result
order key ignores the sign killed
two NaNs return the first operand killed
NaN not skipped killed
median computed as max(min(a, b), c) killed
clamp upper bound 2.0 killed
clamp nan or sign changed to nan and sign killed
abs ignored killed
low-half write doesn't keep the high half killed
op_sel source reads the low half killed
omod accepted killed
N1: clamp NaN term dropped now killed
N2: any_nan ignores src2 now killed
negative inline integers sign-flipped now killed

Restored and rebuilt afterwards; git diff --quiet is clean. I didn't rerun the in-op denormal-gate mutant. It can't be reached, as I noted on 8223b4b7, and this push doesn't change that.

Verdict: approve on df6c0545. It can't merge until main is merged in; the resolution above is the whole conflict.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and WARP, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Windows SDK 10.0.28000.0 DXC, Python 3.12.10, wave32; I did not check the debug-layer status.
CI: Build and test on Windows passes on df6c0545.

This reply was AI-assisted.

@Leyukaka

Copy link
Copy Markdown
Collaborator

@legacy-code-archaeologist git merge-tree --write-tree origin/main df6c0545 (main c3259f27) exits 1 with one conflict, in src/gfx1013_direct_compute.inl: the half_binary op list where this branch adds v_min3_f16, v_max3_f16 and v_med3_f16 after v_fract_f16. On main that list now ends with v_ldexp_f16, v_cvt_norm_i16_f16, v_cvt_norm_u16_f16 (lines 4754-4756). The first main commit that conflicts is 116f1db3 from your #3292 (merged as 24c958ff); 3926a289 (#3123) then adds v_ldexp_f16 in the same place. Could you merge main into the branch?

@Leyukaka

Copy link
Copy Markdown
Collaborator

Taking this over since there has been no activity for 2 h; will hand back if you resume.

Plan: merge current main into df6c0545 with your two commits unchanged, resolve the one conflict (the half_binary op list in src/gfx1013_direct_compute.inl) as the union of both sides, and open a new PR that keeps your authorship.
I don't build third-party code locally, so I will not run the merged head. The pull-request CI job runs ctest --preset pr, whose filter does not include native_minmax_med3_f16_*, so those tests need a run by a reviewer on the new head.

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.

shader: lower v_min3_f16, v_max3_f16, and v_med3_f16 in direct Compute and Pixel DXIL

4 participants