Skip to content

wave64_halves: per-opcode caches, analyse() about 35% faster with identical results - #7014

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/wave64-halves-fast
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/wave64-halves-fast

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted (Claude). Claimed in #6910 (#6910 (comment)); a new item with no ID in the tables.

What

tools/wave64_halves.py spent about a third of analyse() in operands() running chains of str.startswith on the opcode name for every instruction, on every fixed-point pass. What the name alone decides now comes from two lru_cache(maxsize=1024) tables (_op_class, base_op), and operands() reads it. Two small allocation savings in Analysis.step (skip the empty rank/base unions; clear both entries at once for a destination with neither tag) and a width-1 path in _regs().

No rule, taint bit or dataflow logic changes. Only operands, base_op, _regs and two spots in Analysis.step are touched. wave64_emulate_opt.py (#6895) and lane_masks.py (#6086) are untouched. Other open PRs edit wave64_halves.py in transfer/vote/census, which this doesn't touch; operands() is the one shared region (a rebase may need a hand merge of its dispatch).

Measured

Inputs: the 557 analyse() calls the existing wave64 semantic tests make (hand-written listings, 8121 instructions, 7 to 46 per program), captured in-process and replayed against origin/main and this branch on the same objects. python -I, Windows 11, time.perf_counter, best of 15, whole set per sample (three runs: 2 reported below, the third was cut off by the machine's load).

before (origin/main) after
run 1, best / median 0.8550 s / 0.8859 s 0.5354 s / 0.5505 s (-37.4% / -37.9%)
run 2, best / median 0.8688 s / 0.9149 s 0.5618 s / 0.5861 s (-35.3% / -35.9%)
tracemalloc peak over the set 418 KiB 417 KiB

Exactness: tier, reasons, idioms, uniform_readlane, scalar_spills and sites are equal on all 557 programs (checked against the origin/main module run on the same inputs). The caches hold at most 1024 entries each (cap asserted at 4096 in the tests); the keys are opcode names, a few hundred distinct strings.

Tests

  • New tests/semantic/test_wave64_halves_fast.py (6 tests): operands / base_op / _regs equal a verbatim copy of the previous code over every opcode the module names (each with _e32/_e64/_sdwa/_dpp), 12 operand shapes and 3 modifier sets (including the exception type where the old code raised), fresh lists each call, and caches stay bounded after 5000 distinct opcodes.
  • python -m compileall -q tools/wave64_halves.py tests: clean.
  • python -I -m unittest discover -s tests/semantic -p "*wave64*": 240 tests OK.
  • tests/test_bench_wave64.py, test_make_game_wave64.py, test_wave64_emulate_opt_cfg.py, test_wave64_graphics_scan.py, test_wave64_pipeline_audit.py: OK. test_wave64_halves_fixtures.py via pytest: 9 passed, 1 skipped (llvm-mc).
  • Not run: the full tests/semantic suite, the GPU tests. No GPU is involved (pure Python).

🤖 Generated with Claude Code

…aster analyse() with identical results

operands() and base_op() ran a chain of str.startswith tests on every call, a third of an analysis. What the opcode
name alone decides now comes from two lru_cache(maxsize=1024) tables (_op_class, base_op); step() skips the empty
rank/base unions and clears both entries at once for a destination with neither tag, and _regs() has a width-1 path.
No rule or taint logic changes.

tests/semantic/test_wave64_halves_fast.py pins operands/base_op/_regs to a verbatim copy of the previous code over
every opcode the module names and 12 operand shapes, and checks the caches stay bounded.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a7c0a626 on Windows 11 with CPython 3.14.7, CPU only: pass. All 240 tests pass with py -3.14 -I -m unittest discover -s tests/semantic -p "*wave64*.py".

I also timed analyse() on one synthetic vertex-shader block. It contains 192 instructions. The block cycles through v_xor_b32_e32, v_and_b32_e32, and v_or_b32_e32 and uses eight vector registers. The head and parent return equal analysis results.

The median time for ten calls falls from 56.67 ms on the parent to 39.99 ms on the head (29% less time). I used nine samples per revision and alternated the run order. This measures repeated opcodes on a warm cache.

Against the parent, the four new behavior checks pass. The two cache checks error because the parent has no cache API.

AI-assisted.

@Cattn

Cattn commented Nov 23, 2026

Copy link
Copy Markdown

Tested a7c0a626 (only the tools/wave64_halves.py diff, applied on current main 2dbcb882c) on Windows 11, CPython 3.12.10, CPU only.

  • All 16 wave64 test files pass on both main and this PR. test_wave64_halves_fixtures.py gives 9 passed, 1 skipped (no llvm-mc) on both.
  • Exactness: I recorded the 279 analyse() calls the wave64 semantic tests make (4,064 instructions), with mask keys and id() values mapped to (block, index) so they compare across processes. Replaying them through main and this PR gives identical analyse() results, identical full ev, and identical operands() output for every instruction. A main-vs-main replay also matches, and the replay matches the original runs.
  • Speed: best of 15, two alternating rounds: 0.155–0.163 s on main → 0.095–0.098 s here (about 38–40% less). tracemalloc peak is the same.
  • Nit: line 268 lost its \ continuation in the ds_ branch, leaving a run of spaces in a 142-character line.
  • The PR body says 557 calls and 8,121 instructions; I get 279 and 4,064, almost exactly half, so the set may have been counted twice. The percentage is unaffected.
  • Heads-up for merge order: wave64_halves: preserve destination taint on partial writes and reject asymmetric DPP row_mask #4373 adds _preserves_dst(ins, op, mods) at the end of operands(), but mods now exists only in the atomic branch. Whichever lands second has to restore it. wave64_halves: a VALU mask destination printed as one SGPR is the SGPR pair (new SHADER item) #4794 also edits the same operands() lines.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Reviewed a7c0a626 on Ubuntu 24.04, Python 3.14.9, CPU only (no GPU needed).

  • Read tools/wave64_halves.py: base_op / _op_class caches, operands dispatch, the _regs n=1 path, and the Analysis.step skip when neither RNK nor BASE survives.
  • python3 -I -m unittest discover -s tests/semantic -p test_wave64_halves_fast.py: 6/6 OK.
  • Same discovery for test_wave64_halves.py: 125/125 OK.
  • python3 -I -m unittest discover -s tests/semantic -p '*wave64*.py': 240/240 OK.
  • git diff --check clean.

The new tests pin operands and base_op against a copy of the previous implementation over every named opcode × operand shape × mods, so a classification miss would fail there. I did not re-time analyse(). Found nothing.

AI-assisted.

@wasonidas

Copy link
Copy Markdown

AI-assisted independent CPU review of a7c0a626 against main 8575d36e on Windows 11 / CPython 3.15.2. Found zero differences across 159,768 synthetic operand cases and 60 analysis contexts with cold, warm, and actively evicted caches, including per-instruction dataflow state. Both caches stayed capped at 1,024 entries. The 240 wave64 semantic tests and 106 standalone wave64 tests passed.

Across 15 alternating runs, median analyse() time was 26.38 → 18.54 ms with warm caches, 26.30 → 19.49 ms when clearing the caches per program, and 23.68 → 18.90 ms on a block with 1,203 distinct opcodes. A separate 0%-hit direct operands() microbenchmark took 5.98 → 6.25 ms. All inputs were synthetic.

@Kev-Ball

Copy link
Copy Markdown

All 13,405 retail wave64 graphics listings produced identical classification results at a7c0a626 and its parent 1cc6e3c1. This includes 10,974 pixel, 2,236 vertex and 195 geometry shaders. The paired runs agreed on every returned field: tier, reasons, idioms, uniform readlane, scalar spills and reported sites. Both revisions classified 5,059 as A, 101 as B and 8,245 as C.

For each listing I passed the translator’s actual basic blocks, successors, lane masks and stage to both versions of wave64_halves.analyse, alternating which ran first. I checked the completed records against the full eligible set from the 26,503-listing Xbox 1.0.2.1 index, including uniqueness and stage.

Summed process CPU time inside the classifier fell from 3,546.70 s to 2,536.80 s (28.5% less CPU time, 1.40× speedup). This measures the classifier itself; parsing and the rest of translation are outside the timed call. CachyOS, CPython 3.14.10, CPU only, AI-assisted.

@Spggety

Spggety commented Nov 23, 2026

Copy link
Copy Markdown

Tested a7c0a626 on AMD Radeon RX 6600 XT (Navi 23, gfx1032, RDNA2, discrete), AMD proprietary driver 24.10.1 (32.0.12019.1028, Vulkan 1.3.292), subgroup 32..64, Windows 11 (build 22631). Toolchain: MSVC 14.44, Vulkan SDK 1.4.363.1, llvm-mc from rocm-sdk-core 10.0.0, Python 3.11.9. Pass.

  • tests/semantic/test_wave64_halves_fast.py: 6 OK.
  • scripts/run_gpu_tests.py (native wave64, PR merged onto main 1cc6e3c1, the runtime build's commit): 213/235 passed, 20 skipped, the same results as main itself (213/235, 20 skipped). The two failures, ms_clip and ms_wave_info_s3, are the same on main 1cc6e3c1 without this PR (3/3 reruns): on this driver vk_harness exits with 0xC0000005 before any readback, so the FAIL line has only the device line. They are not from this PR.

Synthetic inputs only, no game files. AI-assisted (Claude Code).

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