pixel_aot: lower v_min3_f32, v_max3_f32 and v_med3_f32 #168

Merged
Vonsek merged 2 commits from upstream-pr/169 into main 2026-11-22 06:33:29 +00:00
Vonsek commented 2026-11-21 17:54:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T17:54:28Z.


Change

tools/pixel_aot.py now accepts the three-operand float minimum, maximum and median instructions (v_min3_f32, v_max3_f32, v_med3_f32) in the existing arithmetic path. They are emitted as nested HLSL min/max; the median is max(min(a, b), min(max(a, b), c)). Operand modifiers (-, |x|) and clamp go through the same handling as the other VOP3 arithmetic. Other data types (_i32, _u32, _f16) and wrong operand counts are still rejected.

tests/pixel_aot_check.py adds a three_way() fixture that loads four constants and writes:

  • alpha: median of a constant, green and red;
  • blue: maximum of red, blue and green;
  • red: median of red and two constants (a clamp when the bounds are ordered);
  • green: minimum of |constant|, green and the clamped red.

Every result is one of its inputs, so the CPU reference (three_way_case) is exact in binary32 before the round-toward-zero half export. The draw uses two constant sets, (0.25, 0.75, 0.375, 0.5) and (0.625, 0.125, -0.3125, 0.5); the second reverses the clamp bounds and makes the |s46| modifier matter.

docs/experiments.md#offline-pixel-shader-recompilation lists the new instructions, the HLSL form and the unmeasured NaN behaviour, and describes the new draw and its negative controls.

Verification

  • Linux, LLVM 18.1: python3 -B tests/pixel_aot_check.py binary /usr/lib/llvm-18/bin/llvm-mc /usr/lib/llvm-18/bin/llvm-objcopy /usr/lib/llvm-18/lib/libLLVM.so.18.1 passes. The fixture is assembled for gfx1013, wrapped in a synthetic record, extracted, disassembled and lowered, and the result must equal lowering the source text.
  • The same command with tools/pixel_aot.py from main fails with ValueError: Unsupported instruction 20: v_med3_f32 v15, s47, v13, v12.
  • Controls in the same script: each HLSL template is evaluated over all 125 orderings of five values (including a repeated value) against Python min, max and sorted(...)[1]; the expected HLSL lines are present; an _i32, _u32 or _f16 variant and a two-operand v_med3_f32 are rejected; the newer exp mrt0, spelling gives identical output.
  • The generated HLSL was also evaluated on the CPU against both reference cases: 0 of 64 texels differ for both constant sets. Without the |s46| modifier, 64 texels differ under the second set; with the red median replaced by v_min3_f32, 64 texels differ under both sets. The draw test uses the second set for both negative controls.
  • Windows: ctest --preset dev --no-tests=error -R '^pixel_aot_' -V at e2f31cb (rebased on main d311159, beside #202's signed-export draw and #258's truncation draw; earlier runs at 0aaa488, 0f4a150 and 862628f gave the same result) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, DXC 1.9.2602.17: pixel_aot_hardware passed on an NVIDIA GeForce RTX 5080 (driver 32.0.16.1692) and pixel_aot_warp on the Microsoft Basic Render Driver, every draw with pixels=4096 channels=16384 max_absolute_error=0. The D3D12 debug layer was not available. The full ctest --preset dev --no-tests=error passed 86/86, with bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp skipped.

Evidence and follow-up

  • Only finite UNORM-derived values and finite constants are measured. RDNA v_med3_f32 and v_min3/max3_f32 have their own NaN and IEEE-mode rules; the HLSL lowering may differ for NaN operands and for the sign of zero when +0 and -0 compare equal. The fixture avoids signed zeros.
  • Integer and half-precision three-operand forms are not covered.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532395843 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T17:54:28Z. --- ## Change `tools/pixel_aot.py` now accepts the three-operand float minimum, maximum and median instructions (`v_min3_f32`, `v_max3_f32`, `v_med3_f32`) in the existing arithmetic path. They are emitted as nested HLSL `min`/`max`; the median is `max(min(a, b), min(max(a, b), c))`. Operand modifiers (`-`, `|x|`) and `clamp` go through the same handling as the other VOP3 arithmetic. Other data types (`_i32`, `_u32`, `_f16`) and wrong operand counts are still rejected. `tests/pixel_aot_check.py` adds a `three_way()` fixture that loads four constants and writes: - alpha: median of a constant, green and red; - blue: maximum of red, blue and green; - red: median of red and two constants (a clamp when the bounds are ordered); - green: minimum of `|constant|`, green and the clamped red. Every result is one of its inputs, so the CPU reference (`three_way_case`) is exact in binary32 before the round-toward-zero half export. The draw uses two constant sets, `(0.25, 0.75, 0.375, 0.5)` and `(0.625, 0.125, -0.3125, 0.5)`; the second reverses the clamp bounds and makes the `|s46|` modifier matter. `docs/experiments.md#offline-pixel-shader-recompilation` lists the new instructions, the HLSL form and the unmeasured NaN behaviour, and describes the new draw and its negative controls. ## Verification - Linux, LLVM 18.1: `python3 -B tests/pixel_aot_check.py binary /usr/lib/llvm-18/bin/llvm-mc /usr/lib/llvm-18/bin/llvm-objcopy /usr/lib/llvm-18/lib/libLLVM.so.18.1` passes. The fixture is assembled for gfx1013, wrapped in a synthetic record, extracted, disassembled and lowered, and the result must equal lowering the source text. - The same command with `tools/pixel_aot.py` from `main` fails with `ValueError: Unsupported instruction 20: v_med3_f32 v15, s47, v13, v12`. - Controls in the same script: each HLSL template is evaluated over all 125 orderings of five values (including a repeated value) against Python `min`, `max` and `sorted(...)[1]`; the expected HLSL lines are present; an `_i32`, `_u32` or `_f16` variant and a two-operand `v_med3_f32` are rejected; the newer `exp mrt0,` spelling gives identical output. - The generated HLSL was also evaluated on the CPU against both reference cases: 0 of 64 texels differ for both constant sets. Without the `|s46|` modifier, 64 texels differ under the second set; with the red median replaced by `v_min3_f32`, 64 texels differ under both sets. The draw test uses the second set for both negative controls. - Windows: `ctest --preset dev --no-tests=error -R '^pixel_aot_' -V` at e2f31cb (rebased on main d311159, beside #202's signed-export draw and #258's truncation draw; earlier runs at 0aaa488, 0f4a150 and 862628f gave the same result) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, DXC 1.9.2602.17: `pixel_aot_hardware` passed on an NVIDIA GeForce RTX 5080 (driver 32.0.16.1692) and `pixel_aot_warp` on the Microsoft Basic Render Driver, every draw with `pixels=4096 channels=16384 max_absolute_error=0`. The D3D12 debug layer was not available. The full `ctest --preset dev --no-tests=error` passed 86/86, with `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp` skipped. ## Evidence and follow-up - Only finite UNORM-derived values and finite constants are measured. RDNA `v_med3_f32` and `v_min3/max3_f32` have their own NaN and IEEE-mode rules; the HLSL lowering may differ for NaN operands and for the sign of zero when `+0` and `-0` compare equal. The fixture avoids signed zeros. - Integer and half-precision three-operand forms are not covered.
upstream-sync closed this pull request 2026-11-24 06:41:39 +00:00
R0Y00 commented 2026-11-24 06:47:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:16:09Z.


GitHub reports a conflict on 0aaa4880. Please reconcile the three-operand entries and shader fixture lists with the arithmetic, move and predicate cases now on main. Run the LLVM round trip and pixel_aot_warp on the reconciled branch before marking ready.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067128540 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7067128540). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:16:09Z. --- GitHub reports a conflict on `0aaa4880`. Please reconcile the three-operand entries and shader fixture lists with the arithmetic, move and predicate cases now on main. Run the LLVM round trip and `pixel_aot_warp` on the reconciled branch before marking ready.
R0Y00 commented 2026-11-24 06:47:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:23:54Z.


0f4a1508 retains the earlier arithmetic and predicate fixtures and the Windows check passed. GitHub now reports a conflict after #202 landed. Please keep the signed half_rtz correction and signed-export draw from #202 alongside the new three-way draw and its negative controls, and rerun the shared pixel suite.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067224442 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7067224442). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:23:54Z. --- `0f4a1508` retains the earlier arithmetic and predicate fixtures and the Windows check passed. GitHub now reports a conflict after #202 landed. Please keep the signed `half_rtz` correction and signed-export draw from #202 alongside the new three-way draw and its negative controls, and rerun the shared pixel suite.
chatgpt-codex-connector[bot commented 2026-11-24 06:47:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T18:23:17Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T18:25:42.933140Z 0f4a150 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067216618 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7067216618). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T18:23:17Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"0f4a1508bb0cd41b8194f933865a61739c4ce11f","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-21T18:25:42.933140Z">2026-11-21T18:25:42.933140Z</relative-time> | `0f4a150` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Vonsek commented 2026-11-24 06:47:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T19:46:03Z.


Rebased on main 7233cec (862628f). The conflict was in tests/pixel_aot_check.py: the form-coverage lower wrapper from main is kept (the test now also imports ALU for the three-operand template check), and the draw step keeps #202's signed half_rtz export draw and then runs the three-way draw with both constant sets and its two negative controls. The three-operand ALU entries and fixture lists sit beside the arithmetic, move, predicate and signed-export cases from main.

On Windows 11 26200.9974 (MSVC 14.44.35207, SDK 10.0.26100, DXC 1.9.2602.17): ctest --preset dev --no-tests=error -R '^pixel_aot_' -V passed 2/2, pixel_aot_hardware on the RTX 5080 (driver 32.0.16.1692) and pixel_aot_warp on WARP, every draw with max_absolute_error=0. The full suite passed 121/121. This machine has no gfx1013 LLVM, so the LLVM round trip is covered by the CI "Assemble and translate gfx1013 fixtures" job on this head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068262822 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7068262822). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T19:46:03Z. --- Rebased on main 7233cec (862628f). The conflict was in `tests/pixel_aot_check.py`: the form-coverage `lower` wrapper from main is kept (the test now also imports `ALU` for the three-operand template check), and the draw step keeps #202's signed `half_rtz` export draw and then runs the three-way draw with both constant sets and its two negative controls. The three-operand ALU entries and fixture lists sit beside the arithmetic, move, predicate and signed-export cases from main. On Windows 11 26200.9974 (MSVC 14.44.35207, SDK 10.0.26100, DXC 1.9.2602.17): `ctest --preset dev --no-tests=error -R '^pixel_aot_' -V` passed 2/2, `pixel_aot_hardware` on the RTX 5080 (driver 32.0.16.1692) and `pixel_aot_warp` on WARP, every draw with `max_absolute_error=0`. The full suite passed 121/121. This machine has no gfx1013 LLVM, so the LLVM round trip is covered by the CI "Assemble and translate gfx1013 fixtures" job on this head.
R0Y00 commented 2026-11-24 06:47:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:59:42Z.


The rebase preserves the signed-export draw and resolves the previous conflict. One acceptance gap remains in the shared arithmetic branch: a min3, max3 or med3 writing an outstanding image sample destination can overwrite its tracked register before vmcnt(0). Please reject that write while the sample is pending and add a control proving the same destination is writable after the wait.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068426176 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7068426176). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:59:42Z. --- The rebase preserves the signed-export draw and resolves the previous conflict. One acceptance gap remains in the shared arithmetic branch: a `min3`, `max3` or `med3` writing an outstanding image sample destination can overwrite its tracked register before `vmcnt(0)`. Please reject that write while the sample is pending and add a control proving the same destination is writable after the wait.
R0Y00 commented 2026-11-24 06:47:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:28:10Z.


#273 has supplied the shared pending-destination guard and pre/post-wait controls requested above. The remaining conflicts on 862628f are docs/experiments.md and tests/pixel_aot_check.py. Please preserve the three-way extrema controls alongside the landed pending-write and single-word-load cases.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068774368 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7068774368). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T20:28:10Z. --- #273 has supplied the shared pending-destination guard and pre/post-wait controls requested above. The remaining conflicts on `862628f` are `docs/experiments.md` and `tests/pixel_aot_check.py`. Please preserve the three-way extrema controls alongside the landed pending-write and single-word-load cases.
R0Y00 commented 2026-11-24 06:48:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T21:27:41Z.


At e2f31cb, the merge conflicts are in tests/pixel_aot_check.py and tools/pixel_aot.py. Please retain main's ceil entry and draw alongside the three-way extrema.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069489740 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7069489740). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T21:27:41Z. --- At `e2f31cb`, the merge conflicts are in `tests/pixel_aot_check.py` and `tools/pixel_aot.py`. Please retain main's `ceil` entry and draw alongside the three-way extrema.
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
R0Y00/Xbox2Windows!168
No description provided.