7
7
import platform
8
8
import sys
9
9
import archspec .cpu
10
+ from archspec .cpu .detect import compatible_microarchitectures , raw_info_dictionary
10
11
11
12
VENDOR_MAP = {
12
13
'GenuineIntel' : 'intel' ,
@@ -33,7 +34,26 @@ def det_host_triple():
33
34
Determine host triple: (<cpu_family>, <cpu_vendor>, <cpu_name>).
34
35
<cpu_vendor> may be None if there's no match in VENDOR_MAP.
35
36
"""
36
- host_cpu = archspec .cpu .host ()
37
+ # we can't directly use archspec.cpu.host(), because we may get back a virtual microarchitecture like x86_64_v3...
38
+ def sorting_fn (item ):
39
+ """Helper function to sort compatible microarchitectures."""
40
+ return len (item .ancestors ), len (item .features )
41
+
42
+ raw_cpu_info = raw_info_dictionary ()
43
+ compat_targets = compatible_microarchitectures (raw_cpu_info )
44
+
45
+ # filter out generic targets
46
+ non_generic_compat_targets = [t for t in compat_targets if t .vendor != "generic" ]
47
+
48
+ # Filter the candidates to be descendant of the best generic candidate
49
+ best_generic = max ([t for t in compat_targets if t .vendor == "generic" ], key = sorting_fn )
50
+ best_compat_targets = [t for t in non_generic_compat_targets if t > best_generic ]
51
+
52
+ if best_compat_targets :
53
+ host_cpu = max (best_compat_targets , key = sorting_fn )
54
+ else :
55
+ host_cpu = max (non_generic_compat_targets , key = sorting_fn )
56
+
37
57
host_vendor = VENDOR_MAP .get (host_cpu .vendor )
38
58
host_cpu_family = host_cpu .family .name
39
59
host_cpu_name = host_cpu .name
0 commit comments