Skip to content

startup: lower v_cvt_pknorm_u16_f32 in gfx2hlsl - #1946

Closed
tedin7 wants to merge 6 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-gfx-pknorm16
Closed

tedin7 wants to merge 6 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-gfx-pknorm16

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Part of #870 (no Fixes). Draft.

What

Lowers v_cvt_pknorm_u16_f32 in tools/startup/gfx2hlsl.py. It was refused as unsupported, which stops the pair behind #1710.

The instruction is VOP3 only (opcode 0x369, no e32 form). Per the public RDNA ISA description:

  • bits 15:0 = round-to-nearest-even(clamp(S0, 0, 1) * 65535)
  • bits 31:16 = the same for S1
  • a NaN reads 0

Lowering: uint(round(saturate(a) * 65535.0)) | (uint(round(saturate(b) * 65535.0)) << 16).

Source neg and abs reach the operands. Any output modifier (clamp, mul, div) or op_sel is refused, because it would change the packed words. This matches the refusal style of #1755 and #1778 without depending on them. No other open PR implements this instruction.

Change

Test

  • Reference from the ISA text: 0, 1, -0.5, 1.5, 0.5, 1.5 and 2.5 over 65535 (ties), 0.25, 0.5, NaN, -0.0, +Inf, -Inf, on both halves, in different lanes so a swapped pair cannot pass.
  • The translated lane text is also evaluated in Python (binary32 product, HLSL saturate and round) and compared with the reference on all 8 sets.
  • Encoding: reassembled with llvm-mc, words recorded (checked with llvm-mc-19 for gfx1010 and gfx1030, same 0x369 VOP3 word; CI uses llvm-mc-15 in binary mode).
  • The draw mode (WARP and GPU, DXC only) is registered but was not run here.

The reference rounds the binary32 product. For 1.5/65535 that product is exactly 1.5 and ties to 2, while the exact value of the nearest binary32 input is just under 1.5. No hardware run of this instruction was available, so the draw mode is what confirms it.

Commands executed locally

AI-assisted; commands listed were executed locally.


Replaces closed draft #1815 (rebased cleanly on origin/main).
AI-assisted; commands listed were executed locally.

@tedin7
tedin7 marked this pull request as ready for review November 24, 2026 22:03
@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-24T22:08:56.619964Z 1777b62 Draft marked ready
ℹ️ 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.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Ran it, broke it four ways, and cross-checked the reference model independently

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_pknorm_u16_check.py
exit 0

Mutations to the lowering, each reverted:

mutation result
* 65535.0 → * 65536.0 caught
round(...) → truncation caught
the two halves swapped (S0 in bits 31:16) caught
saturate(...) dropped caught

That is the set I would have written by hand, and each one fails a different rule the description states. Two of them — the
truncation and the off-by-one scale — are the mistakes this instruction invites.

The reference model agrees with an independent implementation

Their unorm16() rounds a binary32 product (to_f32(x * 65535.0)) before taking the integer, which is the part that is
easy to get subtly wrong. I wrote the rule from the ISA text independently — clamp, binary32 product, round half to even, NaN
reads 0 — and compared over 200,012 words (12 chosen: ±0, ±1, quiet NaN, ±inf, 0.5, the two neighbours of 0.5, 1.5,
denormal minimum; plus 200,000 random 32-bit patterns):

words checked: 200012   mismatches: 0

So the model the fixture compares against is not just internally consistent, it matches the rule the description quotes. Your
comment at lines 13–14 about the product being exactly 1.5 and tying to 2 is the kind of thing that usually only shows up when
someone re-derives it — I re-derived it and got your answer.

What this still does not establish

The NaN rule is modelled as saturate(NaN) == 0, which is D3D's documented saturate behaviour, and round() is modelled as
half-to-even, which is HLSL's. Both rest on the backend honouring those, not on anything measured here — your draft status says
as much, and the draw is presumably what closes it. I did not run DXC or WARP for this.

Small note

half = 'uint(round(saturate(%s) * 65535.0))' is applied through self.f(...), so neg/abs reach the source before the
clamp — which the fixture checks. Nothing to change; mentioning it because the ordering is exactly what a later reader will ask
about when a -v0 case looks odd.

Verdict

Reviewed. Every rule in the description is pinned by a mutation, and the reference matches the ISA text under an independent
implementation. Good to go once the draw lands.

Notes

  • AI-assisted review.

@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: 1777b62d24

ℹ️ 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 Resolved
@Makto94

Makto94 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Ran the draw fixtures on WARP and on a physical GPU, in addition to the device-less run and mutations reported above. Head 1777b62d (PR still open), Debug build, Windows 11 build 26300, D3D12 debug layer enabled.

  • startup_gfx2hlsl_pknorm_u16_controls: passed.
  • startup_gfx2hlsl_pknorm_u16_warp (Microsoft Basic Render Driver, driver 10.0.26100.9818): passed.
  • startup_gfx2hlsl_pknorm_u16_hardware (NVIDIA GeForce RTX 3070 Ti, driver 32.0.16.2101): passed.
  • 3/3 passed, 0 capability skips (77). debug_errors=0 debug_warnings=0 in every section of both draw runs.
ctest --test-dir out/build/dev -j1 --no-tests=error -R '^startup_gfx2hlsl_pknorm_u16_' -V

@tedin7
tedin7 force-pushed the x2w-frontier-gfx-pknorm16 branch 5 times, most recently from 932baac to f021e9d Compare November 25, 2026 06:27
tedin7 and others added 2 commits November 25, 2026 07:38
VOP3 only. Each half is clamp(S, 0, 1) * 65535 rounded to nearest even, a NaN reads 0. Output modifiers and op_sel are refused. Refs R0Y00#870.
@tedin7
tedin7 force-pushed the x2w-frontier-gfx-pknorm16 branch from f021e9d to e0bccd7 Compare November 25, 2026 06:47
@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Thank you @ChazY11 and @Makto94 for the independent runs: the device-free checks with the four lowering mutations caught, and the WARP + NVIDIA RTX 3070 Ti draw fixtures at 1777b62d (3/3 passed, 0 debug errors/warnings). Recorded as extra evidence for this PR.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

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.

@tedin7 tedin7 closed this Nov 25, 2026
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