xshader: guard f32 sub and f16 add/sub/mul against AMD host folding (fixes three #333 failures) #375

Closed
github-bot wants to merge 2 commits from gh-pr/375/head into gh-pr/375/base
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-24 18:44 UTC · original
SchmashinPrunes:amd-host-fold-guards → main · +80 −11 · 5 files · 2 commits · closed without merging

Library / area: tombox-xshader. hlsl.rs gets the tb_fsub guard and new tb_hadd/tb_hsub/tb_hmul helpers; tests/exec.rs gets the UAV view fix in run_res_psv.

Fixes the three #333 failures that reproduce on our AMD host, a Radeon RX 9060 XT with TOMBOX_EXEC_HW=1. They pass on WARP, and per #333 on an RTX 3070.

What was wrong

test AMD result cause
vop2_basic runtime inputs #642/643/707/708 v_sub_f32/v_subrev_f32 with ±inf − ±inf gives 0, want 0x7fc00000 Both runtime-input operands are the same expression (0x7f800000 | v200), so DXC's CSE merges them. The DXIL is fsub float %16, %16, and the precise in tb_fsub keeps fast-math flags off it, but AMD's driver still folds x - x to 0.
f16_omod_follows_mode runtime inputs #4 v_add_f16(-0, -0) gives +0 DXC lowers f16tof32(x) + f16tof32(x) to fmul fast x, 2.0, and AMD drops the zero sign. Compiling with -Gis does not help.
scalar_smem_loads #39–46 s_load_dword* descriptor probes read word 3 of the slot instead of word 9 (e.g. a5002323 vs a5002929) Harness bug: run_res_psv only recognised RWStructuredBuffer<TbElem_ubuf0>. These probes use ubuf1/ubuf2/ubuf6, which were therefore given typed R32_UINT views under a structured declaration.

The change

  • tb_fsub returns the quiet NaN from an integer test when either input is NaN or both are the same infinity. Otherwise it returns the precise difference. Finite inputs never produce NaN, so the old tb_qn on the result is no longer needed.

  • v_add_f16, v_sub_f16, v_subrev_f16 and v_mul_f16 now go through tb_hadd/tb_hsub/tb_hmul. When the f32 result is ±0, these OR in the IEEE round-to-nearest zero sign with integer ops:

    • add: −0 only when both operands are −0;
    • sub: −0 only for −0 − (+0);
    • mul: the XOR of the operand signs.

    An f16×f16 or f16±f16 result can't underflow to an f32 zero, so a zero result is always exact.

  • The harness picks the structured view stride from any TbElem_ubufN declaration, not just ubuf0.

Validation

  • RX 9060 XT, TOMBOX_EXEC_HW=1 cargo test --locked -p tombox-xshader --release --test exec: 128 passed, 0 failed. On main the same run fails scalar_smem_loads, vop2_basic and f16_omod_follows_mode.
  • WARP, cargo test --locked -p tombox-xshader --release --no-fail-fast: all pass.
  • isa_sweep on the RX 9060 XT: the first commit fixes the v_mul_f16 signed-zero case. The remaining sweep failures are handled in the second commit (below).
  • The upstream PR gate passes.

Second commit: the remaining AMD sweep failures

This comes from the RX 9060 XT isa_sweep run (TOMBOX_EXEC_HW=1). Failing mnemonics go from 11 to 3.

sweep failure on AMD cause change
v_trunc/floor/ceil/fract_f64 with VOP3 neg: trunc(−1.0) = +1.0, floor(−1.0) = 0 The helpers truncate through (double)(int64_t)x. The DXIL is correct (fsub fast -0.0, x then fptosi), but AMD's driver drops the folded negation in the f64→i64 conversion. tb_trunc64 clears the fraction bits with integer masks, with no int64 conversion. Checked bit for bit against math.trunc on 400k doubles: ±0, denormals, the 2^52 boundary, infinities.
v_fmaak/v_madak_f32 and v_fmaak_f16 with K = 0: −0·0 + 0 gives −0 AMD folds x + 0.0 to x, the same class as the x − x fold above. tb_pz forces an exact zero result to +0. It's also applied to v_add_f32 with an inline/literal +0 and to v_fma/v_mad_f32/v_fma_f16 with a +0 addend, before denorm flushing and output modifiers. For the fused f32 forms, tb_pzf applies it only when an operand is zero, since a product that underflows must still round to a signed zero.
v_ldexp_f16(−0, e) gives +0 Zero sign lost in ldexp. The result keeps the input's zero sign.

It also fixes a bug in this PR's first commit: tb_hz OR-ed the zero sign in, so it could never turn a folded −0 back into +0. It now sets the sign.

Tests.

  • New exec test amd_host_zero_sign_and_f64_neg: 48 cases with constant and runtime inputs, including controls where the guard must not fire (a neg-modified constant, K = −0, a fused underflow).
  • On the RX 9060 XT without the hlsl.rs changes, 23 runtime-input cases fail. With them, the test passes there and on WARP.
  • WARP now matches the reference for v_ceil/floor/trunc/fract_f64, v_madak_f32 and v_fmaak_f16, so they're removed from WARP_KNOWN in isa_sweep.rs and CI guards them.

Still failing on AMD, not addressed: v_add/sub/mul_f16 with SDWA byte select plus abs on src0. The operand reduces to and i32 %x, 255 before legacyF16ToF32, so it is provably a positive f16 denormal, and AMD returns 0 for it in the sweep's compute shader. The same pattern in the exec harness's pixel shader passes, the neg variant passes, and forcing -denorm preserve or -denorm any makes no difference. A real compiler wouldn't emit SDWA byte select on an f16 operand, so I've left it.

Notes

  • The fourth #333 test, mtbuf_d16_store_formats, passes on the RX 9060 XT, so this PR doesn't touch it.
  • #346 edits the same if line in run_res_psv. It's a one-line textual conflict, and I'll rebase whichever lands second. #347 and #368 merge cleanly with this branch.

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 18:44 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/375) `SchmashinPrunes:amd-host-fold-guards` → `main` · +80 −11 · 5 files · 2 commits · closed without merging **Library / area:** tombox-xshader. `hlsl.rs` gets the `tb_fsub` guard and new `tb_hadd`/`tb_hsub`/`tb_hmul` helpers; `tests/exec.rs` gets the UAV view fix in `run_res_psv`. Fixes the three #333 failures that reproduce on our AMD host, a Radeon RX 9060 XT with `TOMBOX_EXEC_HW=1`. They pass on WARP, and per #333 on an RTX 3070. ### What was wrong | test | AMD result | cause | |---|---|---| | `vop2_basic` runtime inputs #642/643/707/708 | `v_sub_f32`/`v_subrev_f32` with ±inf − ±inf gives `0`, want `0x7fc00000` | Both runtime-input operands are the same expression (`0x7f800000 \| v200`), so DXC's CSE merges them. The DXIL is `fsub float %16, %16`, and the `precise` in `tb_fsub` keeps fast-math flags off it, but AMD's driver still folds `x - x` to 0. | | `f16_omod_follows_mode` runtime inputs #4 | `v_add_f16(-0, -0)` gives `+0` | DXC lowers `f16tof32(x) + f16tof32(x)` to `fmul fast x, 2.0`, and AMD drops the zero sign. Compiling with `-Gis` does not help. | | `scalar_smem_loads` #39–46 | `s_load_dword*` descriptor probes read word 3 of the slot instead of word 9 (e.g. `a5002323` vs `a5002929`) | Harness bug: `run_res_psv` only recognised `RWStructuredBuffer<TbElem_ubuf0>`. These probes use `ubuf1`/`ubuf2`/`ubuf6`, which were therefore given typed `R32_UINT` views under a structured declaration. | ### The change - `tb_fsub` returns the quiet NaN from an integer test when either input is NaN or both are the same infinity. Otherwise it returns the `precise` difference. Finite inputs never produce NaN, so the old `tb_qn` on the result is no longer needed. - `v_add_f16`, `v_sub_f16`, `v_subrev_f16` and `v_mul_f16` now go through `tb_hadd`/`tb_hsub`/`tb_hmul`. When the f32 result is ±0, these OR in the IEEE round-to-nearest zero sign with integer ops: - add: −0 only when both operands are −0; - sub: −0 only for −0 − (+0); - mul: the XOR of the operand signs. An f16×f16 or f16±f16 result can't underflow to an f32 zero, so a zero result is always exact. - The harness picks the structured view stride from any `TbElem_ubufN` declaration, not just `ubuf0`. ### Validation - RX 9060 XT, `TOMBOX_EXEC_HW=1 cargo test --locked -p tombox-xshader --release --test exec`: 128 passed, 0 failed. On `main` the same run fails `scalar_smem_loads`, `vop2_basic` and `f16_omod_follows_mode`. - WARP, `cargo test --locked -p tombox-xshader --release --no-fail-fast`: all pass. - `isa_sweep` on the RX 9060 XT: the first commit fixes the `v_mul_f16` signed-zero case. The remaining sweep failures are handled in the second commit (below). - The upstream PR gate passes. ### Second commit: the remaining AMD sweep failures This comes from the RX 9060 XT `isa_sweep` run (`TOMBOX_EXEC_HW=1`). Failing mnemonics go from 11 to 3. | sweep failure on AMD | cause | change | |---|---|---| | `v_trunc/floor/ceil/fract_f64` with VOP3 `neg`: trunc(−1.0) = +1.0, floor(−1.0) = 0 | The helpers truncate through `(double)(int64_t)x`. The DXIL is correct (`fsub fast -0.0, x` then `fptosi`), but AMD's driver drops the folded negation in the f64→i64 conversion. | `tb_trunc64` clears the fraction bits with integer masks, with no int64 conversion. Checked bit for bit against `math.trunc` on 400k doubles: ±0, denormals, the 2^52 boundary, infinities. | | `v_fmaak/v_madak_f32` and `v_fmaak_f16` with K = 0: −0·0 + 0 gives −0 | AMD folds `x + 0.0` to `x`, the same class as the `x − x` fold above. | `tb_pz` forces an exact zero result to +0. It's also applied to `v_add_f32` with an inline/literal +0 and to `v_fma/v_mad_f32`/`v_fma_f16` with a +0 addend, before denorm flushing and output modifiers. For the fused f32 forms, `tb_pzf` applies it only when an operand is zero, since a product that underflows must still round to a signed zero. | | `v_ldexp_f16(−0, e)` gives +0 | Zero sign lost in `ldexp`. | The result keeps the input's zero sign. | It also fixes a bug in this PR's first commit: `tb_hz` OR-ed the zero sign in, so it could never turn a folded −0 back into +0. It now sets the sign. **Tests.** - New exec test `amd_host_zero_sign_and_f64_neg`: 48 cases with constant and runtime inputs, including controls where the guard must not fire (a `neg`-modified constant, K = −0, a fused underflow). - On the RX 9060 XT without the `hlsl.rs` changes, 23 runtime-input cases fail. With them, the test passes there and on WARP. - WARP now matches the reference for `v_ceil/floor/trunc/fract_f64`, `v_madak_f32` and `v_fmaak_f16`, so they're removed from `WARP_KNOWN` in `isa_sweep.rs` and CI guards them. **Still failing on AMD, not addressed:** `v_add/sub/mul_f16` with SDWA byte select plus `abs` on src0. The operand reduces to `and i32 %x, 255` before `legacyF16ToF32`, so it is provably a positive f16 denormal, and AMD returns 0 for it in the sweep's compute shader. The same pattern in the exec harness's pixel shader passes, the `neg` variant passes, and forcing `-denorm preserve` or `-denorm any` makes no difference. A real compiler wouldn't emit SDWA byte select on an f16 operand, so I've left it. ### Notes - The fourth #333 test, `mtbuf_d16_store_formats`, passes on the RX 9060 XT, so this PR doesn't touch it. - #346 edits the same `if` line in `run_res_psv`. It's a one-line textual conflict, and I'll rebase whichever lands second. #347 and #368 merge cleanly with this branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On an AMD host (RX 9060 XT, TOMBOX_EXEC_HW=1) three exec tests from #333 fail. They pass on WARP and NVIDIA.

- v_sub_f32/v_subrev_f32 inf - inf returned 0. The runtime-input operands are identical expressions, so the DXIL is `fsub float %x, %x` with no fast-math flags. AMD's driver still folds it to 0. tb_fsub now returns the quiet NaN from an integer test: either input is NaN, or both are the same infinity.
- v_add_f16(-0, -0) returned +0. DXC lowers `x + x` to `fmul fast x, 2.0` and AMD drops the zero sign. -Gis does not help. v_add/sub/subrev/mul_f16 now go through tb_hadd/tb_hsub/tb_hmul, which restore the IEEE zero sign with integer ops when the result is zero.
- s_load_dword* descriptor-from-table probes read the wrong word. The exec harness only created a structured UAV view for ubuf0. Slots 1, 2 and 6 got typed R32_UINT views under a RWStructuredBuffer declaration. It now matches any TbElem_ubufN declaration.

With this change the full exec suite passes on the RX 9060 XT (128 passed) and on WARP.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Follow-up to the previous commit, from the isa_sweep run on an RX 9060 XT (TOMBOX_EXEC_HW=1). Hardware sweep failures drop from 11 mnemonics to 3.

- v_trunc/floor/ceil/fract_f64 with the VOP3 neg modifier: AMD returns e.g. trunc(-1.0) = +1.0, floor(-1.0) = 0. The helpers truncated through (double)(int64_t)x, and AMD's driver drops a folded fneg in that conversion. tb_trunc64 now clears the fraction bits with integer masks, with no int64 conversion. Checked against math.trunc on 400k doubles.
- v_add_f32 with an inline/literal +0, v_fma/v_mad_f32 with a +0 addend, and v_fmaak/v_madak_f32/v_fmaak_f16 with K = 0: AMD folds x + 0.0 to x, so -0 stays -0. tb_pz forces an exact zero result to +0, ahead of denorm flushing and output modifiers. For the fused forms, tb_pzf does this only when an operand is zero; a product that underflows still rounds to a signed zero.
- v_ldexp_f16(-0, e) returned +0. The result now keeps the input's zero sign.
- tb_hz, from the previous commit, now sets the zero sign instead of OR-ing it in, so a folded -0 + +0 also becomes +0.
- New exec test amd_host_zero_sign_and_f64_neg, 48 cases with constant and runtime inputs. On the RX 9060 XT, 23 runtime-input cases fail without the hlsl.rs changes. All cases pass with them, and on WARP.
- WARP now matches the reference for v_ceil/floor/trunc/fract_f64, v_madak_f32 and v_fmaak_f16. They come off WARP_KNOWN so CI guards them.

Still failing on AMD: v_add/sub/mul_f16 with SDWA byte select plus abs on src0. The operand is provably a positive f16 denormal, and AMD returns 0 for it in a compute shader. A pixel shader with the same pattern passes. It is independent of -denorm preserve/any. Not addressed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:20:44 +00:00
github-bot closed this pull request 2026-11-26 06:20:44 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:50 UTC · original

Applied on main in d0fabcb (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:50 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/375#issuecomment-7164379983) Applied on main in d0fabcb (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.