Skip to content

shader: lower v_min_f16 and v_max_f16 in the direct compute path - #3125

Merged
Leyukaka merged 3 commits into
R0Y00:mainfrom
R0D4X:feat/native-minmax-f16
Nov 28, 2026
Merged

Leyukaka merged 3 commits into
R0Y00:mainfrom
R0D4X:feat/native-minmax-f16

Conversation

@R0D4X

@R0D4X R0D4X commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #3124. Refs #2935.

What this changes

  • Lowers v_min_f16 (VOP2 0x3a / VOP3A 0x13a) and v_max_f16 (VOP2 0x39 / VOP3A 0x139) in src/gfx1013_direct_compute.inl using exact 32-bit integer IEEE-754 binary16 ordering while preserving D[31:16].
  • Orders -0.0h < +0.0h, implements IEEE-754 minNum/maxNum (returning the non-NaN operand if one is NaN and canonical 0x7e00 if both are NaN), supports VOP3A abs/neg and clamp, and refuses unsupported modifiers (omod, op_sel, unused src2).

Tests & docs added

  • tests/native_minmax_f16_check.py & tests/cmake/native_minmax_f16.cmake:
    • native_minmax_f16_controls: validates emitted integer DXIL (icmp slt i32 / icmp sgt i32), refuses invalid modifiers, and verifies that the ignore_signed_zero, propagate_nan, no_clamp, and zero_high negative controls each differ.
    • native_minmax_f16_hardware: dispatches 8 binary16 pairs (±0.0h in both orders, normals, subnormals, ±Inf, single/dual NaN) across VOP2 and VOP3A with pre-seeded D[31:16], matching 2048 result words and 16 sentinel guards.
  • docs/experiments/native-minmax-f16.md: experiment note and reproduction commands.

Verification

  • Commit: eeaa3d78
  • Environment: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK / DXC 10.0.26100.0, NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2124).
  • ctest --preset dev --no-tests=error -R '^native_minmax_f16_|^native_ldexp_' -V: 4/4 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_minmax_f16_|^native_ldexp_': 4/4 passed (0 skipped).
  • tests/docs_links_check.py & tests/bidi_controls_check.py: passed.

Scope: covers synthetic direct-DXIL compute fixtures for v_min_f16 and v_max_f16 only; does not establish full-game execution.

@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 eeaa3d7 (comment only, no Windows run yet).

The ordering key (negative values mapped below -0, then a signed compare), the NaN handling and the kept D[31:16] read correctly to me.

One thing should change before merge: inline float constants. half_src takes operand32(i, i.src[j]) and keeps the low 16 bits, but for sources 240-248 operand32 returns the f32 bit pattern. For 0.5, 1.0, 2.0, 4.0 and their negatives that low half is 0, so v_max_f16 v4, 1.0, v3 compares against +0 instead of 1.0h. The v_add_f16 block in the same file maps these sources through halves[]. Could you do the same, or refuse 240-248, and add an inline-constant case to the fixture? The same point was raised on #3135 and applies to #3123 and the merged #3120, so one follow-up could cover all of them.

Command: git diff origin/main...pull/3125/head -- src/ (read), compared with the v_add_f16 operand path in src/gfx1013_direct_compute.inl.

AI-assisted; commands listed were executed locally.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct lowering of v_min_f16 and v_max_f16 using exact integer-based IEEE-754 binary16 ordering. The key function correctly handles signed zero ordering, NaN semantics (minNum/maxNum with canonical dual-NaN), VOP3A abs/neg/clamp, and properly rejects unsupported modifiers. Test suite covers all edge cases including ±0 ordering, subnormals, Inf, single/dual NaN, and negative controls. Approved.

@KelboLive

Copy link
Copy Markdown
Contributor

This PR shares the same inline float constant bug that tedin7 already flagged on this PR and on #3135: half_src calls operand32(i, i.src[j]) which returns f32 bit patterns for src 240-248 (e.g., 0x3f800000 for 1.0), then masks the low 16 bits to get 0. So v_max_f16 v4, 1.0, v3 compares against +0 instead of 1.0h. The fix is identical: map sources 240-248 through a halves[] array like the v_add_f16 block does (line ~1207-1212), or refuse them. Please add an inline-constant case to the fixture.

Missing graphics guard. The v_add_f16 block has if (graphics_input) return false;. Your new block lacks this, so direct compute lowering now applies to vertex/fragment shaders too. Add the same guard unless that's intentional.

CMake guard. Same issue as #3135: uses X2W_HAVE_DXCAPI AND EXISTS INTAKE_PYTHON instead of TARGET compile_dxil AND TARGET native_store_replay AND EXISTS .... Please match the pattern used by other native fragments.

NaN vs Inf test coverage. The NaN check (a & 0x7FFF) > 31744 (> 0x7C00) correctly catches NaNs (exponent=0x1F, non-zero mantissa). But no source in CASES has exponent=0x1F with zero mantissa (+Inf/-Inf), which should pass through min/max unchanged. Please add ±Inf to the fixture.

VOP2 abs/neg not tested. The lowering supports i.abs & (1u << j) and i.neg & (1u << j) for both sources, but the test program only runs VOP3A with neg/abs/clamp. Either test VOP2 forms with abs/neg or narrow the description.

@pigalot

pigalot commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

I tried merging main b1e92256 into eeaa3d78 to clear the conflict, and did not push, because the merged tree fails an existing test.

The conflict was textual only, in src/gfx1013_direct_compute.inl. This PR's v_min_f16/v_max_f16 block and #3187's v_fma_f16 block were added at the same place, and both end in the same clamp-and-store tail. Keeping both blocks, each with its own tail, gives a tree that adds exactly this PR's 29 lines over main and removes nothing. The local merge commit is 6b89c0bc. dev and release build.

The failure: native_f16_alu_ir fails in Debug and in Release:

ctest --test-dir out/build/dev --no-tests=error -R '^native_f16_alu_ir$' --output-on-failure
AssertionError: ('direct DXIL lowering for v_min_f16', 0, '')

tests/native_f16_alu_check.py has a refusal control from #2964, vop2(0x3a, 3, 257, 2) (VOP2 v_min_f16), that expects direct DXIL lowering for v_min_f16. This PR now lowers that program, so the control's expectation no longer holds. The same control is at this PR's head (line 277), so I expect the head fails it on its own as well; I did not build the head alone to confirm.

A possible direction (unconfirmed): point that refusal control at a binary16 op that is still unlowered, so it keeps checking the refusal path. That is your call, since it edits an existing test.

Other results on the merged tree:

  • The rest of the native_*_f16* family, native_half_int_cvt and native_direct_container pass in dev and release /O2. That includes this PR's native_minmax_f16_*.
  • native_{f16_alu,fma_f16,frexp_f16,minmax_f16,packed_f16_compute,packed_f16_pixel}_hardware pass at -j 1 in both presets, with no skips.
  • The full non-hardware suite in dev, excluding startup_audio_*, has 3 failures. The other two, native_rtv_variant_warp and startup_compute_dispatch_native_warp, are main's existing revision.txt failures since startup: warn when the shader cache and the translator come from different revisions #2820.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088; Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; DXC 1.9.2602.17; Python 3.12.10; wave32; debug layer off.

This reply was AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the min/max lowering. I reviewed eeaa3d78, the current head, and the earlier review points aren't on the branch yet. Two things need fixing before this can merge, and one test gap would be worth closing:

  1. An existing refusal control now fails. tests/native_f16_alu_check.py has vop2(0x3a, 3, 257, 2) expecting direct DXIL lowering for v_min_f16 (line 277 on this branch, 395 on current main). This PR lowers that program, so native_f16_alu_ir fails. pigalot's comment above measured AssertionError: ('direct DXIL lowering for v_min_f16', 0, '') on the merged tree. CI missed it because the Windows job runs only ctest --preset pr (41 tests, none of them native_f16_alu or native_minmax). Could you point that control at a binary16 op that is still unlowered?
  2. Inline float constants read as zero. half_src keeps operand32(i, i.src[j]) & 65535, but for sources 240-248 constant32 returns the f32 pattern (inline_float_words, 0x3f800000 for 1.0). So v_max_f16 v4, 1.0, v3 compares against +0h, and 1/(2π) becomes 0xf983. Main's v_fma_f16 block maps these through halves[] (0x3800, 0xb800, 0x3c00, ...). Either that or refusing 240-248 works, plus one inline-constant case (every fixture source is now a VGPR set by v_mov_lit).
  3. controls doesn't check results. It asserts only that 'icmp slt i32 ' and 'icmp sgt i32 ' appear in the dump, so a lowering with min and max swapped would still pass. The reference() != reference(...) lines test only the Python model. Values are checked only by the hardware leg, which skips on WARP and isn't in CI.

Two earlier requests need no work. ±Inf is already a case ((0x00007C00, 0x0000FC00)), and VOP2 has no abs/neg fields to test (DPP and SDWA are refused). Minor, and consistent with some existing blocks: there's no FP16 MODE check and no graphics_input guard, and the CMake guard doesn't test TARGET compile_dxil.

On the conflict: git merge-tree against main bbf4687b reports one file, src/gfx1013_direct_compute.inl, where #3187's v_fma_f16 block landed in the same spot. Your 29 lines still go in unchanged right before that block, giving +29/−0 against main (and +75/−0 together with #3123's block), so the rebase should be mechanical.

This was a static review: code reading plus git merge-tree. I didn't build or run anything.

This comment was AI-assisted.

@R0D4X

R0D4X commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main (3f780716) and addressed all review points:

  • Updated the unlowered-binary16 refusal control in tests/native_f16_alu_check.py from v_min_f16 (now lowered by this PR) to v_sin_f16 (vop1(0x60, 3, 257)), so native_f16_alu_ir passes.
  • Mapped inline float constants 240..248 in half_src through packed_f16_inline_constant (halves[]) instead of truncating the f32 pattern, and added an inline-constant batch (240..247) to tests/native_minmax_f16_check.py with DXIL constant assertions and an inline_as_zero negative control.
  • Added if (graphics_input) return false; and tightened the CMake guard to TARGET compile_dxil AND TARGET native_store_replay AND EXISTS ${INTAKE_PYTHON}.

Verified on NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2140), Windows 11 (10.0.26300.9974), MSVC 19.44.35230, SDK DXC 10.0.26100.0:

  • ctest --preset dev --no-tests=error -R '^native_minmax_f16_|^native_f16_alu|^native_fma_f16_': 9/9 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_minmax_f16_|^native_f16_alu|^native_fma_f16_': 9/9 passed (0 skipped).

Ready for re-review!

Commit: 1442d8e

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the quick turnaround. I re-reviewed 1442d8ea (git diff 3f780716 1442d8ea, since the fixes ride in the merge commit) and built its trial merge with main 32dc3d7c (clang-cl cross-build, CPU legs under Wine, no GPU).

Fixed from round 1

  • The refusal control now uses v_sin_f16 (vop1(0x60, 3, 257)). native_f16_alu_check.py ir passes with 435 results match the model; 39 refusals carry the named diagnostic.
  • Inline floats 240-248 now go through packed_f16_inline_constant. The inline program's DXIL has 14336 (0.5h) and 17408 (4.0h) 19 times each. The graphics_input and CMake guards are in too, thanks.

Still needs a change

  1. The new inline assert passes without the mapping. controls checks '15360' in dump and '16384' in dump. I changed the range test to r >= 240 && r <= 0, which turns the mapping off and brings the round-1 bug back. native_minmax_f16_check.py controls still exits 0, and the dump still has 15360 9 times and 16384 once. The clamp lowering emits "15360", and the VGPR literal 0x4000 in the 246 case supplies 16384. Asserting 14336/17408 instead would catch it (19 → 0). A CPU value check against the model, like native_f16_alu_check.py ir, would be stronger.
  2. Round-1 point 3 is still open. Swapping "slt"/"sgt" in the min/max cmp also leaves controls at exit 0, so results are still checked only by the hardware leg. One CPU value check would cover this and point 1. The omod refusal check does work: dropping i.scale from the refusal makes controls fail with AssertionError: ('omod', …).

Minor: 248 (1/2π → 0x3118) is mapped but isn't in INLINE_CASES. docs/experiments/native-minmax-f16.md doesn't mention inline constants yet.

CI run 46130052346 passed on this head, but ctest --preset pr ran 73 tests, and grep -c finds 0 native_minmax and 0 native_f16_alu lines in the Windows job log, so CI hasn't run these tests. The CPU legs pass for me. I couldn't check the GPU leg. git merge-tree --write-tree origin/main 1442d8ea is clean against 32dc3d7c.

This comment was AI-assisted.

@R0D4X

R0D4X commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough re-review @JohnConnorNPC! Addressed both points and the minor notes in 58468b35:

  1. CPU IR value check (native_minmax_f16_ir): Added an ir leg in tests/native_minmax_f16_check.py (registered in tests/cmake/native_minmax_f16.cmake via native_compute_ir_dump + native_f16_alu_check.interpret) that evaluates all 59 emitted results on CPU (32 main + 27 inline across all 9 inline float constants 240..248) against the Python model, plus both modifier refusals. Turning off the inline mapping or swapping slt/sgt now fails on CPU without requiring a GPU.
  2. Tightened DXIL inline assert & added 248 (1/(2*pi) -> 0x3118): Extended INLINE_CASES to all 9 inline constants 240..248 (3 ops each, 27 words/lane + 5 sentinel words/lane) and updated controls to assert 14336 (0.5h), 17408 (4.0h), and 12568 (0x3118), none of which appear outside the inline-constant mapping.
  3. Docs: Updated docs/experiments/native-minmax-f16.md to document inline float constants 240..248 and native_minmax_f16_ir.

Verified on NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2140), Windows 11 (10.0.26300.9974), MSVC 19.44.35230, SDK DXC 10.0.26100.0:

  • ctest --preset dev --no-tests=error -R '^native_minmax_f16_|^native_f16_alu|^native_fma_f16_': 10/10 passed (0 skipped).
  • ctest --preset release --no-tests=error -R '^native_minmax_f16_|^native_f16_alu|^native_fma_f16_': 10/10 passed (0 skipped).
  • tests/docs_links_check.py & tests/bidi_controls_check.py: passed.

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

GPU review of 58468b35.

  • The two review rounds' requests are all addressed except the minor FP16 MODE check (below), and there are no open threads.
  • Semantics match the merged v_pk_min_f16 rules: one NaN returns the other operand, two NaNs return 0x7E00, -0 orders below +0, clamp as before; SDWA, DPP, omod, op_sel and src2 are refused.
  • Run here in Windows Sandbox, network off, vGPU, Windows 11 build 26300 host, release build of main d418f70e with this PR and 11 other open PRs merged locally: native_minmax_f16_ir, _controls, _hardware, native_f16_alu_ir and native_f16_alu_hardware pass. Changing the one-NaN select (select(a_nan, b, ...) → select(a_nan, a, ...)) makes native_minmax_f16_ir and native_minmax_f16_hardware fail.

Not covered: no FP16 denorm/MODE refusal as v_pk_min_f16 has; no input reaches it today because accepted MODE writes are forced to the default.

Approve.

@Leyukaka
Leyukaka merged commit 6685de7 into R0Y00:main Nov 28, 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.

shader: lower v_min_f16 and v_max_f16 in the direct compute path

6 participants