shader: lower compute v_med3_u32 and v_med3_i32 #1570

Merged
Asimmimx merged 2 commits from upstream-pr/1577 into main 2026-11-24 16:12:19 +00:00
Asimmimx commented 2026-11-24 14:32:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T14:32:40Z.


Change

Refs #1546. This lowers v_med3_u32 and v_med3_i32 in the compute translator (tools/compute_store_aot.py). Each returns the median of src0, src1 and src2, compared unsigned or as two's complement.

Opcode HLSL lowering
v_med3_u32 max(min(a, b), min(max(a, b), c))
v_med3_i32 the same form on asint(...) of each source, wrapped in asuint(...)

This is the pattern LLVM 15 selects these instructions for (IntMed3Pat in SIInstructions.td, llvmorg-15.0.7), with smin/smax and umin/umax. The change adds two ALU entries and appends both opcodes to the three-source matcher group, (v_mad_u32_u24|v_med3_u32|v_med3_i32).

The fixture and controls are in a new tests/compute_alu_med3_check.py. It imports the helpers of tests/compute_store_check.py and does not change that file.

Fixture READ_MED3. It is a variant of READ_COMPUTE. With w = the lane's source word, a = w << 9 and cap = params[0].y = 0x3a5c0f1d, it runs:

v_med3_u32 v3, v2, s0, 0x80000000
v_med3_i32 v4, v2, s0, 0x80000000
v_med3_i32 v5, v2, v1, v2          (equal operands, src0 and src2)
v_med3_u32 v6, -1, v1, v1          (equal operands, src1 and src2)
stored word = (v3 - v4) + (v5 ^ v6)

0x80000000 is the largest value when compared unsigned and the smallest when compared signed, so the two medians differ in every lane. The controls also assert that a, cap and 0x80000000 each win as the median in some lane.

The expected words come from a Python reference that sorts the three words; it does not use the lowering. Lane 0 is worked by hand: 0xc359730e.

Checks that must fail:

  • 5 wrong references, each at its first differing word:
    • _u32 compared signed;
    • _i32 compared unsigned;
    • minimum instead of median;
    • maximum instead of median;
    • lane 150 also writes.
  • 4 recompiled HLSL mutations: _i32 lowered unsigned, _u32 lowered signed, minimum for every median, and _u32 as max(a, b).

Controls refused with the existing messages:

  • clamp, op_sel, -v2 and |v2|;
  • a v[2:3] range;
  • unknown, descriptor or pending sources, and a 33-bit literal;
  • v_med3_u16, v_med3_i16 and v_med3_f32;
  • a v256 destination;
  • the result used as an address or as a guard operand;
  • a chain of medians that grows too large.

The new CTest is compute_store_aot_alu_med3_${adapter}, after compute_store_aot_mubuf_vector. A "Three-source median" subsection is added to docs/experiments.md.

Verification

Commit 6c0e5380 (main) plus this change, dev preset, Windows 11 Pro build 26300.9974, DXC 1.8.2502.11.

python -B tests/compute_alu_med3_check.py controls                                  # pass
python -B tests/compute_alu_med3_check.py draw <dxc> <offscreen_check.exe> warp     # pass
python -B tests/compute_alu_med3_check.py draw <dxc> <offscreen_check.exe> hardware # pass
#   read_med3: 150 lanes match; 5 reference and 4 source mutations rejected
ctest --preset dev --no-tests=error -R '^compute_store_aot_alu_med3_(warp|hardware)$' -V
Backend Result
WARP 10.0.26100.9818, debug layer enabled 3 dispatches (0, 17, 150 lanes), 320 words each, mismatches 0, debug errors 0
AMD Radeon RX 6600 (RDNA 2, 1002:73ff), driver 32.0.21045.29003, debug layer enabled 3 dispatches, 320 words each, mismatches 0, debug errors 0

These also pass with the change:

  • the compute_store_check translator controls;
  • draw-guard-read on WARP (32 dispatches, mismatches 0);
  • draw-f32 on WARP;
  • tests/docs_links_check.py, tests/bidi_controls_check.py and git diff --check.

I did not run the LLVM 15 binary mode, because it needs LLVM 15 and libLLVM.

Notes

  • Error for an unknown operand. In the shared matcher, the known-word guard binds only to the three-source alternative. So a median with an unknown operand stops with Unsupported compute instruction: … rather than Unverified ALU operand, the same as for v_mad_u32_u24. I left the matcher unchanged.
  • Expression size. Each median repeats two operands inline, so nested medians reach ALU_EXPRESSION_LIMIT quickly. The fixture's stored word is 928 characters.
  • Operand order is not tested. The median is symmetric, so a wrong operand order cannot change a result. The equal-operand lanes check that repeated registers are handled.
  • Rebase. #1573 and #1574 append to the same matcher group. Whichever lands later needs a trivial rebase, which I will do.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6584149872 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1577). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T14:32:40Z. --- ## Change Refs #1546. This lowers `v_med3_u32` and `v_med3_i32` in the compute translator (`tools/compute_store_aot.py`). Each returns the median of src0, src1 and src2, compared unsigned or as two's complement. | Opcode | HLSL lowering | |---|---| | `v_med3_u32` | `max(min(a, b), min(max(a, b), c))` | | `v_med3_i32` | the same form on `asint(...)` of each source, wrapped in `asuint(...)` | This is the pattern LLVM 15 selects these instructions for (`IntMed3Pat` in `SIInstructions.td`, llvmorg-15.0.7), with smin/smax and umin/umax. The change adds two `ALU` entries and appends both opcodes to the three-source matcher group, `(v_mad_u32_u24|v_med3_u32|v_med3_i32)`. The fixture and controls are in a new `tests/compute_alu_med3_check.py`. It imports the helpers of `tests/compute_store_check.py` and does not change that file. **Fixture `READ_MED3`.** It is a variant of `READ_COMPUTE`. With w = the lane's source word, a = w << 9 and cap = `params[0].y` = `0x3a5c0f1d`, it runs: ``` v_med3_u32 v3, v2, s0, 0x80000000 v_med3_i32 v4, v2, s0, 0x80000000 v_med3_i32 v5, v2, v1, v2 (equal operands, src0 and src2) v_med3_u32 v6, -1, v1, v1 (equal operands, src1 and src2) stored word = (v3 - v4) + (v5 ^ v6) ``` `0x80000000` is the largest value when compared unsigned and the smallest when compared signed, so the two medians differ in every lane. The controls also assert that `a`, `cap` and `0x80000000` each win as the median in some lane. The expected words come from a Python reference that sorts the three words; it does not use the lowering. Lane 0 is worked by hand: `0xc359730e`. **Checks that must fail:** - 5 wrong references, each at its first differing word: - `_u32` compared signed; - `_i32` compared unsigned; - minimum instead of median; - maximum instead of median; - lane 150 also writes. - 4 recompiled HLSL mutations: `_i32` lowered unsigned, `_u32` lowered signed, minimum for every median, and `_u32` as `max(a, b)`. **Controls refused with the existing messages:** - `clamp`, `op_sel`, `-v2` and `|v2|`; - a `v[2:3]` range; - unknown, descriptor or pending sources, and a 33-bit literal; - `v_med3_u16`, `v_med3_i16` and `v_med3_f32`; - a `v256` destination; - the result used as an address or as a guard operand; - a chain of medians that grows too large. The new CTest is `compute_store_aot_alu_med3_${adapter}`, after `compute_store_aot_mubuf_vector`. A "Three-source median" subsection is added to `docs/experiments.md`. ## Verification Commit `6c0e5380` (`main`) plus this change, `dev` preset, Windows 11 Pro build 26300.9974, DXC 1.8.2502.11. ```powershell python -B tests/compute_alu_med3_check.py controls # pass python -B tests/compute_alu_med3_check.py draw <dxc> <offscreen_check.exe> warp # pass python -B tests/compute_alu_med3_check.py draw <dxc> <offscreen_check.exe> hardware # pass # read_med3: 150 lanes match; 5 reference and 4 source mutations rejected ctest --preset dev --no-tests=error -R '^compute_store_aot_alu_med3_(warp|hardware)$' -V ``` | Backend | Result | |---|---| | WARP 10.0.26100.9818, debug layer enabled | 3 dispatches (0, 17, 150 lanes), 320 words each, mismatches 0, debug errors 0 | | AMD Radeon RX 6600 (RDNA 2, `1002:73ff`), driver 32.0.21045.29003, debug layer enabled | 3 dispatches, 320 words each, mismatches 0, debug errors 0 | These also pass with the change: - the `compute_store_check` translator controls; - `draw-guard-read` on WARP (32 dispatches, mismatches 0); - `draw-f32` on WARP; - `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `git diff --check`. I did not run the LLVM 15 `binary` mode, because it needs LLVM 15 and libLLVM. ## Notes - **Error for an unknown operand.** In the shared matcher, the known-word guard binds only to the three-source alternative. So a median with an unknown operand stops with `Unsupported compute instruction: …` rather than `Unverified ALU operand`, the same as for `v_mad_u32_u24`. I left the matcher unchanged. - **Expression size.** Each median repeats two operands inline, so nested medians reach `ALU_EXPRESSION_LIMIT` quickly. The fixture's stored word is 928 characters. - **Operand order is not tested.** The median is symmetric, so a wrong operand order cannot change a result. The equal-operand lanes check that repeated registers are handled. - **Rebase.** #1573 and #1574 append to the same matcher group. Whichever lands later needs a trivial rebase, which I will do. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code).
tedin7 commented 2026-11-24 16:03:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T15:39:35Z.

Review: COMMENTED


Peer review at 9cea0b18 (shader: lower compute v_med3_u32 and v_med3_i32): looks good, no blocking issues found.

Code & Semantics Review

  • tools/compute_store_aot.py: v_med3_u32 and v_med3_i32 lower to max(min({0}, {1}), min(max({0}, {1}), {2})) (unsigned) and asuint(max(min(asint({0}), asint({1})), min(max(asint({0}), asint({1})), asint({2})))) (signed), matching the 3-source ALU_SOURCE guard alongside v_mad_u32_u24 with dst <= 255, data-only usage, and ALU_EXPRESSION_LIMIT.
  • tests/compute_alu_med3_check.py: exercises VGPR, SGPR, inline constant (-1), and 0x80000000 literal operands, equal-operand tie cases, hand-worked lane 0 (0xc359730e), modifier/16-bit/f32/unverified-operand rejections, plus 5 wrong references and 4 DXC-compiled HLSL mutations in draw.
  • CMakeLists.txt & docs/experiments.md: registers compute_store_aot_alu_med3_${adapter} and documents the lowering and controls.

Local Verification

  • Linux / LLVM-19:
    • python3 tests/compute_alu_med3_check.py controls — passed
    • llvm-mc-19 -arch=amdgcn -mcpu=gfx1013 -show-encoding & -mcpu=gfx1030 on v_med3_u32 / v_med3_i32 forms — passed
    • python3 tests/docs_links_check.py (16 Markdown files, 344 links) — passed
    • python3 tests/bidi_controls_check.py (1253 tracked files) — passed
  • Win2025 WARP (--preset dev Debug & --preset release /O2):
    • ctest --preset dev -R '^compute_store_aot_alu_med3_warp$' (within 41/41 compute-store suite) — 41/41 passed (compute_store_aot_alu_med3_warp 3.73s)
    • ctest --preset release -R '^compute_store_aot_alu_med3_warp$' (within 41/41 compute-store suite) — 41/41 passed (compute_store_aot_alu_med3_warp 3.85s)

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6042422262 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1577#pullrequestreview-6042422262). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T15:39:35Z. **Review:** COMMENTED --- Peer review at `9cea0b18` (`shader: lower compute v_med3_u32 and v_med3_i32`): looks good, no blocking issues found. ### Code & Semantics Review - `tools/compute_store_aot.py`: `v_med3_u32` and `v_med3_i32` lower to `max(min({0}, {1}), min(max({0}, {1}), {2}))` (unsigned) and `asuint(max(min(asint({0}), asint({1})), min(max(asint({0}), asint({1})), asint({2}))))` (signed), matching the 3-source `ALU_SOURCE` guard alongside `v_mad_u32_u24` with `dst <= 255`, data-only usage, and `ALU_EXPRESSION_LIMIT`. - `tests/compute_alu_med3_check.py`: exercises VGPR, SGPR, inline constant (`-1`), and `0x80000000` literal operands, equal-operand tie cases, hand-worked lane 0 (`0xc359730e`), modifier/16-bit/f32/unverified-operand rejections, plus 5 wrong references and 4 DXC-compiled HLSL mutations in `draw`. - `CMakeLists.txt` & `docs/experiments.md`: registers `compute_store_aot_alu_med3_${adapter}` and documents the lowering and controls. ### Local Verification - **Linux / LLVM-19**: - `python3 tests/compute_alu_med3_check.py controls` — passed - `llvm-mc-19 -arch=amdgcn -mcpu=gfx1013 -show-encoding` & `-mcpu=gfx1030` on `v_med3_u32` / `v_med3_i32` forms — passed - `python3 tests/docs_links_check.py` (16 Markdown files, 344 links) — passed - `python3 tests/bidi_controls_check.py` (1253 tracked files) — passed - **Win2025 WARP (`--preset dev` Debug & `--preset release` `/O2`)**: - `ctest --preset dev -R '^compute_store_aot_alu_med3_warp$'` (within 41/41 compute-store suite) — 41/41 passed (`compute_store_aot_alu_med3_warp` `3.73s`) - `ctest --preset release -R '^compute_store_aot_alu_med3_warp$'` (within 41/41 compute-store suite) — 41/41 passed (`compute_store_aot_alu_med3_warp` `3.85s`) AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 17:01:37 +00:00
R0Y00 commented 2026-11-24 17:02:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T16:09:26Z.


Resolved the main conflict in 20d2ef06 by retaining both opcode sets in the three-source matcher and both documentation sections. Windows Release passed 6/6 across MED3, MIN3/MAX3 and XOR3 on RTX and WARP, with no skips.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7137209857 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1577#issuecomment-7137209857). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T16:09:26Z. --- Resolved the main conflict in 20d2ef06 by retaining both opcode sets in the three-source matcher and both documentation sections. Windows Release passed 6/6 across MED3, MIN3/MAX3 and XOR3 on RTX and WARP, with no skips.
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!1570
No description provided.