Skip to content

[MOD-16730] Fix SIGSEGV in FP32/L2 SSE kernel: unaligned load in residual-3 path#990

Merged
dor-forer merged 3 commits into
mainfrom
dor-forer-MOD-16730-fix-sse-fp32-l2-loadr-alignment
Jul 8, 2026
Merged

[MOD-16730] Fix SIGSEGV in FP32/L2 SSE kernel: unaligned load in residual-3 path#990
dor-forer merged 3 commits into
mainfrom
dor-forer-MOD-16730-fix-sse-fp32-l2-loadr-alignment

Conversation

@dor-forer

@dor-forer dor-forer commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

Fix a SIGSEGV in the FP32/L2 SSE distance kernel. 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 never guaranteed that alignment: VecSimAllocator::allocate() returns malloc + 8 (allocation header), and the dispatcher only sets an alignment hint when dim % 4 == 0 — while this path requires dim % 4 == 3. On machines whose dispatcher selects the SSE tier (no AVX), any FP32/L2 query with dim % 16 ∈ {3, 7, 11, 15} crashed inside VecSimIndex_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_ss masking trick and all results are unchanged; at machine level this swaps movaps for movups with the same shufps the compiler already emitted — zero added instructions (verified by disassembly). _mm_blend_ps was rejected (SSE4.1; this TU compiles with -msse for pre-SSE4.1 hardware). Setting the alignment hint was rejected (it aligns allocation bases only; stride dim*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

  1. MOD-16730
  2. Crash demonstration / CI evidence: [MOD-16730] Repro: SIGSEGV in FP32/L2 SSE kernel (_mm_loadr_ps on unaligned allocator-placed vectors) #987; found while reviewing [MOD-16688] x86 distance kernels: multi-accumulator unrolling + SIMD for smaller dimensions #984

Main objects this PR modified

  1. src/VecSim/spaces/L2/L2_SSE_FP32.hFP32_L2SqrSIMD16_SSE residual % 4 == 3 load path
  2. tests/unit/test_spaces.cpp — new regression test SpacesTest.FP32_L2Sqr_SSE_misaligned_residual3

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

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, the residual % 4 == 3 prologue no longer uses _mm_loadr_ps (movaps). It now uses _mm_loadu_ps plus 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. diff is 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 scalar FP32_L2Sqr baseline on misaligned data.

Reviewed by Cursor Bugbot for commit 9795480. Bugbot is set up for automated code reviews on this repo. Configure here.

…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

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.10%. Comparing base (45daaf3) to head (9795480).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dor-forer dor-forer requested a review from GuyAv46 July 8, 2026 09:37
Comment thread src/VecSim/spaces/L2/L2_SSE_FP32.h Outdated
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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider shuffling v1 and v2 together, to get the _mm_move_ss effect in 1 operation instead of 3

dor-forer added 2 commits July 8, 2026 14:18
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.
@dor-forer dor-forer requested a review from GuyAv46 July 8, 2026 12:25
@dor-forer dor-forer enabled auto-merge July 8, 2026 12:25
@dor-forer dor-forer added this pull request to the merge queue Jul 8, 2026
Merged via the queue into main with commit 6ca08ae Jul 8, 2026
16 checks passed
@dor-forer dor-forer deleted the dor-forer-MOD-16730-fix-sse-fp32-l2-loadr-alignment branch July 8, 2026 13:32
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