pixel_aot: lower v_min3_f32, v_max3_f32 and v_med3_f32 #169

Manually merged
github merged 2 commits from gh-pr/169/head into gh-pr/169/base 2026-11-26 21:31:43 +00:00
Owner

@⁠Vonsek on GitHub · 2026-11-21 17:54 UTC · original
Vonsek:pixel-aot-min3-max3-med3 → main · +93 −6 · 4 files · 2 commits · merged 2026-11-22 06:33 UTC

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.
**@⁠Vonsek** on GitHub · 2026-11-21 17:54 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169) `Vonsek:pixel-aot-min3-max3-med3` → `main` · +93 −6 · 4 files · 2 commits · merged 2026-11-22 06:33 UTC ## 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.
Translate v_min3_f32, v_max3_f32 and v_med3_f32 to nested HLSL min and
max. Add an assembled fixture with an exact CPU reference drawn with two
constant sets, plus controls that drop a source modifier or replace the
median with a minimum.
# Conflicts:
#	docs/experiments.md
#	tests/pixel_aot_check.py
#	tools/pixel_aot.py
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:31:41 +00:00
github manually merged commit 8da230cb6c into gh-pr/169/base 2026-11-26 21:31:43 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 18:16 UTC · original

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** on GitHub · 2026-11-21 18:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7067128540) 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.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 18:23 UTC · original · edited 2026-11-21 18:25 UTC

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.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 18:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7067216618) · edited 2026-11-21 18:25 UTC <!-- 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>
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 18:23 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-21 18:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7067224442) `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.
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 19:46 UTC · original

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.

**@⁠Vonsek** on GitHub · 2026-11-21 19:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7068262822) 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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 19:59 UTC · original

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** on GitHub · 2026-11-21 19:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7068426176) 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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 20:28 UTC · original

#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** on GitHub · 2026-11-21 20:28 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7068774368) #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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 21:27 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-21 21:27 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/169#issuecomment-7069489740) 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.