Skip to content

startup: lower v_cmp_class_f16 and v_cmpx_class_f16 (issue #1855) - #2092

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
ChazY11:startup/cmp-class-f16
Nov 25, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
ChazY11:startup/cmp-class-f16

Conversation

@ChazY11

@ChazY11 ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What

Lowers v_cmp_class_f16 and v_cmpx_class_f16 in tools/startup/gfx2hlsl.py, closing the census refusal in #1855:

v_cmp_class_f16_e64 s14, 0x7c01, v0    -> REFUSED

tools/pixel_aot.py already carried the ten-class half helper, so this ports it into Pipeline.common() as class16, next to class32, and
dispatches the half mnemonics in the class-compare branch.

The two things that make the half form different

  1. The classes follow bits [15:0], not the binary32 layout: exponent 5 bits, mantissa 10, quiet bit 0x0200. The value is therefore the raw
    operand word
    , never self.f(value) — converting would test the wrong bit pattern, and an inline 0x7c01 would arrive as a binary32 float.
  2. abs/neg act on bit 15, so they are applied to the raw word before the helper: (v0 & 0x7fffu) and (v0 ^ 0x8000u). The decoder prints an
    absolute source as |v4| and a negated one as -v4, so both spellings are accepted alongside abs(...).

A float-printed operand is refused by name, because lowering it would take its binary32 pattern as if it were a half:

v_cmp_class_f16_e64 s14, 1.0, v0  -> Unsupported: source 1.0: f16 class compares need the raw half

Emitted forms

v_cmp_class_f16_e64 s14, 0x7c01, v0   -> s14 = (((class16(0x00007c01u) & v0) != 0u) ? 1u : 0u);
v_cmpx_class_f16_e64 vcc_lo, v0, -1   -> exec = (((class16(v0) & 0xffffffffu) != 0u) ? 1u : 0u);
v_cmpx_class_f16_e32 v0, v5           -> exec = (((class16(v0) & v5) != 0u) ? 1u : 0u);
v_cmp_class_f16_e64 s14, |v0|, v1     -> s14 = (((class16((v0 & 0x7fffu)) & v1) != 0u) ? 1u : 0u);
v_cmp_class_f16_e64 s14, -v0, v1      -> s14 = (((class16((v0 ^ 0x8000u)) & v1) != 0u) ? 1u : 0u);

Test

tests/startup_gfx2hlsl_cmp_class_f16_check.py builds the VOPC-in-VOP3 encodings from the checked-in decoder's layout (tools/disasm_rdna2.py,
opcodes 0x8f and 0x9f), decodes them, translates, and checks the emitted text for each form above. Then it does the value-level check: a binary16
classifier written from the IEEE half layout, compared against the emitted helper body evaluated over all 65,536 halves.

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_cmp_class_f16_check.py
the emitted helper agrees with the binary16 classifier on all 65536 halves: ok
startup gfx2hlsl half class compare checks: ok          exit 0

Registered as startup_gfx2hlsl_cmp_class_f16 beside startup_gfx2hlsl_cmp_class in CMakeLists.txt — the same place, and the same
CMake-only route, as the binary32 sibling it mirrors.

Fail-first controls, six of them

Each reverts or breaks one clause, and each fails on the assertion that covers it while the other text checks still pass:

control fails
swap -inf/+inf bits the 65,536-half agreement
swap quiet/signaling NaN bits the 65,536-half agreement
swap -subnormal/+subnormal bits the 65,536-half agreement
abs mask 0x7fffu → the float 0x7fffffffu "an absolute half clears bit 15"
neg xor 0x8000u → the float 0x80000000u "a negated half flips bit 15"
the value goes through self.f() "the value is the raw half word, not a converted float"

The first three are the ones the issue asked for, and they fail at the value level rather than the text level — which is the point: a wrong class
bit produces plausible-looking HLSL.

Whole family still green

All 28 tests/startup_gfx2hlsl_*_check.py exit 0 on this branch, including the binary32 sibling startup_gfx2hlsl_cmp_class_check.py, which shares
the branch this change edits.

What this does not establish

That the emitted HLSL compiles or computes the right predicate on a device. The value check evaluates the helper text in Python; DXC and WARP were not
run for this change. If you want that, it should be a draw fixture, and I would add it as a separate change rather than implying this one was
executed.

Also unchanged and still shared with the binary32 path: a pair operand (v[0:1]) is accepted and the low word is used. That predates this change — the
binary32 compare does the same — so I left it alone rather than mixing a second behaviour into this PR.

Refs #1855. AI-assisted.

The binary16 class comparison had no lowering, so the census form
`v_cmp_class_f16_e64 s14, 0x7c01, v0` was refused (R0Y00#870).

`tools/pixel_aot.py` already carried the ten-class half helper; it is ported into `Pipeline.common()` as
`class16`, next to `class32`, and the class-compare branch dispatches on the mnemonic.

The half classes follow bits [15:0] (exponent 5 bits, mantissa 10, quiet bit 0x0200), so the value is the raw
operand word rather than a converted float, and `abs`/`neg` act on bit 15. The decoder prints an absolute
source as `|v4|` and a negated one as `-v4`, so both spellings are accepted alongside `abs(...)`.

    v_cmp_class_f16_e64 s14, 0x7c01, v0    -> s14 = (((class16(0x00007c01u) & v0) != 0u) ? 1u : 0u);
    v_cmpx_class_f16_e64 vcc_lo, v0, -1    -> exec = (((class16(v0) & 0xffffffffu) != 0u) ? 1u : 0u);
    v_cmpx_class_f16_e32 v0, v5            -> exec = (((class16(v0) & v5) != 0u) ? 1u : 0u);
    v_cmp_class_f16_e64 s14, |v0|, v1      -> s14 = (((class16((v0 & 0x7fffu)) & v1) != 0u) ? 1u : 0u);
    v_cmp_class_f16_e64 s14, -v0, v1       -> s14 = (((class16((v0 ^ 0x8000u)) & v1) != 0u) ? 1u : 0u);

A float-printed operand is refused by name, because it would otherwise be lowered as its binary32 bit pattern.

The test builds the VOPC-in-VOP3 encodings from the checked-in decoder's layout, decodes, translates, then
checks the emitted text for each form above, the float-operand refusal, and - value level - a binary16
classifier written from the IEEE half layout against the emitted helper body evaluated over all 65,536 halves.

Fail-first, three category-bit mutations (each fails the value check while the text checks pass):

    swap -inf/+inf bits          -> the emitted helper disagrees (exit 1)
    swap quiet/signaling NaN     -> the emitted helper disagrees (exit 1)
    swap -subnormal/+subnormal   -> the emitted helper disagrees (exit 1)

Not established here: that the emitted HLSL compiles or computes the right predicate on a device. The value
check evaluates the helper text in Python; DXC and WARP were not run for this change.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 4b15ae0 (comment only).

Scope: tools/startup/gfx2hlsl.py lowers v_cmp_class_f16 / v_cmpx_class_f16 through a new class16 helper on the raw half word. abs/neg act on bit 15. A float-printed operand is refused. CMakeLists.txt only registers the new test. tests/startup_gfx2hlsl_cmp_class_f16_check.py builds its instruction bytes from the decoder field layout, with no copied vendor text. Clean-room looks fine.

P1: 0. P2: 0.

Commands executed locally (Linux, in a worktree of this head):

  • python3 -B tests/startup_gfx2hlsl_cmp_class_f16_check.py PASS (including the 65536-half classifier agreement check)
  • python3 -B tests/startup_gfx2hlsl_cmp_class_check.py PASS (binary32 sibling)
  • python3 tests/docs_links_check.py PASS
  • python3 tests/bidi_controls_check.py PASS
  • git diff --check origin/main...HEAD clean
  • git merge-tree --write-tree --name-only origin/main HEAD no conflicts

Mutations (reverted afterwards):

  • Normal-half threshold 0x0400u -> 0x0800u in class16: killed (the 65536-half agreement check reports MISMATCH).
  • Negation ^ 0x8000u -> ^ 0x80000000u: killed (a negated half flips bit 15 reports MISMATCH).

Windows CI was still pending when this was written.

AI-assisted; commands listed were executed locally.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — that is a more useful review than the CI alone would have been, and two things in it are new to me.

Windows CI finished since your comment

Assemble and translate gfx1013 fixtures        pass 2m8s
Build and test on Windows                      pass 3m44s
Run platform-independent Python checks (Linux) pass 37s

so the platforms are green in both directions, not just the Linux side you ran.

Your 0x0400 → 0x0800 mutation is the better version of what I ran

I mutated the three category bits the issue listed (-inf/+inf, quiet/signaling NaN, -subnormal/+subnormal) and separately caught the abs/neg mixups,
but I did not mutate the normal/denormal boundary itself — which is the one threshold that has no name in the issue, and the one where a wrong
exponent bias would hide: an off-by-one there turns every denormal into a normal and the class table still "looks right" for the values a fixture
usually picks. It is killed by the 65,536-half sweep, which is the point of sweeping all of them rather than sampling.

The one behavioural gap I know about, stated so it is not a surprise later

A pair operand is accepted and the low word is used:

v_cmp_class_f16_e64 s14, v[0:1], v1   -> s14 = (((class16(v0) & v1) != 0u) ? 1u : 0u);
v_cmp_class_f32_e64 s14, v[0:1], v1   -> s14 = (((class32(F(v0)) & v1) != 0u) ? 1u : 0u);

The binary32 path does the same thing, so it predates this PR and I left it alone rather than mixing a second behaviour in. If you would rather the half
form refuse a 64-bit source — an f16 operand is 16 bits, so a pair is arguably malformed — that is a two-line change and I will add it here, but it
would then differ from v_cmp_class_f32 until that one is changed too.

What is still unestablished, in your terms

Nothing in the test says the emitted HLSL computes the right predicate on a device. If you want that closed before merge, say so and I will add the draw
fixture as a separate PR rather than widening this one; the value check evaluates the helper text in Python, and I would rather that stay labelled as
what it is.

AI-assisted.

@R0Y00

R0Y00 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

I added the reproduction notes and kept this slice to the e32/e64 raw-half forms. The census literal reaches the helper unchanged, ambiguous signed inline values and floating class masks are refused, and half SDWA stays outside this patch. The 65,536-value check, decoded operands and refusal controls passed, along with the existing f32 class and SDWA checks. The SDWA work in #2020 can remain a separate follow-up.

@R0Y00
R0Y00 merged commit 8e17f8f into R0Y00:main Nov 25, 2026
5 checks passed
JB2WIN added a commit to JB2WIN/Xbox2Windows that referenced this pull request Nov 25, 2026
The plain e32/e64 register and encoded-literal path, including the 0x7c01
census form, landed with R0Y00#2092 and is used unchanged. It still refused the
SDWA spelling outright. This lowers v_cmp_class_f16_sdwa and
v_cmpx_class_f16_sdwa through the same class16() helper and the same
predicate/exec/destination code.

src0_sel DWORD and WORD_0 are accepted on a plain register (class16 reads
bits [15:0] itself); src1_sel selects and sign-extends the integer mask as
the binary32 class compare does. WORD_1, bytes, sext() on src0, the abs/neg
spellings, a missing or repeated select, a float source or mask and a bad
destination are refused, WORD_1 and bytes with the existing "SDWA form of"
reason.

New check startup_gfx2hlsl_cmp_class_f16_sdwa_check.py: accepted forms times
destination/select/register file against a struct 'e' reference with garbage
in the upper half, refusals, and three mutated helpers (upper half read,
sNaN/qNaN swapped, +0/-0 swapped) that must disagree. The draw mode (warp and
hardware) draws a register with a qNaN upper half, a clean register, the
encoded literal 0x7c01, the e32 spelling and both SDWA selects against one
reference image; classifying the upper half, swapping sNaN/qNaN and ignoring
the BYTE_1 mask select must each fail it.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tedin7 added a commit that referenced this pull request Nov 26, 2026
…per (#2020)

The plain e32/e64 register and encoded-literal path, including the 0x7c01
census form, landed with #2092 and is used unchanged. It still refused the
SDWA spelling outright. This lowers v_cmp_class_f16_sdwa and
v_cmpx_class_f16_sdwa through the same class16() helper and the same
predicate/exec/destination code.

src0_sel DWORD and WORD_0 are accepted on a plain register (class16 reads
bits [15:0] itself); src1_sel selects and sign-extends the integer mask as
the binary32 class compare does. WORD_1, bytes, sext() on src0, the abs/neg
spellings, a missing or repeated select, a float source or mask and a bad
destination are refused, WORD_1 and bytes with the existing "SDWA form of"
reason.

New check startup_gfx2hlsl_cmp_class_f16_sdwa_check.py: accepted forms times
destination/select/register file against a struct 'e' reference with garbage
in the upper half, refusals, and three mutated helpers (upper half read,
sNaN/qNaN swapped, +0/-0 swapped) that must disagree. The draw mode (warp and
hardware) draws a register with a qNaN upper half, a clean register, the
encoded literal 0x7c01, the e32 spelling and both SDWA selects against one
reference image; classifying the upper half, swapping sNaN/qNaN and ignoring
the BYTE_1 mask select must each fail it.

AI-assisted (Claude Code).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: tedin7 <tommasoderosa94@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants