Skip to content

Perf: SIMD candidates in hot CPU loops (sparse page scan, XMA IMDCT, sample conversion) #459

Backlog

Description

@1bugproof

XCL is built for baseline x86-64 (no .cargo/config.toml, no target-cpu / target-feature), so the compiler can only use SSE2, and the crate has no hand-written SIMD. A few CPU loops that run every frame look like good SIMD candidates. This issue lists them so the work can be split into small PRs.

Nothing here has been profiled yet. The order comes from reading the code and how often each loop runs. Before a PR for any of these, please measure it on a real title and put the before/after numbers in the PR.

Candidates

1. Sparse page dirty scan: hle/d3d12x/sparse.rs

scan_page / changed_chunks (around lines 298–366) hash every 64-byte chunk of every dirty 4 KiB page with eight scalar u64 multiplies, and compare the result with the hash last sent. This runs from sparse::before_execute on every ExecuteCommandLists (title_textures.rs:731), so it is probably the hottest of the four.

  • Small: the all-zero test (OR of 8 words) becomes two 256-bit loads and a vptest.
  • Larger: keep a copy of the bytes last sent and compare 32 bytes at a time (_mm256_cmpeq_epi8 + movemask) instead of hashing. That uses 4 KiB per page instead of 512 B of hashes, so it is a design change and needs its own PR and a memory measurement.

2. XMA IMDCT / FFT: hle/xma2/mod.rs

imdct_half (around line 491) is a radix-2 FFT stored as an array of (f32, f32) pairs, with a pre and post rotation. The butterflies and rotations can process 4–8 complex values at a time with SSE / AVX. This runs for every XMA frame of every voice. The output must still match the FFmpeg reference that the existing test checks (decodes_xma2_and_xma1_bitstreams_matching_ffmpeg).

3. Sample format conversion

  • hle/acphal.rs:821: i16 → f32 (v as f32 / 32768.0)
  • hle/xma2/mod.rs:1889: f32 → i16 (round + clamp)

These are simple loops that LLVM vectorizes by itself once AVX2 is available. Otherwise _mm_cvtepi16_epi32 / _mm_packs_epi32 do it by hand.

4. Shader blob hash: gpu/shader.rs:141 (fnv64), low priority

Byte-at-a-time FNV-1a, used for every shader blob and once on the whole xcl.exe (gpu/recomp/pso.rs:148). FNV cannot be vectorized, but a hash that reads a word at a time would be faster. The catch: the hash names the cache files, so changing it throws away every user's shader cache. Only worth doing if it shows up in a profile.

Not in scope

Texture tiling (gpu/tiling.rs, detile / tile) copies one element at a time, but the fix there is copying whole rows rather than SIMD, and #451 and #440 already cover it.

How to enable SIMD

Two options:

  1. Raise the build target to x86-64-v3 (AVX2 + FMA). This is the least code, but CPUs without AVX2 could no longer run XCL. That is the maintainer's call.
  2. Detect at runtime (recommended): #[target_feature(enable = "avx2")] versions of only the functions above, chosen with is_x86_feature_detected! (already used in launcher.rs:419), with the scalar code kept as the fallback.

Each candidate should be its own PR with a unit test that checks the SIMD path gives the same result as the scalar one.


Written with Claude Code (Claude Opus 5.5), from reading the code on main at 2997c9a. Nothing was built, run or profiled.

Activity

  1. AlpPesen commented on Nov 24, 2026

    @AlpPesen
    Contributor

    Measured sparse.rs (scan_page / changed_chunks) on a real title (YouKnowWho, 60 s, Intel Core i9-9900K + AMD Radeon RX 6800, release build):

    • main (508a0c7, scalar OR-reduction): sparse windows timing: write-watch 935 ms (136393 dirty pages), lock wait 0 ms, chunk scan 86 ms, upload 155 ms (15532 runs) (reaching 2257 ms write-watch / 86 ms chunk scan over 137681 dirty pages by 27.8 s).
    • Prototype with runtime-detected AVX2 vptest (_mm256_loadu_si256 + _mm256_or_si256 + _mm256_testz_si256) on scan_page / changed_chunks: write-watch 402 ms (135751 dirty pages), lock wait 0 ms, chunk scan 100 ms, upload 138 ms (14527 runs) (reaching 1196 ms write-watch / 109 ms chunk scan over 265548 dirty pages by 31.7 s).

    Takeaway: on this title, chunk scan is ~0.15–0.18% of 60 s wall time (an order of magnitude smaller than write-watch), and adding an AVX2 vptest pre-check did not improve it — most dirty pages have non-zero chunks, so checking with 256-bit loads before scalar hashing adds extra loads/branch overhead (plus AVX2 frequency downclocking on Coffee Lake). Sharing the measurement here rather than opening a PR that doesn't beat scalar.

    Measured locally with Antigravity (Gemini 4 Neon).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions