Repository navigation
Let the compiler vectorize the filter and permutation helpers - #121403
Conversation
A generic vector comparison converted to a vector of bits gives exactly the instructions the movemask intrinsics produced, `vpcmpeqb` plus `vpmovmskb` at `x86-64-v3` and a single `vptestmb` plus `kmovq` at `x86-64-v4`, so both branches are gone. It also replaces the scalar loop on the little-endian architectures that matched neither branch before. NEON keeps its intrinsics, because the generic form reduces each 16-byte group separately and costs 29 instructions against 19; big-endian keeps the scalar loop, because a bitcast of a vector of bits to an integer is endian dependent and would reverse the mask. Removing `immintrin.h` from the header exposed four `_blsr_u64` uses that relied on it leaking. Every one of them already carried `mask & (mask - 1)` as the non-BMI branch, and Clang compiles that to `blsr` where the target has BMI, so only the portable expression remains. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`toBits64` was a second, SSE2-only copy of `bytes64MaskToBits64Mask`, and both of its callers only took the `popcount` of the result. Counting through a bitmask is the weaker algorithm anyway, so `countBytesInFilter` and `countBytesInFilterWithNull` now use the one the NEON path already used: a counter per byte lane, widened every 255 blocks. Written with generic vectors it needs no intrinsics on either architecture, so the hand-written NEON goes away as well. The x86 loop drops from 13 instructions per 64 bytes to under 6, worth 20 to 37% on the kernel depending on working set. NEON keeps the exact loop the intrinsics produced. The architectures that matched neither branch before, and so counted a byte at a time, now get a vector path too, and there is no endianness concern because nothing is bitcast to an integer. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Six files pulled in an x86 intrinsic header without using a single intrinsic. Two of them, `distanceTransposed.cpp` and `MergeTreeIndexVectorSimilarity.h`, carried the include as a workaround for `simsimd` failing on `__bfloat16`; both translation units are still compiled with `USEARCH_USE_SIMSIMD` and `USEARCH_CAN_COMPILE_BF16` and build without it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The hand-written loop pinned the check to 16 bytes at a time, so an `x86-64-v3` build never used AVX2, and AArch64 had no vector path at all because comparing each element with the previous one carries a dependency across iterations. Stating the property directly, that every element equals its own index, and folding a block into a single value gives one branch per block and lets the compiler vectorize it on both. About 1.4x on x86 for a cache-resident permutation, 0.27 s against 0.33 s for AArch64 under emulation. Sorting 20M already-sorted rows, the worst case since the whole permutation is scanned, drops from 0.096 s to 0.048 s. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [f32e409] Summary: ✅
AI ReviewSummaryThis PR replaces several x86-specific intrinsics in the filter-mask/counting helpers and Missing context / blind spots
Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit f32e409.
Changed lines: Changed C/C++ lines covered: 84/84 (100.00%) · Uncovered code |
The comments explained the choices by comparing against implementations that were removed in this branch, which means nothing to a reader of the result. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units3661 translation units recompiled, 16936 s compile time in total, 3660 of them have a recent master baseline. |
On PowerPC the result type of a vector comparison depends on `-faltivec-src-compat`: the current default warns, and the announced default returns a scalar, which does not compile. A conversion to a vector of `bool` is the same `!= 0` per lane and has a fixed type. The generated code is unchanged on x86-64 and AArch64. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`__bfloat16` needs `<immintrin.h>`, which SimSIMD includes only from `SIMSIMD_TARGET_HASWELL` up, so `X86_ARCH_LEVEL` below 3 failed to compile. Under `SIMSIMD_DYNAMIC_DISPATCH` the bf16 entry points live in `c/lib.c`, which already sets `SIMSIMD_NATIVE_BF16` to 0, so this only picks a typedef and emits the same code. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Algunenano
left a comment
There was a problem hiding this comment.
This is a trivial PR removing intrinsics. There are no reported changes in the performance tests, only on local benchmarks, but the "refactor" alone is worth it
Pin the size the mask copy assumes, avoid forming a pointer past the end of the filter, keep the vector helpers inlined, and restore the early exit on the first element. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Several hot helpers were written with SSE2 intrinsics, which pinned them to 16 bytes per step even though the default build targets
x86-64-v3, and left AArch64 on scalar code. Written in plain C++ or with generic vector types, the compiler picks AVX2 or AVX-512 and vectorizes the ARM paths too.bytes64MaskToBits64Mask: one generic body replaces the AVX2 and AVX-512 branches and compiles to the same instructions at both levels. NEON keeps its intrinsics, and big-endian keeps the scalar loop because a bitcast of a vector of bits to an integer is endian dependent.countBytesInFilterandcountBytesInFilterWithNull:toBits64, a duplicate SSE2-only copy of the mask helper, is removed, and both now count with per-lane byte counters like the NEON path already did. The x86 loop goes from 13 instructions per 64 bytes to under 6.isIdentityPermutation: comparing each element with the previous one carried a dependency across iterations, so it never vectorized. Comparing each element with its own index vectorizes on both architectures.ORDER BYover 20M sorted rowsChangelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up filtering and sorting: the helpers that build filter masks, count filter bytes and check for an identity permutation now use AVX2 or AVX-512 on x86 instead of SSE2, and are vectorized on ARM where they were not.
Workflow [PR]
Sync PR [sync-upstream/pr/121403]
Version info
26.10.1.514-master(included in26.10and later)