66import os
77import platform
88import sys
9+ import subprocess
910
1011import numpy
1112from Cython .Build import cythonize
@@ -45,6 +46,32 @@ def detect_architecture():
4546 return "x86_64"
4647 return machine
4748
49+
50+ def build_supports_avx512 ():
51+ """Check at build time whether the local build machine supports AVX512.
52+
53+ We prefer a lightweight check from /proc/cpuinfo on Linux and use sysctl on
54+ macOS if available. This prevents us from adding global AVX512 compile
55+ flags when the build machine doesn't support them (which can cause
56+ illegal instruction errors if a binary built with those flags runs on a
57+ machine without AVX512).
58+ """
59+ if is_linux ():
60+ try :
61+ with open ('/proc/cpuinfo' , 'r' , encoding = 'utf8' ) as cpuinfo_file :
62+ contents = cpuinfo_file .read ()
63+ return 'avx512f' in contents and 'avx512bw' in contents
64+ except FileNotFoundError :
65+ return False
66+ if is_mac ():
67+ try :
68+ # Attempt to use sysctl to query CPU features
69+ out = subprocess .check_output (['sysctl' , '-n' , 'machdep.cpu.leaf7_features' ], text = True )
70+ return 'AVX512F' in out and 'AVX512BW' in out
71+ except (subprocess .CalledProcessError , FileNotFoundError ):
72+ return False
73+ return False
74+
4875# Compiler flags with SIMD support
4976arch = detect_architecture ()
5077CPP_FLAGS = ["-O3" , "-std=c++17" ]
@@ -61,8 +88,11 @@ def detect_architecture():
6188if arch == "x86_64" :
6289 # Add SIMD support
6390 CPP_FLAGS .extend (["-msse4.2" , "-mavx2" ])
64- # Add AVX512 support
65- CPP_FLAGS .extend (["-mavx512f" , "-mavx512cd" , "-mavx512bw" , "-mavx512dq" , "-mavx512vl" ])
91+ # Add AVX512 support only if the build host supports it. This keeps
92+ # compilation portable and prevents the compiler from embedding AVX512 in
93+ # scalar paths when the instruction set isn't available on the test runner.
94+ if build_supports_avx512 ():
95+ CPP_FLAGS .extend (["-mavx512f" , "-mavx512cd" , "-mavx512bw" , "-mavx512dq" , "-mavx512vl" ])
6696elif arch == "arm" and not is_mac ():
6797 CPP_FLAGS .append ("-mfpu=neon" )
6898
0 commit comments