startup: lower three-source integer min, max and median in gfx2hlsl #1985

Merged
Leyukaka merged 2 commits from upstream-pr/1992 into main 2026-11-24 23:04:31 +00:00
Leyukaka commented 2026-11-24 22:41:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-24T22:41:00Z.


Change

tools/startup/gfx2hlsl.py lowered v_med3_f32, v_min3_f32 and v_max3_f32, but refused the integer forms as unknown instructions. pixel_aot already lowers all six (#308), and the compute translator lowers the unsigned min/max and both medians (#1574, #1577); the startup translator lowered none. Parity with pixel_aot: these ops are not in the #1723 census, so no measured refusal count is claimed here.

Six VEC_U entries add them:

  • v_min3_i32, v_max3_i32 and v_med3_i32 compare signed, and store the result word as uint;
  • v_min3_u32, v_max3_u32 and v_med3_u32 compare unsigned.

The median is max(min(a, b), min(max(a, b), c)). As for the other integer VALU ops, modifiers such as clamp and source negation stay refused.

The check is registered as CTest startup_gfx2hlsl_int_med3_controls and added to the pure-Python CI list in .github/workflows/windows.yml and its mirror in AGENTS.md. docs/experiments.md has a short section.

Verification

On ba2df377 (based on main at 889cd3f4), Windows 11 build 26300, Python 3.14.10:

python -B tests/startup_gfx2hlsl_int_med3_check.py

PASS: startup_gfx2hlsl_int_med3_check. For each of the six ops, in the bare spelling the repository disassembler prints and with an _e64 suffix (the LLVM spelling, which one() strips), the test:

  • checks the emitted HLSL;
  • evaluates the emitted expression with a restricted ast evaluator (only min, max, int, uint and the three source names) on ties, on every permutation of triples that span the sign bit, and on triples whose signed and unsigned medians differ.

It also checks one form with inline-constant and literal sources (v_med3_i32 v1, v2, -1, 0x80000000), the clamp and -v2 refusals, and that removing the six entries in-process refuses v_med3_i32 again.

Translator Result
this branch PASS
main (889cd3f4) the first case is refused: Unsupported: v_min3_i32
this branch with v_med3_i32 given the unsigned template fails at the emitted-text assertion for v_med3_i32

Also run on this branch, each python -B tests/<name> exiting 0: startup_gfx2hlsl_cvt_alu_check.py, startup_gfx2hlsl_bit_alu_check.py, startup_gfx2hlsl_operand_count_check.py, startup_gfx2hlsl_op_refusals_check.py, startup_uniform_lane_check.py, startup_gfx2hlsl_cndmask_neg_check.py, startup_gfx2hlsl_scalar_m0_check.py, startup_gfx2hlsl_vopc_sdwa_check.py, startup_gfx2hlsl_f32_lg_check.py, startup_gfx2hlsl_f32_nan_check.py, startup_ngg_slice_check.py, startup_gfx2hlsl_mad_check.py, startup_translator_check.py, startup_flat_param_check.py, docs_links_check.py, bidi_controls_check.py. CTest was not run for this change.

Evidence and follow-up

  • No fixture file: the test feeds instruction text to Stage.one, so the spellings are not checked against llvm-mc or tools/disasm_rdna2.py, and the decode of these VOP3 words is not covered. No title or package needed.
  • Not covered: a draw through a translated pipeline on WARP or hardware (the expressions are checked by evaluation, not on a GPU), and whether locally refused shaders now translate, which needs local inputs. Open #1706 adds a draw check for the existing three-source VEC_U ops; these six could join it.

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592857061 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1992). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-24T22:41:00Z. --- ## Change `tools/startup/gfx2hlsl.py` lowered `v_med3_f32`, `v_min3_f32` and `v_max3_f32`, but refused the integer forms as unknown instructions. `pixel_aot` already lowers all six (#308), and the compute translator lowers the unsigned min/max and both medians (#1574, #1577); the startup translator lowered none. Parity with `pixel_aot`: these ops are not in the #1723 census, so no measured refusal count is claimed here. Six `VEC_U` entries add them: - `v_min3_i32`, `v_max3_i32` and `v_med3_i32` compare signed, and store the result word as `uint`; - `v_min3_u32`, `v_max3_u32` and `v_med3_u32` compare unsigned. The median is `max(min(a, b), min(max(a, b), c))`. As for the other integer VALU ops, modifiers such as `clamp` and source negation stay refused. The check is registered as CTest `startup_gfx2hlsl_int_med3_controls` and added to the pure-Python CI list in `.github/workflows/windows.yml` and its mirror in `AGENTS.md`. `docs/experiments.md` has a short section. ## Verification On `ba2df377` (based on `main` at `889cd3f4`), Windows 11 build 26300, Python 3.14.10: ```text python -B tests/startup_gfx2hlsl_int_med3_check.py ``` `PASS: startup_gfx2hlsl_int_med3_check`. For each of the six ops, in the bare spelling the repository disassembler prints and with an `_e64` suffix (the LLVM spelling, which `one()` strips), the test: - checks the emitted HLSL; - evaluates the emitted expression with a restricted `ast` evaluator (only `min`, `max`, `int`, `uint` and the three source names) on ties, on every permutation of triples that span the sign bit, and on triples whose signed and unsigned medians differ. It also checks one form with inline-constant and literal sources (`v_med3_i32 v1, v2, -1, 0x80000000`), the `clamp` and `-v2` refusals, and that removing the six entries in-process refuses `v_med3_i32` again. | Translator | Result | |---|---| | this branch | `PASS` | | `main` (`889cd3f4`) | the first case is refused: `Unsupported: v_min3_i32` | | this branch with `v_med3_i32` given the unsigned template | fails at the emitted-text assertion for `v_med3_i32` | Also run on this branch, each `python -B tests/<name>` exiting 0: `startup_gfx2hlsl_cvt_alu_check.py`, `startup_gfx2hlsl_bit_alu_check.py`, `startup_gfx2hlsl_operand_count_check.py`, `startup_gfx2hlsl_op_refusals_check.py`, `startup_uniform_lane_check.py`, `startup_gfx2hlsl_cndmask_neg_check.py`, `startup_gfx2hlsl_scalar_m0_check.py`, `startup_gfx2hlsl_vopc_sdwa_check.py`, `startup_gfx2hlsl_f32_lg_check.py`, `startup_gfx2hlsl_f32_nan_check.py`, `startup_ngg_slice_check.py`, `startup_gfx2hlsl_mad_check.py`, `startup_translator_check.py`, `startup_flat_param_check.py`, `docs_links_check.py`, `bidi_controls_check.py`. CTest was not run for this change. ## Evidence and follow-up - No fixture file: the test feeds instruction text to `Stage.one`, so the spellings are not checked against `llvm-mc` or `tools/disasm_rdna2.py`, and the decode of these VOP3 words is not covered. No title or package needed. - Not covered: a draw through a translated pipeline on WARP or hardware (the expressions are checked by evaluation, not on a GPU), and whether locally refused shaders now translate, which needs local inputs. Open #1706 adds a draw check for the existing three-source `VEC_U` ops; these six could join it. ## 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.
ChazY11 commented 2026-11-24 23:03:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:43:02Z.


Ran it, broke the lowering five ways, and checked the formulas as mathematics

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_int_med3_check.py
PASS: startup_gfx2hlsl_int_med3_check      exit 0
mutation result
v_min3_u32 wrapped in int(...) (signed compare) caught
v_min3_i32 loses its int(...) casts (unsigned compare) caught
v_med3_u32 formula drops a level (min(max(a,b), c)) caught
v_max3_u32 becomes a min caught
v_med3_i32 loses the outer uint(...) caught

The first two are the pair I would expect to be confused, and the fixture separates them — which means it carries values on both sides of
0x80000000, not just small positive ones.

The formulas are true min/max/median, not just "the text the author wrote"

I transcribed all six emitted expressions and compared them against sorted() over 101,000 triples — 1,000 edge combinations
(0, 1, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFF, 0xC0000000, …) plus 100,000 seeded random triples — for both the
unsigned and the signed reading, 606,000 comparisons in total:

triples per form: 101000   total checks: 606000   mismatches: 0

That includes the case where the signed and unsigned answers differ, e.g. 0x80000000 versus 0x7FFFFFFF is less signed and
greater unsigned, so a swapped reading cannot pass both.

Two things I would put in the description

  • v_med3_* is not min(max(a,b), c): the emitted max(min(a,b), min(max(a,b), c)) is the three-value median, and it is worth
    saying so, because the shorter form is what a reader will assume and it is wrong for the third operand.
  • The uint(...) wrapper on the _i32 forms is not decoration: the result word is a uint register, and dropping the wrapper (last
    mutation above) changes the emitted type. That is the one line a copy-paste of this block will lose.

Scope

Parity with pixel_aot is a claim about the other translator; I did not diff the two tables. The six forms here are exercised only
by this check, and no draw was run, which matches the description's own framing.

Verdict

Reviewed. Six forms, five mutations all caught, and the median formula holds up as a median.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146561009 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1992#issuecomment-7146561009). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:43:02Z. --- ## Ran it, broke the lowering five ways, and checked the formulas as mathematics Windows 11 build 26300.9974, device-free: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_int_med3_check.py PASS: startup_gfx2hlsl_int_med3_check exit 0 ``` | mutation | result | |---|---| | `v_min3_u32` wrapped in `int(...)` (signed compare) | **caught** | | `v_min3_i32` loses its `int(...)` casts (unsigned compare) | **caught** | | `v_med3_u32` formula drops a level (`min(max(a,b), c)`) | **caught** | | `v_max3_u32` becomes a `min` | **caught** | | `v_med3_i32` loses the outer `uint(...)` | **caught** | The first two are the pair I would expect to be confused, and the fixture separates them — which means it carries values on both sides of `0x80000000`, not just small positive ones. ## The formulas are true min/max/median, not just "the text the author wrote" I transcribed all six emitted expressions and compared them against `sorted()` over 101,000 triples — 1,000 edge combinations (`0`, `1`, `0x7FFFFFFF`, `0x80000000`, `0x80000001`, `0xFFFFFFFF`, `0xC0000000`, …) plus 100,000 seeded random triples — for both the unsigned and the signed reading, 606,000 comparisons in total: ```text triples per form: 101000 total checks: 606000 mismatches: 0 ``` That includes the case where the signed and unsigned answers differ, e.g. `0x80000000` versus `0x7FFFFFFF` is *less* signed and *greater* unsigned, so a swapped reading cannot pass both. ## Two things I would put in the description - `v_med3_*` is not `min(max(a,b), c)`: the emitted `max(min(a,b), min(max(a,b), c))` is the three-value median, and it is worth saying so, because the shorter form is what a reader will assume and it is wrong for the third operand. - The `uint(...)` wrapper on the `_i32` forms is not decoration: the result word is a `uint` register, and dropping the wrapper (last mutation above) changes the emitted type. That is the one line a copy-paste of this block will lose. ## Scope Parity with `pixel_aot` is a claim about the *other* translator; I did not diff the two tables. The six forms here are exercised only by this check, and no draw was run, which matches the description's own framing. ## Verdict Reviewed. Six forms, five mutations all caught, and the median formula holds up as a median. ## Notes - AI-assisted review.
upstream-sync closed this pull request 2026-11-25 00:04:48 +00:00
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!1985
No description provided.