tombox-xshader: VOP2 0x36-0x38 are the fused f16 forms on GFX10 (v_fmac_f16, v_fmamk_f16, v_fmaak_f16) #45

Closed
opened 2026-11-26 06:08:17 +00:00 by github-bot · 2 comments
Owner

@⁠realkodr on GitHub · 2026-11-21 19:14 UTC · original

rdna2/mnem.rs names VOP2 opcodes 0x36, 0x37 and 0x38 v_mac_f16, v_madmk_f16 and v_madak_f16. LLVM's AMDGPU backend has these three opcodes as the fused forms on GFX10. The unfused names exist only on GFX8/9, at other opcodes.

Evidence

llvm/lib/Target/AMDGPU/VOP2Instructions.td at LLVM commit 901520f:

Instruction GFX10 encoding GFX8/9 encoding
V_FMAC_F16, V_FMAMK_F16, V_FMAAK_F16 VOP2_Real_gfx10<0x036>, <0x037>, <0x038> (lines 2401-2403) none
V_MAC_F16, V_MADMK_F16, V_MADAK_F16 none _vi<0x23>, <0x24>, <0x25> (lines 2819-2821)

LLVM's assembler tests agree. From llvm/test/MC/AMDGPU/gfx10_asm_vop2.s:

v_fmac_f16_e32 v5, v1, v2 ; encoding: [0x01,0x05,0x0a,0x6c]

0x6c0a0501 >> 25 is 0x36.

I found this by running the decoder over every GFX10 encoding in LLVM's MC tests (40,984 vectors, PR to follow). These are the only three names that disagree with LLVM across all of GFX10. Opcodes 0x06 and 0x140 also differ, but only between gfx10.1 and gfx10.3, so I left those alone.

Why it matters

The translator has two paths for these opcodes, and they give different results:

  • hlsl.rs, the default path: tb_hz(a * b) + k in f32, then one conversion to f16. Apart from flushing the product, this behaves like a fused operation.
  • hlsl/mode.rs (mode_own, taken when a shader changes the half/double rounding or denormal mode): tb_h_mad rounds the product to f16 with tb_h_mul, then rounds again in tb_h_add. This is unfused.

An input that separates fused from unfused (exact arithmetic, round to nearest even):

a = b = 0x3e01 (1.5009765625)     c = 0xc080 (-2.25)
exact a*b           = 2.2529306...
a*b rounded to f16  = 0x4082 (2.25390625)

fused:          a*b + c = 0x1a00 (0.0029296875)
product first:  a*b + c = 0x1c00 (0.00390625)

So v_fmac_f16 v0, v1, v2 with v1 = v2 = 0x3e01 and v0 = 0xc080 leaves 0x1a00 in v0[15:0] if the hardware fuses, and 0x1c00 if it does not.

Not checked

Nothing here was executed. I have no RDNA2 hardware and no Windows machine, so this comes from reading the translator and LLVM. The rdna2-hwprobe tool from #41 could settle it with the input above.

If the hardware agrees with LLVM, the fix is to rename the three opcodes and route them through tb_h_fma, which v_fma_mixlo_f16 already uses. I can send that once someone confirms the measurement.

**@⁠realkodr** on GitHub · 2026-11-21 19:14 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/45) `rdna2/mnem.rs` names VOP2 opcodes 0x36, 0x37 and 0x38 `v_mac_f16`, `v_madmk_f16` and `v_madak_f16`. LLVM's AMDGPU backend has these three opcodes as the **fused** forms on GFX10. The unfused names exist only on GFX8/9, at other opcodes. ## Evidence `llvm/lib/Target/AMDGPU/VOP2Instructions.td` at LLVM commit `901520f`: | Instruction | GFX10 encoding | GFX8/9 encoding | |---|---|---| | `V_FMAC_F16`, `V_FMAMK_F16`, `V_FMAAK_F16` | `VOP2_Real_gfx10<0x036>`, `<0x037>`, `<0x038>` (lines 2401-2403) | none | | `V_MAC_F16`, `V_MADMK_F16`, `V_MADAK_F16` | none | `_vi<0x23>`, `<0x24>`, `<0x25>` (lines 2819-2821) | LLVM's assembler tests agree. From `llvm/test/MC/AMDGPU/gfx10_asm_vop2.s`: ``` v_fmac_f16_e32 v5, v1, v2 ; encoding: [0x01,0x05,0x0a,0x6c] ``` `0x6c0a0501 >> 25` is 0x36. I found this by running the decoder over every GFX10 encoding in LLVM's MC tests (40,984 vectors, PR to follow). These are the only three names that disagree with LLVM across all of GFX10. Opcodes 0x06 and 0x140 also differ, but only between gfx10.1 and gfx10.3, so I left those alone. ## Why it matters The translator has two paths for these opcodes, and they give different results: - `hlsl.rs`, the default path: `tb_hz(a * b) + k` in f32, then one conversion to f16. Apart from flushing the product, this behaves like a fused operation. - `hlsl/mode.rs` (`mode_own`, taken when a shader changes the half/double rounding or denormal mode): `tb_h_mad` rounds the product to f16 with `tb_h_mul`, then rounds again in `tb_h_add`. This is unfused. An input that separates fused from unfused (exact arithmetic, round to nearest even): ``` a = b = 0x3e01 (1.5009765625) c = 0xc080 (-2.25) exact a*b = 2.2529306... a*b rounded to f16 = 0x4082 (2.25390625) fused: a*b + c = 0x1a00 (0.0029296875) product first: a*b + c = 0x1c00 (0.00390625) ``` So `v_fmac_f16 v0, v1, v2` with v1 = v2 = 0x3e01 and v0 = 0xc080 leaves 0x1a00 in v0[15:0] if the hardware fuses, and 0x1c00 if it does not. ## Not checked Nothing here was executed. I have no RDNA2 hardware and no Windows machine, so this comes from reading the translator and LLVM. The `rdna2-hwprobe` tool from #41 could settle it with the input above. If the hardware agrees with LLVM, the fix is to rename the three opcodes and route them through `tb_h_fma`, which `v_fma_mixlo_f16` already uses. I can send that once someone confirms the measurement.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:08:18 +00:00
Author
Owner

@⁠realkodr on GitHub · 2026-11-21 21:10 UTC · original

#49 covers this, so nobody needs to pick it up separately. It renames VOP2 0x36-0x38 to v_fmac_f16 / v_fmamk_f16 / v_fmaak_f16, sends them down the fused path, and adds exec cases where fused and unfused results differ. It also handles the VOP3 form at 0x136, which I missed here.

As of 1159c96 main still has the old names, so this stays open until #49 (or an equivalent) lands. I won't send a competing PR.

**@⁠realkodr** on GitHub · 2026-11-21 21:10 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/45#issuecomment-7069287868) #49 covers this, so nobody needs to pick it up separately. It renames VOP2 0x36-0x38 to `v_fmac_f16` / `v_fmamk_f16` / `v_fmaak_f16`, sends them down the fused path, and adds exec cases where fused and unfused results differ. It also handles the VOP3 form at 0x136, which I missed here. As of `1159c96` main still has the old names, so this stays open until #49 (or an equivalent) lands. I won't send a competing PR.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:20 UTC · original

Fixed on main in b72625c: VOP2 0x36-0x38 are v_fmac_f16 / v_fmamk_f16 / v_fmaak_f16 (mnem.rs, hlsl.rs, mode.rs), with fused lowering. Thanks!

**@⁠omonkeyz** on GitHub · 2026-11-22 13:20 UTC · [original](https://github.com/omonkeyz/xbox-emulator/issues/45#issuecomment-7079737055) Fixed on main in b72625c: VOP2 0x36-0x38 are v_fmac_f16 / v_fmamk_f16 / v_fmaak_f16 (mnem.rs, hlsl.rs, mode.rs), with fused lowering. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.