Lower GFX7 legacy min and max with a compare #140

Closed
Yeetbot159 wants to merge 2 commits from upstream-pr/140 into main
Yeetbot159 commented 2026-11-22 15:15:55 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by Yeetbot159 on 2026-11-22T15:15:55Z.


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.
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6544052072 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/140). Originally posted by [Yeetbot159](https://github.com/Yeetbot159) on 2026-11-22T15:15:55Z. --- ## 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.
omonkeyz commented 2026-11-22 15:19:00 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-22T15:19:00Z.


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.
<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7081208835 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081208835). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T15:19:00Z. --- 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.
Yeetbot159 commented 2026-11-22 15:52:46 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by Yeetbot159 on 2026-11-22T15:52:46Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7081643003 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081643003). Originally posted by [Yeetbot159](https://github.com/Yeetbot159) on 2026-11-22T15:52:46Z. --- 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.
MiguelMatMar commented 2026-11-22 16:03:34 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by MiguelMatMar on 2026-11-22T16:03:34Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7081779553 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081779553). Originally posted by [MiguelMatMar](https://github.com/MiguelMatMar) on 2026-11-22T16:03:34Z. --- 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.
omonkeyz commented 2026-11-22 16:10:59 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-22T16:10:59Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7081876983 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/140#issuecomment-7081876983). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T16:10:59Z. --- Applied in 860d34b and 9d211b5 (merged in 1a1e283) with your authorship kept, thanks, it's much simpler now!

Pull request closed

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
omonkeyz/xbox-emulator!140
No description provided.