77import platform
88import sys
99import archspec .cpu
10+ from archspec .cpu .detect import compatible_microarchitectures , raw_info_dictionary
1011
1112VENDOR_MAP = {
1213 'GenuineIntel' : 'intel' ,
@@ -33,7 +34,26 @@ def det_host_triple():
3334 Determine host triple: (<cpu_family>, <cpu_vendor>, <cpu_name>).
3435 <cpu_vendor> may be None if there's no match in VENDOR_MAP.
3536 """
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+
3757 host_vendor = VENDOR_MAP .get (host_cpu .vendor )
3858 host_cpu_family = host_cpu .family .name
3959 host_cpu_name = host_cpu .name
0 commit comments