Skip to content

shader: lower v_fma_f16, v_fmac_f16, v_fmamk_f16 and v_fmaak_f16 in the direct C++ path - #3187

Merged
pigalot merged 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-fma-f16
Nov 27, 2026
Merged

pigalot merged 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-fma-f16

Conversation

@legacy-code-archaeologist

@legacy-code-archaeologist legacy-code-archaeologist commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Lowers scalar binary16 FMA instructions (v_fma_f16, v_fmac_f16, v_fmamk_f16, v_fmaak_f16) in the direct C++ compute path (DirectCompute::float_alu() in src/gfx1013_direct_compute.inl).

Summary of changes

  • Shared single-rounded binary16 FMA (fused_half_rne()): Reuses the shared 32-bit integer fused_half_rne(a, b, c) helper introduced in #3151 (25a4fb23) rather than adding a duplicate FMA helper, preserving D[31:16].
  • Instruction & modifier coverage:
    • v_fma_f16 (VOP3), v_fmac_f16 (VOP2/VOP3A), v_fmamk_f16 (VOP2), v_fmaak_f16 (VOP2), including SGPR/literal/inline-float/inline-int sources and dst = src0 overlap.
    • Supports VOP3 abs, neg, and clamp ([0.0, 1.0] saturation).
    • Enforces the default binary16 MODE contract via half_binary in DirectCompute::lower().
    • Refuses DPP, SDWA, omod, high-half op_sel, and third-source modifiers/src2 on v_fmac_f16_e64.
  • Tests & Docs:
    • Added tests/cmake/native_fma_f16.cmake, tests/native_fma_f16_check.py (41 cases incl. all 4 #2073 tie witnesses, diff_e >= 30 sticky-bit midpoint 128.0625 + 2^-24 -> 0x5801, underflow/overflow/cancellation/Inf/NaN + 12 refusals), and docs/experiments/native-direct-fma-f16.md.
    • Updated the unlowered placeholder in tests/native_f16_alu_check.py from v_fmac_f16 to v_pk_fmac_f16, and added @dx.op.unaryBits.i32(i32 33, ...) evaluation to its IR interpreter so native_fma_f16_ir evaluates fused_half_rne() directly.

Fixes #3186
Refs #2935

AI-assisted.

@KelboLive

Copy link
Copy Markdown
Contributor

The title says "lower v_fma_f16...", but this PR removes the implementation (fma_half_rne, float_alu cases) and tests for these instructions from the direct compute path.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

@KelboLive You may have caught GitHub's diff view right during the force-push/rebase onto 3d7b3a26 (#3159 + #3126). On the current head 6d5f8ee2 (#3187), all implementation and tests are added (+531 / -2 across 5 files):

  • src/gfx1013_direct_compute.inl: +175/-1 (fma_half_rne(), float_alu() cases for v_fma_f16/v_fmac_f16/v_fmamk_f16/v_fmaak_f16, and half_binary)
  • tests/cmake/native_fma_f16.cmake: +19
  • tests/native_fma_f16_check.py: +298
  • tests/native_f16_alu_check.py: +1/-1 (v_fmac_f16 -> v_pk_fmac_f16)
  • docs/experiments/native-direct-fma-f16.md: +38

AI-assisted.

R0Y00 pushed a commit that referenced this pull request Nov 27, 2026

@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 tried to review 6d5f8ee2 over current main 8a8da963. The trial merge (git merge --no-ff --no-commit) conflicts in src/gfx1013_direct_compute.inl, in two places:

  1. Next to the binary16 helpers: #3151 (25a4fb23, merged today) added packed_f16_arithmetic and fused_half_rne, where this PR adds fma_half_rne.
  2. The half_binary mode scan: #3144 (e219d724) reformatted that condition and added v_pk_min_f16 / v_pk_max_f16, where this PR adds the four FMA names.

The PR's merge base is 3d7b3a26, so both landed after it.

Not just a textual conflict. main now has fused_half_rne(a, b, c), which #3151 uses for v_pk_fma_f16. Its contract is the same as this PR's fma_half_rne: three binary16 encodings in the low 16 bits, one exact single-rounded RNE result, and 0x7e00 for every NaN. Keeping both would put two independent 120-line implementations of the same arithmetic in one file, and they could disagree on edge cases without any test noticing.

Suggested direction (unconfirmed): rebase onto current main, and have the scalar v_fma_f16 / v_fmac_f16 / v_fmamk_f16 / v_fmaak_f16 path call fused_half_rne on the decoded and modified halves (including abs and neg) instead of adding fma_half_rne. Your native_fma_f16_ir / _controls / _hardware cases, the four #2073 tie witnesses and the subnormal, overflow and cancellation edges, would then also pin the shared helper. If fused_half_rne gets one of those cases wrong, that is worth a finding of its own, so please report it on the PR if it does.

I stopped at the conflict, so I have not built or run this head. I'll retest once it merges cleanly.

Environment: Windows 11 25H2 build 26200.9974; Git trial merge only.
CI: Build and test on Windows passes on 6d5f8ee2 against its own base.

This reply was AI-assisted.

…irect compute

Lower v_fma_f16 (VOP3), v_fmac_f16 (VOP2/VOP3A), v_fmamk_f16 (VOP2) and v_fmaak_f16 (VOP2) in DirectCompute::float_alu() using the shared single-rounded binary16 FMA helper fused_half_rne() (R0Y00#3151) while preserving D[31:16]. Supports VOP3 abs/neg/clamp, enforces the default binary16 MODE contract via half_binary, refuses DPP/SDWA/omod/high-half op_sel/third-source modifiers on v_fmac_f16, updates the unlowered placeholder in tests/native_f16_alu_check.py from v_fmac_f16 to v_pk_fmac_f16, and adds @dx.op.unaryBits.i32(i32 33, ...) evaluation to native_f16_alu_check.py's IR interpreter.

Fixes R0Y00#3186

Refs R0Y00#2935

AI-assisted.
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

@pigalot Thank you for the review! Rebased onto c45bf9ab (#3151 + #3144), dropped fma_half_rne(), and wired scalar v_fma_f16, v_fmac_f16, v_fmamk_f16, and v_fmaak_f16 to upstream/main's shared fused_half_rne(a, b, c) helper.

Also added @dx.op.unaryBits.i32(i32 33, ...) (FirstbitHi) evaluation to tests/native_f16_alu_check.py's IR interpreter so native_fma_f16_ir evaluates fused_half_rne() directly.

Result on fused_half_rne(): All 41 single-rounded cases (including the four #2073 tie witnesses, subnormals, exact cancellation, underflow ties, overflow, Inf/NaN, and the diff_e >= 30 sticky-bit midpoint 128.0625 + 2^-24 -> 0x5801) matched the fractions.Fraction reference with zero discrepancies.

Local verification (c45bf9ab + rebased shader/direct-fma-f16, Windows 10.0.26100, dev preset):

  • cmake --build --preset dev --target compile_dxil native_compute_ir_dump native_store_replay: passed
  • ctest --preset dev --no-tests=error -R '^(native_fma_f16_|native_packed_f16_|native_cvt_f16_i16_|native_f16_alu_)' -V: 8 passed (native_f16_alu_ir, native_fma_f16_ir, native_fma_f16_controls, native_cvt_f16_i16_ir, native_cvt_f16_i16_controls, native_packed_f16_controls, native_packed_f16_pixel_warp, native_packed_f16_pixel_hardware), 4 skipped (77, *_hardware compute tests on Intel(R) HD Graphics 530 31.0.101.2111: requires SM 6.6 with wave32)
  • ctest --preset dev --no-tests=error -R '^native_.*_(controls|ir)$' --output-on-failure: 57/57 passed
  • 5 broken-lowering sensitivity checks (zero_high_half, drop_sticky, swap_fmamk_fmaak, drop_clamp, drop_inf_cancel_nan): all exited 1 on native_fma_f16_ir; native_fma_f16_ir passed again after restoring src/gfx1013_direct_compute.inl
  • python -B tests/docs_links_check.py: passed (108 Markdown files, 600 links)

AI-assisted.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I independently applied exact head eb3524f6 over main e582a9a4, including #3188's DPP16 support (clean trial tree dd9b7dc2). The scalar forms now use #3151's shared fused_half_rne; the packed path remains and the resolved change retains #3188's handling. I built compile_dxil native_store_replay native_compute_ir_dump and ran ctest --test-dir <build> --no-tests=error -j1 -V -R '^(native_fma_f16_.*|native_f16_alu_(ir|hardware)|native_packed_f16_controls)$'. Debug and Release /O2 each passed 6/6, with no skips. The IR and RTX 4060 Laptop readback matched all 41 scalar witnesses and the controls matched 12 named refusals. Windows 11 26200.9974, driver 32.0.16.1656, debug layer off for the standalone replay. Required hosted Windows passed. @pigalot, the two conflicts from the previous head no longer reproduce; could you update your review on this head?

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

Retested eb3524f6 merged over current main f6206217, which includes #3188's DPP16 change. The merge is clean (tree 6d04976f) and git diff --cached --check is clean.

Earlier finding: addressed. fma_half_rne is gone. The scalar v_fma_f16, v_fmac_f16, v_fmamk_f16 and v_fmaak_f16 forms call #3151's fused_half_rne on the decoded halves, after abs and neg are applied. The IR interpreter in tests/native_f16_alu_check.py now evaluates FirstbitHi (op 33), which the shared helper uses. This supersedes my changes-requested review on 6d5f8ee2.

What I traced.

  • Operands: inline constants 240 to 248 map to binary16 encodings. Inline integers become 16-bit integers. fmamk takes the literal as b, and fmaak takes it as c. fmac reads its addend from the low half of D, and the e64 form refuses a src2 field or a third-source modifier.
  • Result: clamp sends NaN and negatives to 0 and anything above 1.0 to 0x3c00. D[31:16] is kept.
  • Refusals: DPP, SDWA, omod, high-half op_sel and non-default MODE are refused. The scan entry makes MODE known for these ops.
  • #3188: it does not reach these ops, because they are not in binary16_alu. Their DPP forms are still refused, as before.

What I ran.

  • dev and release /O2: built compile_dxil, native_store_replay, native_compute_ir_dump and offscreen_check, then ran

    ctest --test-dir out/build/<preset> --no-tests=error -j 4 -R '^(native_f16_alu_ir|native_fma_f16_ir|native_fma_f16_controls|native_cvt_f16_i16_ir|native_packed_f16_controls|native_cvt_f16_i16_controls|native_packed_f16_pixel_warp|native_direct_container)$'
    ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(native_f16_alu_hardware|native_fma_f16_hardware)$'

    10/10 pass in each preset, with no skips.

  • Fuzz: a local fuzz compares the dumped IR, run through the test's interpreter, against an exact Fraction FMA reference rounded once to binary16. It uses random and near-cancellation v_fma_f16 VOP3 cases. 250,000 cases over five seeds give 0 mismatches. The last 50,000 ran on this merge, and the rest ran on the earlier 886a5a28 merge.

Mutants, each rebuilt in dev (on the 886a5a28 merge, where the new block is byte-identical) and run against native_fma_f16_ir, _controls and _hardware. The unmutated baseline passes first.

Mutant Result
new block off (negative control) killed
fmamk / fmaak literal positions swapped killed
fmac addend from src2 instead of D killed
clamp keeps NaN killed
clamp upper bound dropped killed
D[31:16] zeroed killed
neg applied before abs killed
negative inline integer wrong killed
op_sel refusal dropped killed
fmac third-source modifier refusal dropped killed
MODE scan entry dropped killed
inline 248 (1/(2π)) off by one ulp survives
MODE refusal dropped survives

Two mutants of the shared helper, sticky bit dropped and round-to-odd dropped, both fail native_fma_f16_ir. Your tie witnesses catch the round-to-odd one, which my random fuzz misses. Restored and rebuilt afterwards; git diff --quiet is clean.

Non-blocking.

  1. Inline 248 is not exercised. No case reads it, so a wrong constant would pass.
  2. The MODE refusal is not pinned. No control sets a non-default rounding or denormal mode before one of these ops. One control with s_round_mode before a v_fma_f16 would pin it.

Verdict: approve on eb3524f6.

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.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on eb3524f6.

This reply was AI-assisted.

@pigalot
pigalot merged commit d6912f5 into R0Y00:main Nov 27, 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_fma_f16, v_fmac_f16, v_fmamk_f16 and v_fmaak_f16 in the direct C++ path

4 participants