Fix #13839: GHCi warnings do not respect the default module header
[ghc.git] / testsuite / driver / cpu_features.py
1 import os
2 from testglobals import config
3 import subprocess
4 import re
5
6 # Feature names generally follow the naming used by Linux's /proc/cpuinfo.
7 SUPPORTED_CPU_FEATURES = {
8 # These aren't comprehensive; they are only CPU features that we care about
9
10 # x86:
11 'sse', 'sse2', 'sse3', 'ssse3', 'sse4_1', 'sse4_2',
12 'avx1', 'avx2',
13 'popcnt', 'bmi1', 'bmi2'
14 }
15
16 cpu_feature_cache = None
17
18 def get_cpu_features():
19 if config.os in ['mingw32', 'linux'] and os.path.exists('/proc/cpuinfo'):
20 f = open('/proc/cpuinfo').read()
21 flags = re.search(r'flags\s*:\s*.*$', f, re.M)
22 if flags is None:
23 print('get_cpu_features: failed to find cpu features')
24 return {}
25 flags = set(flags.group(0).split())
26 if 'pni' in flags:
27 flags.add('sse3')
28 flags.remove('pni')
29 return flags
30
31 elif config.os == 'darwin':
32 out = subprocess.check_output(['sysctl', 'hw']).decode('UTF-8')
33 features = set()
34 def check_feature(darwin_name, our_name=None):
35 if re.search(r'hw\.optional.%s:\s*1' % darwin_name, out) is not None:
36 features.add(darwin_name if our_name is None else our_name)
37
38 for feature in SUPPORTED_CPU_FEATURES:
39 check_feature(feature)
40
41 # A few annoying cases
42 check_feature('avx1_0', 'avx1')
43 check_feature('avx2_0', 'avx2')
44 return features
45
46 else:
47 # TODO: Add {Open,Free}BSD support
48 print('get_cpu_features: Lacking support for your platform')
49
50 return {}
51
52 def have_cpu_feature(feature):
53 """
54 A testsuite predicate for testing the availability of CPU features.
55 """
56 assert feature in SUPPORTED_CPU_FEATURES
57 global cpu_feature_cache
58 if cpu_feature_cache is None:
59 cpu_feature_cache = get_cpu_features()
60 print('Found CPU features:', ' '.join(cpu_feature_cache))
61
62 return feature in cpu_feature_cache
63
64
65 if __name__ == '__main__':
66 import sys
67 config.os = sys.argv[1]
68 print(get_cpu_features())
69