Skip to content

Use native x86 shuffles for reverse - #448

Closed
Yikai-Liao wants to merge 1 commit into
linebender:mainfrom
Yikai-Liao:opt-x86-reverse
Closed

Yikai-Liao wants to merge 1 commit into
linebender:mainfrom
Yikai-Liao:opt-x86-reverse

Conversation

@Yikai-Liao

@Yikai-Liao Yikai-Liao commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

Lower all non-mask x86 reverse implementations (8/16/32/64-bit elements across 128/256/512-bit vectors) directly to native shuffle/permute intrinsics instead of SSE2 scalar fallbacks or byte-level swizzle_dyn. Vector-backed masks (reverse_vector_mask_method) and wider-than-native vectors (generic_op) inherit this automatically.

Only fearless_simd_gen/src/mk_x86.rs (+100 / -19) is hand-written; the remaining diff is regenerated output. Non-x86 backends are left unchanged.

Motivation

I ran into this while optimizing my sorting library tessera-order against vqsort and prototyping portable bitonic sorting networks on fearless_simd.

In trivial -O3 loops (or with #[inline(always)] everywhere) LLVM folds swizzle_dyn, so steady-state throughput is already close. However, routing through 16/32/64-byte index arrays and dynamic byte swizzles can be much slower when inlining or auto-vectorization fails:

  • -O3 inline threshold (AVX-512): For a small #[inline] helper inside simd.vectorize(...):
    #[inline]
    fn bitonic_step<S: Simd>(a: u32x16<S>, b: u32x16<S>) -> (u32x16<S>, u32x16<S>) {
        let rb = b.reverse();
        (a.min(rb), a.max(rb).reverse())
    }
    swizzle_dyn_u8x64 pushed bitonic_step past LLVM's inline threshold, leaving out-of-line stack-passed calls to swizzle_dyn_u8x64::kernel. Native _mm512_permutexvar_* inlines completely with zero calls.
  • opt-level = "z" / 0: SSE2 fallback emitted scalar movs instead of shuffles, and AVX2/AVX-512 executed the full 9-intrinsic / dynamic byte-swizzle kernels.

Benchmarks

AMD Ryzen 7 8845H (Zen 4, rustc 1.100.0, default x86_64-unknown-linux-gnu runtime dispatch, median of 5 runs; 32/64-bit cases only — 8/16-bit timings were not separately benchmarked). Each bitonic_* run executes 5M × 5 = 25M bitonic_step calls (under -Oz bitonic_step stays out-of-line in both builds):

Benchmark Profile Before After Speedup
bitonic_avx512_u32x16 / u64x8 (25M steps) opt-level = 3 496.8 / 508.0 ms 20.9 / 21.7 ms 23.8× / 23.5×
bitonic_avx2_{u32x8,u64x4,f32x8,f64x4} (25M steps) opt-level = 3 16.3–25.7 ms 16.4–26.1 ms ~1.0× (already inlined)
stream_avx2 / stream_avx512 (8 KiB × 100k) opt-level = 3 5.66–8.19 ms 5.58–7.19 ms 1.01–1.15×
bitonic_avx2 / bitonic_avx512 (25M steps) opt-level = "z" 456–575 ms 141–189 ms 2.89–3.54×
stream_sse2_u32x4 (8 KiB × 100k) opt-level = "z" 46.72 ms 12.80 ms 3.65×
stream_avx2_{u32x8,u64x4,f64x4} (8 KiB × 200) opt-level = 0 11.95–13.20 ms 3.43–6.11 ms 2.16–3.52×

Testing

  • cargo run --bin fearless_simd_gen && git diff --exit-code
  • cargo fmt --all --check, bash .github/copyright.sh, cargo clippy
  • Static checks: target-cpu=icelake-server, x86-64-v3, x86-64-v2, and i686-unknown-linux-gnu
  • cargo test --workspace --locked --all-features (fearless_simd_tests: 13,966 passed, 175 ignored, 0 failed; all 305 reverse tests passed across fallback, SSE2, SSE4.2, AVX2, and AVX-512)
  • MSRV 1.89 and remaining cross-targets rely on CI.

AI Disclosure

Gemini 4 Neon assisted with drafting the generator change and microbenchmarks, and Codex CLI assisted with code review. I have reviewed and verified all changes and measurements myself.

Lower all non-mask x86 reverse() implementations (8/16/32/64-bit elements across 128/256/512-bit vectors) directly to native shuffle/permute intrinsics instead of SSE2 scalar fallbacks or byte-level swizzle_dyn.

Vector-backed masks on SSE2/SSE4.2/AVX2 inherit this via reverse_vector_mask_method, and wider-than-native vectors inherit it via generic_op split/combine.
@RunDevelopment

Copy link
Copy Markdown
Contributor

However, routing through 16/32/64-byte index arrays and dynamic byte swizzles can be much slower when inlining or auto-vectorization fails:

  • -O3 inline threshold (AVX-512): For a small #[inline] helper inside simd.vectorize(...):
    #[inline]
    fn bitonic_step<S: Simd>(a: u32x16<S>, b: u32x16<S>) -> (u32x16<S>, u32x16<S>) {

That's just using the library wrong, isn't it? All SIMD functions have to be #[inline(always)] or #[simd] (unless you manually call simd.vectorize inside the function). See MANUAL_INLINING.md for more details.

If you don't want to worry about any of that, use the #[simd] macro. With #[simd] fn bitonic_step<S: Simd>(...), the compiler isn't forced to inline and can emit potentially smaller binaries with -Os.

@Shnatsel

Copy link
Copy Markdown
Contributor

I agree with @RunDevelopment - you're going to get poor performance on inlining failures no matter what. Please apply proper inlining to your helper and re-measure.

@Yikai-Liao

Yikai-Liao commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Oh thanks, my fault — that #[inline] example didn't follow MANUAL_INLINING.md (#[inline(always)] / #[simd]).

Re-testing with reverse() properly inlined inside dispatch! on Zen 4,

  • SSE2: still gets a real speedup (u8x16/u16x8/u32x4 1.19–1.85× at -O3, 2.06–5.09× at -Os, since swizzle_dyn falls back to scalar indexing),
  • AVX2 / AVX-512: ~1.00× as LLVM already constant-folds the byte swizzle

Would you prefer keeping all x86 backends for direct shuffle codegen, or scoping this PR to SSE2 only?

@Shnatsel

Copy link
Copy Markdown
Contributor

SSE2-only hardware is extinct in the wild and you're already doing runtime dispatch, so it's not going to affect performance in practice. This is why SSE2 is implemented as either reusing SSE4.2 or as scalar fallback, with only a handful of exceptions.

@Yikai-Liao

Copy link
Copy Markdown
Author

Makes total sense — closing this PR. Thanks for the review

@Yikai-Liao Yikai-Liao closed this Nov 25, 2026
@Yikai-Liao
Yikai-Liao deleted the opt-x86-reverse branch November 25, 2026 13:05
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