Skip to content

startup: lower v_swap_b32 in gfx2hlsl - #1995

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
JB2WIN:startup/gfx2hlsl-swap
Nov 24, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
JB2WIN:startup/gfx2hlsl-swap

Conversation

@JB2WIN

@JB2WIN JB2WIN commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Refs #1723

Change

tools/startup/gfx2hlsl.py now lowers v_swap_b32. It is one of the refusals in the #1723 census (2 pairs).

  • Lowering: both VGPRs take the other's old value. The new op_v_swap_b32 emits { uint t = vA; vA = vB; vB = t; }, so both sources are read before either one is written. When exec is unknown, the swap is guarded with if (exec & 1), as the neighbouring lane ops are.
  • Tracking: used_v is updated. A uniform flag moves with its value and is dropped when exec is unknown.
  • Semantics: the AMD RDNA 2 ISA (30 November 2020, VOP1 opcode 101) says "Swap operands. Input and output modifiers not supported; this is an untyped operation". Section 12.19 lists V_SWAP_B32 among the instructions that cannot use DPP or SDWA. The edge cases follow the pixel_aot lowering from shader: lower v_swap_b32 in pixel_aot #771.
  • Encoding: LLVM 15.0.7 assembles v_swap_b32 v1, v2 as 02 cb 02 7e, the self-swap v3, v3 and v255, v0 for gfx1013. It rejects _e64, _dpp, _sdwa, clamp, negation, SGPR and constant sources, and pairs.
  • Refused: the translator refuses all of the above, as well as omod, op_sel and the VOP3A form, which the decoder already marks unsupported.

Test

New device-free check tests/startup_gfx2hlsl_swap_check.py, registered as startup_gfx2hlsl_swap and listed in the pure-Python job and AGENTS.md. A short note is added to docs/experiments.md.

  • Positive cases: a plain swap with both registers read afterwards, reversed operand order, a self-swap, a swap after an earlier write, two consecutive swaps (a rotation over three registers), the same swap twice, and v255, v0. The emitted text is evaluated and compared with a reference list swap.
  • Refusals: 15 Unsupported cases.
  • Decoder: the check also decodes the VOP1 words.
  • Mutations: "write before read" and "only one destination" make the check fail. Against main's gfx2hlsl.py it fails with Unsupported: v_swap_b32.

Results:

  • After rebasing on e63721f9, all tests/startup_gfx2hlsl_*_check.py, tests/startup_translator_check.py and tests/docs_links_check.py pass.
  • verify.py on the base 1d825c18:
    • controls, form_coverage, rdna2_tools, pso_readiness, docs_links and the LLVM 15 binary round trip passed;
    • the focused ctest passed 40/40;
    • the full ctest passed 1257/1258, with 25 capability skips.
    • The one failure is startup_view_heap_hardware (shortcut logged -1 times, expected 1). It fails the same way on main at 1d825c18 without this change and passed at 387bdaf5, on an RTX 3080 (32.0.16.2124, Windows 11 build 26200). I'm narrowing it down separately.

The check evaluates the emitted HLSL in Python. It does not compile it or draw with it.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

One of the refusals in the R0Y00#1723 census (2 pairs). Both VGPRs take the
other's old value: the swap reads both sources before writing either,
through a temporary, and keeps the uniform flag with the moved value.
The RDNA 2 ISA (VOP1 opcode 101) lists no input or output modifiers and
no DPP or SDWA form for it; LLVM 15 rejects those spellings, and the
translator refuses them. New device-free check startup_gfx2hlsl_swap.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Ran it and broke the swap four ways — all caught

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_swap_check.py
control: "write before read" fails the fixtures (6 of 7): ok
control: "one destination" fails the fixtures (6 of 7): ok
v_swap_b32 checks: 0 failed      exit 0
mutation in op_v_swap_b32 result
{ vA = vB; vB = vA; } (write before read, no temporary) caught
only the destination written ({ vA = vB; }) caught
the if (exec & 1) guard dropped when exec is unknown caught
the uniform flag kept under an unknown exec caught

The first two are the same mistakes the fixture already names as its own controls, which is a good sign: your controls and my mutations agree on what
can go wrong, and both fail the suite. The fourth is the subtler one — a swap under an unknown mask no longer proves either register uniform, and
dropping that makes a later v_readfirstlane_b32 accept a source it should not.

On the uniformity rule

uniform = (dst in self.uniform_v and self.exec_known, src in self.uniform_v and self.exec_known) — the exec_known term is what keeps the property
honest when the mask is unknown, and it is the line a copy of this handler into another lane op would most likely lose. Worth a comment of its own if
the pattern spreads.

What the guard costs

if (exec & 1) around the whole body is the same shape as the neighbouring lane ops, so a reader recognises it; the alternative (guarding each write)
would be wrong, because a half-swap is worse than none. That reasoning is not in the code comment — it says "guarded with if (exec & 1), as the
neighbouring lane ops are" — and a one-clause addition would stop someone "optimising" it into two guarded writes later.

Scope

v_swap_b32 only; v_swaprel_b32 and the DPP/SDWA spellings stay refused per the comment. The fixture's 7 cases and the two controls above cover the
accepted and refused forms; no draw was run, which matches the description.

Verdict

Reviewed. Four independent breakages, four failures, and the uniformity rule travels with the values as intended.

Notes

  • AI-assisted review.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Your commit f018831b is already the code in main; this PR page still shows open

Checked while reviewing a duplicate: tools/startup/gfx2hlsl.py on main today contains your handler verbatim, including the
uniformity-preserving line and the if (exec & 1) guard:

1304  def op_v_swap_b32(self, ops, mods):
1318      uniform = (dst in self.uniform_v and self.exec_known, src in self.uniform_v and self.exec_known)
1322      self.emit(body if self.exec_known else 'if (exec & 1) ' + body)

and tests/startup_gfx2hlsl_swap_check.py is on main and green (exit 0). So either this was merged through another PR that carried the
same commit, or it was pushed directly — worth knowing only because #2012 is a duplicate of it and I have pointed that out there.

Nothing needed from you if the content is in; just flagging so the open state does not look like unlanded work.

Notes

  • AI-assisted.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Correction: my previous comment was wrong — your commit is not already in main

I wrote that f018831b is the code in main and that your PR page merely still showed open. That was wrong. Verified against the commit object,
not a working tree:

git show origin/main:tools/startup/gfx2hlsl.py | Select-String 'v_swap_b32'   # no match
git grep -l 'op_v_swap_b32' origin/main -- tools/startup/gfx2hlsl.py          # no match
git show origin/main:tests/startup_gfx2hlsl_swap_check.py                     # path does not exist
origin/main   contains op_v_swap_b32: False
pr-1995       contains op_v_swap_b32: True

origin/main at 66423e45 has 75 op_* methods on Stage, none of them op_v_swap_b32, and tests/startup_gfx2hlsl_swap_check.py is not in
the tree. So this PR is genuine unlanded work, and I am sorry for suggesting otherwise.

What I did wrong: I had a branch checked out earlier in the session to run a test, and my later greps read that working tree instead of
origin/main. The line numbers I quoted were real, in that branch; the claim I built on them was not.

For what it is worth, the substance of my review of your handler stands: four mutations (temp removed, one destination, exec guard dropped,
uniform flag kept under an unknown exec) all fail your fixture, and the uniformity rule travelling with the value is the precise behaviour.

Notes

  • AI-assisted, and self-corrected.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Merge matrix for the 16 open PRs that touch tools/startup/gfx2hlsl.py

I computed each PR's changed line ranges against its own merge base (not against main, which makes stale branches look like they delete other
people's work), then looked for pairs whose ranges overlap.

PR      hunks  lines  behind  ranges
#2020      3     54       7  (1444,1486) (1528,1530) (1907,1914)
#2014      5     15       7  (677,679) (682,683) (686,692) (704,705) (1719,1719)
#2012      1     18     121  (1272,1289)
#2010      2      7      22  (1649,1649) (1846,1851)
#2008      6     48      22  (8,8) (13,13) (2009,2015) (2037,2038) (2041,2071) (2337,2342)
#2007      1     43      27  (923,965)
#2006      2      4      22  (78,79) (87,88)
#2005      1      3      22  (87,89)
#2004      1      1      22  (85,85)
#2003      1     33      22  (923,955)
#2002      3     12      48  (104,105) (157,162) (1079,1082)
#1997      3     23      38  (1146,1158) (1162,1170) (1993,1993)
#1995      1     20      40  (1304,1323)
#1984      1      5      69  (293,297)
#1952      2      6     119  (1597,1597) (1792,1796)
#1946      1     11     121  (424,434)

Only 2 of 120 pairs overlap:

pair overlap what it is
#2007 vs #2003 923–955 both add op_s_bfm_b64
#2006 vs #2005 87–88 both extend FETCH_FORMATS (10-10-10-2 UNORM vs 16-16 SNORM)

But line overlap is only half the story

I merged the overlapping pairs, and then a pair that does not overlap:

#2006 + #2005   conflict: CMakeLists.txt
#2003 + #2007   conflict: AGENTS.md, tests/startup_gfx2hlsl_bfm_b64_check.py (AA), tools/startup/gfx2hlsl.py
#2012 + #1995   order-dependent:
                  merge 2012 then 1995 -> conflict only in CMakeLists.txt, gfx2hlsl.py merges to ONE op_v_swap_b32
                  merge 1995 then 2012 -> conflicts in CMakeLists.txt, tests/..._swap_check.py (AA), gfx2hlsl.py
                                          and then BOTH implementations are present in that file

#2012 and #1995 add the same method at lines 1272 and 1304 — far apart, so nothing overlaps, yet they are the same feature. In one merge order
git silently keeps a single definition (one author's behaviour is lost with no marker); in the other it keeps both. Neither outcome is a rebase of
the other, and the reviewer would have to notice by reading the file.

So: line-overlap predicts textual conflicts, not duplicate additions. For a file where 16 people are each adding one handler, the dangerous
case is the one this matrix does not flag, and the two duplicate pairs above (#1995/#2012 on v_swap_b32, #2003/#2007 on s_bfm_b64) are
exactly that.

What I would do with this

  1. Land one of each duplicate pair first, and tell the other author to rebase onto it (I have already reviewed startup: lower s_bfm_b64 in gfx2hlsl #2007 at behaviour level and startup: lower v_swap_b32 in gfx2hlsl #1995
    the same way, so both are safe choices).
  2. For #2006 / #2005, whichever goes second needs a one-line rebase of two adjacent dict entries — trivial, but it will be a conflict rather
    than a merge.
  3. Everything else in the table is disjoint: any order among the remaining 12 lands without touching another PR's lines.

The script is 40 lines (merge-base, git diff -U0, range intersection) if you want it as a CI job — but a cheaper version is just "search the
open PR titles for the mnemonic", which would have caught both duplicates before either branch existed.

Notes

  • AI-assisted analysis, computed on main 7f149ac9.

@R0Y00
R0Y00 merged commit 032faab into R0Y00:main Nov 24, 2026
5 checks passed
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