Skip to content

Commit 461c12e

Browse files
committed
fix: avx fixes
1 parent f77c24a commit 461c12e

2 files changed

Lines changed: 74 additions & 21 deletions

File tree

‎include/sqlite-vec-cpp/quantization/lvq.hpp‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929

3030
#ifdef SQLITE_VEC_ENABLE_AVX
3131
#include <immintrin.h>
32+
namespace sqlite_vec_cpp::quantization::detail {
33+
/// FMA-safe fused multiply-add: uses hardware FMA when available, otherwise mul+add.
34+
inline __m256 lvq_fmadd_ps(__m256 a, __m256 b, __m256 c) {
35+
#ifdef SQLITE_VEC_ENABLE_FMA
36+
return _mm256_fmadd_ps(a, b, c);
37+
#else
38+
return _mm256_add_ps(_mm256_mul_ps(a, b), c);
39+
#endif
40+
}
41+
} // namespace sqlite_vec_cpp::quantization::detail
3242
#endif
3343

3444
namespace sqlite_vec_cpp::quantization {
@@ -252,7 +262,7 @@ struct LVQ8 {
252262
__m256 q_lo = _mm256_loadu_ps(&query[i]);
253263
__m256 qn_lo = _mm256_mul_ps(_mm256_sub_ps(q_lo, voffset), vinv_scale);
254264
__m256 d_lo = _mm256_sub_ps(qn_lo, cf_lo);
255-
sum0 = _mm256_fmadd_ps(d_lo, d_lo, sum0);
265+
sum0 = detail::lvq_fmadd_ps(d_lo, d_lo, sum0);
256266

257267
// Process next 8
258268
__m128i codes8_hi =
@@ -263,7 +273,7 @@ struct LVQ8 {
263273
__m256 q_hi = _mm256_loadu_ps(&query[i + 8]);
264274
__m256 qn_hi = _mm256_mul_ps(_mm256_sub_ps(q_hi, voffset), vinv_scale);
265275
__m256 d_hi = _mm256_sub_ps(qn_hi, cf_hi);
266-
sum1 = _mm256_fmadd_ps(d_hi, d_hi, sum1);
276+
sum1 = detail::lvq_fmadd_ps(d_hi, d_hi, sum1);
267277

268278
i += 16;
269279
}
@@ -531,7 +541,7 @@ struct LVQ4 {
531541
__m256 q_a = _mm256_loadu_ps(query + i);
532542
__m256 qn_a = _mm256_mul_ps(_mm256_sub_ps(q_a, voffset), vinv_scale);
533543
__m256 d_a = _mm256_sub_ps(qn_a, cf_a);
534-
sum0 = _mm256_fmadd_ps(d_a, d_a, sum0);
544+
sum0 = detail::lvq_fmadd_ps(d_a, d_a, sum0);
535545

536546
// Widen next 8 bytes
537547
__m128i upper_0_15 = _mm_srli_si128(unpacked_0_15, 8);
@@ -540,23 +550,23 @@ struct LVQ4 {
540550
__m256 q_b = _mm256_loadu_ps(query + i + 8);
541551
__m256 qn_b = _mm256_mul_ps(_mm256_sub_ps(q_b, voffset), vinv_scale);
542552
__m256 d_b = _mm256_sub_ps(qn_b, cf_b);
543-
sum1 = _mm256_fmadd_ps(d_b, d_b, sum1);
553+
sum1 = detail::lvq_fmadd_ps(d_b, d_b, sum1);
544554

545555
// === Process dims i+16..i+31 ===
546556
__m256i codes32_c = _mm256_cvtepu8_epi32(unpacked_16_31);
547557
__m256 cf_c = _mm256_cvtepi32_ps(codes32_c);
548558
__m256 q_c = _mm256_loadu_ps(query + i + 16);
549559
__m256 qn_c = _mm256_mul_ps(_mm256_sub_ps(q_c, voffset), vinv_scale);
550560
__m256 d_c = _mm256_sub_ps(qn_c, cf_c);
551-
sum0 = _mm256_fmadd_ps(d_c, d_c, sum0);
561+
sum0 = detail::lvq_fmadd_ps(d_c, d_c, sum0);
552562

553563
__m128i upper_16_31 = _mm_srli_si128(unpacked_16_31, 8);
554564
__m256i codes32_d = _mm256_cvtepu8_epi32(upper_16_31);
555565
__m256 cf_d = _mm256_cvtepi32_ps(codes32_d);
556566
__m256 q_d = _mm256_loadu_ps(query + i + 24);
557567
__m256 qn_d = _mm256_mul_ps(_mm256_sub_ps(q_d, voffset), vinv_scale);
558568
__m256 d_d = _mm256_sub_ps(qn_d, cf_d);
559-
sum1 = _mm256_fmadd_ps(d_d, d_d, sum1);
569+
sum1 = detail::lvq_fmadd_ps(d_d, d_d, sum1);
560570

561571
i += 32;
562572
}

‎meson.build‎

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,22 @@ elif host_cpu == 'arm'
9898
endif
9999

100100
# x86/x86_64 AVX Detection
101+
#
102+
# IMPORTANT: We use cpp.run() for native builds to verify the CPU actually
103+
# supports the instruction set at runtime. cpp.compiles() only checks whether
104+
# the compiler can generate the instructions — any modern GCC/Clang will pass
105+
# for AVX/AVX2/FMA/AVX-512 regardless of the host CPU, leading to SIGILL at
106+
# runtime on VMs or older CPUs that lack those features.
107+
# For cross-builds (where we can't run host binaries), we fall back to
108+
# compile-tests and trust the user's configuration.
101109
has_avx = false
102110
has_avx2 = false
103111
has_fma = false
104112
has_avx512 = false
105113

114+
# Helper: check SIMD support via runtime test (native) or compile test (cross).
115+
can_run = meson.can_run_host_binaries()
116+
106117
if host_cpu in ['x86', 'x86_64']
107118
if get_option('enable_simd_avx')
108119
# Test AVX
@@ -114,12 +125,20 @@ if host_cpu in ['x86', 'x86_64']
114125
return 0;
115126
}
116127
'''
117-
if cpp.compiles(avx_test, args: ['-mavx'], name: 'AVX')
128+
avx_ok = false
129+
if can_run
130+
avx_run = cpp.run(avx_test, args: ['-mavx'], name: 'AVX (runtime)')
131+
avx_ok = avx_run.compiled() and avx_run.returncode() == 0
132+
else
133+
avx_ok = cpp.compiles(avx_test, args: ['-mavx'], name: 'AVX (compile-only)')
134+
endif
135+
136+
if avx_ok
118137
has_avx = true
119138
simd_defines += '-DSQLITE_VEC_ENABLE_AVX'
120139
simd_args += '-mavx'
121-
message('AVX: enabled (non-portable opt-in)')
122-
140+
message('AVX: enabled')
141+
123142
# Test AVX2 (needed for integer operations)
124143
avx2_test = '''
125144
#include <immintrin.h>
@@ -129,15 +148,23 @@ if host_cpu in ['x86', 'x86_64']
129148
return 0;
130149
}
131150
'''
132-
if cpp.compiles(avx2_test, args: ['-mavx2'], name: 'AVX2')
151+
avx2_ok = false
152+
if can_run
153+
avx2_run = cpp.run(avx2_test, args: ['-mavx2'], name: 'AVX2 (runtime)')
154+
avx2_ok = avx2_run.compiled() and avx2_run.returncode() == 0
155+
else
156+
avx2_ok = cpp.compiles(avx2_test, args: ['-mavx2'], name: 'AVX2 (compile-only)')
157+
endif
158+
159+
if avx2_ok
133160
has_avx2 = true
134161
simd_defines += '-DSQLITE_VEC_ENABLE_AVX2'
135162
simd_args += '-mavx2'
136-
message('AVX2: enabled (non-portable opt-in)')
163+
message('AVX2: enabled')
137164
else
138-
message('AVX2: not available')
165+
message('AVX2: not available on this CPU')
139166
endif
140-
167+
141168
# Test FMA (fused multiply-add, usually with AVX2)
142169
fma_test = '''
143170
#include <immintrin.h>
@@ -149,15 +176,23 @@ if host_cpu in ['x86', 'x86_64']
149176
return 0;
150177
}
151178
'''
152-
if cpp.compiles(fma_test, args: ['-mfma'], name: 'FMA')
179+
fma_ok = false
180+
if can_run
181+
fma_run = cpp.run(fma_test, args: ['-mfma'], name: 'FMA (runtime)')
182+
fma_ok = fma_run.compiled() and fma_run.returncode() == 0
183+
else
184+
fma_ok = cpp.compiles(fma_test, args: ['-mfma'], name: 'FMA (compile-only)')
185+
endif
186+
187+
if fma_ok
153188
has_fma = true
154189
simd_defines += '-DSQLITE_VEC_ENABLE_FMA'
155190
simd_args += '-mfma'
156-
message('FMA: enabled (non-portable opt-in)')
191+
message('FMA: enabled')
157192
else
158-
message('FMA: not available')
193+
message('FMA: not available on this CPU')
159194
endif
160-
195+
161196
# Test AVX-512 (optional, for future use)
162197
avx512_test = '''
163198
#include <immintrin.h>
@@ -167,16 +202,24 @@ if host_cpu in ['x86', 'x86_64']
167202
return 0;
168203
}
169204
'''
170-
if cpp.compiles(avx512_test, args: ['-mavx512f'], name: 'AVX-512')
205+
avx512_ok = false
206+
if can_run
207+
avx512_run = cpp.run(avx512_test, args: ['-mavx512f'], name: 'AVX-512 (runtime)')
208+
avx512_ok = avx512_run.compiled() and avx512_run.returncode() == 0
209+
else
210+
avx512_ok = cpp.compiles(avx512_test, args: ['-mavx512f'], name: 'AVX-512 (compile-only)')
211+
endif
212+
213+
if avx512_ok
171214
has_avx512 = true
172215
simd_defines += '-DSQLITE_VEC_ENABLE_AVX512'
173216
simd_args += '-mavx512f'
174-
message('AVX-512: enabled (non-portable opt-in)')
217+
message('AVX-512: enabled')
175218
else
176-
message('AVX-512: not available')
219+
message('AVX-512: not available on this CPU')
177220
endif
178221
else
179-
message('AVX: requested but not available for this toolchain')
222+
message('AVX: requested but not available for this toolchain/CPU')
180223
endif
181224
else
182225
message('AVX: disabled by default (enable_simd_avx=false)')

0 commit comments

Comments
 (0)