Repository navigation
Use native x86 shuffles for reverse - #448
Yikai-Liao wants to merge 1 commit into
Conversation
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.
That's just using the library wrong, isn't it? All SIMD functions have to be If you don't want to worry about any of that, use the |
|
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. |
|
Oh thanks, my fault — that #[inline] example didn't follow MANUAL_INLINING.md (#[inline(always)] / #[simd]). Re-testing with reverse() properly inlined inside
Would you prefer keeping all x86 backends for direct shuffle codegen, or scoping this PR to SSE2 only? |
|
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. |
|
Makes total sense — closing this PR. Thanks for the review |
Summary
Lower all non-mask x86
reverseimplementations (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-levelswizzle_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-orderagainstvqsortand prototyping portable bitonic sorting networks onfearless_simd.In trivial
-O3loops (or with#[inline(always)]everywhere) LLVM foldsswizzle_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:-O3inline threshold (AVX-512): For a small#[inline]helper insidesimd.vectorize(...):swizzle_dyn_u8x64pushedbitonic_steppast LLVM's inline threshold, leaving out-of-line stack-passed calls toswizzle_dyn_u8x64::kernel. Native_mm512_permutexvar_*inlines completely with zero calls.opt-level = "z"/0: SSE2 fallback emitted scalarmovs 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, defaultx86_64-unknown-linux-gnuruntime dispatch, median of 5 runs; 32/64-bit cases only — 8/16-bit timings were not separately benchmarked). Eachbitonic_*run executes 5M × 5 = 25Mbitonic_stepcalls (under-Ozbitonic_stepstays out-of-line in both builds):bitonic_avx512_u32x16/u64x8(25M steps)opt-level = 3bitonic_avx2_{u32x8,u64x4,f32x8,f64x4}(25M steps)opt-level = 3stream_avx2/stream_avx512(8 KiB × 100k)opt-level = 3bitonic_avx2/bitonic_avx512(25M steps)opt-level = "z"stream_sse2_u32x4(8 KiB × 100k)opt-level = "z"stream_avx2_{u32x8,u64x4,f64x4}(8 KiB × 200)opt-level = 0Testing
cargo run --bin fearless_simd_gen && git diff --exit-codecargo fmt --all --check,bash .github/copyright.sh,cargo clippytarget-cpu=icelake-server,x86-64-v3,x86-64-v2, andi686-unknown-linux-gnucargo test --workspace --locked --all-features(fearless_simd_tests: 13,966 passed, 175 ignored, 0 failed; all 305reversetests passed across fallback, SSE2, SSE4.2, AVX2, and AVX-512)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.