Skip to content

rdna2spirv: v_madak_f32 and v_madmk_f32 flush f32 denormals like v_mad_f32 - #4189

Open
Exopus-0x65f7ad0 wants to merge 1 commit into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:shader/madak-madmk-denorm-flush
Open

Exopus-0x65f7ad0 wants to merge 1 commit into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:shader/madak-madmk-denorm-flush

Conversation

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Claim: #3918 (comment) (new SHADER item)

Problem

v_mad_f32/v_mac_f32/v_mad_legacy_f32 flush f32 denormals (SHADER-118, Gen.flush_f32): a subnormal operand, the rounded product or the sum reads as a zero of its sign, measured natively on gfx1010 (SHADER-340). v_madak_f32 and v_madmk_f32 (tools/rdna2spirv.py, the ("v_madak_f32", "v_madmk_f32") branch) are the same multiply-add with a 32-bit literal, but went through a plain OpFMul + OpFAdd, so denormals survived:

v_madak_f32 v0, v1, v2, 0x0 with v1 = 0x00000001, v2 = 2^31 gave 0x04800000 (2^-118) instead of 0.

Why they share the rule: LLVM only forms V_MADAK_F32/V_MADMK_F32 by shrinking a V_MAD_F32 whose operand is a literal (SIShrinkInstructions), and forms V_MAD_F32 only when f32 denormals are flushed (SITargetLowering::isFMADLegal, the reference SHADER-118 cites). Both are GFX10.1 VOP2 opcodes like v_mac_f32, so, like it, they can't be run natively on the gfx1036 parts testers have.

Change

  • The branch flushes the three operands, the rounded product and the sum with flush_f32, as v_mad_f32 does. VOP2 only, so there's no clamp/omod path.
  • flush_f32's docstring and the SHADER-118 row in docs/design.md name the two ops (with the "not measured natively for these two" caveat).
  • tests/semantic/test_valu_mad_lit.py: its two sabotage controls (swap multiplicand/addend, fuse into Fma) matched an OpFAdd whose first operand is the OpFMul result. With the flush in between, they no longer injected anything. They now match the add whether it takes the product or its flushed copy (and still pass on main's translator: checked with LEONIDA_TOOLS_DIR), so the controls keep catching what they were written for.

Tests

tests/semantic/test_madak_madmk_denorm.py (new, hand-written assembly, CPU): for both ops, a denormal operand, a denormal product (2^-70 x 2^-70), a denormal sum (1.5·2^-126 - 2^-126), a denormal literal, a denormal addend, the sign of a zero result, and normal values, infinity and NaN passing through.

test_madak_madmk_denorm.py
main's translator 10 failures (every denormal case; normal/inf/NaN pass)
this branch 2/2 tests OK (14 cases)

Full python -m unittest discover -s tests/semantic (Windows 11, Python 3.12): 7,458 tests, the same 9 failures/errors as main aa2988cc (7,456 there; all red on main and already taken: TEST-857, #3995, ...). test_*mad* (144 tests): OK. tests/test_design_contract_rows.py, test_docs_design_rows.py, test_docs_table_rows.py, test_docs_repo_paths.py: OK.

Limitations

  • Not measured natively for v_madak/v_madmk (needs an RDNA1/GFX10.1 part; tools/mad_denorm_probe.py could take them).
  • No corpus count (no work folder here).

AI-assisted (Claude Code). CPU only.

…d_f32

v_madak_f32/v_madmk_f32 are v_mad_f32 with a 32-bit literal: LLVM only
forms them by shrinking a V_MAD_F32 (SIShrinkInstructions), and forms
V_MAD_F32 only with f32 denormals flushed (isFMADLegal). The translator
flushed v_mad_f32/v_mac_f32 (SHADER-118, measured on gfx1010) but not
these two, so `v_madak_f32 v0, v1, v2, 0` with v1 = 0x00000001 and
v2 = 2^31 gave 0x04800000. They now flush the operands, the rounded
product and the sum through flush_f32 (VOP2 only: no clamp or omod).

tests/semantic/test_madak_madmk_denorm.py: denormal operand, product,
sum, literal and addend, plus normal/inf/NaN pass-through (10 cases fail
on the old translator). test_valu_mad_lit.py's sabotage controls now
match the add whether its first operand is the product or its flushed
copy, so they still inject their faults. docs/design.md: the SHADER-118
row covers the two.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 270b98c534b210416c5a5b7dcc11a1666c790630 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

At 270b98c5, translated v_madak_f32 and v_madmk_f32 produced the expected binary32 words on Linux, NVIDIA RTX 5090 (595.99.02). All 4,096 pixels matched in each of 17 synthetic draws. I supplied the register operands at draw time and exported the two 16-bit halves of each result separately so the sign of zero survived readback. All modules passed SPIR-V validation for Vulkan 1.2.

The smallest positive subnormal times 2³¹ yielded +0. The negative counterpart with a −0 addend yielded 0x80000000. A subnormal product and sums just inside the positive and negative normal boundaries flushed to the expected signed zeros. Both forms preserved the smallest positive normal (0x00800000) and returned 11.0 for 2 × 3 + 5. These are translated Vulkan results; native GFX10.1 measurements remain open.

AI-assisted verification.

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

Ran this at 270b98c5 on Windows 11: python tests/semantic/test_madak_madmk_denorm.py -> 2 tests, OK; python tests/semantic/test_valu_mad_lit.py -> 15 tests, OK; git diff --check clean.

v_madak_f32/v_madmk_f32 now flush f32 denormals on both operands, on the rounded product and on the sum, matching v_mad_f32/v_mac_f32; the existing literal-operand order (madak = S0S1+K, madmk = S0K+S1) is unchanged. The new cases compare against the full v_mad_f32 path on subnormal inputs and results, and the 15 literal tests still pass. No blocking issue.

@dcastell1

Copy link
Copy Markdown
Contributor

Code read + CPU tests, 270b98c: pass — python -m unittest discover -s tests/semantic -p test_madak_madmk_denorm.py → OK (SDK on PATH).

Flushing f32 denormals in v_madak_f32/v_madmk_f32 like v_mad_f32 is the consistent rule; the test pinning both the flush and the kept-denormal case is what makes it more than a rename.

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

The literal MAD lowering now applies the same denormal treatment at each binary32 rounding point as the existing MAD path, and the added cases cover operands, product, sum, signed zero, and normal-value control. I found no separate blocker.

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.

5 participants