Skip to content

Add concat_swizzle_dyn and concat_swizzle_dyn_precise - #354

Open
Shnatsel wants to merge 3 commits into
linebender:mainfrom
Shnatsel:concat-swizzle-dyn-2
Open

Add concat_swizzle_dyn and concat_swizzle_dyn_precise#354
Shnatsel wants to merge 3 commits into
linebender:mainfrom
Shnatsel:concat-swizzle-dyn-2

Conversation

@Shnatsel

@Shnatsel Shnatsel commented Aug 27, 2026

Copy link
Copy Markdown
Contributor
  1. Add concat_swizzle_dyn and concat_swizzle_dyn_precise
  2. Change swizzle_dyn_precise SSE4.2 128-bit formulation; it's one fewer instruction, slightly worse latency, but much better optimization when larger vectors are decomposed into it.
  3. Use select_unpredictable stabilized in 1.88 in scalar fallbacks of concat_swizzle_dyn_precise and swizzle_dyn_precise.

Rationale for the branchless formulation: cmov is far from a silver bullet and is often a bad idea. In my benchmarks this is 20% to 40% slower than branches for swizzle_dyn_precise if the indices are all out of range, but avoids a 5x (400%) performance degradation on unpredictable indices. If you know your indices are predictable, you don't use _precise in the first place, so tuning for unpredictability seems fine. Most importantly, removing branches helps autovectorizer a lot, so we get more reliable autovectorization on e.g. RISC-V, which is the primary use case.

Implementation-wise, NEON and AVX-512 are straightforward with native-ish instructions, SSE4.2 uses the same decomposition idea as swizzle_dyn_precise, and AVX2 needs babysitting as always.

The first commit is squashed because I did a bunch of experiments with different formulations, e.g. a specialized one for 256-bit AVX2 that reduces shuffle port pressure on haswell and doesn't regress recent AVX2 much if at all, but in the end decided those are not worth the extra complexity.

I'm not thrilled by the lhs.concat_swizzle_dyn(rhs, indices) API but I don't see how to do better without it getting really awkward in other ways: concat_swizzle_dyn(simd, lhs, rhs, indices) isn't any better and breaks convention, and indices.concat_swizzle_dyn(lhs, rhs) runs into issues with matching vector lengths.

@Shnatsel

Copy link
Copy Markdown
Contributor Author

@Novum please let me know if this looks good to you

@Novum

Novum commented Aug 27, 2026

Copy link
Copy Markdown

Looks good for me for those operations

…wizzle_dyn_precise SSE4.2 formulation somewhat so that recursive decomposition optimizes better.
…vectorization on RISC-V (16-bit case now vectorizes) and better fallback performance (no more 3x degradation on unpredictable indices) without too much of a hit on predictable ones
@Shnatsel
Shnatsel force-pushed the concat-swizzle-dyn-2 branch from 8c3d911 to f5de48d Compare August 30, 2026 10: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.

2 participants