Skip to content

rdna2spirv: implement v_mullit_f32 (gfx1036-measured) - #3779

Closed
TheAirborneCoder wants to merge 1 commit into
ItsNotRexed:mainfrom
TheAirborneCoder:shader-mullit-f32
Closed

TheAirborneCoder wants to merge 1 commit into
ItsNotRexed:mainfrom
TheAirborneCoder:shader-mullit-f32

Conversation

@TheAirborneCoder

Copy link
Copy Markdown
Contributor

Implements RDNA2 v_mullit_f32 (VOP3 opcode 0x150) in tools/rdna2spirv.py, verified natively against RDNA2 gfx1036 hardware, with unit tests and contract documentation.

Claimed in #3574 (comment 7085961458).

Hardware contract (measured natively on RDNA2 gfx1036)

Measured on AMD Radeon Graphics (gfx1036, Windows driver 32.0.21042.62) in both wave32 and wave64 under default game MODE (IEEE=0, DX10_CLAMP=1, denorm32=0) via tools/hw_oracle:

  1. Input modifiers & denormal flush: VOP3 abs/neg apply first to S0, S1, S2, then f32 denormals flush to signed zero (flush_f32).
  2. Ordered range gates: (S2 > 0.0) && (S1 > -FLT_MAX) (OpFOrdGreaterThan). Excludes S2 <= 0.0 (including flushed denormals and NaNs) and S1 == -FLT_MAX (0xFF7FFFFF), -inf, or NaN, returning -FLT_MAX (0xFF7FFFFF) even when S0 is ±0.0 or NaN.
  3. Legacy zero multiply: When both gates pass, mul_legacy_f32(S0, S1) returns +0.0 (0x00000000) if either S0 or S1 is ±0.0 (or flushed denormal) — even against ±inf or S0 = NaN — and otherwise flush_f32(S0 * S1) (preserving product sign on underflow, e.g. -2^-120 * 2^-120 → 0x80000000).
  4. VOP3 output modifiers (mul:2, mul:4, div:2) before clamp: Canonicalizes ±0.0 → +0.0 before scaling (-0.0 mul:2 → +0.0), scales, and flushes post-div:2 subnormals to signed zero (-FLT_MIN div:2 → 0x80000000, -FLT_MAX div:2 → 0xFEFFFFFF, -FLT_MAX mul:2 → -inf).
  5. DX10 clamp (clamp): Runs after omod; maps NaN and <= 0 (including -0.0) to +0.0, and >= 1.0 to 1.0.

Changes

  • tools/rdna2spirv.py: Add v_mullit_f32 lowering (pure 32-bit float/int SPIR-V, no Float64/Int64 capability).
  • tests/semantic/test_alu_mullit_f32.py: Add 7 test methods covering pinned gfx1036 edge cases, input abs/neg ordering before range/zero gates, omod (mul:2/mul:4/div:2) + clamp combinations, SGPR/inline/literal/aliasing forms, 100-case seeded sweep, single-lane & multilane compute EXEC masking, and SPIR-V assembly without Float64/Int64.
  • docs/design.md: Add v_mullit_f32 row to the translator runtime contract table with gfx1036 measurements.

Verification

  • Native RDNA2 hardware oracle (tools/hw_oracle): AMD Radeon Graphics (gfx1036), Windows driver 32.0.21042.62: 1,530 cases × 2 wave sizes (wave32 & wave64) = 3,060 executions, wave32 == wave64 everywhere, 0 mismatches vs. translated SPIR-V (spirv_mini_interp) — 1,518 bit-exact, 12 both-NaN (S0 = NaN with S1 > -FLT_MAX non-zero and S2 > 0):
    • plain-grid: 720 cases, 0 mismatches
    • plain-rand: 250 cases, 0 mismatches
    • clamp, mul:2, mul:4, div:2, mul:2 clamp, div:2 clamp, neg/abs, abs-s1-neg-s2: 70 cases each (560 cases), 0 mismatches
  • CPU / repo checks (no game files):
    • python -I tests/semantic/test_alu_mullit_f32.py -v — 7/7 OK
    • python -m unittest discover -s tests/semantic — 7,329 tests OK (skipped=4, expected failures=1)
    • python -I tests/semantic/regen_fixtures.py --check — OK
    • python -I tests/test_docs_links.py — 5/5 OK
    • python -I tests/test_docs_repo_paths.py — 2/2 OK
    • python -m compileall -q make_game.py tools tests — OK
    • python -I make_game.py --help — OK

No game files or game data were used or included. AI-assisted (Antigravity).

@ray3242

ray3242 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Overlap: #3116 (SHADER-578, @Vonsek) is also open and also implements v_mullit_f32 in tools/rdna2spirv.py, with its own docs/design.md row. It has been open since 19:26Z and was claimed in the previous coordination issue (#3047 comment). #3182 (SHADER-595) is a GPU readback that depends on #3116. This PR's claim says there are no open PRs for the opcode, but #3116 is one.

I ran both PRs locally (Linux, CPython 3.12.16, python -I -m unittest discover -s tests/semantic):

run result
merge this head (24f980a) onto #3116 (f9d6d3e) merges without a textual conflict. The result has two elif op == "v_mullit_f32" branches in Gen._insn; #3116's comes first, so this PR's branch never runs. It also has two v_mullit_f32 rows in docs/design.md.
#3116's test_mullit.py against this PR's translator 2 of 16 cases fail
this PR's test_alu_mullit_f32.py against #3116's translator 2 of 7 error: #3116 raises Unsupported for clamp / mul:2

The two failing #3116 cases are cases where the two PRs disagree on what the hardware returns:

Both PRs say their behaviour was measured on gfx1036. I have no RDNA2 hardware, so I can't tell which is right. Running these two inputs on hardware (for example through #3182's readback) would settle it before one implementation lands.

Update: I missed a hardware result posted on #3116 before this comment. A native RX 6900 XT (gfx1030) readback (comment) matches #3116 on all 16 inputs in all three forms, with IEEE off and f32 denormals flushed. That includes both inputs above. With IEEE on, the signalling NaN is quieted, as this PR does. So for gfx1030 in the game's mode, #3116's results are the measured ones. Whether gfx1036 matches is still open (follow-up).

AI-assisted (Devin).

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: the new file 7/7 (44 pinned hardware cases + 24 omod/clamp cases + a 100-case seeded sweep), regen_fixtures.py --check clean, docs tests pass, merge clean. Negative control: reverting only the translator hunk gives 7/7 Unsupported: v_mullit_f32.

The rule matches the description: the literal is S2 and the gates are (S2 > 0) && (S1 > -FLT_MAX) (rdna2spirv.py:6994-7000), abs/neg apply before flushing via src_f->modify (:378-423), a zero multiply gives +0 (:6989-6993), then omod and the DX10 clamp (:7001-7015), with no Float64/Int64. docs/design.md:395 documents opcode 0x150, both gates, the +/-0 rule, omod/clamp and the gfx1036 measurement, and points at the test.

Minor:

  • test_alu_mullit_f32.py:121 ref_mullit_f32 is a test-local model also used as the oracle (:289,344), so the sweep and modifier coverage are self-referential; only the 44 pinned words are hardware-derived.
  • assertFloatWord:187 accepts any NaN, so the 12 both-NaN payloads in the docs row are not bit-checked.
  • :6984 dispatches on the name only; no .dxbc/corpus fixture exercises opcode 0x150 end to end.
  • :7001-7015 duplicates the omod/clamp logic instead of clamp_mods (needed for the +/-0 canonicalization) - maintainability only.

Approve. AI-assisted review, no game files, no GPU.

@eyyun

eyyun commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Reviewed and tested 24f980a9 on Windows 11 (Python 3.13, spirv-val from Vulkan SDK):

  • python -I tests/semantic/test_alu_mullit_f32.py -v: 7/7 pass (0.17s).
  • Validated all 6 modifier combinations (none, clamp, mul:2, mul:4, div:2, clamp mul:2) with spirv-val: 6/6 valid.
  • Scoped 8-point mutation check on the v_mullit_f32 block in tools/rdna2spirv.py (z_pos >= 0, y_gt >= -FLT_MAX, removing input flush_f32, removing product flush_f32, removing pre-omod -0.0 -> +0.0, removing post-omod flush_f32, removing clamp NaN -> +0.0, and removing post-clamp -0.0 -> +0.0): 7/8 killed (the post-clamp -0.0 -> +0.0 select is equivalent in spirv_mini_interp's Python max(min(x, 1.0), 0.0) FClamp, though needed on Vulkan drivers where FClamp(-0.0, +0.0, 1.0) can preserve -0.0).

Two small non-blocking improvements:

  1. Modifier / arity guard in tools/rdna2spirv.py: v_mullit_f32 v3, v0, v1, v2 op_sel:[1,0,0,0] is currently accepted and silently ignores op_sel, and len(a) != 4 (e.g. v_mullit_f32 v3, v0, v1) raises an uncaught ValueError: not enough values to unpack instead of Unsupported.
  2. newline="\n" in tests/semantic/test_alu_mullit_f32.py:184: with open(_SRC_PATH, "w", encoding="utf-8") as f: omits newline="\n" (unlike line 405).

AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @ray3242.

There is a native RX 6900 XT readback on #3116 for the two triples in this question. The reporter ran all 16 inputs × three instruction forms on gfx1030. With IEEE off and f32 denormals flushed, all 48 matched #3116 at its tested revision. Turning IEEE on quieted the signalling NaN; the other 15 triples stayed the same.

I compared current #3116 (f9d6d3e3) and #3779 (24f980a9) in the same CPU evaluator, XORing each result with 0x40000000 before export to preserve the NaN bits for comparison. Four of 48 words differed. In each cell below the pair is plain / sign-modified (-S0, |S1|, S2); S2 was 1.0.

S0 × S1 (binary32 bits) #3116 #3779
7fa12345 × 3f800000 7fa12345 / ffa12345 7fe12345 / ffe12345
3f7fffff × 00800000 00000000 / 80000000 00800000 / 80800000

The gfx1030 measurement supports #3116 for these inputs in the stated mode. The remaining hardware question is whether gfx1036 gives the same four words with those mode bits. That comparison is separate from #3779’s output-modifier coverage. AI-assisted review (Codex).

AI-assisted.

@TheAirborneCoder

Copy link
Copy Markdown
Contributor Author

Thanks @ray3242 for catching the overlap with #3116 (I missed it when searching open PR titles for v_mullit_f32 because #3116 is titled SHADER-578: translate RDNA2 lighting multiplication), and thanks @tupacfan04, @eyyun, and @tylerstraub for the thorough reviews.

To close @tylerstraub's open hardware question, I just measured both disputed triples natively on RDNA2 gfx1036 (AMD Radeon Graphics, Windows driver 32.0.21042.62, raw tools/hw_oracle kernels, wave32 == wave64):

S0 * S1 (S2 = 1.0) gfx1036 game MODE (ieee=0, denorm32=0) gfx1036 ieee=1, denorm32=0 #3116
7fa12345 * 3f800000 (plain / `-S0, S1 `) 7fa12345 / ffa12345
3f7fffff * 00800000 (plain / `-S0, S1 `) 00000000 / 80000000

gfx1036 matches gfx1030 (RX 6900 XT) and #3116 on all 4 words: ieee=0 preserves sNaN payloads unquieted, and 3f7fffff * 00800000 flushes to signed zero before rounding.

Closing #3779 in favor of @Vonsek's earlier #3116 (and #3182). Happy to open a clean follow-up PR on top of #3116 for VOP3 omod (mul:2/mul:4/div:2) and clamp once #3116 merges.

AI-assisted. No game files.

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