D3D12-SHADER-RDNA-LEGACY-MAD: restore RDNA LEGACY lowering path in legacy_mad #2624

Merged
bobcatchris merged 1 commit from upstream-pr/2624 into main 2026-11-30 15:58:49 +00:00
bobcatchris commented 2026-11-29 18:50:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-29T18:50:19Z.


Summary

Restores the host_rdna() & super::rdna::LEGACY lowering path in legacy_mad (d3d12/crates/leonida-shader/src/emit/lower.rs), fixing the failing emit::valu_tests::rdna_forms_give_what_the_spelled_out_ones_give (mad_legacy: written alike) test on AMD RDNA hardware on main (4755667f5).

Root Cause

0cc6eee20 replaced v_mad_legacy_f32 / v_mac_legacy_f32 lowering with legacy_mad (subnormal flushing + fused fmad + zero-factor check), which unconditionally emitted the two-compare az | bz path and stopped consulting host_rdna() & super::rdna::LEGACY. On AMD RDNA cards (rdna_card() == true), rdna_forms_give_what_the_spelled_out_ones_give compares TEST_RDNA = 0 vs TEST_RDNA = rdna::ALL and panics on mad_legacy because both emit identical DXIL.

Fix

Mirror legacy_mul's host_rdna() & super::rdna::LEGACY path inside legacy_mad after flushing subnormals:

  • Skip the zero-factor select when orig_a == orig_b, both factors are bounded, or either factor is a finite normal constant.
  • Otherwise use a single fmin(|a|, |b|) == 0 compare (matching legacy_mul), preserving subnormal flushing and 0 * (inf|NaN) + addend = +0 + addend.

Verification

Windows 11 10.0.26200, Rust 1.98, AMD Radeon RX 7900 XTX (gfx1100, driver 32.0.32029.2010), no game files:

  • Red on main 4755667f5: cargo test -p leonida-shader --lib rdna_forms_give_what_the_spelled_out_ones_give -> FAILED (assertion left != right failed: mad_legacy: written alike).
  • Green on 3f027c6bc:
    • cargo test -p leonida-shader --lib rdna_forms_give_what_the_spelled_out_ones_give -> PASS (17.03 s, all 14 RDNA forms across 100 float/Inf/NaN/zero cases on RX 7900 XTX).
    • cargo test -p leonida-shader --lib mad_legacy_takes_a_subnormal_factor_as_a_zero -> PASS (0.12 s).
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/lower.rs -> clean.

AI-assisted: Antigravity (Gemini 4 Neon).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5553942282 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2624). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-29T18:50:19Z. --- ## Summary Restores the `host_rdna() & super::rdna::LEGACY` lowering path in `legacy_mad` (`d3d12/crates/leonida-shader/src/emit/lower.rs`), fixing the failing `emit::valu_tests::rdna_forms_give_what_the_spelled_out_ones_give` (`mad_legacy: written alike`) test on AMD RDNA hardware on `main` (`4755667f5`). - Claim: https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7260653026 ## Root Cause `0cc6eee20` replaced `v_mad_legacy_f32` / `v_mac_legacy_f32` lowering with `legacy_mad` (subnormal flushing + fused `fmad` + zero-factor check), which unconditionally emitted the two-compare `az | bz` path and stopped consulting `host_rdna() & super::rdna::LEGACY`. On AMD RDNA cards (`rdna_card() == true`), `rdna_forms_give_what_the_spelled_out_ones_give` compares `TEST_RDNA = 0` vs `TEST_RDNA = rdna::ALL` and panics on `mad_legacy` because both emit identical DXIL. ## Fix Mirror `legacy_mul`'s `host_rdna() & super::rdna::LEGACY` path inside `legacy_mad` after flushing subnormals: - Skip the zero-factor select when `orig_a == orig_b`, both factors are `bounded`, or either factor is a finite normal constant. - Otherwise use a single `fmin(|a|, |b|) == 0` compare (matching `legacy_mul`), preserving subnormal flushing and `0 * (inf|NaN) + addend = +0 + addend`. ## Verification Windows 11 `10.0.26200`, Rust `1.98`, AMD Radeon RX 7900 XTX (`gfx1100`, driver `32.0.32029.2010`), no game files: - **Red on `main` `4755667f5`**: `cargo test -p leonida-shader --lib rdna_forms_give_what_the_spelled_out_ones_give` -> `FAILED` (`assertion left != right failed: mad_legacy: written alike`). - **Green on `3f027c6bc`**: - `cargo test -p leonida-shader --lib rdna_forms_give_what_the_spelled_out_ones_give` -> `PASS` (17.03 s, all 14 RDNA forms across 100 float/Inf/NaN/zero cases on RX 7900 XTX). - `cargo test -p leonida-shader --lib mad_legacy_takes_a_subnormal_factor_as_a_zero` -> `PASS` (0.12 s). - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/lower.rs` -> clean. AI-assisted: Antigravity (Gemini 4 Neon).
merzleckeier commented 2026-11-29 18:58:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-29T18:58:26Z.


Independent AMD recheck of 3f027c6b on main 7c330c76 (merge tree b1c422ff). Windows x64 diag cross-build, CachyOS, Wine 11.22, VKD3D-Proton 71cae091, AMD Raphael iGPU / RADV Mesa 26.3.1.

  • rdna_forms_give_what_the_spelled_out_ones_give ran on hardware in both builds. Plain main reached the mad_legacy: written alike assertion and failed. The PR passed.
  • mad_legacy_takes_a_subnormal_factor_as_a_zero passed on both trees.
  • A separate synthetic bitwise probe covered 50 inputs across dynamic factors, ±1 inline constants, a repeated factor and a saturated factor. The generic and LEGACY paths gave identical bits on RADV. Forcing the test-only LEGACY mask on an RTX 4070 SUPER (620.73.04) changed 10/50 outputs from +0 to −0, all in the constant or bounded-factor cases. Production gates this path to AMD RDNA and the documented LEGACY mode permits zero-sign differences.

Synthetic shaders only. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7260904929 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2624#issuecomment-7260904929). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-29T18:58:26Z. --- Independent AMD recheck of `3f027c6b` on `main` `7c330c76` (merge tree `b1c422ff`). Windows x64 `diag` cross-build, CachyOS, Wine 11.22, VKD3D-Proton `71cae091`, AMD Raphael iGPU / RADV Mesa 26.3.1. - `rdna_forms_give_what_the_spelled_out_ones_give` ran on hardware in both builds. Plain `main` reached the `mad_legacy: written alike` assertion and failed. The PR passed. - `mad_legacy_takes_a_subnormal_factor_as_a_zero` passed on both trees. - A separate synthetic bitwise probe covered 50 inputs across dynamic factors, ±1 inline constants, a repeated factor and a saturated factor. The generic and `LEGACY` paths gave identical bits on RADV. Forcing the test-only `LEGACY` mask on an RTX 4070 SUPER (620.73.04) changed 10/50 outputs from `+0` to `−0`, all in the constant or bounded-factor cases. Production gates this path to AMD RDNA and the documented `LEGACY` mode permits zero-sign differences. Synthetic shaders only. AI-assisted with OpenAI Codex.
enlight3d commented 2026-11-30 10:47:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-30T10:47:09Z.


RUN (independent run, #2624 head 74a2fb5a3 on main e1fe82f63; merged locally with git merge --no-ff, no conflicts; also no conflict with #2566 head 70ea7d6ec per git merge-tree --write-tree). Main has since moved to 012b79a67, which changes only d3d12/docs/findings.md and d3d12/scripts/play-nvidia.ps1, so the crates tested are the same. It still merges cleanly there. The head is the rebase of 3f027c6bc, with the same change.

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with --locked:

  • cargo test -p leonida-shader --no-fail-fast: green, 385 passed / 0 failed / 4 ignored, as on main. The PR adds no test. mad_legacy_takes_a_subnormal_factor_as_a_zero passes (hardware and WARP).
  • rdna_forms_give_what_the_spelled_out_ones_give prints "no RDNA card; skipped" on this card and passes on main and on the merge alike, so the PR's own red does not show here.
  • cargo clippy -p leonida-shader --all-targets (without -D warnings, which already fails on main in leonida-shader), on main and on the merge: no new warning in lower.rs (2 already on main, the same 2 on the merge).
  • Red, behavioural, through a local probe (a test I added locally, not pushed). It writes v_mad_legacy_f32 with the RDNA forms off and on, as that test does, and makes the same assert_ne! on the two outputs, without the hardware run. On main it fails with "mad_legacy: written alike" (both forms 2,224 bytes, identical). On the merge it passes: the RDNA form is now 2,304 bytes. So the code-generation half of the claim holds.
  • Not checked: that the RDNA form gives the same numbers as the spelled-out one on an RDNA card (the hardware half of that test). That needs an AMD RDNA card.

Side effect worth a look: with the RDNA forms off, which is every other host and this NVIDIA card too, the output also changes. It is still 2,224 bytes, but its content differs between main and the merge (one run each). That matches the diff: the +0 + addend add now comes before the two compares instead of after them. The suite shows no change in results. Still, every program that uses v_mad_legacy_f32 or v_mac_legacy_f32 is now written differently on every host, so its translations and pipelines already in a cache are built again. Keeping the plain line inside each branch would leave the non-RDNA output identical to main's.

Perf: no (outside RDNA hosts, the same operations in another order).
No runtime/ C++ or Python in this PR, so no link or import check applies.
Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7276075296 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2624#issuecomment-7276075296). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-30T10:47:09Z. --- RUN (independent run, #2624 head 74a2fb5a3 on main e1fe82f63; merged locally with `git merge --no-ff`, no conflicts; also no conflict with #2566 head 70ea7d6ec per `git merge-tree --write-tree`). Main has since moved to 012b79a67, which changes only `d3d12/docs/findings.md` and `d3d12/scripts/play-nvidia.ps1`, so the crates tested are the same. It still merges cleanly there. The head is the rebase of 3f027c6bc, with the same change. Commands, in `d3d12/`, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with `--locked`: - `cargo test -p leonida-shader --no-fail-fast`: green, 385 passed / 0 failed / 4 ignored, as on main. The PR adds no test. `mad_legacy_takes_a_subnormal_factor_as_a_zero` passes (hardware and WARP). - `rdna_forms_give_what_the_spelled_out_ones_give` prints "no RDNA card; skipped" on this card and passes on main and on the merge alike, so the PR's own red does not show here. - `cargo clippy -p leonida-shader --all-targets` (without `-D warnings`, which already fails on main in leonida-shader), on main and on the merge: no new warning in `lower.rs` (2 already on main, the same 2 on the merge). - Red, behavioural, through a local probe (a test I added locally, not pushed). It writes `v_mad_legacy_f32` with the RDNA forms off and on, as that test does, and makes the same `assert_ne!` on the two outputs, without the hardware run. On main it fails with "mad_legacy: written alike" (both forms 2,224 bytes, identical). On the merge it passes: the RDNA form is now 2,304 bytes. So the code-generation half of the claim holds. - Not checked: that the RDNA form gives the same numbers as the spelled-out one on an RDNA card (the hardware half of that test). That needs an AMD RDNA card. Side effect worth a look: with the RDNA forms off, which is every other host and this NVIDIA card too, the output also changes. It is still 2,224 bytes, but its content differs between main and the merge (one run each). That matches the diff: the `+0 + addend` add now comes before the two compares instead of after them. The suite shows no change in results. Still, every program that uses `v_mad_legacy_f32` or `v_mac_legacy_f32` is now written differently on every host, so its translations and pipelines already in a cache are built again. Keeping the `plain` line inside each branch would leave the non-RDNA output identical to main's. Perf: no (outside RDNA hosts, the same operations in another order). No runtime/ C++ or Python in this PR, so no link or import check applies. Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40.
MoonEyes commented 2026-11-30 15:58:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T15:58:42Z.


Verified: independent AMD RDNA hardware check reproduced the main failure and passed with this change; CI and no-game-data checks pass. Ready for merge.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7283918726 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2624#issuecomment-7283918726). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T15:58:42Z. --- Verified: independent AMD RDNA hardware check reproduced the main failure and passed with this change; CI and no-game-data checks pass. Ready for merge.
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
ProjectLeonida/mandy!2624
No description provided.