[MOD-16730] Fix SIGSEGV in FP32/L2 SSE kernel: unaligned load in residual-3 path#990
Merged
dor-forer merged 3 commits intoJul 8, 2026
Merged
Conversation
…al-3 path
The residual % 4 == 3 path of FP32_L2SqrSIMD16_SSE used _mm_loadr_ps, which
compiles to movaps and requires a 16-byte-aligned address. Vectors are not
guaranteed that alignment: VecSimAllocator::allocate() returns malloc + 8
(allocation header), and the dispatcher sets no alignment hint when
dim % 4 != 0 - which is always the case on this path. On machines whose
dispatcher selects the SSE tier (no AVX), any FP32/L2 query with
dim % 16 in {3, 7, 11, 15} crashed with SIGSEGV.
Replace the reversed aligned load with an unaligned load plus an in-register
reverse shuffle - the same register contents, so the _mm_move_ss masking
trick and all results are unchanged. At machine level this swaps movaps for
movups with the identical shufps the compiler already emitted; no added
instructions.
Add a regression test that feeds the kernel buffers at the allocator's exact
placement (16-aligned base + 8); it segfaults on the previous kernel and
passes now.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #990 +/- ##
==========================================
- Coverage 97.12% 97.10% -0.02%
==========================================
Files 141 141
Lines 8164 8179 +15
==========================================
+ Hits 7929 7942 +13
- Misses 235 237 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
GuyAv46
reviewed
Jul 8, 2026
Comment on lines
40
to
43
| v1 = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(0, 1, 2, 3)); | ||
| v2 = _mm_shuffle_ps(v2, v2, _MM_SHUFFLE(0, 1, 2, 3)); | ||
| // sets the low float of v1 to the low float of v2, so the diff is 0. | ||
| v1 = _mm_move_ss(v1, v2); |
Collaborator
There was a problem hiding this comment.
Consider shuffling v1 and v2 together, to get the _mm_move_ss effect in 1 operation instead of 3
Instead of reversing both vectors before the subtraction, subtract the raw unaligned loads, rotate the out-of-residual 4th element of the diff into the low lane with one shuffle, and zero it via the still-zero accumulator with _mm_move_ss. One shuffle instead of two; results unchanged (lane order is irrelevant to the squared sum).
Avoids depending on sum still being zero at this point; the zero idiom is free and the compiler typically reuses an existing zeroed register.
GuyAv46
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the changes in the pull request
Fix a SIGSEGV in the FP32/L2 SSE distance kernel. The
residual % 4 == 3path ofFP32_L2SqrSIMD16_SSEused_mm_loadr_ps, which compiles tomovapsand requires a 16-byte-aligned address. Vectors are never guaranteed that alignment:VecSimAllocator::allocate()returnsmalloc + 8(allocation header), and the dispatcher only sets an alignment hint whendim % 4 == 0— while this path requiresdim % 4 == 3. On machines whose dispatcher selects the SSE tier (no AVX), any FP32/L2 query withdim % 16 ∈ {3, 7, 11, 15}crashed insideVecSimIndex_TopKQuery.The fix replaces the reversed aligned load with an unaligned load + in-register reverse shuffle (
_mm_loadu_ps+_mm_shuffle_ps(v, v, _MM_SHUFFLE(0,1,2,3))). Register contents are identical to the old_mm_loadr_ps, so the_mm_move_ssmasking trick and all results are unchanged; at machine level this swapsmovapsformovupswith the sameshufpsthe compiler already emitted — zero added instructions (verified by disassembly)._mm_blend_pswas rejected (SSE4.1; this TU compiles with-mssefor pre-SSE4.1 hardware). Setting the alignment hint was rejected (it aligns allocation bases only; stridedim*4 ≡ 12 (mod 16)keeps 3 of 4 vectors misaligned regardless).Includes a regression test that feeds the kernel buffers at the allocator's exact placement (16-aligned base + 8): segfaults on the previous kernel (reproduced locally and in CI on #987), passes with this fix. All FP32 spaces optimization tests pass with exact equality vs. the scalar baseline.
Which issues this PR fixes
Main objects this PR modified
src/VecSim/spaces/L2/L2_SSE_FP32.h—FP32_L2SqrSIMD16_SSEresidual % 4 == 3 load pathtests/unit/test_spaces.cpp— new regression testSpacesTest.FP32_L2Sqr_SSE_misaligned_residual3Mark if applicable
Note
Medium Risk
Touches the FP32 L2 SIMD hot path used during TopK search, but the change is narrowly scoped to one residual branch and is guarded by a targeted regression test with exact scalar equality.
Overview
Fixes a SIGSEGV in the FP32 L2 SSE distance kernel when vectors are not 16-byte aligned—a common case for allocator placement and dimensions where
dim % 4 != 0.In
FP32_L2SqrSIMD16_SSE, theresidual % 4 == 3prologue no longer uses_mm_loadr_ps(movaps). It now uses_mm_loadu_psplus an in-register shuffle and zeroing so only the three valid lanes contribute to the partial sum, matching the prior masking behavior without requiring alignment.diffis computed inside each residual branch instead of after the pointer bump.Adds
SpacesTest.FP32_L2Sqr_SSE_misaligned_residual3(dim 19, buffers at 16-aligned base + 8) to assert the SSE implementation matches the scalarFP32_L2Sqrbaseline on misaligned data.Reviewed by Cursor Bugbot for commit 9795480. Bugbot is set up for automated code reviews on this repo. Configure here.