shader: lower startup scalar high-halfword packing #2007

Merged
avt0m4t48 merged 1 commit from upstream-pr/2014 into main 2026-11-24 23:23:46 +00:00
avt0m4t48 commented 2026-11-24 23:09:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by avt0m4t48 on 2026-11-24T23:09:19Z.


Refs #1996, item A.

Lower s_pack_hh_b32_b16 in the startup shader translator. It puts the high half of source 0 in the low result half and the high half of source 1 in the high result half. SCC stays unchanged. Whole-wave lane masks, symbolic descriptors and reserved scalar encodings are refused. LLVM AMDGPU instruction selector

Verification

  • python3 -B tests/startup_gfx2hlsl_sop2_complement_check.py refused the hand-assembled pack on the base and passed on the patch. The check evaluates 11 numeric forms over 2,184 input pairs, checks SCC, and catches disabled and source-swapped lowerings.
  • LLVM 15 assembled and disassembled all 101 SOP2 fixtures in the shared check, including the 11 new forms.
  • All 26 local Python scripts for startup shader translation, docs links and bidi controls exited successfully.
  • A separately assembled gfx1013 pixel fixture translated and compiled with DXC to ps_6_0. Microsoft dxv passed, and each typed output channel in the generated DXIL carried 0xedcb1234.

The DXIL check was device-free. Hardware and WARP draw readback were outside this run.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593257979 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2014). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-24T23:09:19Z. --- Refs #1996, item A. Lower `s_pack_hh_b32_b16` in the startup shader translator. It puts the high half of source 0 in the low result half and the high half of source 1 in the high result half. SCC stays unchanged. Whole-wave lane masks, symbolic descriptors and reserved scalar encodings are refused. [LLVM AMDGPU instruction selector](https://llvm.googlesource.com/llvm-project/llvm/+/b4656fe19c50f9b0c8af4366cea0d887d956bf52/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp#826) ## Verification - `python3 -B tests/startup_gfx2hlsl_sop2_complement_check.py` refused the hand-assembled pack on the base and passed on the patch. The check evaluates 11 numeric forms over 2,184 input pairs, checks SCC, and catches disabled and source-swapped lowerings. - LLVM 15 assembled and disassembled all 101 SOP2 fixtures in the shared check, including the 11 new forms. - All 26 local Python scripts for startup shader translation, docs links and bidi controls exited successfully. - A separately assembled gfx1013 pixel fixture translated and compiled with DXC to `ps_6_0`. Microsoft `dxv` passed, and each typed output channel in the generated DXIL carried `0xedcb1234`. The DXIL check was device-free. Hardware and WARP draw readback were outside this run.
upstream-sync closed this pull request 2026-11-25 00:04:55 +00:00
ChazY11 commented 2026-11-25 01:01:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T23:18:15Z.


The s_pack_hh_b32_b16 half is well covered; the SMEM offset half is not

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sop2_complement_check.py
pack_hh negative control catches a disabled lowering: ok
pack_hh negative control catches reversed source halves: ok
PASS      exit 0
mutation result
s_pack_hh_b32_b16 sources swapped caught
the lowering takes the low halves instead of the high ones caught
the & 0xffff0000u mask on source 1 dropped caught
if value > 0xfffff: → if value > 0x1fffff: (the SMEM fix undone) not caught
the offset: regex allowed a leading - again not caught

The fourth is the one that matters: reverting the SMEM change this PR introduces — accepting 0x100000..0x1fffff as a positive byte offset — leaves the
whole suite green. The check has no fixture in that range:

Select-String -Path tests/startup_gfx2hlsl_sop2_complement_check.py -Pattern '0x1fffff|0x100000'   # 0 matches

One line would close it, in the shape the rest of the file already uses:

# OFFSET[20:0] is signed: 0x100000 is a negative byte offset, not +1 MiB.
assert refused('s_load_dword s0, s[0:1], 0x100000')          # or the file's own helper
assert 'SMEM immediate offset' in <the refusal text>

The negative-offset form changed its message, which is fine but worth knowing

offset:-4 no longer matches the offset: branch at all (the regex lost its -?), so it now falls to the following else and is refused as
SMEM modifier offset:-4 instead of SMEM immediate offset offset:-4. Still refused, different wording — no behaviour lost, but if anything greps
for the old text it will stop matching.

One process point

This PR carries two independent behaviours: the s_pack_hh_b32_b16 lowering and the SMEM immediate-offset sign fix. That is why one half is
thoroughly covered and the other is not — the fixture that came with the PR is the pack one. Splitting them would have made the missing SMEM case
obvious at review time. As it stands, the SMEM change is a fix to existing behaviour, which makes an unpinned case more costly than an unpinned
new feature.

Merge state

Clean against main a0bc0f9a (merge base 66423e45, 10 commits behind): no conflicts, and startup_gfx2hlsl_sop2_complement_check.py passes on
the merged tree.

What I did not check

The LLVM 15 assembly/disassembly claim for the 101 SOP2 fixtures — no LLVM here. And SCC staying unchanged for s_pack_hh_b32_b16 is asserted by
your check, not by me.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7147246056 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2014#issuecomment-7147246056). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T23:18:15Z. --- ## The `s_pack_hh_b32_b16` half is well covered; the SMEM offset half is not Windows 11 build 26300.9974, device-free: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sop2_complement_check.py pack_hh negative control catches a disabled lowering: ok pack_hh negative control catches reversed source halves: ok PASS exit 0 ``` | mutation | result | |---|---| | `s_pack_hh_b32_b16` sources swapped | **caught** | | the lowering takes the low halves instead of the high ones | **caught** | | the `& 0xffff0000u` mask on source 1 dropped | **caught** | | **`if value > 0xfffff:` → `if value > 0x1fffff:` (the SMEM fix undone)** | **not caught** | | the `offset:` regex allowed a leading `-` again | not caught | The fourth is the one that matters: reverting the SMEM change this PR introduces — accepting `0x100000..0x1fffff` as a positive byte offset — leaves the whole suite green. The check has no fixture in that range: ```powershell Select-String -Path tests/startup_gfx2hlsl_sop2_complement_check.py -Pattern '0x1fffff|0x100000' # 0 matches ``` One line would close it, in the shape the rest of the file already uses: ```python # OFFSET[20:0] is signed: 0x100000 is a negative byte offset, not +1 MiB. assert refused('s_load_dword s0, s[0:1], 0x100000') # or the file's own helper assert 'SMEM immediate offset' in <the refusal text> ``` ## The negative-offset form changed its message, which is fine but worth knowing `offset:-4` no longer matches the `offset:` branch at all (the regex lost its `-?`), so it now falls to the following `else` and is refused as `SMEM modifier offset:-4` instead of `SMEM immediate offset offset:-4`. Still refused, different wording — no behaviour lost, but if anything greps for the old text it will stop matching. ## One process point This PR carries two independent behaviours: the `s_pack_hh_b32_b16` lowering and the SMEM immediate-offset sign fix. That is why one half is thoroughly covered and the other is not — the fixture that came with the PR is the pack one. Splitting them would have made the missing SMEM case obvious at review time. As it stands, the SMEM change is a fix to *existing* behaviour, which makes an unpinned case more costly than an unpinned new feature. ## Merge state Clean against `main` `a0bc0f9a` (merge base `66423e45`, 10 commits behind): no conflicts, and `startup_gfx2hlsl_sop2_complement_check.py` passes on the merged tree. ## What I did not check The LLVM 15 assembly/disassembly claim for the 101 SOP2 fixtures — no LLVM here. And SCC staying unchanged for `s_pack_hh_b32_b16` is asserted by your check, not by me. ## Notes - AI-assisted review.
ChazY11 commented 2026-11-25 01:01:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T23:18:55Z.


Correction to my review: the SMEM guard gap is pre-existing on main, not introduced here

I wrote that "reverting the SMEM change this PR introduces leaves the suite green". That framing was wrong, and the reason matters:

main already refuses the high range. The condition there is

if not 0 <= value <= 0xfffff:
    raise Unsupported('SMEM immediate offset %s' % m)

so offset:0x100000 is refused today — your PR preserves that behaviour with a different shape (int(m[7:], 0) and if value > 0xfffff), it does not
add protection. I ran the same mutation on main, without your branch:

# tools/startup/gfx2hlsl.py on main: 0 <= value <= 0xfffff  ->  0 <= value <= 0x1fffff
out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sop2_complement_check.py    # exit 0, not caught

So the missing coverage predates your PR and the guard was already unpinned.

What survives from that paragraph, correctly stated

  • The guard is unpinned on main today: widening the upper bound from 0xfffff to 0x1fffff passes the suite, both before and after your change.
    A fixture in that range would pin it for both shapes.
  • Your PR's rewrite keeps the behaviour identical for every value: negative forms still refused (now via the else branch as
    SMEM modifier …), 0x100000..0x1fffff still refused, 0..0xfffff still accepted. I traced all three against the old code rather than assuming.

What is unchanged and still useful

The s_pack_hh_b32_b16 half is well covered: sources swapped, low halves instead of high, and the dropped & 0xffff0000u mask all fail the fixture.
The merge into current main a0bc0f9a is clean and the check passes on the merged tree.

But the process point stands for a different reason: this PR rewrites an existing guard. A reviewer cannot tell from the diff that the two shapes are
equivalent without tracing them, and a one-line fixture would have made that visible — which is exactly what the missing case would provide.

Notes

  • AI-assisted, corrected after running the same mutation against main instead of only against the branch.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7147258300 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2014#issuecomment-7147258300). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T23:18:55Z. --- ## Correction to my review: the SMEM guard gap is pre-existing on `main`, not introduced here I wrote that "reverting the SMEM change this PR introduces leaves the suite green". That framing was wrong, and the reason matters: **`main` already refuses the high range.** The condition there is ```python if not 0 <= value <= 0xfffff: raise Unsupported('SMEM immediate offset %s' % m) ``` so `offset:0x100000` is refused today — your PR preserves that behaviour with a different shape (`int(m[7:], 0)` and `if value > 0xfffff`), it does not add protection. I ran the same mutation on **`main`**, without your branch: ```powershell # tools/startup/gfx2hlsl.py on main: 0 <= value <= 0xfffff -> 0 <= value <= 0x1fffff out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sop2_complement_check.py # exit 0, not caught ``` So the missing coverage predates your PR and the guard was already unpinned. ## What survives from that paragraph, correctly stated - The **guard is unpinned on `main` today**: widening the upper bound from `0xfffff` to `0x1fffff` passes the suite, both before and after your change. A fixture in that range would pin it for both shapes. - Your PR's rewrite keeps the behaviour identical for every value: negative forms still refused (now via the `else` branch as `SMEM modifier …`), `0x100000..0x1fffff` still refused, `0..0xfffff` still accepted. I traced all three against the old code rather than assuming. ## What is unchanged and still useful The `s_pack_hh_b32_b16` half is well covered: sources swapped, low halves instead of high, and the dropped `& 0xffff0000u` mask all fail the fixture. The merge into current `main` `a0bc0f9a` is clean and the check passes on the merged tree. But the process point stands for a different reason: this PR rewrites an **existing** guard. A reviewer cannot tell from the diff that the two shapes are equivalent without tracing them, and a one-line fixture would have made that visible — which is exactly what the missing case would provide. ## Notes - AI-assisted, corrected after running the same mutation against `main` instead of only against the branch.
ChazY11 commented 2026-11-25 01:01:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T23:19:39Z.


Second correction: the SMEM guard is covered — I ran the wrong check file

My first comment said the guard was unpinned; my correction said it was pre-existing but still unpinned. Both were wrong for the same reason: I ran
tests/startup_gfx2hlsl_sop2_complement_check.py, which has nothing to do with SMEM. The covering fixture is
tests/startup_smem_address_check.py, and it already has the case:

# 5. Negative 21-bit signed SMEM immediate encodings (0x100000..0x1fffff) are refused on all paths.
for neg_imm in (0x100000, 0x1ffff0, 0x1ffffc, 0x1fffff):
    must_refuse_bytes(encode_smem(SMEM_BUFFER_LOAD_DWORD, 24, 16, soffset=125, imm=neg_imm),
                      'SMEM immediate offset')

With the bound widened, that check fails. Both shapes, measured:

# main:        0 <= value <= 0xfffff   ->   0 <= value <= 0x1fffff
out/pe-env/Scripts/python.exe -B tests/startup_smem_address_check.py    # exit 1  (caught)

# your branch: if value > 0xfffff      ->   if value > 0x1fffff
out/pe-env/Scripts/python.exe -B tests/startup_smem_address_check.py    # exit 1  (caught)

So there is no gap here, in either shape, and nothing for this PR to add.

What I actually got right

  • main already refuses 0x100000..0x1fffff; your rewrite preserves that for every value, and the negative forms stay refused (as
    SMEM modifier … rather than SMEM immediate offset …).
  • The s_pack_hh_b32_b16 half is well covered by the three mutations I ran (sources swapped, low halves, dropped & 0xffff0000u).
  • The merge into main a0bc0f9a is clean and both checks pass on the merged tree.

Where my process failed

I assumed one fixture covered a handler because the PR touched that fixture. The correct rule is the one I apply when reviewing others' claims: run
the check that actually names the behaviour, and if it does not mention the mnemonic or the modifier at all, that is evidence about my choice of
file, not about the code. Two corrections in a row on the same point is a bad enough ratio that I am writing it down rather than quietly moving on.

Notes

  • AI-assisted, corrected twice; the second correction supersedes the first.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7147274335 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2014#issuecomment-7147274335). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T23:19:39Z. --- ## Second correction: the SMEM guard **is** covered — I ran the wrong check file My first comment said the guard was unpinned; my correction said it was pre-existing but still unpinned. Both were wrong for the same reason: I ran `tests/startup_gfx2hlsl_sop2_complement_check.py`, which has nothing to do with SMEM. The covering fixture is `tests/startup_smem_address_check.py`, and it already has the case: ```python # 5. Negative 21-bit signed SMEM immediate encodings (0x100000..0x1fffff) are refused on all paths. for neg_imm in (0x100000, 0x1ffff0, 0x1ffffc, 0x1fffff): must_refuse_bytes(encode_smem(SMEM_BUFFER_LOAD_DWORD, 24, 16, soffset=125, imm=neg_imm), 'SMEM immediate offset') ``` With the bound widened, that check fails. Both shapes, measured: ```powershell # main: 0 <= value <= 0xfffff -> 0 <= value <= 0x1fffff out/pe-env/Scripts/python.exe -B tests/startup_smem_address_check.py # exit 1 (caught) # your branch: if value > 0xfffff -> if value > 0x1fffff out/pe-env/Scripts/python.exe -B tests/startup_smem_address_check.py # exit 1 (caught) ``` So there is no gap here, in either shape, and nothing for this PR to add. ## What I actually got right - `main` already refuses `0x100000..0x1fffff`; your rewrite preserves that for every value, and the negative forms stay refused (as `SMEM modifier …` rather than `SMEM immediate offset …`). - The `s_pack_hh_b32_b16` half is well covered by the three mutations I ran (sources swapped, low halves, dropped `& 0xffff0000u`). - The merge into `main` `a0bc0f9a` is clean and both checks pass on the merged tree. ## Where my process failed I assumed one fixture covered a handler because the PR touched that fixture. The correct rule is the one I apply when reviewing others' claims: run the check that actually names the behaviour, and if it does not mention the mnemonic or the modifier at all, that is evidence about my choice of file, not about the code. Two corrections in a row on the same point is a bad enough ratio that I am writing it down rather than quietly moving on. ## Notes - AI-assisted, corrected twice; the second correction supersedes the first.
ChazY11 commented 2026-11-25 01:01:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T23:21:25Z.


Third correction, and the finding that replaces it: this PR reverts ed7d09a4 (merged with #1984) without saying so

My two previous comments on this point were wrong. Here is what the history actually shows, and it turns the SMEM half of this PR into a different
question than "is it covered".

The timeline

3cc12064  2026-11-24  lower combined register+immediate SMEM offsets      -> "if value > 0xfffff" shape
ed7d09a4  2026-11-25  Preserve SMEM refusals with signed decoder offsets  -> "-?" regex + "0 <= value <= 0xfffff" shape   (merged via #1984)
#2014     open        restores the 3cc12064 shape

main today, after ed7d09a4 (author Royoo, merged as #1984):

if m.startswith('offset:') and imm is None and re.fullmatch(r'-?(?:0[xX][0-9a-fA-F]+|\d+)', m[7:]):
    # Reject signed negative offsets and older unsigned spellings with bit 20 set.
    token = m[7:]
    value = int(token, 16 if '0x' in token.lower() else 10)
    if not 0 <= value <= 0xfffff:
        raise Unsupported('SMEM immediate offset %s' % m)

this PR:

if m.startswith('offset:') and imm is None and re.fullmatch(r'0[xX][0-9a-fA-F]+|\d+', m[7:]):
    # SMEM OFFSET[20:0] is signed two's complement on RDNA 2; disasm_rdna2 prints w1 & 0x1fffff …
    value = int(m[7:], 0)
    if value > 0xfffff:
        raise Unsupported('SMEM immediate offset %s' % m)

That is the pre-ed7d09a4 text, comment included. Your merge base is 66423e45, which is one commit before ed7d09a4 landed, so this is a
branch written against the older tree rather than a deliberate disagreement — but the effect is that merging this PR undoes a merged change
and the description does not mention it.

They are behaviourally equivalent, which is why it matters

I traced all three regions rather than assuming:

input main this PR
offset:0x10 accepted accepted
offset:0xfffff accepted accepted
offset:0x100000 … 0x1fffff refused (SMEM immediate offset) refused (same message)
offset:-4 matched, then refused by the range check not matched, refused by the following else as SMEM modifier offset:-4

So nothing breaks. But a reviewer reading git diff sees one author's rewrite of another author's guard, and the only visible difference is a
changed diagnostic for negative forms — which is exactly the kind of change that gets merged by accident and then makes a future bisect confusing.

What I would ask for

  1. Rebase onto current main and drop the SMEM hunk, leaving only the s_pack_hh_b32_b16 lowering. If the newer shape has a defect you care
    about, say which, in its own PR.
  2. If you do want the older shape back, mention ed7d09a4 in the description so the person who wrote it can weigh in.

And my earlier claim, retracted properly

There is no missing coverage: tests/startup_smem_address_check.py:228 already refuses 0x100000, 0x1ffff0, 0x1ffffc, 0x1fffff on three paths, and
widening either shape's bound fails it. I said otherwise twice because I ran startup_gfx2hlsl_sop2_complement_check.py, which does not mention SMEM
at all.

Notes

  • AI-assisted; this supersedes my two earlier comments on the SMEM half.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7147307236 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2014#issuecomment-7147307236). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T23:21:25Z. --- ## Third correction, and the finding that replaces it: this PR reverts `ed7d09a4` (merged with #1984) without saying so My two previous comments on this point were wrong. Here is what the history actually shows, and it turns the SMEM half of this PR into a different question than "is it covered". ## The timeline ```text 3cc12064 2026-11-24 lower combined register+immediate SMEM offsets -> "if value > 0xfffff" shape ed7d09a4 2026-11-25 Preserve SMEM refusals with signed decoder offsets -> "-?" regex + "0 <= value <= 0xfffff" shape (merged via #1984) #2014 open restores the 3cc12064 shape ``` `main` today, after `ed7d09a4` (author Royoo, merged as #1984): ```python if m.startswith('offset:') and imm is None and re.fullmatch(r'-?(?:0[xX][0-9a-fA-F]+|\d+)', m[7:]): # Reject signed negative offsets and older unsigned spellings with bit 20 set. token = m[7:] value = int(token, 16 if '0x' in token.lower() else 10) if not 0 <= value <= 0xfffff: raise Unsupported('SMEM immediate offset %s' % m) ``` this PR: ```python if m.startswith('offset:') and imm is None and re.fullmatch(r'0[xX][0-9a-fA-F]+|\d+', m[7:]): # SMEM OFFSET[20:0] is signed two's complement on RDNA 2; disasm_rdna2 prints w1 & 0x1fffff … value = int(m[7:], 0) if value > 0xfffff: raise Unsupported('SMEM immediate offset %s' % m) ``` That is the pre-`ed7d09a4` text, comment included. Your merge base is `66423e45`, which is one commit before `ed7d09a4` landed, so this is a branch written against the older tree rather than a deliberate disagreement — but the effect is that merging this PR **undoes a merged change** and the description does not mention it. ## They are behaviourally equivalent, which is why it matters I traced all three regions rather than assuming: | input | `main` | this PR | |---|---|---| | `offset:0x10` | accepted | accepted | | `offset:0xfffff` | accepted | accepted | | `offset:0x100000` … `0x1fffff` | refused (`SMEM immediate offset`) | refused (same message) | | `offset:-4` | matched, then refused by the range check | not matched, refused by the following `else` as `SMEM modifier offset:-4` | So nothing breaks. But a reviewer reading `git diff` sees one author's rewrite of another author's guard, and the only visible difference is a changed diagnostic for negative forms — which is exactly the kind of change that gets merged by accident and then makes a future bisect confusing. ## What I would ask for 1. Rebase onto current `main` and drop the SMEM hunk, leaving only the `s_pack_hh_b32_b16` lowering. If the newer shape has a defect you care about, say which, in its own PR. 2. If you do want the older shape back, mention `ed7d09a4` in the description so the person who wrote it can weigh in. ## And my earlier claim, retracted properly There is no missing coverage: `tests/startup_smem_address_check.py:228` already refuses `0x100000, 0x1ffff0, 0x1ffffc, 0x1fffff` on three paths, and widening either shape's bound fails it. I said otherwise twice because I ran `startup_gfx2hlsl_sop2_complement_check.py`, which does not mention SMEM at all. ## Notes - AI-assisted; this supersedes my two earlier comments on the SMEM half.
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!2007
No description provided.