shader: lower compute v_med3_u32 and v_med3_i32 #1577

Manually merged
github merged 2 commits from gh-pr/1577/head into gh-pr/1577/base 2026-11-26 23:18:06 +00:00
Owner

@⁠Asimmimx on GitHub · 2026-11-24 14:32 UTC · original
Asimmimx:shader/compute-med3 → main · +224 −1 · 4 files · 2 commits · merged 2026-11-24 16:12 UTC

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).

**@⁠Asimmimx** on GitHub · 2026-11-24 14:32 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1577) `Asimmimx:shader/compute-med3` → `main` · +224 −1 · 4 files · 2 commits · merged 2026-11-24 16:12 UTC ## 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).
Part of #1546. v_med3_u32 and v_med3_i32 (VOP3) return the median of
src0, src1 and src2, compared as unsigned or as two's-complement words.
They join the three-source known-word matcher of the compute store
profile and lower to max(min(a, b), min(max(a, b), c)); the signed form
compares asint of each source and stores through asuint. This is the
pattern LLVM 15 selects as these instructions (IntMed3Pat in
SIInstructions.td).

tests/compute_alu_med3_check.py keeps the guarded read and store of the
read_compute fixture with hand-written gfx1013 VOP3 forms (VGPR, SGPR,
inline constant and literal sources, equal operands in src0/src2 and
src1/src2) on words that straddle 0x80000000, so the unsigned and
signed medians differ in every lane. Expected words come from a CPU
reference that sorts the three words; lane 0 is worked by hand. On WARP,
all 320 readback words match for three dispatches, and wrong-signedness,
min, max and past-the-bound references and four recompiled mutations
must fail. Translator controls refuse clamp, op_sel, neg and abs, other
median widths and types, register ranges, unknown operands, a
destination past v255, a result used as an address or guard operand,
and an oversized chain.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:18:05 +00:00
github manually merged commit 20d2ef06cf into gh-pr/1577/base 2026-11-26 23:18:06 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 16:09 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-24 16:09 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1577#issuecomment-7137209857) 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.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 15:39 UTC · original · reviewed

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.

**@⁠tedin7** on GitHub · 2026-11-24 15:39 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1577#pullrequestreview-6042422262) · **reviewed** 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.