From d485c22b0870f75d429eea6a546d8479e4946b2d Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Wed, 27 Feb 2013 16:32:00 -0600 Subject: [PATCH] Add llvm.workaround package --- llvm/workaround/__init__.py | 0 llvm/workaround/avx_support.py | 60 ++++++++++++++++++++++++++++++++++ setup.py | 4 ++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 llvm/workaround/__init__.py create mode 100644 llvm/workaround/avx_support.py diff --git a/llvm/workaround/__init__.py b/llvm/workaround/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/llvm/workaround/avx_support.py b/llvm/workaround/avx_support.py new file mode 100644 index 0000000..81cb936 --- /dev/null +++ b/llvm/workaround/avx_support.py @@ -0,0 +1,60 @@ +""" +Auto-detect avx and xsave + +According to Intel manual [0], both AVX and XSAVE features must be present to +use AVX instructions. + +References: + +[0] Intel Architecture Instruction Set Extensions Programming Reference + +http://software.intel.com/sites/default/files/m/a/b/3/4/d/41604-319433-012a.pdf + +""" + +import sys, os, subprocess + +def detect_avx_support(option='detect'): + '''Detect AVX support''' + option = os.environ.get('LLVMPY_AVX_SUPPORT', option).lower() + if option in ('disable', '0', 'false'): + return False + elif option in ('enable', '1', 'true'): + return True + else: # do detection + plat = sys.platform + if plat.startswith('darwin'): + return detect_osx_like() + elif plat.startswith('win32'): + return False # don't know how to detect in windows + else: + return detect_unix_like() + +def detect_unix_like(): + try: + info = open('/proc/cpuinfo') + except IOError: + return False + + for line in info: + if line.lstrip().startswith('flags'): + features = line.split() + if 'avx' in features and 'xsave' in features: + # enable AVX if flags contain AVX + return True + return False + +def detect_osx_like(): + try: + info = subprocess.Popen(['sysctl', '-n', 'machdep.cpu.features'], + stdout=subprocess.PIPE) + except OSError: + return False + + features = info.stdout.read() + features = features.split() + return 'AVX1.0' in features and 'OSXSAVE' in features and 'XSAVE' in features + + +if __name__ == '__main__': + print("AVX support: %s" % detect_avx_support()) diff --git a/setup.py b/setup.py index 6fa6653..ea7edb6 100644 --- a/setup.py +++ b/setup.py @@ -189,7 +189,9 @@ setup( maintainer = 'Continuum Analytics, Inc.', maintainer_email = 'llvmpy@continuum.io', url = 'http://www.llvmpy.org/', - packages = ['llvm', 'llvm_cbuilder', 'llpython', + packages = ['llvm', 'llvm.workaround', + 'llvm_cbuilder', + 'llpython', 'llvmpy.api', 'llvmpy.api.llvm'], py_modules = ['llvmpy', 'llvmpy._capsule',