Lower GFX7 legacy min and max with a compare #140

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

@⁠Yeetbot159 on GitHub · 2026-11-22 15:15 UTC · original
Yeetbot159:yeet/legacy-minmax → main · +186 −2 · 2 files · 2 commits · closed without merging

Summary

  • GFX7 v_min_legacy_f32 and v_max_legacy_f32 are not HLSL min and max. The HLSL functions return the other operand when one input is NaN. The legacy ops return source 1 when the compare is false, which is what happens when source 1 is NaN.
  • Min is src0 < src1 ? src0 : src1. Max is src0 > src1 ? src0 : src1, except a tie of +0 and -0 returns source 0. The compare is false for NaN, so source 1 is kept.
  • VCC is saved in s104:s105 and restored. Max also uses v254 and v255 for the signed-zero test. A VOP3 form with abs, neg, clamp or an output modifier is refused, because dropping the modifier would select the original bits. A VOP3 literal source is refused.

Test plan

  • cargo test -p tombox-xshader --lib gcn::tests passed (22 tests).
  • legacy_min_returns_source1_when_source1_is_nan checks the compare, the VOP3 form, a relocated branch, and that a modifier is an error.
  • legacy_max_tie_of_signed_zero_keeps_source0 checks the magnitude test and a NaN literal in source 0.
  • legacy_minmax_refuses_to_clobber_a_live_scratch_register checks that an instruction already using s104 is rejected.
**@⁠Yeetbot159** on GitHub · 2026-11-22 15:15 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/140) `Yeetbot159:yeet/legacy-minmax` → `main` · +186 −2 · 2 files · 2 commits · closed without merging ## Summary - GFX7 `v_min_legacy_f32` and `v_max_legacy_f32` are not HLSL `min` and `max`. The HLSL functions return the other operand when one input is NaN. The legacy ops return source 1 when the compare is false, which is what happens when source 1 is NaN. - Min is `src0 < src1 ? src0 : src1`. Max is `src0 > src1 ? src0 : src1`, except a tie of +0 and -0 returns source 0. The compare is false for NaN, so source 1 is kept. - VCC is saved in s104:s105 and restored. Max also uses v254 and v255 for the signed-zero test. A VOP3 form with abs, neg, clamp or an output modifier is refused, because dropping the modifier would select the original bits. A VOP3 literal source is refused. ## Test plan - `cargo test -p tombox-xshader --lib gcn::tests` passed (22 tests). - `legacy_min_returns_source1_when_source1_is_nan` checks the compare, the VOP3 form, a relocated branch, and that a modifier is an error. - `legacy_max_tie_of_signed_zero_keeps_source0` checks the magnitude test and a NaN literal in source 0. - `legacy_minmax_refuses_to_clobber_a_live_scratch_register` checks that an instruction already using s104 is rejected.
HLSL min and max return the other operand when one input is NaN, which is the opposite of v_min_legacy_f32 and v_max_legacy_f32. A VOP3 encoding with a modifier is refused rather than dropped.

Co-authored-by: Cursor <[email protected]>
A greater-or-equal compare is false for NaN and true for a +0/-0 tie, so the magnitude test and the vcc save are unnecessary. The select reads that pair directly.

Co-authored-by: Cursor <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:11:32 +00:00
github-bot closed this pull request 2026-11-26 06:11:32 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 15:19 UTC · original

Thanks, the NaN analysis is right: DX9-style legacy min is s0 < s1 ? s0 : s1 and legacy max is s0 >= s1 ? s0 : s1. That >= is the simplification, please rework:

  1. Max: v_cmp_ge_f32 already gives exactly the legacy max. NaN makes it false, so you get source 1, and the +0/-0 tie makes it true, so you get source 0. That removes the magnitude test, the v254/v255 scratch, and the conflict with MUBUF addr64.
  2. No vcc save: a VOP3 compare can write any SGPR pair, and VOP3 v_cndmask_b32 takes that pair as src2. So each op becomes two instructions, v_cmp_{lt,ge}_f32 s[104:105], src0, src1 and v_cndmask_b32 vdst, src1, src0, s[104:105], with no vcc save/restore. Keep your check that nothing else uses s104:s105.
  3. Keep the NaN, signed-zero, relocated-branch and modifier-refusal tests. Please also add one test that runs the lowered code on WARP for a NaN in each source.
**@⁠omonkeyz** on GitHub · 2026-11-22 15:19 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081208835) Thanks, the NaN analysis is right: DX9-style legacy min is `s0 < s1 ? s0 : s1` and legacy max is `s0 >= s1 ? s0 : s1`. That `>=` is the simplification, please rework: 1. **Max:** `v_cmp_ge_f32` already gives exactly the legacy max. NaN makes it false, so you get source 1, and the +0/-0 tie makes it true, so you get source 0. That removes the magnitude test, the v254/v255 scratch, and the conflict with MUBUF addr64. 2. **No vcc save:** a VOP3 compare can write any SGPR pair, and VOP3 `v_cndmask_b32` takes that pair as src2. So each op becomes two instructions, `v_cmp_{lt,ge}_f32 s[104:105], src0, src1` and `v_cndmask_b32 vdst, src1, src0, s[104:105]`, with no vcc save/restore. Keep your check that nothing else uses s104:s105. 3. Keep the NaN, signed-zero, relocated-branch and modifier-refusal tests. Please also add one test that runs the lowered code on WARP for a NaN in each source.
Author
Owner

@⁠Yeetbot159 on GitHub · 2026-11-22 15:52 UTC · original

Legacy max is now v_cmp_ge_f32 s[104:105], src0, src1 followed by v_cndmask_b32 vdst, src1, src0, s[104:105]. The magnitude test, v254/v255 and the vcc save are gone. The NaN, signed-zero, branch and modifier tests still pass, and legacy_minmax_on_warp_keeps_a_nan_in_either_source passed on WARP, including a NaN in each source and the +0/-0 tie.

**@⁠Yeetbot159** on GitHub · 2026-11-22 15:52 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081643003) Legacy max is now `v_cmp_ge_f32 s[104:105], src0, src1` followed by `v_cndmask_b32 vdst, src1, src0, s[104:105]`. The magnitude test, v254/v255 and the vcc save are gone. The NaN, signed-zero, branch and modifier tests still pass, and `legacy_minmax_on_warp_keeps_a_nan_in_either_source` passed on WARP, including a NaN in each source and the +0/-0 tie.
Author
Owner

@⁠MiguelMatMar on GitHub · 2026-11-22 16:03 UTC · original

Independent hardware run: legacy_minmax_on_warp_keeps_a_nan_in_either_source passes on WARP and on hardware (TOMBOX_EXEC_HW=1, NVIDIA Ada / RTX 40-series), PR head 5f1f9b7. That covers the NaN-in-either-source and +0/-0 tie cases on a real GPU as well. opcodes, synthetic and gcn_dims also pass.

**@⁠MiguelMatMar** on GitHub · 2026-11-22 16:03 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081779553) Independent hardware run: `legacy_minmax_on_warp_keeps_a_nan_in_either_source` passes on WARP and on hardware (`TOMBOX_EXEC_HW=1`, NVIDIA Ada / RTX 40-series), PR head 5f1f9b7. That covers the NaN-in-either-source and +0/-0 tie cases on a real GPU as well. `opcodes`, `synthetic` and `gcn_dims` also pass.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 16:10 UTC · original

Applied in 860d34b and 9d211b5 (merged in 1a1e283) with your authorship kept, thanks, it's much simpler now!

**@⁠omonkeyz** on GitHub · 2026-11-22 16:10 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081876983) Applied in 860d34b and 9d211b5 (merged in 1a1e283) with your authorship kept, thanks, it's much simpler now!

Pull request closed

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