Skip to content

startup: lower the binary16 class comparison - #1856

Closed
triggered02 wants to merge 4 commits into
R0Y00:mainfrom
triggered02:startup/cmp-class-f16
Closed

triggered02 wants to merge 4 commits into
R0Y00:mainfrom
triggered02:startup/cmp-class-f16

Conversation

@triggered02

Copy link
Copy Markdown
Contributor

Fixes #1855. Found in the refusal census (#870). 1 refused shader:

v_cmp_class_f16_e64 s14, 0x7c01, v0    -> REFUSED: v_cmp_class_f16

Problem

The binary16 class comparison had no lowering. The binary32 form is handled by the class-compare branch (PR #1798), but the half form needs a different helper: the value is the raw binary16 bit pattern in the low half of the operand word, the ten class bits follow the half format (exponent 5 bits, mantissa 10, quiet bit 0x0200), and an abs/neg modifier acts on bit 15 rather than bit 31.

Fix

tools/pixel_aot.py already carried the binary16 helper; it is ported into Pipeline.common() as class16, and the class-compare branch dispatches on the mnemonic:

if base in ('v_cmp_class_f32', 'v_cmpx_class_f32', 'v_cmp_class_f16', 'v_cmpx_class_f16'):
    f16 = base.endswith('_f16')
...
    cond = '(class16(%s) & %s) != 0u' % (bits, self.u(mask))   # f16

Measured on the branch:

form before after
v_cmp_class_f16_e64 s14, 0x7c01, v0 refused s14 = (((class16(0x00007c01u) & v0) != 0u) ? 1u : 0u);
v_cmpx_class_f16_e64 vcc_lo, v0, -1 refused exec = (((class16(v0) & 0xffffffffu) != 0u) ? 1u : 0u);
v_cmpx_class_f16_e32 v0, v5 refused exec = (((class16(v0) & v5) != 0u) ? 1u : 0u);

The abs/neg modifier forms lower to class16((v0 & 0x7fffu)) and class16((v0 ^ 0x8000u)). A float-printed operand is refused by name, because it would otherwise be lowered as its binary32 bit pattern.

Test

startup_gfx2hlsl_cmp_class_f16 builds VOPC-in-VOP3 encodings from the checked-in decoder's layout, decodes them, then translates. Ten checks: the census form, both exec-mask shapes, the abs/neg handling on bit 15, the float-operand refusal, and — value-level — a Python binary16 classifier (written from the IEEE half layout) compared against the emitted helper body evaluated over all 65,536 binary16 patterns.

dev:     3/3 (cmp_class, cmp_class_f16, startup_translator)
release: startup_gfx2hlsl_cmp_class_f16 Passed 3.18 s

Fail-first, three genuine class-bit mutations (all fail the value-level check while the text checks pass):

swap -inf/+inf bits        -> FAIL
swap quiet/signaling NaN   -> FAIL
swap -subnormal/+subnormal -> FAIL

Depends on #1798

This branch is based on startup/cmp-class (#1798) because the class-compare branch and class32 helper are not on main yet. Merge #1798 first; after that this PR's diff is only the class16 helper, the mnemonic dispatch and the new test.

What this does not establish

  • That the emitted HLSL compiles or computes the right predicate on a device — the test compares values from the emitted text; DXC/WARP were not run here.
  • v_cmp_class_f64 and the arithmetic/compare f16 forms that are not class comparisons are out of scope.

AI-assisted (Claude Code).

Fixes R0Y00#1797. v_cmp_class_f32 and v_cmpx_class_f32 had no lowering, so both census forms were refused - 2 of the remaining refused shaders in the loading-screen cache (R0Y00#870). The ten-class helper is ported from tools/pixel_aot.py into the shader preamble, and compare() lowers the predicate to (class32(value) & mask) != 0u with the mask read as an integer operand and the value as a float. The non-exec form writes VCC; the v_cmpx_ form writes EXEC and clears the straight-line assumption. Test builds VOPC-in-VOP3 encodings, decodes, then translates: both spellings, a literal mask, a register mask, and the helper's presence and NaN distinction in the common preamble. Fail-first: & -> | fails 'a register mask is read as an integer'; renaming the helper fails 'the class helper is emitted in the common preamble'. Both presets pass.
Review on R0Y00#1798 (1 P1, 2 P2). (P1) The ISA order is value in SRC0 and class-bit mask in SRC1; the branch had them swapped, so 'v_cmp_class_f32 vcc, v4, 0x200' tested class(mask) against the value instead of classifying v4 for +inf. (P2) A narrowed EXEC must stay narrowed: the predicate is now intersected with exec when exec_known is false, as the generic compare path does. (P2) The _e32 cmpx spelling prints no destination because it writes EXEC implicitly, so the two-operand form is accepted and routed to EXEC. The test now encodes the ISA operand order, adds the e32 exec-mask form, and asserts the intersection with a narrowed EXEC. Fail-first: swapping the operands back, removing the intersection, and rejecting the two-operand form each fail the test.
Review on R0Y00#1798 (P2): every class check was a substring match on the emitted text, so a wrong class bit passed. The test now carries a Python classifier written from the IEEE binary32 layout (sign, exponent, mantissa) and a small recursive-descent evaluator for the integer HLSL the helper emits; it runs the emitted helper body over 68626 bit patterns (18 edge cases plus every exponent with mantissa boundaries, plus a strided sweep of the whole 32-bit space) and requires the returned class bits to match the classifier. Fail-first: swapping the -inf/+inf bits, the quiet/signaling NaN bits, or the subnormal bits each fail this check while the old text checks still passed. Also added the abs/neg SRC0 cases: those modifiers arrive as operand text (|v4|, -v4) rather than a modifier list, so the helper must see the modified bits.
Fixes R0Y00#1855. v_cmp_class_f16 and v_cmpx_class_f16 had no lowering, so the census form v_cmp_class_f16_e64 s14, 0x7c01, v0 was refused. tools/pixel_aot.py already carried the binary16 helper; it is ported into Pipeline.common() as class16, and the class-compare branch dispatches on the mnemonic: the value is the raw binary16 bits in the low half of the word, abs/neg act on bit 15, and a float-printed operand is refused (it would be lowered as its binary32 bits). The _e32/_e64 and cmpx shapes share the binary32 path now with a generic v_cmpx_ check. Test builds VOPC encodings, decodes then translates, and checks the helper by value: a Python binary16 classifier against the emitted helper evaluated over all 65536 patterns; the three class-bit mutations fail it. Stacked on R0Y00#1798 (class-compare branch not on main yet).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T19:45:52.202076Z 7187e51 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7187e51d48

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/gfx2hlsl.py
if operand in FLOATS or re.fullmatch(r'-?\d+\.\d+', operand):
raise Unsupported('binary16 class operand %s' % value)
bits = self.u(operand)
if value.startswith('-') and not re.match(r'-[\d.]', value):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve NEG on immediate class operands

When a VOP3 class compare applies NEG to an immediate, the lowering can lose the sign because it infers modifiers from the printed token: for f16, literal 0x7c00 becomes -0x7c00, but this check treats the leading -0 as a numeric sign and skips the XOR, so class16 sees 0x8400 (negative normal) instead of 0xfc00 (-inf). The f32 arm likewise turns a negated inline zero (-0) into positive zero through self.f. Masks distinguishing those classes therefore produce wrong predicates; preserve the encoded modifier or refuse these numeric forms.

Useful? React with 👍 / 👎.

@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 7187e51d483bab89ae0d671c091567968feee431.

Verdict: 0 P1 / 0 P2. The lowering matches the RDNA 2 class-compare semantics as I read them. Comment only, not an approval.

Linux limits. Linux x86_64, Python 3. No Windows, no DXC, no GPU. I ran the Python checks directly; I did not configure CMake, so the two new add_test lines (startup_gfx2hlsl_cmp_class, startup_gfx2hlsl_cmp_class_f16) were not executed through ctest. Nothing here compiled the emitted HLSL; the helper agreement checks use the checks' own Python model of it.

What I checked

  • ISA class bits, in encoding order: sNaN, qNaN, -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf = 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200. Both class32 and class16 return exactly these. The quiet bit is 0x00400000 (binary32) and 0x0200 (binary16). The thresholds (0x00800000 / 0x0400 for normal) and the infinity/NaN cut (0x7f800000 / 0x7c00) are right.
  • Operand order is value first, mask second. The mask goes through self.u, so -1 becomes 0xffffffffu.
  • The _e32 v_cmpx_ spelling (2 operands, implicit EXEC) is handled. Other operand counts raise Unsupported.
  • For binary16 the value is read as raw bits, neg XORs 0x8000 and abs ANDs 0x7fff, and a float-printed operand (1.0, -0.5, ...) is refused. The refusal is the safe choice, because a float literal would otherwise be lowered as binary32 bits.
  • When EXEC is not known to be full, the predicate is intersected with it, as the generic compare path does; a v_cmpx_ form clears exec_known.

What I ran at this SHA (worktree at the exact head, git status clean after every mutation)

  • python3 -B tests/startup_gfx2hlsl_cmp_class_check.py: PASS (helper agrees with the binary32 classifier on 68626 patterns).
  • python3 -B tests/startup_gfx2hlsl_cmp_class_f16_check.py: PASS (all 65536 binary16 patterns).
  • All 11 neighbouring tests/startup_gfx2hlsl_*_check.py scripts: exit 0.
  • tests/docs_links_check.py (17 files, 367 links), tests/bidi_controls_check.py (1309 files), git diff --check origin/main...HEAD: clean.
  • Mutations in tools/startup/gfx2hlsl.py, each followed by both checks and then git checkout -- .. Every one was caught by at least one check:
Mutation f32 check f16 check
f32 sNaN/qNaN result swapped fail pass
f32 -inf/+inf swapped fail pass
f16 normal threshold >= to > pass fail
f16 neg XORs bit 31 pass fail
f16 abs masks 0x7fffffff pass fail
EXEC intersection removed fail pass
v_cmpx_ keeps exec_known fail fail
f16 float operand allowed pass fail
mask read as float fail pass

Findings (non-blocking)

  1. P3, stacking: this branch carries the whole binary32 change (the class32 helper, its test, its docs paragraph, its CMake entry), which is also the content of open #1798. Whichever lands second will conflict or duplicate. The title and the Fixes #1855 line name only binary16. Please land #1798 first and rebase, or say here that this PR supersedes it.
  2. P3, modifiers: compare() is called as self.compare(base, ops), so trailing modifiers (clamp, op_sel:[...]) are dropped silently for the class forms. That is also true of the existing generic compare, so it is not new, and I did not find a census form that prints one. #1778 ("refuse a modifier for handlers that do not model one") looks like the right place to cover this; it would be worth checking that it reaches compare once both are merged.
  3. P3, evidence: the checks encode the instruction bytes by hand from the decoder's field layout, and the helper is tested against a Python model. Neither confirms HLSL compilation or the draw result. That is the same footing as the neighbouring startup lowering checks.

Not run here: ctest, DXC compile of the emitted shader, any Windows or GPU run.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@triggered02 could you rebase this branch onto current main and force-push?

GitHub reports head 7187e51d as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (cf97bee1):

  • CMakeLists.txt
  • docs/experiments.md
  • tests/startup_gfx2hlsl_cmp_class_f16_check.py
  • tools/startup/gfx2hlsl.py

Commands run locally:

git fetch origin main pull/1856/head
git merge-tree --write-tree --name-only origin/main FETCH_HEAD
gh pr view 1856 --json mergeable   # CONFLICTING

Once it is rebased and CI is green it will be re-checked right away.

AI-assisted; commands listed were executed locally.

@triggered02

Copy link
Copy Markdown
Contributor Author

Closing: this content is already on main. I checked origin/main and it has the class16 helper, the v_cmp_class_f16/v_cmpx_class_f16 dispatch inside the class branch (with the base.startswith('v_cmpx_') shape) and tests/startup_gfx2hlsl_cmp_class_f16_check.py — i.e. the branch was integrated directly rather than through this PR, which is why the PR still showed as conflicting.

For the record, the substance of the branch is in main:

  • uint class16(uint bits) with the ten binary16 class bits and the quiet bit at 0x0200,
  • the value read as raw binary16 bits in the low half of the word (abs/neg acting on bit 15),
  • a float-printed operand refused by name,
  • and the value-level test that evaluates the emitted helper over all 65,536 binary16 patterns against a Python classifier, which is what caught the three class-bit mutations.

Nothing more to do here; thanks for integrating it.

AI-assisted (Claude Code).

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.

startup: v_cmp_class_f16 has no lowering (binary16 class compare)

2 participants