shader: VOP3 0x140 and VOP2 0x006 are RDNA2's fused legacy multiply-adds (#321) #323

Merged
gagarinyury merged 2 commits from upstream-pr/323 into main 2026-11-27 13:05:59 +00:00
gagarinyury commented 2026-11-27 11:31:19 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T11:31:19Z.


What this changes

Refs #321

VOP3 0x140 and VOP2 0x006 were named and lowered as GFX10.1's v_mad_legacy_f32 and v_mac_legacy_f32 (legacy product, rounded, then the add). On GFX10.3 (RDNA2) the same encodings are v_fma_legacy_f32 and v_fmac_legacy_f32, one rounding: llvm-mc -mcpu=gfx1030 assembles v_fma_legacy_f32 to the bytes gfx1010 gives v_mad_legacy_f32 and refuses the latter; gfx1010 and gfx1013 do the opposite.

  • gfx10/opcodes.rs: the two names.
  • emit/lower.rs: legacy_fma makes both factors 0 where either is 0 (DX9's rule: a zero factor gives +0 against infinity and NaN too), then fmad, as v_fma_f32 already uses. v_mad_f32 and the plain v_mac_f32 are unchanged.
  • semantics_tests.rs (Windows): fma_legacy is the console's result for both opcodes, with the unfused one still allowed, as for v_fma_f32 and v_fmac_f32.
  • docs/native-host.md: a short section.

How it was tested

  • legacy_fma_tests::the_legacy_multiply_adds_round_once_and_keep_dx9_zeros (victory-gpu, Metal, Metal Shader Converter 4.0 beta 3): (1 + 2^-12)^2 - 1 is 0x3a000400 from v_fma_f32, v_fma_legacy_f32 and v_fmac_legacy_f32 (on main the legacy two give 0x3a000000, the product rounded first), and v_fma_legacy_f32 of 0 and infinity plus 1.0 is 1.0. The test fails on main.
  • cargo test -p victory-shader (330 passed), cargo clippy --no-deps -p victory-shader --all-targets -- -D warnings for the host and for x86_64-pc-windows-msvc (the semantics tests only build on Windows: type-checked, not run), cargo xtask policy.
  • Not run here: the semantics probes themselves (Windows) and the HLE/loader tests, which this does not touch. No game run.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none: the encodings are LLVM's, the values measured on Metal)
  • New diagnostics are behind a VICTORY_* variable and documented (none added)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5525891618 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/323). Originally posted by [gagarinyury](https://github.com/gagarinyury) on 2026-11-27T11:31:19Z. --- ## What this changes Refs #321 VOP3 0x140 and VOP2 0x006 were named and lowered as GFX10.1's `v_mad_legacy_f32` and `v_mac_legacy_f32` (legacy product, rounded, then the add). On GFX10.3 (RDNA2) the same encodings are `v_fma_legacy_f32` and `v_fmac_legacy_f32`, one rounding: `llvm-mc -mcpu=gfx1030` assembles `v_fma_legacy_f32` to the bytes gfx1010 gives `v_mad_legacy_f32` and refuses the latter; gfx1010 and gfx1013 do the opposite. - `gfx10/opcodes.rs`: the two names. - `emit/lower.rs`: `legacy_fma` makes both factors 0 where either is 0 (DX9's rule: a zero factor gives +0 against infinity and NaN too), then `fmad`, as `v_fma_f32` already uses. `v_mad_f32` and the plain `v_mac_f32` are unchanged. - `semantics_tests.rs` (Windows): `fma_legacy` is the console's result for both opcodes, with the unfused one still allowed, as for `v_fma_f32` and `v_fmac_f32`. - docs/native-host.md: a short section. ## How it was tested - `legacy_fma_tests::the_legacy_multiply_adds_round_once_and_keep_dx9_zeros` (victory-gpu, Metal, Metal Shader Converter 4.0 beta 3): (1 + 2^-12)^2 - 1 is 0x3a000400 from `v_fma_f32`, `v_fma_legacy_f32` and `v_fmac_legacy_f32` (on main the legacy two give 0x3a000000, the product rounded first), and `v_fma_legacy_f32` of 0 and infinity plus 1.0 is 1.0. The test fails on main. - `cargo test -p victory-shader` (330 passed), `cargo clippy --no-deps -p victory-shader --all-targets -- -D warnings` for the host and for `x86_64-pc-windows-msvc` (the semantics tests only build on Windows: type-checked, not run), `cargo xtask policy`. - Not run here: the semantics probes themselves (Windows) and the HLE/loader tests, which this does not touch. No game run. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none: the encodings are LLVM's, the values measured on Metal) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (none added)
LakshanSivakumar commented 2026-11-27 12:00:38 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T12:00:38Z.


Thank you. The point that RDNA2 gives these two encodings the fused names, with the llvm-mc output, is clear and the value test is a good one.

I ran every shader by output on 0fe4ab0a (33,625 containers, against main c1b95875): nothing gained, nothing lost, nothing changed. So no shader in my dump of the game uses VOP3 0x140 or VOP2 0x006, and this has no effect on the game today. If your dump has shaders that use them, please say how many.

Before I merge: I have approved your workflow, so please check it is green. The doc line for legacy_mul (0 × anything = 0, as in D3D9) is now above legacy_fma; please move it back. And does the console flush a denormal input to zero for these two? Your semantics probe uses a flush, the lowering does not. Say which you know and which you guess.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7217730290 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/323#issuecomment-7217730290). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T12:00:38Z. --- Thank you. The point that RDNA2 gives these two encodings the fused names, with the `llvm-mc` output, is clear and the value test is a good one. I ran every shader by output on 0fe4ab0a (33,625 containers, against main c1b95875): nothing gained, nothing lost, nothing changed. So no shader in my dump of the game uses VOP3 0x140 or VOP2 0x006, and this has no effect on the game today. If your dump has shaders that use them, please say how many. Before I merge: I have approved your workflow, so please check it is green. The doc line for `legacy_mul` (`0 × anything = 0, as in D3D9`) is now above `legacy_fma`; please move it back. And does the console flush a denormal input to zero for these two? Your semantics probe uses a flush, the lowering does not. Say which you know and which you guess.
gagarinyury commented 2026-11-27 12:06:22 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T12:06:22Z.


Workflow is green (0fe4ab0). Moved the legacy_mul doc line back (132294b). I have no dump of the game, so I cannot count shaders that use these opcodes. On denormals, known: the semantics model reads a denormal factor of v_mul_legacy_f32 as 0, measured on RDNA2 by the Leonida project as its comment says. Guessed: I gave fma_legacy the same rule in the model; nothing I have measures v_fma_legacy_f32 with a denormal. The lowering does not flush, and did not before: measured on Metal now, v_mul_legacy_f32 and v_fma_legacy_f32 of the smallest denormal and infinity both give infinity (the model says 0 for the multiply), and 0x400000 times 2.0 gives 0x800000, so the host keeps denormals. legacy_fma therefore does what legacy_mul already does; reading a denormal factor as 0 in both would change the title's most used instruction, so I would make it a separate pull request if you want it.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7217845683 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/323#issuecomment-7217845683). Originally posted by [gagarinyury](https://github.com/gagarinyury) on 2026-11-27T12:06:22Z. --- Workflow is green (0fe4ab0). Moved the `legacy_mul` doc line back (132294b). I have no dump of the game, so I cannot count shaders that use these opcodes. On denormals, known: the semantics model reads a denormal factor of `v_mul_legacy_f32` as 0, measured on RDNA2 by the Leonida project as its comment says. Guessed: I gave `fma_legacy` the same rule in the model; nothing I have measures `v_fma_legacy_f32` with a denormal. The lowering does not flush, and did not before: measured on Metal now, `v_mul_legacy_f32` and `v_fma_legacy_f32` of the smallest denormal and infinity both give infinity (the model says 0 for the multiply), and 0x400000 times 2.0 gives 0x800000, so the host keeps denormals. `legacy_fma` therefore does what `legacy_mul` already does; reading a denormal factor as 0 in both would change the title's most used instruction, so I would make it a separate pull request if you want it.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
LakshanSivakumar/Native-Victory-macOS!323
No description provided.