Skip to content

lds_fuzz.py: add ds_permute_b32 differential fuzzing (FUZZ-10) - #789

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
eIBart0:fuzz-10-ds-permute
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
eIBart0:fuzz-10-ds-permute

Conversation

@eIBart0

@eIBart0 eIBart0 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Implement FUZZ-10 by adding ds_permute_b32 (forward scatter permute, supported in tools/rdna2spirv.py since SHADER-90 / #628) to the LDS differential fuzzer tools/lds_fuzz.py and adding unit test coverage in tests/test_lds_fuzz.py.

Changes

  • tools/lds_fuzz.py:
    • Added "permute" to EXEC_KINDS, Program.random_op(), Program.make(), Program.asm_op() (ds_permute_b32 v{dst}, v{ADDR}, v{src} offset:{offset}), and Program.dst_regs().
    • Implemented the RDNA2 forward-scatter model in Program.model_op(): within each 32-lane half, active source lanes scatter src[s] to (s & ~31) | ((((s * mul + add) * 4 + offset) >> 2) & 31) in ascending lane order so the highest-numbered active source wins collisions; active destination lanes not targeted by any active source receive 0, and inactive destination lanes preserve their prior value.
    • Updated the module docstring and comment (removing ds_permute_b32 from the list of translator-rejected instructions).
  • tests/test_lds_fuzz.py:
    • Added test_permute_is_generated_and_no_exec_clears_only_permute verifying "permute" is in EXEC_KINDS, generated with EXEC masks, and cleanly stripped by no_exec=("permute",) without perturbing the rest of the program.
    • Added test_permute_collision_highest_active_wins_and_unaddressed_is_zero and test_permute_half_isolation_and_offset verifying collisions, unaddressed active lanes (0), inactive destination preservation, 32-lane half isolation in wave64, and offset handling.

Commits

  1. lds_fuzz.py: add ds_permute_b32 generator and forward-scatter RDNA2 model (FUZZ-10) (tools/lds_fuzz.py)
  2. tests/test_lds_fuzz.py: test ds_permute_b32 collisions, inactive lanes, and no-exec (tests/test_lds_fuzz.py)

Verification

  • Negative control (python3 -I tests/test_lds_fuzz.py -v against unmodified origin/main tools/lds_fuzz.py): all 3 new permute tests fail/error (failures=1, errors=2).
  • python3 -I tests/test_lds_fuzz.py -v: 21 tests passed (OK).
  • python3 -I tools/lds_fuzz.py --model-only --programs 50 --seed 1 --wave both: 100 programs generated and modelled cleanly.
  • End-to-end translator + spirv-val + spirv_mini_interp: generated collision + unaddressed-active + inactive-destination programs assemble with llvm-mc-22, translate with tools/rdna2spirv.py, pass spirv-val in wave32, wave64 native, and --wave64-emulate, and match Program.model() bit-for-bit across all 64 threads × 16 registers in spirv_mini_interp.run_workgroup.
  • LLVM_MC=/usr/bin/llvm-mc-22 pytest tests/test_lds_fuzz.py tests/test_translate.py -q: 233 passed, 1 skipped.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested dfe9789 on Linux (WSL2 Ubuntu), Python 3.12, CPU only: pass

  • pytest tests/test_lds_fuzz.py: 21 passed
  • Negative control: with tools/lds_fuzz.py from main, the 3 new permute tests fail.
  • Code read: permute is added consistently to EXEC_KINDS, random_op, make, asm_op, dst_regs and model_op. The address goes through v_mul_u32_u24 with both factors at most 63, so the 24-bit multiply can't overflow. Before reading mismatches as translator bugs, it would be worth checking the scatter model on an RDNA2 GPU: highest source wins, an active lane nobody targets gets 0, and the halves are 32 lanes.

@shelbykb2

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of ds_permute_b32 / ds_bpermute_b32, as @kapitche asked: the raw instructions run as compute kernels on an AMD Radeon RX 6800 (gfx1030, not the console's gfx1013; the hw-oracle tool from the AnyPS5 project), wave32 and wave64, sentinel in the destination, per-lane distinct data.

All three assumptions of the scatter model hold:

Assumption Hardware Deciding case
The highest source lane wins a collision agrees all 32 lanes target lane 5: lane 5 gets lane 31's data. With the top sender's EXEC off, the next-highest active sender wins
An active lane nobody targets gets 0 agrees same case: lane 0 reads 00000000, not its sentinel and not its own data
wave64: the two 32-lane halves are separate agrees lane 5 gets lane 31's data and lane 37 gets lane 63's; a rotate by one wraps inside each half

The rule as measured:

half(i) = i & ~31
ds_permute_b32 vD, vAddr, vData offset:OFF
    tmp[*] = 0
    for i ascending: if EXEC[i]: tmp[half(i) | (((vAddr[i] + OFF) >> 2) & 31)] = vData[i]
    for i: if EXEC[i]: vD[i] = tmp[i]
ds_bpermute_b32: s = half(i) | (((vAddr[i] + OFF) >> 2) & 31)
    if EXEC[i]: vD[i] = EXEC[s] ? vData[s] : 0
  • The address is a byte offset: offset: is added before the shift and only bits 6:2 of the sum matter (indices of 32, 64 or 4096+n wrap modulo 32).
  • An EXEC-off source does not send; an EXEC-off destination keeps its old value. LDS memory is not touched.
  • "Highest lane wins" and "last write in ascending lane order wins" give the same result and can't be told apart with one instruction.
  • Reading the translator on main (not run): ds_permute_b32 from Translator: ds_permute_b32, the forward (scatter) cross-lane permute (SHADER-90) #628 agrees on every point above. One difference on main is on the other instruction: the plain wave32 ds_bpermute_b32 path has no EXEC test on the source, where hardware reads 0 from an EXEC-off source lane. Translator: wave32 ds_bpermute_b32 reads 0 from inactive source lanes #253 addresses that.

Not measured: gfx1013 itself, partial waves, addr + offset above 0xffff. 6 dispatches for this part, each about 1 to 3 seconds, wave32 and wave64 in agreement on every word.

AI-assisted (Claude Code).

@caua-an

caua-an commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested PR head dfe9789 on AMD Radeon RX 7600 (RDNA3/Navi 33), RADV Mesa 26.2.4, Zorin OS 18.1, LLVM 21.1.8 and SPIRV-Tools 2025.1.

  • python3 -I tests/test_lds_fuzz.py -v: 21/21 passed.
  • lds_fuzz.py --programs 100 --seed 7600 --wave both: 187/200 matched natively and 187/200 with --emulate; 13 mismatches in each run.
  • With --no-exec bpermute added as a diagnostic: 200/200 matched natively and 200/200 with --emulate. There were no skips, untranslated programs or harness errors. Of the 200 generated programs, 89 contained ds_permute_b32.

Most reported mismatches in the unrestricted run point to bpermute with inactive lanes, consistent with the known wave32 issue fixed by #253. This PR branch does not contain that merge yet. I have not confirmed that every mismatch has the same cause.

--no-exec bpermute runs that operation with all lanes active; it does not remove it. The 200/200 diagnostic result therefore does not replace an unrestricted pass. I can rerun after the branch is updated against main.

No game files or game-derived data were used.

@crastinator

Copy link
Copy Markdown
Contributor

Tested dfe9789 on a discrete RDNA2 card: AMD Radeon RX 6700 XT (Navi 22, gfx1031), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1. Pass.

The branch conflicts with main now, but only in a comment. Main added the 8-bit d16 reads to READS (SHADER-85), so both versions of the "the translator rejects ..." comment above WRITES are stale. I dropped both and merged into main c0ac5fd6 locally.

  • tests/test_lds_fuzz.py: 25 passed.
  • lds_fuzz.py --wave both --programs 300, seeds 1, 2 and 3:
Seed Native (wave32 + wave64) --emulate (wave64 at subgroup 32)
1 600/600 match 600/600 match
2 600/600 match 600/600 match
3 600/600 match 600/600 match

0 skipped, 0 not translated, 0 harness errors. 423 of seed 1's 600 programs contain at least one ds_permute_b32.

  • Negative controls, native, seed 1, 100 programs, both waves. Each control changes one rule of the permute model:

    • lowest active source wins a collision (instead of highest): 39 of 200 programs mismatch;
    • an active destination lane that no source addresses keeps its value (instead of 0): 56 of 200 mismatch.

    So the GPU runs do exercise collisions and unaddressed lanes, and the model's rules (highest active source wins, unaddressed active lanes get 0, inactive lanes keep their value, each 32-lane half on its own) match RADV's translation on this card.

No game files. AI-assisted (Claude Code). I wrote the original lds_fuzz.py (FUZZ-6).

@eIBart0

eIBart0 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main (89b047cd) at 9c326c2c and reconciled with #1345 (--oracle hw) and #1447:

  • Kept permute=False by default in tools/lds_fuzz.py (enabled via --permute / --extended) so default seed program indices remain unchanged.
  • Widened Program.make("permute") multipliers from odd-only (randrange(1, 64, 2)) to 0..63 (randrange(0, 64)) so --permute / --extended exercises many-to-one scatter collisions in addition to bijections, matching the RDNA2 gfx1030 hardware oracle measurement by @shelbykb2 (SHADER-133) and the gfx1031 runs by @crastinator.
  • Added the permute_lowest_wins negative-control perturbation, collision/half-isolation/no-exec unit tests in tests/test_lds_fuzz.py, parity coverage in tests/test_lds_fuzz_hw.py, and RDNA2 ds_permute_b32 notes in docs/shader_validation.md.

Verified CPU-only locally (tests/test_lds_fuzz.py, tests/test_lds_fuzz_hw.py, tests/test_lds_fuzz_cli.py, and --model-only --extended --programs 50); ready for @caua-an / GPU rerun against main (which now includes #253).

@x2Tweek

x2Tweek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Static review — PR #789

Branch: fuzz-10-ds-permute (head of PR #789)
Commit: (head of PR #789)
Author: elBarto
State: OPEN, mergeable

What it does

Adds ds_permute_b32 differential fuzzing to lds_fuzz.py (FUZZ-10 / SHADER-133):

tools/lds_fuzz.py:

  • New perturb permute_lowest_wins (lowest active source lane wins collision) alongside the existing permute_no_zero.
  • Permute generator now emits both odd (bijective) and even (colliding) multipliers; hypothesis updated to "highest active sender wins collisions, unaddressed lanes get 0, halves separate."
  • model_op for permute: scatters within each 32-lane half; highest-numbered active source wins collisions (iterates lanes in reverse); unaddressed active destinations get 0; inactive destinations keep old values; halves stay separate.

tests/test_lds_fuzz.py:

  • test_permute_is_generated_and_no_exec_clears_only_permute — permute ops generated, no_exec clears only permute exec masks.
  • test_permute_collision_highest_active_wins_and_unaddressed_is_zero — collision winner, zero-fill, half isolation, inactive destinations preserve values.
  • test_permute_half_isolation_and_offset — identity + offset within each 32-lane half.

tests/test_lds_fuzz_hw.py:

  • Permute programs carry hypotheses; generates both colliding and bijective multipliers (parities == {0, 1}).

docs/shader_validation.md — documents RDNA2 ds_permute_b32 hardware verification results (RX 6800 gfx1030, RX 6700 XT gfx1031, Radeon 880M gfx1150).

Reviewer recommendation

Approve. The permute model matches measured RDNA2 hardware behavior (highest active sender wins collisions, zero-fill, half isolation). Generator covers both bijective and colliding multipliers. Tests are thorough. All 30 unit tests + 38 HW tests pass, compileall clean.

CPU test results

  • tests/test_lds_fuzz.py: 30 passed
  • tests/test_lds_fuzz_hw.py: 38 passed, 1 skipped (OnHardware test skipped without GPU)
  • python -m compileall: clean on all changed .py files

AI-assisted. Reviewed with Claude Code.

@ItsNotRexed
ItsNotRexed merged commit 134b29a into ItsNotRexed:main Nov 22, 2026
10 of 11 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.

7 participants