Shaders: RDNA2's legacy multiply-adds are the fused ones #284

Open
github-bot wants to merge 1 commit from github-bot/gh-284 into main AGit
Owner

@⁠Dufftwt on GitHub · 2026-11-24 02:02 UTC · original
Dufftwt:fix/rdna2-fused-legacy-fma → main · +57 −13 · 4 files · 1 commits · open

What's wrong

The opcode tables in gfx10/opcodes.rs are generated from the GFX10 column of Mesa's aco_opcodes.py. That column shares opcode numbers between GFX10.1 and GFX10.3 (RDNA2), and for two of them it keeps the GFX10.1 name:

encoding our table (GFX10.1) RDNA2 / GFX10.3
VOP2 0x006 (and VOP3 0x106) v_mac_legacy_f32 v_fmac_legacy_f32
VOP3 0x140 v_mad_legacy_f32 v_fma_legacy_f32

On RDNA2 these are fused multiply-adds: the product isn't rounded before the add. They keep the D3D9 rule that a zero factor gives a zero product, even against infinity or NaN. References:

  • AMD's RDNA2 ISA lists V_FMAC_LEGACY_F32 as VOP2 6 and V_FMA_LEGACY_F32 as VOP3 320.
  • LLVM has V_FMAC_LEGACY_F32 : VOP2_Real_gfx10<0x006> and V_FMA_LEGACY_F32 : VOP3_Real_gfx10<0x140>. Its gfx1030 assembler tests (gfx1030_new.s) encode v_fma_legacy_f32 v0, v1, v2, v3 as 00 00 40 d5 01 05 0e 04 and v_fmac_legacy_f32_e32 v0, v1, v2 as 01 05 00 0c, and llvm-mc -mcpu=gfx1030 has no v_mad_f32 there at all.
  • Mesa's generator itself notes that v_mad_legacy_f32 is replaced by v_fma_legacy_f32 on GFX10.3.

The decoder already treats the console as GFX10.3: it handles GFX10.3's ray-tracing instructions (image_bvh_intersect_ray).

lower.rs lowered the GFX10.1 forms: a legacy multiply (rounded) followed by a separate add. That gives a result rounded twice, and the select around the multiply also stops the driver from contracting the two. For example, v1 = v2 = 0x3F800001 and v3 = 0xBF800002 give 0x28800000 (2⁻⁴⁶) fused on RDNA2, and 0 with the multiply rounded first.

What the fix does

  • opcodes.rs names the two opcodes as RDNA2 does: v_fmac_legacy_f32 and v_fma_legacy_f32.
  • lower.rs lowers both with a new helper, fma_legacy(a, b, c): c (as 0.0 + c) when a or b is ±0, otherwise one FMad(a, b, c). This is the same "the driver may fuse it" shortcut that v_fma_f32 and v_fmac_f32 already use, since DXIL has no fused f32 FMA.
  • The v_mad_f32 / v_madak_f32 / v_madmk_f32 arm stays as it was, minus the legacy name.

How it behaves after the fix

Shaders using these opcodes translate as fused multiply-adds with the D3D9 zero rule, on hosts whose driver fuses FMad. Their results are then RDNA2's, bit for bit. A host that doesn't fuse FMad rounds twice, as it already does for v_fma_f32. I measured that on GitHub's runner: both its WARP device and its hardware device return 0 for the example above, with or without this change.

Tests

  • legacy_multiply_adds_are_the_fused_ones (gfx10/mod.rs) decodes LLVM's two gfx1030 encodings above and checks the names.
  • v_fma_legacy_f32_follows_the_d3d9_zero_rule (valu_tests.rs) runs v_fma_legacy_f32 (VOP3 0x140) on the host GPU and on WARP, with three cases:
    • 2 × 3 + 1 = 7;
    • 0 × ∞ + 1.5 = 1.5;
    • NaN × 0 − 2 = −2.

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: everything passes. victory-shader has 71 passed, the other crates have 0 failed.
  • The new tests alone on main: the decoder test fails: left: "v_mad_legacy_f32", right: "v_fma_legacy_f32". The zero-rule test passes on main too, since the old lowering also kept that rule.
  • I ran with --test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244).

Not tested: a GPU that fuses FMad, so the fused rounding itself isn't shown by a test here, and a title shader that uses these opcodes.

Related: #200 adds match arms next to the ones this edits in lower.rs. Whichever merges second needs a trivial rebase.

**@⁠Dufftwt** on GitHub · 2026-11-24 02:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/284) `Dufftwt:fix/rdna2-fused-legacy-fma` → `main` · +57 −13 · 4 files · 1 commits · open ## What's wrong The opcode tables in `gfx10/opcodes.rs` are generated from the GFX10 column of Mesa's `aco_opcodes.py`. That column shares opcode numbers between GFX10.1 and GFX10.3 (RDNA2), and for two of them it keeps the **GFX10.1** name: | encoding | our table (GFX10.1) | RDNA2 / GFX10.3 | |---|---|---| | VOP2 `0x006` (and VOP3 `0x106`) | `v_mac_legacy_f32` | **`v_fmac_legacy_f32`** | | VOP3 `0x140` | `v_mad_legacy_f32` | **`v_fma_legacy_f32`** | On RDNA2 these are **fused** multiply-adds: the product isn't rounded before the add. They keep the D3D9 rule that a zero factor gives a zero product, even against infinity or NaN. References: - AMD's RDNA2 ISA lists `V_FMAC_LEGACY_F32` as VOP2 6 and `V_FMA_LEGACY_F32` as VOP3 320. - LLVM has `V_FMAC_LEGACY_F32 : VOP2_Real_gfx10<0x006>` and `V_FMA_LEGACY_F32 : VOP3_Real_gfx10<0x140>`. Its gfx1030 assembler tests (`gfx1030_new.s`) encode `v_fma_legacy_f32 v0, v1, v2, v3` as `00 00 40 d5 01 05 0e 04` and `v_fmac_legacy_f32_e32 v0, v1, v2` as `01 05 00 0c`, and `llvm-mc -mcpu=gfx1030` has no `v_mad_f32` there at all. - Mesa's generator itself notes that `v_mad_legacy_f32` is replaced by `v_fma_legacy_f32` on GFX10.3. The decoder already treats the console as GFX10.3: it handles GFX10.3's ray-tracing instructions (`image_bvh_intersect_ray`). `lower.rs` lowered the GFX10.1 forms: a legacy multiply (rounded) followed by a separate add. That gives a result rounded twice, and the `select` around the multiply also stops the driver from contracting the two. For example, `v1 = v2 = 0x3F800001` and `v3 = 0xBF800002` give `0x28800000` (2⁻⁴⁶) fused on RDNA2, and `0` with the multiply rounded first. ## What the fix does - `opcodes.rs` names the two opcodes as RDNA2 does: `v_fmac_legacy_f32` and `v_fma_legacy_f32`. - `lower.rs` lowers both with a new helper, `fma_legacy(a, b, c)`: `c` (as `0.0 + c`) when `a` or `b` is ±0, otherwise one `FMad(a, b, c)`. This is the same "the driver may fuse it" shortcut that `v_fma_f32` and `v_fmac_f32` already use, since DXIL has no fused f32 FMA. - The `v_mad_f32` / `v_madak_f32` / `v_madmk_f32` arm stays as it was, minus the legacy name. ## How it behaves after the fix Shaders using these opcodes translate as fused multiply-adds with the D3D9 zero rule, on hosts whose driver fuses `FMad`. Their results are then RDNA2's, bit for bit. A host that doesn't fuse `FMad` rounds twice, as it already does for `v_fma_f32`. I measured that on GitHub's runner: both its WARP device and its hardware device return `0` for the example above, with or without this change. ## Tests - `legacy_multiply_adds_are_the_fused_ones` (`gfx10/mod.rs`) decodes LLVM's two gfx1030 encodings above and checks the names. - `v_fma_legacy_f32_follows_the_d3d9_zero_rule` (`valu_tests.rs`) runs `v_fma_legacy_f32` (VOP3 `0x140`) on the host GPU and on WARP, with three cases: - `2 × 3 + 1 = 7`; - `0 × ∞ + 1.5 = 1.5`; - `NaN × 0 − 2 = −2`. I ran `cargo test --workspace` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** everything passes. `victory-shader` has 71 passed, the other crates have 0 failed. - **The new tests alone on `main`:** the decoder test fails: `left: "v_mad_legacy_f32"`, `right: "v_fma_legacy_f32"`. The zero-rule test passes on `main` too, since the old lowering also kept that rule. - I ran with `--test-threads=1`. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (#181, #244). **Not tested:** a GPU that fuses `FMad`, so the fused rounding itself isn't shown by a test here, and a title shader that uses these opcodes. Related: #200 adds match arms next to the ones this edits in `lower.rs`. Whichever merges second needs a trivial rebase.
The opcode tables come from Mesa's GFX10 column, which shares opcode
numbers between GFX10.1 and GFX10.3 and keeps the older names: VOP2
0x006 and VOP3 0x140 were v_mac_legacy_f32 and v_mad_legacy_f32. On
GFX10.3 (RDNA2) those opcodes are v_fmac_legacy_f32 and
v_fma_legacy_f32, fused multiply-adds with the D3D9 rule that a zero
factor gives a zero product (AMD's RDNA2 ISA; LLVM's gfx1030 tables and
assembler tests). They were lowered as a multiply rounded on its own,
then an add.

Both are now named for RDNA2 and lowered as one FMad (the driver may
fuse it, as for v_fma_f32), unless a factor is zero, when the result
is the addend.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:57:53 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:16 UTC · original

Tested PR #284 at head fa5189da66a91611c41fc1bdafd3675e15eb50f1 (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at fa5189da66a91611c41fc1bdafd3675e15eb50f1.

  • cargo test --locked -p victory-shader fma_legacy — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 70 filtered out; finished in 0.61s ); cases: emit::valu_tests::v_fma_legacy_f32_follows_the_d3d9_zero_rule.

Behavior covered: Checks legacy multiply-add lowering follows fused RDNA2/D3D9 behavior, including the signed-zero case.
Code: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/valu_tests.rs

**@⁠jeremygold02** on GitHub · 2026-11-24 04:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7121863075) Tested PR #284 at head `fa5189da66a91611c41fc1bdafd3675e15eb50f1` (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at `fa5189da66a91611c41fc1bdafd3675e15eb50f1`. - `cargo test --locked -p victory-shader fma_legacy` — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 70 filtered out; finished in 0.61s ); cases: emit::valu_tests::v_fma_legacy_f32_follows_the_d3d9_zero_rule. Behavior covered: Checks legacy multiply-add lowering follows fused RDNA2/D3D9 behavior, including the signed-zero case. Code: [crates/victory-shader/src/emit/lower.rs](https://github.com/Marvisak/VIctory/blob/fa5189da66a91611c41fc1bdafd3675e15eb50f1/crates/victory-shader/src/emit/lower.rs), [crates/victory-shader/src/emit/valu_tests.rs](https://github.com/Marvisak/VIctory/blob/fa5189da66a91611c41fc1bdafd3675e15eb50f1/crates/victory-shader/src/emit/valu_tests.rs)
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 05:15 UTC · original

Ran fa5189d merged onto main d2a4bae (clean) on NVIDIA. valu_tests runs each case on the hardware device and on WARP.

  • NVIDIA GeForce RTX 5070 12 GB (32-lane waves), driver 621.24; Windows 11 Pro 26300; Rust 1.98.1 (pinned) x86_64-pc-windows-msvc, debug
  • v_fma_legacy_f32_follows_the_d3d9_zero_rule: ok on both
  • cargo test -p victory-shader --lib -- --test-threads=1: 71 passed, 0 failed
  • cargo test --workspace -- --test-threads=1: 266 passed, 0 failed, 4 ignored

The test checks only the zero rule, so I also ran the rounding example from the description (v1 = v2 = 0x3F800001, v3 = 0xBF800002) locally through v_fma_legacy_f32, with a probe added to valu_tests (not committed):

main d2a4bae this PR
RTX 5070 0x0 0x28800000
WARP 0x0 0x0

So NVIDIA's driver contracts the fmad and gives the RDNA2 result with this change, while WARP does not fuse it. That is probably also why the case can't go into the test as an exact value: it would fail on WARP.

**@⁠marquezinii** on GitHub · 2026-11-24 05:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7122782093) Ran `fa5189d` merged onto `main` `d2a4bae` (clean) on NVIDIA. `valu_tests` runs each case on the hardware device and on WARP. - NVIDIA GeForce RTX 5070 12 GB (32-lane waves), driver 621.24; Windows 11 Pro 26300; Rust 1.98.1 (pinned) `x86_64-pc-windows-msvc`, debug - `v_fma_legacy_f32_follows_the_d3d9_zero_rule`: ok on both - `cargo test -p victory-shader --lib -- --test-threads=1`: 71 passed, 0 failed - `cargo test --workspace -- --test-threads=1`: 266 passed, 0 failed, 4 ignored The test checks only the zero rule, so I also ran the rounding example from the description (`v1 = v2 = 0x3F800001`, `v3 = 0xBF800002`) locally through `v_fma_legacy_f32`, with a probe added to `valu_tests` (not committed): | | `main` `d2a4bae` | this PR | |---|---|---| | RTX 5070 | `0x0` | `0x28800000` | | WARP | `0x0` | `0x0` | So NVIDIA's driver contracts the `fmad` and gives the RDNA2 result with this change, while WARP does not fuse it. That is probably also why the case can't go into the test as an exact value: it would fail on WARP.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 05:55 UTC · original

RDNA2 hardware measurement of both claims, on GFX10.3 silicon: v_fma_legacy_f32 and v_fmac_legacy_f32 are fused, and keep the zero-factor rule.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (GFX10.3 / RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr, which accepts both mnemonics for gfx1036) runs v_fma_legacy_f32 v10, v4, v5, v6 and v_fmac_legacy_f32 v11, v4, v5 (v11 = src2), with v_fma_f32 and v_mul_legacy_f32 alongside for reference. There are 2035 operand triples: 17 special values (±0, ±1, ±inf, ±NaN, max, min normal, the smallest denormal, 0x3f800001, 0xbf800002, ±1e30, 3, 0.1) in all pairs × six src2 values, plus 300 triples where src2 is minus the product rounded, so a fused result is the rounding error and an unfused one is about 0. Default mode (IEEE on, f32 denormals flushed). RDNA2 desktop silicon, not the console's. No game data.

a b c v_fma_legacy_f32 v_fmac_legacy_f32 v_fma_f32
0x3f800001 0x3f800001 0xbf800002 0x28800000 0x28800000 0x28800000
0x00000000 0x7f800000 0x3f800000 0x3f800000 0x3f800000 0xffc00000
0x7f800000 0x00000000 0x3f800000 0x3f800000 0x3f800000 0xffc00000
0x80000000 0x7f800000 0x3f800000 0x3f800000 0x3f800000 0xffc00000
0x00000000 0x7fc00000 0x3f800000 0x3f800000 0x3f800000 0x7fc00000
0x00000000 0x7f800000 0x80000000 0x00000000 0x00000000 0xffc00000
0x00000000 0x7f800000 0x7fc00000 0x7fc00000 0x7fc00000 0xffc00000
0x7f800000 0x3f800000 0x3f800000 0x7f800000 0x7f800000 0x7f800000

Model fit over all 2035 triples, for both opcodes:

  • Fused (one rounding of the exact a*b + c), with a zero factor giving a +0 product even against inf or NaN: 2035/2035.
  • Product rounded first, then added (the GFX10.1 v_mad_legacy_f32 / v_mac_legacy_f32 lowering on main): 1716/2035. It is wrong on your 0x28800000 example, which gives 0, and on all 300 residual triples.

So the GFX10.3 names and the fused lowering in this PR match the hardware. A zero factor makes the product +0: 0 × inf + (−0) is +0, and 0 × NaN + c is c.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

**@⁠Lyrnc** on GitHub · 2026-11-24 05:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7123407986) RDNA2 hardware measurement of both claims, on GFX10.3 silicon: **`v_fma_legacy_f32` and `v_fmac_legacy_f32` are fused, and keep the zero-factor rule.** **Setup.** AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (GFX10.3 / RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr, which accepts both mnemonics for gfx1036) runs `v_fma_legacy_f32 v10, v4, v5, v6` and `v_fmac_legacy_f32 v11, v4, v5` (v11 = src2), with `v_fma_f32` and `v_mul_legacy_f32` alongside for reference. There are 2035 operand triples: 17 special values (±0, ±1, ±inf, ±NaN, max, min normal, the smallest denormal, `0x3f800001`, `0xbf800002`, ±1e30, 3, 0.1) in all pairs × six src2 values, plus 300 triples where src2 is minus the product rounded, so a fused result is the rounding error and an unfused one is about 0. Default mode (IEEE on, f32 denormals flushed). RDNA2 desktop silicon, not the console's. No game data. | a | b | c | `v_fma_legacy_f32` | `v_fmac_legacy_f32` | `v_fma_f32` | |---|---|---|---|---|---| | `0x3f800001` | `0x3f800001` | `0xbf800002` | `0x28800000` | `0x28800000` | `0x28800000` | | `0x00000000` | `0x7f800000` | `0x3f800000` | `0x3f800000` | `0x3f800000` | `0xffc00000` | | `0x7f800000` | `0x00000000` | `0x3f800000` | `0x3f800000` | `0x3f800000` | `0xffc00000` | | `0x80000000` | `0x7f800000` | `0x3f800000` | `0x3f800000` | `0x3f800000` | `0xffc00000` | | `0x00000000` | `0x7fc00000` | `0x3f800000` | `0x3f800000` | `0x3f800000` | `0x7fc00000` | | `0x00000000` | `0x7f800000` | `0x80000000` | `0x00000000` | `0x00000000` | `0xffc00000` | | `0x00000000` | `0x7f800000` | `0x7fc00000` | `0x7fc00000` | `0x7fc00000` | `0xffc00000` | | `0x7f800000` | `0x3f800000` | `0x3f800000` | `0x7f800000` | `0x7f800000` | `0x7f800000` | Model fit over all 2035 triples, for both opcodes: - **Fused** (one rounding of the exact `a*b + c`), with a zero factor giving a +0 product even against inf or NaN: **2035/2035**. - Product rounded first, then added (the GFX10.1 `v_mad_legacy_f32` / `v_mac_legacy_f32` lowering on `main`): 1716/2035. It is wrong on your `0x28800000` example, which gives 0, and on all 300 residual triples. So the GFX10.3 names and the fused lowering in this PR match the hardware. A zero factor makes the product +0: `0 × inf + (−0)` is `+0`, and `0 × NaN + c` is `c`. AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:50 UTC · original

A follow-up on the GFX10.1-vs-GFX10.3 naming this PR fixes for 0x006 / 0x140: four more entries of the table are GFX10.1 opcodes that RDNA2 does not implement, and main lowers them as real multiply-adds.

Two checks on the gfx1036 iGPU from my comment above:

  1. AMD's assembler (the driver's amd_comgr) on every name in the VOP1/VOP2/VOP3/VOP3P tables for gfx1036. "Instruction not supported on this GPU" came back for exactly these:
Table entry Lowered by main Raw encoding run on gfx1036 (destination preset to 0xdeadbeef)
VOP2 0x006 v_mac_legacy_f32 yes v_fmac_legacy_f32 (this PR)
VOP3 0x140 v_mad_legacy_f32 yes v_fma_legacy_f32 (this PR)
VOP2 0x01f v_mac_f32 yes writes 0
VOP2 0x020 v_madmk_f32 (+ literal) yes writes 0
VOP2 0x021 v_madak_f32 (+ literal) yes writes 0
VOP3 0x141 v_mad_f32 yes writes 0
VOP3 0x16b v_mul_lo_i32 no (#92 adds it) writes 0
  1. For the five "writes 0" rows: three input sets (2·3+5, 1.5·(-4)+0.25, 1·1+1) give 0x00000000 every time, while v_fmac_f32, v_fmamk_f32 and v_fmaak_f32 in the same kernel give 11, 23, 16, …. A differential run of main's lowering against the hardware (220 inputs) shows the same for VOP3 0x141: the host computes a mad, the hardware writes 0 on all but the rows where the mad is 0 anyway.

If the console is GFX10.3, as the ray-tracing opcodes suggest, its compiler can't emit these five, so translating them as mads is harmless but wrong as a model. Refusing them, or naming them as not implemented, would match the hardware. I didn't touch the table myself, since this PR is already reworking those two names.

AI use: Claude Code (Claude Opus 5.5) ran the probes on the GPU above and drafted this comment; the numbers come from the hardware run.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7124339078) A follow-up on the GFX10.1-vs-GFX10.3 naming this PR fixes for `0x006` / `0x140`: **four more entries of the table are GFX10.1 opcodes that RDNA2 does not implement, and `main` lowers them as real multiply-adds.** Two checks on the gfx1036 iGPU from my comment above: 1. AMD's assembler (the driver's amd_comgr) on every name in the VOP1/VOP2/VOP3/VOP3P tables for `gfx1036`. "Instruction not supported on this GPU" came back for exactly these: | Table entry | Lowered by `main` | Raw encoding run on gfx1036 (destination preset to `0xdeadbeef`) | |---|---|---| | VOP2 `0x006` `v_mac_legacy_f32` | yes | `v_fmac_legacy_f32` (this PR) | | VOP3 `0x140` `v_mad_legacy_f32` | yes | `v_fma_legacy_f32` (this PR) | | VOP2 `0x01f` `v_mac_f32` | yes | writes `0` | | VOP2 `0x020` `v_madmk_f32` (+ literal) | yes | writes `0` | | VOP2 `0x021` `v_madak_f32` (+ literal) | yes | writes `0` | | VOP3 `0x141` `v_mad_f32` | yes | writes `0` | | VOP3 `0x16b` `v_mul_lo_i32` | no (#92 adds it) | writes `0` | 2. For the five "writes 0" rows: three input sets (`2·3+5`, `1.5·(-4)+0.25`, `1·1+1`) give `0x00000000` every time, while `v_fmac_f32`, `v_fmamk_f32` and `v_fmaak_f32` in the same kernel give 11, 23, 16, …. A differential run of `main`'s lowering against the hardware (220 inputs) shows the same for VOP3 `0x141`: the host computes a mad, the hardware writes 0 on all but the rows where the mad is 0 anyway. If the console is GFX10.3, as the ray-tracing opcodes suggest, its compiler can't emit these five, so translating them as mads is harmless but wrong as a model. Refusing them, or naming them as not implemented, would match the hardware. I didn't touch the table myself, since this PR is already reworking those two names. AI use: Claude Code (Claude Opus 5.5) ran the probes on the GPU above and drafted this comment; the numbers come from the hardware run.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:59 UTC · original

One more RDNA2 rule for these two opcodes, which the fused lowering here doesn't have yet: v_fma_legacy_f32 and v_fmac_legacy_f32 flush f32 denormals, inputs and result, whatever the FP32 denormal mode. v_fma_f32 and v_mul_legacy_f32 follow the mode.

Same gfx1036 setup as above. The 2035 triples from my first comment plus 15 with denormal operands or a denormal exact result, run under both f32 denormal modes:

Model denormals flushed (mode 0) denormals kept (mode 3)
fused, zero factor → +0, denormal inputs and result flushed to ±0 2050/2050 (both opcodes) 2050/2050 (both opcodes)
v_fma_f32 in the same kernel 0 denormal results 34 denormal results (so mode 3 was in effect)

Examples in mode 3 (denormals kept):

a b c v_fma_legacy_f32 v_fma_f32 v_mul_legacy_f32 (a·b)
0 3.0 0x00000100 0x00000000 0x00000100
0x00000100 2.0 0 0x00000000 0x00000200 0x00000200
2^-63 2^-64 0 0x00000000 0x00400000 0x00400000

What it does end to end: I ran a differential check of the translator against the hardware (the same raw VOP3 words through build_valu_cs on a host GPU, 220 inputs). On the iGPU as host, VOP3 0x140 differs from the hardware in 25 of 220 cases on main and in 52 with this PR. The 27 extra are all denormal operands that the host's fma keeps and the hardware flushes, plus one sNaN addend that the hardware quiets in IEEE mode. Flushing the three inputs and the result, for example by selecting ±0 where the exponent field is 0, would make the PR's lowering match on every row. With that, the earlier example (0x28800000) and the zero-factor rule are unchanged.

AI use: Claude Code (Claude Opus 5.5) ran the probes and the differential check on the GPUs above and drafted this comment; the numbers come from those runs.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7124493786) One more RDNA2 rule for these two opcodes, which the fused lowering here doesn't have yet: **`v_fma_legacy_f32` and `v_fmac_legacy_f32` flush f32 denormals, inputs and result, whatever the FP32 denormal mode.** `v_fma_f32` and `v_mul_legacy_f32` follow the mode. Same gfx1036 setup as above. The 2035 triples from my first comment plus 15 with denormal operands or a denormal exact result, run under both f32 denormal modes: | Model | denormals flushed (mode 0) | denormals kept (mode 3) | |---|---|---| | fused, zero factor → +0, **denormal inputs and result flushed to ±0** | 2050/2050 (both opcodes) | **2050/2050** (both opcodes) | | `v_fma_f32` in the same kernel | 0 denormal results | 34 denormal results (so mode 3 was in effect) | Examples in mode 3 (denormals kept): | a | b | c | `v_fma_legacy_f32` | `v_fma_f32` | `v_mul_legacy_f32` (a·b) | |---|---|---|---|---|---| | `0` | `3.0` | `0x00000100` | `0x00000000` | `0x00000100` | | | `0x00000100` | `2.0` | `0` | `0x00000000` | `0x00000200` | `0x00000200` | | `2^-63` | `2^-64` | `0` | `0x00000000` | `0x00400000` | `0x00400000` | What it does end to end: I ran a differential check of the translator against the hardware (the same raw VOP3 words through `build_valu_cs` on a host GPU, 220 inputs). On the iGPU as host, VOP3 `0x140` differs from the hardware in 25 of 220 cases on `main` and in **52 with this PR**. The 27 extra are all denormal operands that the host's `fma` keeps and the hardware flushes, plus one sNaN addend that the hardware quiets in IEEE mode. Flushing the three inputs and the result, for example by selecting ±0 where the exponent field is 0, would make the PR's lowering match on every row. With that, the earlier example (`0x28800000`) and the zero-factor rule are unchanged. AI use: Claude Code (Claude Opus 5.5) ran the probes and the differential check on the GPUs above and drafted this comment; the numbers come from those runs.
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 07:35 UTC · original

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for cargo test -p victory-shader --release (local adapter-selection patch, not part of this PR).

  • Whole victory-shader suite: 71 passed, 0 failed on both GPUs.

  • Negative control: with lower.rs/gfx10 reverted to main, v_fma_legacy_f32_follows_the_d3d9_zero_rule still passes on both GPUs: its three cases give the same result fused or unfused, so the test does not see the fusion.

  • I added locally a rounding-sensitive case, a = b = 0x3F800800 (1+2^-12), c = 0xBF801000 (-(1+2^-11)): fused gives 0x33800000 (2^-24), a rounded product gives 0.

    • this PR: 0x33800000 on the RX 9070 and on the RDNA2 iGPU (the driver fuses the FMad), but 0 on WARP;
    • main: 0 on the RX 9070.

    So the PR does fix the fused result on AMD hardware. A case like this one would make the test catch a regression, but it fails on WARP because WARP does not fuse FMad (the shortcut: comment), so it would need the hardware backend only or an explicit fma.

**@⁠slint6614-dev** on GitHub · 2026-11-24 07:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7125151879) AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and the Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the D3D12 device for `cargo test -p victory-shader --release` (local adapter-selection patch, not part of this PR). - Whole `victory-shader` suite: 71 passed, 0 failed on both GPUs. - Negative control: with `lower.rs`/`gfx10` reverted to main, `v_fma_legacy_f32_follows_the_d3d9_zero_rule` still **passes** on both GPUs: its three cases give the same result fused or unfused, so the test does not see the fusion. - I added locally a rounding-sensitive case, `a = b = 0x3F800800` (1+2^-12), `c = 0xBF801000` (-(1+2^-11)): fused gives `0x33800000` (2^-24), a rounded product gives 0. - this PR: `0x33800000` on the RX 9070 and on the RDNA2 iGPU (the driver fuses the FMad), but 0 on WARP; - main: 0 on the RX 9070. So the PR does fix the fused result on AMD hardware. A case like this one would make the test catch a regression, but it fails on WARP because WARP does not fuse FMad (the `shortcut:` comment), so it would need the hardware backend only or an explicit fma.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:29 UTC · original · edited 2026-11-25 22:15 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflict in crates/victory-shader/src/gfx10/opcodes.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: model 0x06/0x140 as fused legacy in semantics_tests.rs, flush f32 denormal inputs and result in fma_legacy, and add a test case that depends on fusion.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7148559707) · edited 2026-11-25 22:15 UTC <!-- victory-triage v1 head=fa5189da66a91611c41fc1bdafd3675e15eb50f1 verdict=needs-rebase at=2026-11-25T22:16:30Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main` (conflict in `crates/victory-shader/src/gfx10/opcodes.rs`). It will be reviewed again once it is rebased. From the earlier needs-work note, still open: model 0x06/0x140 as fused legacy in `semantics_tests.rs`, flush f32 denormal inputs and result in `fma_legacy`, and add a test case that depends on fusion. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 17:20 UTC · original

Test commit for the triage's point 3 (and rows for point 2): df8ac2e on branch ev-284-tests of upipibi/VIctory, on top of fa5189d, one file (emit/valu_tests.rs, +82). It also cherry-picks cleanly onto main 67e8ae1.

  • v_fma_legacy_f32_rounds_once_on_hardware: VOP3 0x140 and VOP2 0x006, 0x3F800001·0x3F800001 + 0xBF800002 → 0x28800000 and 0x3F800800·0x3F800800 + 0xBF801000 → 0x33800000. It runs on the hardware backend only, since WARP doesn't fuse the FMad.
  • v_fma_legacy_f32_flushes_denormals: the three mode-3 rows from @⁠Lyrnc's gfx1036 table (comment), on both backends.

RTX 3070, driver 616.56, Windows 11:

main 67e8ae1 PR fa5189d
fusion rows, 0x140 and 0x006 (hardware) all 0x00000000: fail 0x28800000 / 0x33800000: pass
denormal rows (hardware + WARP) 0x00000000: pass 0x00000000: pass

So the fusion test fails on main and passes on the PR. The denormal rows pass on both here, because the RTX 3070 and WARP flush f32 denormals in this shader themselves. They don't show point 2 on NVIDIA; they would only catch a missing flush on a host that keeps denormals (as in Lyrnc's AMD-host differential run). Point 1 (semantics_tests.rs) isn't touched.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠upipibi** on GitHub · 2026-11-25 17:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/284#issuecomment-7169369622) Test commit for the triage's point 3 (and rows for point 2): [`df8ac2e`](https://github.com/upipibi/VIctory/commit/df8ac2e) on branch `ev-284-tests` of upipibi/VIctory, on top of `fa5189d`, one file (`emit/valu_tests.rs`, +82). It also cherry-picks cleanly onto `main` `67e8ae1`. - `v_fma_legacy_f32_rounds_once_on_hardware`: VOP3 0x140 and VOP2 0x006, `0x3F800001·0x3F800001 + 0xBF800002 → 0x28800000` and `0x3F800800·0x3F800800 + 0xBF801000 → 0x33800000`. It runs on the hardware backend only, since WARP doesn't fuse the FMad. - `v_fma_legacy_f32_flushes_denormals`: the three mode-3 rows from @⁠Lyrnc's gfx1036 table ([comment](https://git.gtahub.st/github/Marvisak--VIctory/pulls/284)), on both backends. RTX 3070, driver 616.56, Windows 11: | | main `67e8ae1` | PR `fa5189d` | |---|---|---| | fusion rows, 0x140 and 0x006 (hardware) | all `0x00000000`: **fail** | `0x28800000` / `0x33800000`: pass | | denormal rows (hardware + WARP) | `0x00000000`: pass | `0x00000000`: pass | So the fusion test fails on main and passes on the PR. The denormal rows pass on both here, because the RTX 3070 and WARP flush f32 denormals in this shader themselves. They don't show point 2 on NVIDIA; they would only catch a missing flush on a host that keeps denormals (as in Lyrnc's AMD-host differential run). Point 1 (`semantics_tests.rs`) isn't touched. AI-assisted (Claude Code, Claude Opus 5.5).
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/gfx10/opcodes.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/284/head:github-bot/gh-284'
git switch 'github-bot/gh-284'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-284'
git switch 'github-bot/gh-284'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-284'
git switch 'github-bot/gh-284'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-284'
git switch 'main'
git merge --squash 'github-bot/gh-284'
git switch 'main'
git merge --ff-only 'github-bot/gh-284'
git switch 'main'
git merge 'github-bot/gh-284'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!284
No description provided.