Skip to content

rdna2spirv: lower rotate and FFT wave swizzles - #4245

Open
thatirishguyyy wants to merge 4 commits into
ItsNotRexed:mainfrom
thatirishguyyy:shader/swizzle-rotate-fft
Open

thatirishguyyy wants to merge 4 commits into
ItsNotRexed:mainfrom
thatirishguyyy:shader/swizzle-rotate-fft

Conversation

@thatirishguyyy

Copy link
Copy Markdown

Claimed in the coordination issue.

What and why

LLVM 18 emits numeric ds_swizzle_b32 offsets for rotate and FFT on synthetic gfx1013 inputs. This change lowers those encodings, selects lanes within each 32-lane half, and reuses the lane-read helper. Related to #3126, which covers inactive source lanes. Lane mapping reference: omonkeyz/xbox-emulator#186.

Tested

  • On baseline aa2988c, minimal rotate and FFT shaders were refused with ds_swizzle pattern. Both translate and pass spirv-val with this change.
  • 153 focused tests passed, including seven swizzle regression cases.
  • Six synthetic wave64 compute shaders passed SPIR-V validation and matched 384/384 lane outputs on Windows with an RX 9070 XT and AMD Software 26.11.2. Vulkan synchronization validation reported no diagnostics.

Checklist

  • No game data, keys or decryption.
  • The translator rule and verification are in the docs/design.md contract table.

AI-assisted (Codex).

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 90c18f3 (merged with main aa2988c). The numeric path is correct: offset:0xc020 and offset:0xe003 lower exactly as the GCN/RDNA DS_SWIZZLE_B32 pseudocode says (rotate: (i & mask) | ((i ± rot) & ~mask); FFT: bitrev5(i) >> popcount(mask) | (i & mask)). tests/test_swizzle_numeric.py passes, and it merges cleanly with #3932.

One regression: LLVM 20+ disassembles these offsets symbolically for gfx9+ (gfx1013 included), as offset:swizzle(ROTATE,dir,size) and offset:swizzle(FFT,mask) (llvm/llvm-project#108064, AMDGPUInstPrinter::printSwizzle). The tools already need llvm-mc 21 for some checks. In that form the swizzle\((\w+)… regex sets kind to ROTATE/FFT, but offset stays 0 because there is no offset:<number>. So the result is silently wrong:

Input main this PR Expected
offset:swizzle(ROTATE,0,1) Unsupported ds_swizzle ROTATE OpIAdd … %c0 (rotate by 0) rotate by 1 (0xc020)
offset:swizzle(FFT,3) Unsupported ds_swizzle FFT keep mask 0xffffffe0, shift 27 (mask 0) keep mask 0xffffffe3, shift 29 (0xe003)

Main refuses these forms, while this PR translates them into a wrong lane mapping. Suggested fix: when kind is ROTATE/FFT, rebuild offset from args: 0xc000 | dir << 10 | size << 5 and 0xe000 | mask. Add the two symbolic forms to the parametrized test, each paired with its numeric twin.

Repro (from tests/semantic): support.translate_lines("ps_probe", ["ds_swizzle_b32 v1, v0 offset:swizzle(FFT,3)", "s_endpgm"]).

AI-assisted (Devin), CPU-only.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this at 90c18f34 on Windows 11 with LLVM 21 and the SDK tools on the environment: python -I -m pytest tests/test_swizzle_numeric.py -q -> 7 passed; compileall and git diff --check clean. (Without the tools the module skips, which is the intended requires_tools behavior.)

The translator now decodes the numeric ds_swizzle_b32 offset: forms: 0xC000 rotates the low 5 lane bits by the encoded amount (with the direction bit), 0xE000 is the FFT butterfly (bit-reverse, then shift down by 27 + popcount(mask)), and both preserve the high lane bits and the mask bit. The new tests exercise the encoded offsets; the existing textual swizzle(...) and BITMASK paths still pass. No blocking issue.

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Re-tested at d4faae7: the symbolic-operand fix works. swizzle(ROTATE,0,1), swizzle(ROTATE,1,1) and swizzle(FFT,3) now emit the same lane arithmetic as 0xc020, 0xc420 and 0xe003.

One problem in the new test file: on its own it no longer collects. python -m pytest tests/test_swizzle_numeric.py from the repository root (as AGENTS.md runs it) gives:

ImportError while importing test module 'tests/test_swizzle_numeric.py'
E   ModuleNotFoundError: No module named 'spirv_mini_interp'

from semantic.support import translate_lines imports tests/semantic/support.py, which imports spirv_mini_interp as a top-level module, and tests/semantic is not on sys.path. So all 11 tests are lost, including the 7 that passed at 90c18f3. With PYTHONPATH=tests/semantic all 11 pass. The fix is to add the path the way tests/test_rdna2spirv_pixelinput.py:30-31 does (sys.path.insert(0, os.path.join(ROOT, "tests", "semantic")), then from support import translate_lines).

AI-assisted (Devin), CPU-only.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At b98edb9 the root-level test import issue is fixed, and the symbolic ROTATE/FFT operands decode to the same lane arithmetic as their numeric encodings in the added cases. Independent review found no remaining actionable issue.

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b98edb9a on NVIDIA GeForce RTX 5070 Ti (Blackwell, driver 616.56, subgroup 32..32), Windows 11 10.0.26300; translator from the PR head, runtime test programs built from main aa2988cc (MSVC 19.44, Ninja RelWithDebInfo): pass. scripts/run_gpu_tests.py: 164/210 passed, 46 skipped (wave64 fixtures without emulation on a 32-wide GPU), 0 failed: the same result as main aa2988cc on this machine for every test (diffed test by test). No game files. AI-assisted (Claude Code).

@bielfreedom

Copy link
Copy Markdown

Full-corpus check on the game copy (Xbox Series 1.0.2.1; Windows 11, RX 9070 XT). Counts only, no shader names or data.

PR head b98edb9a, merge-base aa2988cc (same commit as my baseline). All 26,503 disassembled shaders translated with --no-asm, 8 shards + --merge.

Result: byte-identical to main.

main this PR
translated 26,503 26,503
pass spirv-val 26,422 26,422
tier A / B / C (32-wide) 510 / 11 / 1,161 510 / 11 / 1,161
.spv that differ — 0
index.json — identical

No shipped shader is refused on main either: 26,503 of 26,503 translate. So the game corpus never reaches the rotate/FFT ds_swizzle_b32 encodings this PR adds, and the change carries no regression risk for the game's shaders. Its coverage is the synthetic and LLVM-emitted cases described above.

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.

6 participants