shader: lower packed VOP3P f16/i16/u16 and dot-product ops in gfx2hlsl (Part of #1723) #2074

Closed
tedin7 wants to merge 5 commits from upstream-pr/2081 into main
tedin7 commented 2026-11-25 00:04:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:04:05Z.


Part of #1723

What this unblocks: shaders that use packed 16-bit math or dot products (v_pk_*_f16/i16/u16, v_dot2_f32_f16, v_dot4_u32_u8) no longer stop translation in gfx2hlsl.

Lowers each packed op to two independent 16-bit lanes, honouring op_sel, op_sel_hi, neg_lo, neg_hi and clamp where they make sense. Float lanes are computed in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 already does. Not modelled, so still refused: neg_* and clamp on integer lanes, any modifier on v_dot4_u32_u8, modifiers on the dot2 accumulator, and float inline constants as packed sources. v_pk_sub_f16 does not exist in the ISA table, so it is not added.

Verification:

  • python3 -B tests/startup_gfx2hlsl_vop3p_check.py on this branch: all checks ok, exit 0.
  • Same test on origin/main (detached copy): fails with Unsupported: v_pk_add_f16.
  • python3 -B tests/startup_gfx2hlsl_{opsel,refusals,op_refusals,operand_count}_check.py: all exit 0.
  • Not run: Win2025 DXC dispatch. The test checks the emitted HLSL text and the refusals only.

Registered in CMakeLists.txt as startup_gfx2hlsl_vop3p.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593992397 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:04:05Z. --- Part of #1723 What this unblocks: shaders that use packed 16-bit math or dot products (v_pk_*_f16/i16/u16, v_dot2_f32_f16, v_dot4_u32_u8) no longer stop translation in gfx2hlsl. Lowers each packed op to two independent 16-bit lanes, honouring op_sel, op_sel_hi, neg_lo, neg_hi and clamp where they make sense. Float lanes are computed in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 already does. Not modelled, so still refused: neg_* and clamp on integer lanes, any modifier on v_dot4_u32_u8, modifiers on the dot2 accumulator, and float inline constants as packed sources. v_pk_sub_f16 does not exist in the ISA table, so it is not added. Verification: - `python3 -B tests/startup_gfx2hlsl_vop3p_check.py` on this branch: all checks ok, exit 0. - Same test on origin/main (detached copy): fails with `Unsupported: v_pk_add_f16`. - `python3 -B tests/startup_gfx2hlsl_{opsel,refusals,op_refusals,operand_count}_check.py`: all exit 0. - Not run: Win2025 DXC dispatch. The test checks the emitted HLSL text and the refusals only. Registered in CMakeLists.txt as `startup_gfx2hlsl_vop3p`. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-25 01:01:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:10:10Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 203fbf01b5

ℹ️ 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".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049514784 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#pullrequestreview-6049514784). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:10:10Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `203fbf01b5` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). 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". </details>
chatgpt-codex-connector[bot commented 2026-11-25 01:02:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:04:19Z.


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-25T00:10:12.639090Z 203fbf0 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148106119 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#issuecomment-7148106119). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:04:19Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"203fbf01b505eda92f51cc105a994f68d68853ab","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-25T00:10:12.639090Z">2026-11-25T00:10:12.639090Z</relative-time> | `203fbf0` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). 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. </details> </details>
chatgpt-codex-connector[bot commented 2026-11-25 01:02:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:10:10Z.

Code review location: tools/startup/gfx2hlsl.py; line 1636

@@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods):
 
     def op_v_ashrrev_i64(self, ops, mods):
         self.shift64(ops, mods, 'shr64i')
+
     def op_v_cvt_pkrtz_f16_f32(self, ops, mods):
         self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2])))
 
+    # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half
+    # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed
+    # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does.
+    def pk_modifiers(self, mods, arity, allow_neg, allow_clamp):
+        fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity}
+        clamp = False
+        for m in mods:
+            if m == 'clamp' and allow_clamp:
+                clamp = True
+                continue
+            match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m)
+            if not match or (match.group(1).startswith('neg') and not allow_neg):
+                raise Unsupported('modifier %s' % m)
+            bits = [int(b) for b in match.group(2).split(',')]
+            if len(bits) != arity:
+                raise Unsupported('modifier %s for %d sources' % (m, arity))
+            fields[match.group(1)] = bits
+        return fields, clamp
+
+    def pk_half(self, tok, high_half):
+        if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok):
+            raise Unsupported('packed operand %s' % tok)
+        word = self.u(tok)
+        return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word
+
+    def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None):
+        """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources."""
+        if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v'
+                                         and reg_range(ops[0])[1] == reg_range(ops[0])[2]):
+            raise Unsupported('packed operation operands %s' % ', '.join(ops))
+        fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok)
+        lanes = ([], [])
+        for index, tok in enumerate(ops[1:1 + (packed or arity)]):
+            for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))):
+                half = self.pk_half(tok, sel[index])
+                if float_lanes:
+                    half = 'f16tof32(%s)' % half
+                    if neg[index]:
+                        half = '(-%s)' % half
+                lanes[lane].append(half)
+        return lanes[0], lanes[1], clamp, fields
+
+    def pk_float(self, ops, mods, combine):
+        low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True)
+        results = []
+        for lane in (low, high):
+            expr = combine(*lane)
+            results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr))
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def pk_int(self, ops, mods, combine, signed):
+        low, high, _, _ = self.pk_lanes(ops, mods, 2)
+        wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x)
+        results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)]

P1 Badge Parenthesize signed half-lane expressions before combining them

For v_pk_add_i16 and v_pk_sub_i16, wrap() returns an unparenthesized int(...) >> 16. Combining two such strings emits expressions like int(...) >> 16 + int(...) >> 16, where addition/subtraction binds more tightly than shifting, so the second operand becomes part of the first shift count rather than an arithmetic operand. With v1 = 0x00010001 and v2 = 0, both handlers consequently evaluate to 0 instead of 0x00010001. Parenthesize each complete sign-extension expression before passing it to combine, and cover the resulting values in the regression test.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688377011 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#discussion_r4688377011). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:10:10Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1636 ~~~~diff @@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods): def op_v_ashrrev_i64(self, ops, mods): self.shift64(ops, mods, 'shr64i') + def op_v_cvt_pkrtz_f16_f32(self, ops, mods): self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2]))) + # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half + # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed + # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does. + def pk_modifiers(self, mods, arity, allow_neg, allow_clamp): + fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity} + clamp = False + for m in mods: + if m == 'clamp' and allow_clamp: + clamp = True + continue + match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m) + if not match or (match.group(1).startswith('neg') and not allow_neg): + raise Unsupported('modifier %s' % m) + bits = [int(b) for b in match.group(2).split(',')] + if len(bits) != arity: + raise Unsupported('modifier %s for %d sources' % (m, arity)) + fields[match.group(1)] = bits + return fields, clamp + + def pk_half(self, tok, high_half): + if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok): + raise Unsupported('packed operand %s' % tok) + word = self.u(tok) + return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word + + def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None): + """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources.""" + if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v' + and reg_range(ops[0])[1] == reg_range(ops[0])[2]): + raise Unsupported('packed operation operands %s' % ', '.join(ops)) + fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok) + lanes = ([], []) + for index, tok in enumerate(ops[1:1 + (packed or arity)]): + for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))): + half = self.pk_half(tok, sel[index]) + if float_lanes: + half = 'f16tof32(%s)' % half + if neg[index]: + half = '(-%s)' % half + lanes[lane].append(half) + return lanes[0], lanes[1], clamp, fields + + def pk_float(self, ops, mods, combine): + low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True) + results = [] + for lane in (low, high): + expr = combine(*lane) + results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr)) + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def pk_int(self, ops, mods, combine, signed): + low, high, _, _ = self.pk_lanes(ops, mods, 2) + wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x) + results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)] ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Parenthesize signed half-lane expressions before combining them** For `v_pk_add_i16` and `v_pk_sub_i16`, `wrap()` returns an unparenthesized `int(...) >> 16`. Combining two such strings emits expressions like `int(...) >> 16 + int(...) >> 16`, where addition/subtraction binds more tightly than shifting, so the second operand becomes part of the first shift count rather than an arithmetic operand. With `v1 = 0x00010001` and `v2 = 0`, both handlers consequently evaluate to `0` instead of `0x00010001`. Parenthesize each complete sign-extension expression before passing it to `combine`, and cover the resulting values in the regression test. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-25 01:02:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T00:10:10Z.

Code review location: tools/startup/gfx2hlsl.py; line 1652

@@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods):
 
     def op_v_ashrrev_i64(self, ops, mods):
         self.shift64(ops, mods, 'shr64i')
+
     def op_v_cvt_pkrtz_f16_f32(self, ops, mods):
         self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2])))
 
+    # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half
+    # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed
+    # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does.
+    def pk_modifiers(self, mods, arity, allow_neg, allow_clamp):
+        fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity}
+        clamp = False
+        for m in mods:
+            if m == 'clamp' and allow_clamp:
+                clamp = True
+                continue
+            match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m)
+            if not match or (match.group(1).startswith('neg') and not allow_neg):
+                raise Unsupported('modifier %s' % m)
+            bits = [int(b) for b in match.group(2).split(',')]
+            if len(bits) != arity:
+                raise Unsupported('modifier %s for %d sources' % (m, arity))
+            fields[match.group(1)] = bits
+        return fields, clamp
+
+    def pk_half(self, tok, high_half):
+        if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok):
+            raise Unsupported('packed operand %s' % tok)
+        word = self.u(tok)
+        return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word
+
+    def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None):
+        """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources."""
+        if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v'
+                                         and reg_range(ops[0])[1] == reg_range(ops[0])[2]):
+            raise Unsupported('packed operation operands %s' % ', '.join(ops))
+        fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok)
+        lanes = ([], [])
+        for index, tok in enumerate(ops[1:1 + (packed or arity)]):
+            for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))):
+                half = self.pk_half(tok, sel[index])
+                if float_lanes:
+                    half = 'f16tof32(%s)' % half
+                    if neg[index]:
+                        half = '(-%s)' % half
+                lanes[lane].append(half)
+        return lanes[0], lanes[1], clamp, fields
+
+    def pk_float(self, ops, mods, combine):
+        low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True)
+        results = []
+        for lane in (low, high):
+            expr = combine(*lane)
+            results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr))
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def pk_int(self, ops, mods, combine, signed):
+        low, high, _, _ = self.pk_lanes(ops, mods, 2)
+        wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x)
+        results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)]
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def op_v_pk_add_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: '%s + %s' % (a, b))
+
+    def op_v_pk_mul_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: '%s * %s' % (a, b))
+
+    def op_v_pk_max_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: 'max(%s, %s)' % (a, b))
+
+    def op_v_pk_min_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: 'min(%s, %s)' % (a, b))
+
+    def op_v_pk_fma_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b, c: '%s * %s + %s' % (a, b, c))

P2 Badge Preserve single-rounding semantics for packed f16 FMA

For RNE v_pk_fma_f16 lanes near a binary16 midpoint, evaluating the expression in binary32 before narrowing introduces double rounding. For example, finite normal half encodings a = 0x6a00, b = 0x3c01, and c = 0x8400 have the correctly fused result 0x6a01, but this lowering first rounds to binary32 3075.0 and then produces 0x6a02 under RNE. This discrepancy remains even if the compiler contracts the multiply/add into a binary32 FMA. Use a single-round binary16 implementation, such as the approach already implemented by fma16_rne in tools/pixel_aot.py, rather than routing this instruction through the generic binary32 calculation.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688377018 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#discussion_r4688377018). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T00:10:10Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1652 ~~~~diff @@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods): def op_v_ashrrev_i64(self, ops, mods): self.shift64(ops, mods, 'shr64i') + def op_v_cvt_pkrtz_f16_f32(self, ops, mods): self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2]))) + # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half + # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed + # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does. + def pk_modifiers(self, mods, arity, allow_neg, allow_clamp): + fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity} + clamp = False + for m in mods: + if m == 'clamp' and allow_clamp: + clamp = True + continue + match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m) + if not match or (match.group(1).startswith('neg') and not allow_neg): + raise Unsupported('modifier %s' % m) + bits = [int(b) for b in match.group(2).split(',')] + if len(bits) != arity: + raise Unsupported('modifier %s for %d sources' % (m, arity)) + fields[match.group(1)] = bits + return fields, clamp + + def pk_half(self, tok, high_half): + if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok): + raise Unsupported('packed operand %s' % tok) + word = self.u(tok) + return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word + + def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None): + """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources.""" + if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v' + and reg_range(ops[0])[1] == reg_range(ops[0])[2]): + raise Unsupported('packed operation operands %s' % ', '.join(ops)) + fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok) + lanes = ([], []) + for index, tok in enumerate(ops[1:1 + (packed or arity)]): + for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))): + half = self.pk_half(tok, sel[index]) + if float_lanes: + half = 'f16tof32(%s)' % half + if neg[index]: + half = '(-%s)' % half + lanes[lane].append(half) + return lanes[0], lanes[1], clamp, fields + + def pk_float(self, ops, mods, combine): + low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True) + results = [] + for lane in (low, high): + expr = combine(*lane) + results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr)) + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def pk_int(self, ops, mods, combine, signed): + low, high, _, _ = self.pk_lanes(ops, mods, 2) + wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x) + results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)] + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def op_v_pk_add_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: '%s + %s' % (a, b)) + + def op_v_pk_mul_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: '%s * %s' % (a, b)) + + def op_v_pk_max_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: 'max(%s, %s)' % (a, b)) + + def op_v_pk_min_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: 'min(%s, %s)' % (a, b)) + + def op_v_pk_fma_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b, c: '%s * %s + %s' % (a, b, c)) ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve single-rounding semantics for packed f16 FMA** For RNE `v_pk_fma_f16` lanes near a binary16 midpoint, evaluating the expression in binary32 before narrowing introduces double rounding. For example, finite normal half encodings `a = 0x6a00`, `b = 0x3c01`, and `c = 0x8400` have the correctly fused result `0x6a01`, but this lowering first rounds to binary32 `3075.0` and then produces `0x6a02` under RNE. This discrepancy remains even if the compiler contracts the multiply/add into a binary32 FMA. Use a single-round binary16 implementation, such as the approach already implemented by `fma16_rne` in `tools/pixel_aot.py`, rather than routing this instruction through the generic binary32 calculation. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
tedin7 commented 2026-11-25 02:01:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:43Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049955300 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#pullrequestreview-6049955300). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:02:43Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:01:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:44Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049955488 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#pullrequestreview-6049955488). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:02:44Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 02:02:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:43Z.

Code review location: tools/startup/gfx2hlsl.py; line 1636

@@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods):
 
     def op_v_ashrrev_i64(self, ops, mods):
         self.shift64(ops, mods, 'shr64i')
+
     def op_v_cvt_pkrtz_f16_f32(self, ops, mods):
         self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2])))
 
+    # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half
+    # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed
+    # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does.
+    def pk_modifiers(self, mods, arity, allow_neg, allow_clamp):
+        fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity}
+        clamp = False
+        for m in mods:
+            if m == 'clamp' and allow_clamp:
+                clamp = True
+                continue
+            match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m)
+            if not match or (match.group(1).startswith('neg') and not allow_neg):
+                raise Unsupported('modifier %s' % m)
+            bits = [int(b) for b in match.group(2).split(',')]
+            if len(bits) != arity:
+                raise Unsupported('modifier %s for %d sources' % (m, arity))
+            fields[match.group(1)] = bits
+        return fields, clamp
+
+    def pk_half(self, tok, high_half):
+        if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok):
+            raise Unsupported('packed operand %s' % tok)
+        word = self.u(tok)
+        return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word
+
+    def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None):
+        """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources."""
+        if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v'
+                                         and reg_range(ops[0])[1] == reg_range(ops[0])[2]):
+            raise Unsupported('packed operation operands %s' % ', '.join(ops))
+        fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok)
+        lanes = ([], [])
+        for index, tok in enumerate(ops[1:1 + (packed or arity)]):
+            for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))):
+                half = self.pk_half(tok, sel[index])
+                if float_lanes:
+                    half = 'f16tof32(%s)' % half
+                    if neg[index]:
+                        half = '(-%s)' % half
+                lanes[lane].append(half)
+        return lanes[0], lanes[1], clamp, fields
+
+    def pk_float(self, ops, mods, combine):
+        low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True)
+        results = []
+        for lane in (low, high):
+            expr = combine(*lane)
+            results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr))
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def pk_int(self, ops, mods, combine, signed):
+        low, high, _, _ = self.pk_lanes(ops, mods, 2)
+        wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x)
+        results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)]

P1: the sign extension is parenthesized (07b3b365, already in the branch) and the values test covers v1=0x00010001, v2=0. Verified with python3 tests/startup_gfx2hlsl_vop3p_check.py. AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688767725 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#discussion_r4688767725). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:02:43Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1636 ~~~~diff @@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods): def op_v_ashrrev_i64(self, ops, mods): self.shift64(ops, mods, 'shr64i') + def op_v_cvt_pkrtz_f16_f32(self, ops, mods): self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2]))) + # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half + # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed + # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does. + def pk_modifiers(self, mods, arity, allow_neg, allow_clamp): + fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity} + clamp = False + for m in mods: + if m == 'clamp' and allow_clamp: + clamp = True + continue + match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m) + if not match or (match.group(1).startswith('neg') and not allow_neg): + raise Unsupported('modifier %s' % m) + bits = [int(b) for b in match.group(2).split(',')] + if len(bits) != arity: + raise Unsupported('modifier %s for %d sources' % (m, arity)) + fields[match.group(1)] = bits + return fields, clamp + + def pk_half(self, tok, high_half): + if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok): + raise Unsupported('packed operand %s' % tok) + word = self.u(tok) + return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word + + def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None): + """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources.""" + if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v' + and reg_range(ops[0])[1] == reg_range(ops[0])[2]): + raise Unsupported('packed operation operands %s' % ', '.join(ops)) + fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok) + lanes = ([], []) + for index, tok in enumerate(ops[1:1 + (packed or arity)]): + for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))): + half = self.pk_half(tok, sel[index]) + if float_lanes: + half = 'f16tof32(%s)' % half + if neg[index]: + half = '(-%s)' % half + lanes[lane].append(half) + return lanes[0], lanes[1], clamp, fields + + def pk_float(self, ops, mods, combine): + low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True) + results = [] + for lane in (low, high): + expr = combine(*lane) + results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr)) + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def pk_int(self, ops, mods, combine, signed): + low, high, _, _ = self.pk_lanes(ops, mods, 2) + wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x) + results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)] ~~~~ --- P1: the sign extension is parenthesized (07b3b365, already in the branch) and the values test covers v1=0x00010001, v2=0. Verified with python3 tests/startup_gfx2hlsl_vop3p_check.py. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 02:02:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T01:02:44Z.

Code review location: tools/startup/gfx2hlsl.py; line 1652

@@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods):
 
     def op_v_ashrrev_i64(self, ops, mods):
         self.shift64(ops, mods, 'shr64i')
+
     def op_v_cvt_pkrtz_f16_f32(self, ops, mods):
         self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2])))
 
+    # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half
+    # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed
+    # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does.
+    def pk_modifiers(self, mods, arity, allow_neg, allow_clamp):
+        fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity}
+        clamp = False
+        for m in mods:
+            if m == 'clamp' and allow_clamp:
+                clamp = True
+                continue
+            match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m)
+            if not match or (match.group(1).startswith('neg') and not allow_neg):
+                raise Unsupported('modifier %s' % m)
+            bits = [int(b) for b in match.group(2).split(',')]
+            if len(bits) != arity:
+                raise Unsupported('modifier %s for %d sources' % (m, arity))
+            fields[match.group(1)] = bits
+        return fields, clamp
+
+    def pk_half(self, tok, high_half):
+        if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok):
+            raise Unsupported('packed operand %s' % tok)
+        word = self.u(tok)
+        return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word
+
+    def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None):
+        """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources."""
+        if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v'
+                                         and reg_range(ops[0])[1] == reg_range(ops[0])[2]):
+            raise Unsupported('packed operation operands %s' % ', '.join(ops))
+        fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok)
+        lanes = ([], [])
+        for index, tok in enumerate(ops[1:1 + (packed or arity)]):
+            for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))):
+                half = self.pk_half(tok, sel[index])
+                if float_lanes:
+                    half = 'f16tof32(%s)' % half
+                    if neg[index]:
+                        half = '(-%s)' % half
+                lanes[lane].append(half)
+        return lanes[0], lanes[1], clamp, fields
+
+    def pk_float(self, ops, mods, combine):
+        low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True)
+        results = []
+        for lane in (low, high):
+            expr = combine(*lane)
+            results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr))
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def pk_int(self, ops, mods, combine, signed):
+        low, high, _, _ = self.pk_lanes(ops, mods, 2)
+        wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x)
+        results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)]
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def op_v_pk_add_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: '%s + %s' % (a, b))
+
+    def op_v_pk_mul_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: '%s * %s' % (a, b))
+
+    def op_v_pk_max_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: 'max(%s, %s)' % (a, b))
+
+    def op_v_pk_min_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b: 'min(%s, %s)' % (a, b))
+
+    def op_v_pk_fma_f16(self, ops, mods):
+        self.pk_float(ops, mods, lambda a, b, c: '%s * %s + %s' % (a, b, c))

P2: fixed in 40dab089 (rebased series, push pending). v_pk_fma_f16 now calls the integer single-rounding fma16_rne routine, with a WARP draw case for the 0x6a00/0x3c01/0x8400 midpoint (expects 0x6a01). Verified: python3 tests/startup_gfx2hlsl_vop3p_check.py. The WARP draw test needs the Win2025 run. AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4688767873 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#discussion_r4688767873). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T01:02:44Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1652 ~~~~diff @@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods): def op_v_ashrrev_i64(self, ops, mods): self.shift64(ops, mods, 'shr64i') + def op_v_cvt_pkrtz_f16_f32(self, ops, mods): self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2]))) + # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half + # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed + # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does. + def pk_modifiers(self, mods, arity, allow_neg, allow_clamp): + fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity} + clamp = False + for m in mods: + if m == 'clamp' and allow_clamp: + clamp = True + continue + match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m) + if not match or (match.group(1).startswith('neg') and not allow_neg): + raise Unsupported('modifier %s' % m) + bits = [int(b) for b in match.group(2).split(',')] + if len(bits) != arity: + raise Unsupported('modifier %s for %d sources' % (m, arity)) + fields[match.group(1)] = bits + return fields, clamp + + def pk_half(self, tok, high_half): + if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok): + raise Unsupported('packed operand %s' % tok) + word = self.u(tok) + return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word + + def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None): + """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources.""" + if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v' + and reg_range(ops[0])[1] == reg_range(ops[0])[2]): + raise Unsupported('packed operation operands %s' % ', '.join(ops)) + fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok) + lanes = ([], []) + for index, tok in enumerate(ops[1:1 + (packed or arity)]): + for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))): + half = self.pk_half(tok, sel[index]) + if float_lanes: + half = 'f16tof32(%s)' % half + if neg[index]: + half = '(-%s)' % half + lanes[lane].append(half) + return lanes[0], lanes[1], clamp, fields + + def pk_float(self, ops, mods, combine): + low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True) + results = [] + for lane in (low, high): + expr = combine(*lane) + results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr)) + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def pk_int(self, ops, mods, combine, signed): + low, high, _, _ = self.pk_lanes(ops, mods, 2) + wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x) + results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)] + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def op_v_pk_add_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: '%s + %s' % (a, b)) + + def op_v_pk_mul_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: '%s * %s' % (a, b)) + + def op_v_pk_max_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: 'max(%s, %s)' % (a, b)) + + def op_v_pk_min_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b: 'min(%s, %s)' % (a, b)) + + def op_v_pk_fma_f16(self, ops, mods): + self.pk_float(ops, mods, lambda a, b, c: '%s * %s + %s' % (a, b, c)) ~~~~ --- P2: fixed in 40dab089 (rebased series, push pending). v_pk_fma_f16 now calls the integer single-rounding fma16_rne routine, with a WARP draw case for the 0x6a00/0x3c01/0x8400 midpoint (expects 0x6a01). Verified: python3 tests/startup_gfx2hlsl_vop3p_check.py. The WARP draw test needs the Win2025 run. AI-assisted; commands listed were executed locally.
thepatrickworld commented 2026-11-25 03:01:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-25T02:05:58Z.


Independent run, and the fma routine against counterexamples from #2073

Their draw passes here, and the commit message says the draw was added on WARP, so as far as I can tell this is its first run on hardware: startup_gfx2hlsl_vop3p_draw_check.py draw … hardware … exits 0 on an AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010), FXC and DXC, head 40dab089.

fma16_rne against inputs that break a binary32 intermediate. In #2073, at the head I measured (61447651), the same triples are one binary16 unit low under f16rn(a*b + c). Packed into v_pk_fma_f16, both halves of every lane, both compilers, on the card:

startup_flat_param words=4e334e33,d55bd55b,450f450f,4b9f4b9f

4e33, d55b, 450f, 4b9f — the exact values, where f16rn(a*b + c) gives 4e32, d55a, 450e, 4b9e on the same adapter. So what the commit claims is confirmed by measurement on triples it did not contain, not only on the midpoint case it added.

Limits. AMD and WARP only; there is no NVIDIA adapter here, and the expected side is this PR's own reference, so this says the lowering and the reference agree on the card.

AI-assisted; runs executed on the machine above.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7150315386 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#issuecomment-7150315386). Originally posted by [thepatrickworld](https://github.com/thepatrickworld) on 2026-11-25T02:05:58Z. --- **Independent run, and the fma routine against counterexamples from #2073** Their draw passes here, and the commit message says the draw was added on WARP, so as far as I can tell this is its first run on hardware: `startup_gfx2hlsl_vop3p_draw_check.py draw … hardware …` exits 0 on an AMD Radeon RX 9070 XT (`1002:7550`, driver `32.0.32029.2010`), FXC and DXC, head `40dab089`. **`fma16_rne` against inputs that break a binary32 intermediate.** In #2073, at the head I measured (`61447651`), the same triples are one binary16 unit low under `f16rn(a*b + c)`. Packed into `v_pk_fma_f16`, both halves of every lane, both compilers, on the card: ``` startup_flat_param words=4e334e33,d55bd55b,450f450f,4b9f4b9f ``` `4e33`, `d55b`, `450f`, `4b9f` — the exact values, where `f16rn(a*b + c)` gives `4e32, d55a, 450e, 4b9e` on the same adapter. So what the commit claims is confirmed by measurement on triples it did not contain, not only on the midpoint case it added. **Limits.** AMD and WARP only; there is no NVIDIA adapter here, and the expected side is this PR's own reference, so this says the lowering and the reference agree on the card. _AI-assisted; runs executed on the machine above._
tedin7 commented 2026-11-25 11:02:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:01:55Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6056373182 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#pullrequestreview-6056373182). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:01:55Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-25 11:02:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T10:52:41Z.


Thank you @thepatrickworld for the independent hardware run of the draw (FXC and DXC) and for checking the fma routine against the #2073 triples.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7160292580 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#issuecomment-7160292580). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T10:52:41Z. --- Thank you @thepatrickworld for the independent hardware run of the draw (FXC and DXC) and for checking the fma routine against the #2073 triples. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 11:02:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:01:55Z.

Code review location: tools/startup/gfx2hlsl.py; line 1636

@@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods):
 
     def op_v_ashrrev_i64(self, ops, mods):
         self.shift64(ops, mods, 'shr64i')
+
     def op_v_cvt_pkrtz_f16_f32(self, ops, mods):
         self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2])))
 
+    # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half
+    # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed
+    # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does.
+    def pk_modifiers(self, mods, arity, allow_neg, allow_clamp):
+        fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity}
+        clamp = False
+        for m in mods:
+            if m == 'clamp' and allow_clamp:
+                clamp = True
+                continue
+            match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m)
+            if not match or (match.group(1).startswith('neg') and not allow_neg):
+                raise Unsupported('modifier %s' % m)
+            bits = [int(b) for b in match.group(2).split(',')]
+            if len(bits) != arity:
+                raise Unsupported('modifier %s for %d sources' % (m, arity))
+            fields[match.group(1)] = bits
+        return fields, clamp
+
+    def pk_half(self, tok, high_half):
+        if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok):
+            raise Unsupported('packed operand %s' % tok)
+        word = self.u(tok)
+        return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word
+
+    def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None):
+        """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources."""
+        if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v'
+                                         and reg_range(ops[0])[1] == reg_range(ops[0])[2]):
+            raise Unsupported('packed operation operands %s' % ', '.join(ops))
+        fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok)
+        lanes = ([], [])
+        for index, tok in enumerate(ops[1:1 + (packed or arity)]):
+            for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))):
+                half = self.pk_half(tok, sel[index])
+                if float_lanes:
+                    half = 'f16tof32(%s)' % half
+                    if neg[index]:
+                        half = '(-%s)' % half
+                lanes[lane].append(half)
+        return lanes[0], lanes[1], clamp, fields
+
+    def pk_float(self, ops, mods, combine):
+        low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True)
+        results = []
+        for lane in (low, high):
+            expr = combine(*lane)
+            results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr))
+        self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results))
+
+    def pk_int(self, ops, mods, combine, signed):
+        low, high, _, _ = self.pk_lanes(ops, mods, 2)
+        wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x)
+        results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)]

Re-checked on current head 67f5b8ba: the signed half-lane expressions are parenthesized (07b3b365, in this head) and covered by tests/startup_gfx2hlsl_vop3p_check.py. Resolving. AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4693922794 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#discussion_r4693922794). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:01:55Z. **Code review location:** `tools/startup/gfx2hlsl.py`; line 1636 ~~~~diff @@ -1576,9 +1576,121 @@ def op_v_lshrrev_b64(self, ops, mods): def op_v_ashrrev_i64(self, ops, mods): self.shift64(ops, mods, 'shr64i') + def op_v_cvt_pkrtz_f16_f32(self, ops, mods): self.set_v(reg_range(ops[0])[1], 'f32tof16(%s) | (f32tof16(%s) << 16)' % (self.f(ops[1]), self.f(ops[2]))) + # VOP3P packed math. Source i feeds the low lane from half op_sel[i] and the high lane from half + # op_sel_hi[i] (defaults 0 and 1); neg_lo/neg_hi flip the sign of a float lane. Float lanes are computed + # in binary32 and narrowed with f32tof16, as v_cvt_f16_f32 does. + def pk_modifiers(self, mods, arity, allow_neg, allow_clamp): + fields = {'op_sel': [0] * arity, 'op_sel_hi': [1] * arity, 'neg_lo': [0] * arity, 'neg_hi': [0] * arity} + clamp = False + for m in mods: + if m == 'clamp' and allow_clamp: + clamp = True + continue + match = re.fullmatch(r'(op_sel|op_sel_hi|neg_lo|neg_hi):\[([01](?:,[01])*)\]', m) + if not match or (match.group(1).startswith('neg') and not allow_neg): + raise Unsupported('modifier %s' % m) + bits = [int(b) for b in match.group(2).split(',')] + if len(bits) != arity: + raise Unsupported('modifier %s for %d sources' % (m, arity)) + fields[match.group(1)] = bits + return fields, clamp + + def pk_half(self, tok, high_half): + if tok in FLOATS or tok.startswith(('|', '-')) and not re.match(r'-\d', tok): + raise Unsupported('packed operand %s' % tok) + word = self.u(tok) + return '(%s >> 16)' % word if high_half else '(%s & 0xffffu)' % word + + def pk_lanes(self, ops, mods, arity, *, float_lanes=False, clamp_ok=False, packed=None): + """-> (low lane exprs, high lane exprs, clamp, modifier fields) of the first `packed` sources.""" + if len(ops) != arity + 1 or not (reg_range(ops[0]) and reg_range(ops[0])[0] == 'v' + and reg_range(ops[0])[1] == reg_range(ops[0])[2]): + raise Unsupported('packed operation operands %s' % ', '.join(ops)) + fields, clamp = self.pk_modifiers(mods, arity, float_lanes, clamp_ok) + lanes = ([], []) + for index, tok in enumerate(ops[1:1 + (packed or arity)]): + for lane, (sel, neg) in enumerate(((fields['op_sel'], fields['neg_lo']), (fields['op_sel_hi'], fields['neg_hi']))): + half = self.pk_half(tok, sel[index]) + if float_lanes: + half = 'f16tof32(%s)' % half + if neg[index]: + half = '(-%s)' % half + lanes[lane].append(half) + return lanes[0], lanes[1], clamp, fields + + def pk_float(self, ops, mods, combine): + low, high, clamp, _ =self.pk_lanes(ops, mods, len(ops) - 1, float_lanes=True, clamp_ok=True) + results = [] + for lane in (low, high): + expr = combine(*lane) + results.append('f32tof16(%s)' % ('saturate(%s)' % expr if clamp else expr)) + self.set_v(reg_range(ops[0])[1], '%s | (%s << 16)' % tuple(results)) + + def pk_int(self, ops, mods, combine, signed): + low, high, _, _ = self.pk_lanes(ops, mods, 2) + wrap = (lambda x: 'int(((%s) << 16)) >> 16' % x) if signed else (lambda x: x) + results = ['((%s) & 0xffffu)' % combine(wrap(lane[0]), wrap(lane[1])) for lane in (low, high)] ~~~~ --- Re-checked on current head 67f5b8ba: the signed half-lane expressions are parenthesized (07b3b365, in this head) and covered by tests/startup_gfx2hlsl_vop3p_check.py. Resolving. AI-assisted; commands listed were executed locally.
Makto94 commented 2026-11-25 13:00:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-25T12:21:57Z.


First NVIDIA run on 67f5b8ba

The AMD run earlier in this thread names head 40dab089, which is not the current head 67f5b8ba, so this is a separate measurement on the current head and on a different vendor.

.\tools\dev.ps1; ctest --preset dev --no-tests=error -j1 -R '^startup_gfx2hlsl_vop3p_hardware$' -V

The test passes (startup_gfx2hlsl_vop3p_draw: ok, 100% tests passed, 0 tests failed out of 1). The log has 18 fxc and 18 dxc result lines, one per case for each compiler: pk_add_f16, pk_mul_f16, pk_fma_f16, pk_fma_f16_midpoint, pk_min_f16, pk_max_f16, pk_mul_f16_op_sel, pk_add_f16_neg, dot2_f32_f16, dot4_u32_u8, and add/sub/max/min for i16 and u16. No debug_errors= line has a nonzero value.

For the non-hardware side, ctest --preset dev --no-tests=error -j1 -R 'vop3p' -E '_hardware$' -V gives 100% tests passed, 0 tests failed out of 2 (startup_gfx2hlsl_vop3p, startup_gfx2hlsl_vop3p_warp).

The four v_fmac_f16 triples from #2073, through v_pk_fma_f16. In a probe on this card, the correction term of the float fma16o helper of #2073 read zero (FXC only, one draw per variant; report). I drew the same four binary16 triples (4a54/1ef4/4e2d, 51ea/bf3e/1d5f, 2bd5/be83/4528, 3915/387a/4b71) through this PR's v_pk_fma_f16 lowering, with the shader built by this PR's check functions and the same value in both halves of each packed source. Expected words come from an exact single rounding to binary16 (Fraction), which gives 4e33,d55b,450f,4b9f:

fxc: startup_flat_param words=4e334e33,d55bd55b,450f450f,4b9f4b9f   (expected, rc=0)
dxc: startup_flat_param words=4e334e33,d55bd55b,450f450f,4b9f4b9f   (expected, rc=0)

With the first expected word moved by one ulp (4e344e34), the renderer exits with 1 and reports startup_flat_param mismatch for both compilers, so the comparison is not vacuous. debug_errors=0 debug_warnings=0 on every draw. WARP gives the same words.

Environment: NVIDIA GeForce RTX 3070 Ti (10de:2482), driver 32.0.16.2101, Windows 11 build 26300.9974, Debug build, fxc 10.0.26100.9457, dxc 1.9.2602.17, D3D12 debug layer enabled. The hardware runs were serial on an idle machine. 0 capability skips (77).

Limits: the triples used the same value in both halves, so they do not show that the two lanes stay independent with different inputs. AMD and Intel are not measured by me at this head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7162274636 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#issuecomment-7162274636). Originally posted by [Makto94](https://github.com/Makto94) on 2026-11-25T12:21:57Z. --- **First NVIDIA run on `67f5b8ba`** The AMD run earlier in this thread names head `40dab089`, which is not the current head `67f5b8ba`, so this is a separate measurement on the current head and on a different vendor. ``` .\tools\dev.ps1; ctest --preset dev --no-tests=error -j1 -R '^startup_gfx2hlsl_vop3p_hardware$' -V ``` The test passes (`startup_gfx2hlsl_vop3p_draw: ok`, `100% tests passed, 0 tests failed out of 1`). The log has 18 `fxc` and 18 `dxc` result lines, one per case for each compiler: `pk_add_f16`, `pk_mul_f16`, `pk_fma_f16`, `pk_fma_f16_midpoint`, `pk_min_f16`, `pk_max_f16`, `pk_mul_f16_op_sel`, `pk_add_f16_neg`, `dot2_f32_f16`, `dot4_u32_u8`, and `add/sub/max/min` for `i16` and `u16`. No `debug_errors=` line has a nonzero value. For the non-hardware side, `ctest --preset dev --no-tests=error -j1 -R 'vop3p' -E '_hardware$' -V` gives `100% tests passed, 0 tests failed out of 2` (`startup_gfx2hlsl_vop3p`, `startup_gfx2hlsl_vop3p_warp`). **The four `v_fmac_f16` triples from #2073, through `v_pk_fma_f16`.** In a probe on this card, the correction term of the float `fma16o` helper of #2073 read zero (FXC only, one draw per variant; [report](https://github.com/R0Y00/Xbox2Windows/pull/2073#issuecomment-7162131372)). I drew the same four binary16 triples (`4a54/1ef4/4e2d`, `51ea/bf3e/1d5f`, `2bd5/be83/4528`, `3915/387a/4b71`) through this PR's `v_pk_fma_f16` lowering, with the shader built by this PR's check functions and the same value in both halves of each packed source. Expected words come from an exact single rounding to binary16 (`Fraction`), which gives `4e33,d55b,450f,4b9f`: ``` fxc: startup_flat_param words=4e334e33,d55bd55b,450f450f,4b9f4b9f (expected, rc=0) dxc: startup_flat_param words=4e334e33,d55bd55b,450f450f,4b9f4b9f (expected, rc=0) ``` With the first expected word moved by one ulp (`4e344e34`), the renderer exits with 1 and reports `startup_flat_param mismatch` for both compilers, so the comparison is not vacuous. `debug_errors=0 debug_warnings=0` on every draw. WARP gives the same words. Environment: NVIDIA GeForce RTX 3070 Ti (`10de:2482`), driver 32.0.16.2101, Windows 11 build 26300.9974, Debug build, fxc 10.0.26100.9457, dxc 1.9.2602.17, D3D12 debug layer enabled. The hardware runs were serial on an idle machine. 0 capability skips (77). Limits: the triples used the same value in both halves, so they do not show that the two lanes stay independent with different inputs. AMD and Intel are not measured by me at this head.
upstream-sync closed this pull request 2026-11-25 14:04:39 +00:00
tedin7 commented 2026-11-25 14:05:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:45:14Z.


Thank you @Makto94 for the NVIDIA run.

This is a PASS on 67f5b8ba (RTX 3070 Ti, FXC and DXC, 0 capability skips). The current head is b9bbaf68, which is only a merge of main into the branch, so no new vop3p change is waiting for a re-run.

On the limit you named: the two lanes are not only checked with equal halves. The draw check also uses different values per half in pk_mul_f16_op_sel and in the i16/u16 cases (for example 0x7fff8000 against 0x00018000), with the reference computed lane by lane. Your same-value fma triples add the exact-rounding evidence on top of that.

AMD (head 40dab089) and NVIDIA (head 67f5b8ba) are both measured. Intel is still open.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7164239396 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#issuecomment-7164239396). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T13:45:14Z. --- Thank you @Makto94 for the NVIDIA run. This is a PASS on `67f5b8ba` (RTX 3070 Ti, FXC and DXC, 0 capability skips). The current head is `b9bbaf68`, which is only a merge of main into the branch, so no new vop3p change is waiting for a re-run. On the limit you named: the two lanes are not only checked with equal halves. The draw check also uses different values per half in `pk_mul_f16_op_sel` and in the `i16`/`u16` cases (for example `0x7fff8000` against `0x00018000`), with the reference computed lane by lane. Your same-value fma triples add the exact-rounding evidence on top of that. AMD (head `40dab089`) and NVIDIA (head `67f5b8ba`) are both measured. Intel is still open. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 14:05:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:51:04Z.


Closing this PR for now to keep the Windows test queue short. Nothing is wrong with it and the branch stays as it is.

We will reopen it once the queue is clear, re-run the Win2025 WARP checks at its head, and post the result here.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7164381761 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2081#issuecomment-7164381761). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T13:51:04Z. --- Closing this PR for now to keep the Windows test queue short. Nothing is wrong with it and the branch stays as it is. We will reopen it once the queue is clear, re-run the Win2025 WARP checks at its head, and post the result here. AI-assisted; commands listed were executed locally.

Pull request closed

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!2074
No description provided.