@@ -98,11 +98,22 @@ elif host_cpu == 'arm'
9898endif
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.
101109has_avx = false
102110has_avx2 = false
103111has_fma = false
104112has_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+
106117if 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