startup: lower three-source integer min, max and median in gfx2hlsl #1985
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1992"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-24T22:41:00Z.
Change
tools/startup/gfx2hlsl.pyloweredv_med3_f32,v_min3_f32andv_max3_f32, but refused the integer forms as unknown instructions.pixel_aotalready lowers all six (#308), and the compute translator lowers the unsigned min/max and both medians (#1574, #1577); the startup translator lowered none. Parity withpixel_aot: these ops are not in the #1723 census, so no measured refusal count is claimed here.Six
VEC_Uentries add them:v_min3_i32,v_max3_i32andv_med3_i32compare signed, and store the result word asuint;v_min3_u32,v_max3_u32andv_med3_u32compare unsigned.The median is
max(min(a, b), min(max(a, b), c)). As for the other integer VALU ops, modifiers such asclampand source negation stay refused.The check is registered as CTest
startup_gfx2hlsl_int_med3_controlsand added to the pure-Python CI list in.github/workflows/windows.ymland its mirror inAGENTS.md.docs/experiments.mdhas a short section.Verification
On
ba2df377(based onmainat889cd3f4), Windows 11 build 26300, Python 3.14.10:PASS: startup_gfx2hlsl_int_med3_check. For each of the six ops, in the bare spelling the repository disassembler prints and with an_e64suffix (the LLVM spelling, whichone()strips), the test:astevaluator (onlymin,max,int,uintand 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), theclampand-v2refusals, and that removing the six entries in-process refusesv_med3_i32again.PASSmain(889cd3f4)Unsupported: v_min3_i32v_med3_i32given the unsigned templatev_med3_i32Also 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
Stage.one, so the spellings are not checked againstllvm-mcortools/disasm_rdna2.py, and the decode of these VOP3 words is not covered. No title or package needed.VEC_Uops; these six could join it.Checklist
AI-assisted.
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:
v_min3_u32wrapped inint(...)(signed compare)v_min3_i32loses itsint(...)casts (unsigned compare)v_med3_u32formula drops a level (min(max(a,b), c))v_max3_u32becomes aminv_med3_i32loses the outeruint(...)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 theunsigned and the signed reading, 606,000 comparisons in total:
That includes the case where the signed and unsigned answers differ, e.g.
0x80000000versus0x7FFFFFFFis less signed andgreater unsigned, so a swapped reading cannot pass both.
Two things I would put in the description
v_med3_*is notmin(max(a,b), c): the emittedmax(min(a,b), min(max(a,b), c))is the three-value median, and it is worthsaying so, because the shorter form is what a reader will assume and it is wrong for the third operand.
uint(...)wrapper on the_i32forms is not decoration: the result word is auintregister, and dropping the wrapper (lastmutation above) changes the emitted type. That is the one line a copy-paste of this block will lose.
Scope
Parity with
pixel_aotis a claim about the other translator; I did not diff the two tables. The six forms here are exercised onlyby 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