20 #include "llvm/Config/llvm-config.h" 38 #if defined(__APPLE__) && (!defined(__x86_64__)) 39 #include <mach/host_info.h> 40 #include <mach/mach.h> 41 #include <mach/mach_host.h> 42 #include <mach/machine.h> 45 #define DEBUG_TYPE "host-detection" 55 static std::unique_ptr<llvm::MemoryBuffer>
59 if (std::error_code EC = Text.
getError()) {
61 <<
"/proc/cpuinfo: " << EC.message() <<
"\n";
64 return std::move(*Text);
71 const char *
generic =
"generic";
85 while (CIP < CPUInfoEnd && CPUStart == 0) {
86 if (CIP < CPUInfoEnd && *CIP ==
'\n')
89 if (CIP < CPUInfoEnd && *CIP ==
'c') {
91 if (CIP < CPUInfoEnd && *CIP ==
'p') {
93 if (CIP < CPUInfoEnd && *CIP ==
'u') {
95 while (CIP < CPUInfoEnd && (*CIP ==
' ' || *CIP ==
'\t'))
98 if (CIP < CPUInfoEnd && *CIP ==
':') {
100 while (CIP < CPUInfoEnd && (*CIP ==
' ' || *CIP ==
'\t'))
103 if (CIP < CPUInfoEnd) {
105 while (CIP < CPUInfoEnd && (*CIP !=
' ' && *CIP !=
'\t' &&
106 *CIP !=
',' && *CIP !=
'\n'))
108 CPULen = CIP - CPUStart;
116 while (CIP < CPUInfoEnd && *CIP !=
'\n')
124 .Case(
"604e",
"604e")
126 .
Case(
"7400",
"7400")
127 .
Case(
"7410",
"7400")
128 .
Case(
"7447",
"7400")
129 .
Case(
"7455",
"7450")
131 .
Case(
"POWER4",
"970")
132 .
Case(
"PPC970FX",
"970")
133 .
Case(
"PPC970MP",
"970")
135 .
Case(
"POWER5",
"g5")
137 .
Case(
"POWER6",
"pwr6")
138 .
Case(
"POWER7",
"pwr7")
139 .
Case(
"POWER8",
"pwr8")
140 .
Case(
"POWER8E",
"pwr8")
141 .
Case(
"POWER8NVL",
"pwr8")
142 .
Case(
"POWER9",
"pwr9")
156 ProcCpuinfoContent.
split(Lines,
"\n");
161 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I) {
163 Implementer = Lines[
I].substr(15).ltrim(
"\t :");
165 Hardware = Lines[
I].substr(8).ltrim(
"\t :");
168 if (Implementer ==
"0x41") {
176 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
182 .Case(
"0x926",
"arm926ej-s")
183 .
Case(
"0xb02",
"mpcore")
184 .
Case(
"0xb36",
"arm1136j-s")
185 .
Case(
"0xb56",
"arm1156t2-s")
186 .
Case(
"0xb76",
"arm1176jz-s")
187 .
Case(
"0xc08",
"cortex-a8")
188 .
Case(
"0xc09",
"cortex-a9")
189 .
Case(
"0xc0f",
"cortex-a15")
190 .
Case(
"0xc20",
"cortex-m0")
191 .
Case(
"0xc23",
"cortex-m3")
192 .
Case(
"0xc24",
"cortex-m4")
193 .
Case(
"0xd04",
"cortex-a35")
194 .
Case(
"0xd03",
"cortex-a53")
195 .
Case(
"0xd07",
"cortex-a57")
196 .
Case(
"0xd08",
"cortex-a72")
197 .
Case(
"0xd09",
"cortex-a73")
198 .
Case(
"0xd0a",
"cortex-a75")
199 .
Case(
"0xd0b",
"cortex-a76")
203 if (Implementer ==
"0x42" || Implementer ==
"0x43") {
204 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I) {
207 .Case(
"0x516",
"thunderx2t99")
208 .
Case(
"0x0516",
"thunderx2t99")
209 .
Case(
"0xaf",
"thunderx2t99")
210 .
Case(
"0x0af",
"thunderx2t99")
211 .
Case(
"0xa1",
"thunderxt88")
212 .
Case(
"0x0a1",
"thunderxt88")
218 if (Implementer ==
"0x48")
220 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
226 .Case(
"0xd01",
"tsv110")
229 if (Implementer ==
"0x51")
231 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
237 .Case(
"0x06f",
"krait")
238 .
Case(
"0x201",
"kryo")
239 .
Case(
"0x205",
"kryo")
240 .
Case(
"0x211",
"kryo")
241 .
Case(
"0x800",
"cortex-a73")
242 .
Case(
"0x801",
"cortex-a73")
243 .
Case(
"0x802",
"cortex-a73")
244 .
Case(
"0x803",
"cortex-a73")
245 .
Case(
"0x804",
"cortex-a73")
246 .
Case(
"0x805",
"cortex-a73")
247 .
Case(
"0xc00",
"falkor")
248 .
Case(
"0xc01",
"saphira")
251 if (Implementer ==
"0x53") {
254 unsigned Variant = 0, Part = 0;
259 if (
I.consume_front(
"CPU variant"))
260 I.ltrim(
"\t :").getAsInteger(0, Variant);
265 if (
I.consume_front(
"CPU part"))
266 I.ltrim(
"\t :").getAsInteger(0, Part);
268 unsigned Exynos = (Variant << 12) | Part;
289 ProcCpuinfoContent.
split(Lines,
"\n");
293 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
295 size_t Pos = Lines[
I].find(
":");
297 Lines[
I].drop_front(Pos + 1).split(CPUFeatures,
' ');
305 bool HaveVectorSupport =
false;
306 for (
unsigned I = 0,
E = CPUFeatures.
size();
I !=
E; ++
I) {
307 if (CPUFeatures[
I] ==
"vx")
308 HaveVectorSupport =
true;
312 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I) {
314 size_t Pos = Lines[
I].find(
"machine = ");
316 Pos +=
sizeof(
"machine = ") - 1;
318 if (!Lines[
I].drop_front(Pos).getAsInteger(10, Id)) {
319 if (Id >= 8561 && HaveVectorSupport)
321 if (Id >= 3906 && HaveVectorSupport)
323 if (Id >= 2964 && HaveVectorSupport)
339 #if !defined(__linux__) || !defined(__x86_64__) 342 uint8_t v3_insns[40] __attribute__ ((
aligned (8))) =
344 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
346 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
348 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
350 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
352 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
354 uint8_t v2_insns[40] __attribute__ ((
aligned (8))) =
356 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
358 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
360 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
362 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
364 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
366 struct bpf_prog_load_attr {
379 attr.insns = (uint64_t)v3_insns;
380 attr.license = (uint64_t)
"DUMMY";
382 int fd = syscall(321 , 5 , &attr,
390 memset(&attr, 0,
sizeof(attr));
393 attr.insns = (uint64_t)v2_insns;
394 attr.license = (uint64_t)
"DUMMY";
395 fd = syscall(321 , 5 , &attr,
sizeof(attr));
404 #if defined(__i386__) || defined(_M_IX86) || \ 405 defined(__x86_64__) || defined(_M_X64) 407 enum VendorSignatures {
408 SIG_INTEL = 0x756e6547 ,
419 static bool isCpuIdSupported() {
420 #if defined(__GNUC__) || defined(__clang__) 421 #if defined(__i386__) 422 int __cpuid_supported;
425 " movl %%eax,%%ecx\n" 426 " xorl $0x00200000,%%eax\n" 432 " cmpl %%eax,%%ecx\n" 436 :
"=r"(__cpuid_supported)
439 if (!__cpuid_supported)
449 static bool getX86CpuIDAndInfo(
unsigned value,
unsigned *rEAX,
unsigned *rEBX,
450 unsigned *rECX,
unsigned *rEDX) {
451 #if defined(__GNUC__) || defined(__clang__) 452 #if defined(__x86_64__) 455 __asm__(
"movq\t%%rbx, %%rsi\n\t" 457 "xchgq\t%%rbx, %%rsi\n\t" 458 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
461 #elif defined(__i386__) 462 __asm__(
"movl\t%%ebx, %%esi\n\t" 464 "xchgl\t%%ebx, %%esi\n\t" 465 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
471 #elif defined(_MSC_VER) 474 __cpuid(registers, value);
475 *rEAX = registers[0];
476 *rEBX = registers[1];
477 *rECX = registers[2];
478 *rEDX = registers[3];
488 static bool getX86CpuIDAndInfoEx(
unsigned value,
unsigned subleaf,
489 unsigned *rEAX,
unsigned *rEBX,
unsigned *rECX,
491 #if defined(__GNUC__) || defined(__clang__) 492 #if defined(__x86_64__) 495 __asm__(
"movq\t%%rbx, %%rsi\n\t" 497 "xchgq\t%%rbx, %%rsi\n\t" 498 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
499 :
"a"(value),
"c"(subleaf));
501 #elif defined(__i386__) 502 __asm__(
"movl\t%%ebx, %%esi\n\t" 504 "xchgl\t%%ebx, %%esi\n\t" 505 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
506 :
"a"(value),
"c"(subleaf));
511 #elif defined(_MSC_VER) 513 __cpuidex(registers, value, subleaf);
514 *rEAX = registers[0];
515 *rEBX = registers[1];
516 *rECX = registers[2];
517 *rEDX = registers[3];
525 static bool getX86XCR0(
unsigned *rEAX,
unsigned *rEDX) {
526 #if defined(__GNUC__) || defined(__clang__) 530 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a"(*rEAX),
"=d"(*rEDX) :
"c"(0));
532 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) 533 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
535 *rEDX = Result >> 32;
542 static void detectX86FamilyModel(
unsigned EAX,
unsigned *Family,
544 *Family = (EAX >> 8) & 0xf;
545 *Model = (EAX >> 4) & 0xf;
546 if (*Family == 6 || *Family == 0xf) {
549 *Family += (EAX >> 20) & 0xff;
551 *Model += ((EAX >> 16) & 0xf) << 4;
556 getIntelProcessorTypeAndSubtype(
unsigned Family,
unsigned Model,
557 unsigned Brand_id,
unsigned Features,
558 unsigned Features2,
unsigned Features3,
559 unsigned *
Type,
unsigned *Subtype) {
564 *Type = X86::INTEL_i386;
567 *Type = X86::INTEL_i486;
570 if (Features & (1 << X86::FEATURE_MMX)) {
571 *Type = X86::INTEL_PENTIUM_MMX;
574 *Type = X86::INTEL_PENTIUM;
579 *Type = X86::INTEL_PENTIUM_PRO;
586 *Type = X86::INTEL_PENTIUM_II;
594 *Type = X86::INTEL_PENTIUM_III;
601 *Type = X86::INTEL_PENTIUM_M;
605 *Type = X86::INTEL_CORE_DUO;
614 *Type = X86::INTEL_CORE2;
615 *Subtype = X86::INTEL_CORE2_65;
623 *Type = X86::INTEL_CORE2;
624 *Subtype = X86::INTEL_CORE2_45;
632 *Type = X86::INTEL_COREI7;
633 *Subtype = X86::INTEL_COREI7_NEHALEM;
639 *Type = X86::INTEL_COREI7;
640 *Subtype = X86::INTEL_COREI7_WESTMERE;
645 *Type = X86::INTEL_COREI7;
646 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
650 *Type = X86::INTEL_COREI7;
651 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
659 *Type = X86::INTEL_COREI7;
660 *Subtype = X86::INTEL_COREI7_HASWELL;
668 *Type = X86::INTEL_COREI7;
669 *Subtype = X86::INTEL_COREI7_BROADWELL;
677 *Type = X86::INTEL_COREI7;
678 *Subtype = X86::INTEL_COREI7_SKYLAKE;
683 *Type = X86::INTEL_COREI7;
684 if (Features2 & (1 << (X86::FEATURE_AVX512BF16 - 32)))
685 *Subtype = X86::INTEL_COREI7_COOPERLAKE;
686 else if (Features2 & (1 << (X86::FEATURE_AVX512VNNI - 32)))
687 *Subtype = X86::INTEL_COREI7_CASCADELAKE;
689 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
694 *Type = X86::INTEL_COREI7;
695 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
701 *Type = X86::INTEL_COREI7;
702 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
708 *Type = X86::INTEL_COREI7;
709 *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER;
717 *Type = X86::INTEL_BONNELL;
727 *Type = X86::INTEL_SILVERMONT;
732 *Type = X86::INTEL_GOLDMONT;
735 *Type = X86::INTEL_GOLDMONT_PLUS;
738 *Type = X86::INTEL_TREMONT;
742 *Type = X86::INTEL_KNL;
746 *Type = X86::INTEL_KNM;
751 if (Features3 & (1 << (X86::FEATURE_AVX512VP2INTERSECT - 64))) {
752 *Type = X86::INTEL_COREI7;
753 *Subtype = X86::INTEL_COREI7_TIGERLAKE;
757 if (Features & (1 << X86::FEATURE_AVX512VBMI2)) {
758 *Type = X86::INTEL_COREI7;
759 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
763 if (Features & (1 << X86::FEATURE_AVX512VBMI)) {
764 *Type = X86::INTEL_COREI7;
765 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
769 if (Features2 & (1 << (X86::FEATURE_AVX512BF16 - 32))) {
770 *Type = X86::INTEL_COREI7;
771 *Subtype = X86::INTEL_COREI7_COOPERLAKE;
775 if (Features2 & (1 << (X86::FEATURE_AVX512VNNI - 32))) {
776 *Type = X86::INTEL_COREI7;
777 *Subtype = X86::INTEL_COREI7_CASCADELAKE;
781 if (Features & (1 << X86::FEATURE_AVX512VL)) {
782 *Type = X86::INTEL_COREI7;
783 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
787 if (Features & (1 << X86::FEATURE_AVX512ER)) {
788 *Type = X86::INTEL_KNL;
792 if (Features3 & (1 << (X86::FEATURE_CLFLUSHOPT - 64))) {
793 if (Features3 & (1 << (X86::FEATURE_SHA - 64))) {
794 *Type = X86::INTEL_GOLDMONT;
796 *Type = X86::INTEL_COREI7;
797 *Subtype = X86::INTEL_COREI7_SKYLAKE;
801 if (Features3 & (1 << (X86::FEATURE_ADX - 64))) {
802 *Type = X86::INTEL_COREI7;
803 *Subtype = X86::INTEL_COREI7_BROADWELL;
806 if (Features & (1 << X86::FEATURE_AVX2)) {
807 *Type = X86::INTEL_COREI7;
808 *Subtype = X86::INTEL_COREI7_HASWELL;
811 if (Features & (1 << X86::FEATURE_AVX)) {
812 *Type = X86::INTEL_COREI7;
813 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
816 if (Features & (1 << X86::FEATURE_SSE4_2)) {
817 if (Features3 & (1 << (X86::FEATURE_MOVBE - 64))) {
818 *Type = X86::INTEL_SILVERMONT;
820 *Type = X86::INTEL_COREI7;
821 *Subtype = X86::INTEL_COREI7_NEHALEM;
825 if (Features & (1 << X86::FEATURE_SSE4_1)) {
826 *Type = X86::INTEL_CORE2;
827 *Subtype = X86::INTEL_CORE2_45;
830 if (Features & (1 << X86::FEATURE_SSSE3)) {
831 if (Features3 & (1 << (X86::FEATURE_MOVBE - 64))) {
832 *Type = X86::INTEL_BONNELL;
834 *Type = X86::INTEL_CORE2;
835 *Subtype = X86::INTEL_CORE2_65;
839 if (Features3 & (1 << (X86::FEATURE_EM64T - 64))) {
840 *Type = X86::INTEL_CORE2;
841 *Subtype = X86::INTEL_CORE2_65;
844 if (Features & (1 << X86::FEATURE_SSE3)) {
845 *Type = X86::INTEL_CORE_DUO;
848 if (Features & (1 << X86::FEATURE_SSE2)) {
849 *Type = X86::INTEL_PENTIUM_M;
852 if (Features & (1 << X86::FEATURE_SSE)) {
853 *Type = X86::INTEL_PENTIUM_III;
856 if (Features & (1 << X86::FEATURE_MMX)) {
857 *Type = X86::INTEL_PENTIUM_II;
860 *Type = X86::INTEL_PENTIUM_PRO;
865 if (Features3 & (1 << (X86::FEATURE_EM64T - 64))) {
866 *Type = X86::INTEL_NOCONA;
869 if (Features & (1 << X86::FEATURE_SSE3)) {
870 *Type = X86::INTEL_PRESCOTT;
873 *Type = X86::INTEL_PENTIUM_IV;
881 static void getAMDProcessorTypeAndSubtype(
unsigned Family,
unsigned Model,
882 unsigned Features,
unsigned *Type,
889 *Type = X86::AMD_i486;
892 *Type = X86::AMDPENTIUM;
896 *Subtype = X86::AMDPENTIUM_K6;
899 *Subtype = X86::AMDPENTIUM_K62;
903 *Subtype = X86::AMDPENTIUM_K63;
906 *Subtype = X86::AMDPENTIUM_GEODE;
911 if (Features & (1 << X86::FEATURE_SSE)) {
912 *Type = X86::AMD_ATHLON_XP;
915 *Type = X86::AMD_ATHLON;
918 if (Features & (1 << X86::FEATURE_SSE3)) {
919 *Type = X86::AMD_K8SSE3;
925 *Type = X86::AMDFAM10H;
928 *Subtype = X86::AMDFAM10H_BARCELONA;
931 *Subtype = X86::AMDFAM10H_SHANGHAI;
934 *Subtype = X86::AMDFAM10H_ISTANBUL;
939 *Type = X86::AMD_BTVER1;
942 *Type = X86::AMDFAM15H;
943 if (Model >= 0x60 && Model <= 0x7f) {
944 *Subtype = X86::AMDFAM15H_BDVER4;
947 if (Model >= 0x30 && Model <= 0x3f) {
948 *Subtype = X86::AMDFAM15H_BDVER3;
951 if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
952 *Subtype = X86::AMDFAM15H_BDVER2;
956 *Subtype = X86::AMDFAM15H_BDVER1;
961 *Type = X86::AMD_BTVER2;
964 *Type = X86::AMDFAM17H;
965 if ((Model >= 0x30 && Model <= 0x3f) || Model == 0x71) {
966 *Subtype = X86::AMDFAM17H_ZNVER2;
970 *Subtype = X86::AMDFAM17H_ZNVER1;
979 static void getAvailableFeatures(
unsigned ECX,
unsigned EDX,
unsigned MaxLeaf,
980 unsigned *FeaturesOut,
unsigned *Features2Out,
981 unsigned *Features3Out) {
982 unsigned Features = 0;
983 unsigned Features2 = 0;
984 unsigned Features3 = 0;
987 auto setFeature = [&](
unsigned F) {
989 Features |= 1U << (
F & 0x1f);
991 Features2 |= 1U << ((
F - 32) & 0x1f);
993 Features3 |= 1U << ((
F - 64) & 0x1f);
999 setFeature(X86::FEATURE_CMOV);
1000 if ((EDX >> 23) & 1)
1001 setFeature(X86::FEATURE_MMX);
1002 if ((EDX >> 25) & 1)
1003 setFeature(X86::FEATURE_SSE);
1004 if ((EDX >> 26) & 1)
1005 setFeature(X86::FEATURE_SSE2);
1008 setFeature(X86::FEATURE_SSE3);
1010 setFeature(X86::FEATURE_PCLMUL);
1012 setFeature(X86::FEATURE_SSSE3);
1013 if ((ECX >> 12) & 1)
1015 if ((ECX >> 19) & 1)
1016 setFeature(X86::FEATURE_SSE4_1);
1017 if ((ECX >> 20) & 1)
1018 setFeature(X86::FEATURE_SSE4_2);
1019 if ((ECX >> 23) & 1)
1020 setFeature(X86::FEATURE_POPCNT);
1021 if ((ECX >> 25) & 1)
1022 setFeature(X86::FEATURE_AES);
1024 if ((ECX >> 22) & 1)
1025 setFeature(X86::FEATURE_MOVBE);
1030 const unsigned AVXBits = (1 << 27) | (1 << 28);
1031 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
1032 ((EAX & 0x6) == 0x6);
1033 #if defined(__APPLE__) 1037 bool HasAVX512Save =
true;
1040 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
1044 setFeature(X86::FEATURE_AVX);
1047 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1049 if (HasLeaf7 && ((EBX >> 3) & 1))
1050 setFeature(X86::FEATURE_BMI);
1051 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
1052 setFeature(X86::FEATURE_AVX2);
1053 if (HasLeaf7 && ((EBX >> 8) & 1))
1054 setFeature(X86::FEATURE_BMI2);
1055 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
1056 setFeature(X86::FEATURE_AVX512F);
1057 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
1058 setFeature(X86::FEATURE_AVX512DQ);
1059 if (HasLeaf7 && ((EBX >> 19) & 1))
1060 setFeature(X86::FEATURE_ADX);
1061 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
1062 setFeature(X86::FEATURE_AVX512IFMA);
1063 if (HasLeaf7 && ((EBX >> 23) & 1))
1064 setFeature(X86::FEATURE_CLFLUSHOPT);
1065 if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
1066 setFeature(X86::FEATURE_AVX512PF);
1067 if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
1068 setFeature(X86::FEATURE_AVX512ER);
1069 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
1070 setFeature(X86::FEATURE_AVX512CD);
1071 if (HasLeaf7 && ((EBX >> 29) & 1))
1072 setFeature(X86::FEATURE_SHA);
1073 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
1074 setFeature(X86::FEATURE_AVX512BW);
1075 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
1076 setFeature(X86::FEATURE_AVX512VL);
1078 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
1079 setFeature(X86::FEATURE_AVX512VBMI);
1080 if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
1081 setFeature(X86::FEATURE_AVX512VBMI2);
1082 if (HasLeaf7 && ((ECX >> 8) & 1))
1083 setFeature(X86::FEATURE_GFNI);
1084 if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
1085 setFeature(X86::FEATURE_VPCLMULQDQ);
1086 if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
1087 setFeature(X86::FEATURE_AVX512VNNI);
1088 if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
1089 setFeature(X86::FEATURE_AVX512BITALG);
1090 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
1091 setFeature(X86::FEATURE_AVX512VPOPCNTDQ);
1093 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
1094 setFeature(X86::FEATURE_AVX5124VNNIW);
1095 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
1096 setFeature(X86::FEATURE_AVX5124FMAPS);
1097 if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)
1098 setFeature(X86::FEATURE_AVX512VP2INTERSECT);
1100 bool HasLeaf7Subleaf1 =
1101 MaxLeaf >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1102 if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
1103 setFeature(X86::FEATURE_AVX512BF16);
1105 unsigned MaxExtLevel;
1106 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1108 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1109 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1110 if (HasExtLeaf1 && ((ECX >> 6) & 1))
1111 setFeature(X86::FEATURE_SSE4_A);
1112 if (HasExtLeaf1 && ((ECX >> 11) & 1))
1113 setFeature(X86::FEATURE_XOP);
1114 if (HasExtLeaf1 && ((ECX >> 16) & 1))
1115 setFeature(X86::FEATURE_FMA4);
1117 if (HasExtLeaf1 && ((EDX >> 29) & 1))
1118 setFeature(X86::FEATURE_EM64T);
1120 *FeaturesOut = Features;
1121 *Features2Out = Features2;
1122 *Features3Out = Features3;
1126 unsigned EAX = 0,
EBX = 0, ECX = 0, EDX = 0;
1127 unsigned MaxLeaf, Vendor;
1129 #if defined(__GNUC__) || defined(__clang__) 1134 if(!isCpuIdSupported())
1137 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
1139 getX86CpuIDAndInfo(0x1, &EAX, &
EBX, &ECX, &EDX);
1141 unsigned Brand_id =
EBX & 0xff;
1142 unsigned Family = 0, Model = 0;
1143 unsigned Features = 0, Features2 = 0, Features3 = 0;
1144 detectX86FamilyModel(EAX, &Family, &Model);
1145 getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2, &Features3);
1148 unsigned Subtype = 0;
1150 if (Vendor == SIG_INTEL) {
1151 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features,
1152 Features2, Features3, &Type, &Subtype);
1153 }
else if (Vendor == SIG_AMD) {
1154 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
1158 #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \ 1159 if (Subtype == X86::ENUM) \ 1161 #include "llvm/Support/X86TargetParser.def" 1164 #define X86_CPU_TYPE(ARCHNAME, ENUM) \ 1165 if (Type == X86::ENUM) \ 1167 #include "llvm/Support/X86TargetParser.def" 1172 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) 1174 host_basic_info_data_t hostInfo;
1175 mach_msg_type_number_t infoCount;
1177 infoCount = HOST_BASIC_INFO_COUNT;
1178 mach_port_t hostPort = mach_host_self();
1179 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1181 mach_port_deallocate(mach_task_self(), hostPort);
1186 switch (hostInfo.cpu_subtype) {
1216 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__)) 1219 StringRef Content = P ? P->getBuffer() :
"";
1222 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) 1225 StringRef Content = P ? P->getBuffer() :
"";
1228 #elif defined(__linux__) && defined(__s390x__) 1231 StringRef Content = P ? P->getBuffer() :
"";
1234 #elif defined(__APPLE__) && defined(__aarch64__) 1238 #elif defined(__APPLE__) && defined(__arm__) 1240 host_basic_info_data_t hostInfo;
1241 mach_msg_type_number_t infoCount;
1243 infoCount = HOST_BASIC_INFO_COUNT;
1244 mach_port_t hostPort = mach_host_self();
1245 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1247 mach_port_deallocate(mach_task_self(), hostPort);
1250 assert(
false &&
"CPUType not equal to ARM should not be possible on ARM");
1253 switch (hostInfo.cpu_subtype) {
1265 #if defined(__linux__) && defined(__x86_64__) 1274 if (std::error_code EC = Text.
getError()) {
1276 <<
"/proc/cpuinfo: " << EC.message() <<
"\n";
1280 (*Text)->getBuffer().split(strs,
"\n", -1,
1282 int CurPhysicalId = -1;
1285 for (
auto &Line : strs) {
1287 if (!Line.startswith(
"physical id") && !Line.startswith(
"core id"))
1289 std::pair<StringRef, StringRef>
Data = Line.split(
':');
1290 auto Name = Data.first.trim();
1291 auto Val = Data.second.trim();
1292 if (
Name ==
"physical id") {
1293 assert(CurPhysicalId == -1 &&
1294 "Expected a core id before seeing another physical id");
1295 Val.getAsInteger(10, CurPhysicalId);
1297 if (
Name ==
"core id") {
1298 assert(CurCoreId == -1 &&
1299 "Expected a physical id before seeing another core id");
1300 Val.getAsInteger(10, CurCoreId);
1302 if (CurPhysicalId != -1 && CurCoreId != -1) {
1303 UniqueItems.
insert(std::make_pair(CurPhysicalId, CurCoreId));
1308 return UniqueItems.
size();
1310 #elif defined(__APPLE__) && defined(__x86_64__) 1311 #include <sys/param.h> 1312 #include <sys/sysctl.h> 1317 size_t len =
sizeof(
count);
1318 sysctlbyname(
"hw.physicalcpu", &count, &len, NULL, 0);
1322 nm[1] = HW_AVAILCPU;
1323 sysctl(nm, 2, &count, &len, NULL, 0);
1339 #if defined(__i386__) || defined(_M_IX86) || \ 1340 defined(__x86_64__) || defined(_M_X64) 1342 unsigned EAX = 0,
EBX = 0, ECX = 0, EDX = 0;
1349 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1353 getX86CpuIDAndInfo(1, &EAX, &
EBX, &ECX, &EDX);
1355 Features[
"cx8"] = (EDX >> 8) & 1;
1356 Features[
"cmov"] = (EDX >> 15) & 1;
1357 Features[
"mmx"] = (EDX >> 23) & 1;
1358 Features[
"fxsr"] = (EDX >> 24) & 1;
1359 Features[
"sse"] = (EDX >> 25) & 1;
1360 Features[
"sse2"] = (EDX >> 26) & 1;
1362 Features[
"sse3"] = (ECX >> 0) & 1;
1363 Features[
"pclmul"] = (ECX >> 1) & 1;
1364 Features[
"ssse3"] = (ECX >> 9) & 1;
1365 Features[
"cx16"] = (ECX >> 13) & 1;
1366 Features[
"sse4.1"] = (ECX >> 19) & 1;
1367 Features[
"sse4.2"] = (ECX >> 20) & 1;
1368 Features[
"movbe"] = (ECX >> 22) & 1;
1369 Features[
"popcnt"] = (ECX >> 23) & 1;
1370 Features[
"aes"] = (ECX >> 25) & 1;
1371 Features[
"rdrnd"] = (ECX >> 30) & 1;
1376 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1377 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1378 #if defined(__APPLE__) 1382 bool HasAVX512Save =
true;
1385 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1388 Features[
"avx"] = HasAVXSave;
1389 Features[
"fma"] = ((ECX >> 12) & 1) && HasAVXSave;
1391 Features[
"xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
1392 Features[
"f16c"] = ((ECX >> 29) & 1) && HasAVXSave;
1394 unsigned MaxExtLevel;
1395 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &
EBX, &ECX, &EDX);
1397 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1398 !getX86CpuIDAndInfo(0x80000001, &EAX, &
EBX, &ECX, &EDX);
1399 Features[
"sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1);
1400 Features[
"lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1401 Features[
"sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1402 Features[
"prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1403 Features[
"xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1404 Features[
"lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
1405 Features[
"fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1406 Features[
"tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1407 Features[
"mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1409 Features[
"64bit"] = HasExtLeaf1 && ((EDX >> 29) & 1);
1413 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1414 !getX86CpuIDAndInfo(0x80000008, &EAX, &
EBX, &ECX, &EDX);
1415 Features[
"clzero"] = HasExtLeaf8 && ((
EBX >> 0) & 1);
1416 Features[
"wbnoinvd"] = HasExtLeaf8 && ((
EBX >> 9) & 1);
1419 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &
EBX, &ECX, &EDX);
1421 Features[
"fsgsbase"] = HasLeaf7 && ((
EBX >> 0) & 1);
1422 Features[
"sgx"] = HasLeaf7 && ((
EBX >> 2) & 1);
1423 Features[
"bmi"] = HasLeaf7 && ((
EBX >> 3) & 1);
1425 Features[
"avx2"] = HasLeaf7 && ((
EBX >> 5) & 1) && HasAVXSave;
1426 Features[
"bmi2"] = HasLeaf7 && ((
EBX >> 8) & 1);
1427 Features[
"invpcid"] = HasLeaf7 && ((
EBX >> 10) & 1);
1428 Features[
"rtm"] = HasLeaf7 && ((
EBX >> 11) & 1);
1430 Features[
"avx512f"] = HasLeaf7 && ((
EBX >> 16) & 1) && HasAVX512Save;
1431 Features[
"avx512dq"] = HasLeaf7 && ((
EBX >> 17) & 1) && HasAVX512Save;
1432 Features[
"rdseed"] = HasLeaf7 && ((
EBX >> 18) & 1);
1433 Features[
"adx"] = HasLeaf7 && ((
EBX >> 19) & 1);
1434 Features[
"avx512ifma"] = HasLeaf7 && ((
EBX >> 21) & 1) && HasAVX512Save;
1435 Features[
"clflushopt"] = HasLeaf7 && ((
EBX >> 23) & 1);
1436 Features[
"clwb"] = HasLeaf7 && ((
EBX >> 24) & 1);
1437 Features[
"avx512pf"] = HasLeaf7 && ((
EBX >> 26) & 1) && HasAVX512Save;
1438 Features[
"avx512er"] = HasLeaf7 && ((
EBX >> 27) & 1) && HasAVX512Save;
1439 Features[
"avx512cd"] = HasLeaf7 && ((
EBX >> 28) & 1) && HasAVX512Save;
1440 Features[
"sha"] = HasLeaf7 && ((
EBX >> 29) & 1);
1441 Features[
"avx512bw"] = HasLeaf7 && ((
EBX >> 30) & 1) && HasAVX512Save;
1442 Features[
"avx512vl"] = HasLeaf7 && ((
EBX >> 31) & 1) && HasAVX512Save;
1444 Features[
"prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1);
1445 Features[
"avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
1446 Features[
"pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1447 Features[
"waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1);
1448 Features[
"avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
1449 Features[
"shstk"] = HasLeaf7 && ((ECX >> 7) & 1);
1450 Features[
"gfni"] = HasLeaf7 && ((ECX >> 8) & 1);
1451 Features[
"vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
1452 Features[
"vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1453 Features[
"avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
1454 Features[
"avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
1455 Features[
"avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
1456 Features[
"rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);
1457 Features[
"cldemote"] = HasLeaf7 && ((ECX >> 25) & 1);
1458 Features[
"movdiri"] = HasLeaf7 && ((ECX >> 27) & 1);
1459 Features[
"movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1);
1460 Features[
"enqcmd"] = HasLeaf7 && ((ECX >> 29) & 1);
1472 Features[
"pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
1473 bool HasLeaf7Subleaf1 =
1474 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &
EBX, &ECX, &EDX);
1475 Features[
"avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save;
1477 bool HasLeafD = MaxLevel >= 0xd &&
1478 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &
EBX, &ECX, &EDX);
1481 Features[
"xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1482 Features[
"xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1483 Features[
"xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
1485 bool HasLeaf14 = MaxLevel >= 0x14 &&
1486 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &
EBX, &ECX, &EDX);
1488 Features[
"ptwrite"] = HasLeaf14 && ((
EBX >> 4) & 1);
1492 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) 1499 P->getBuffer().split(Lines,
"\n");
1504 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
1506 Lines[
I].split(CPUFeatures,
' ');
1510 #if defined(__aarch64__) 1512 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1516 for (
unsigned I = 0,
E = CPUFeatures.
size();
I !=
E; ++
I) {
1518 #
if defined(__aarch64__)
1519 .
Case(
"asimd",
"neon")
1520 .
Case(
"fp",
"fp-armv8")
1521 .
Case(
"crc32",
"crc")
1523 .
Case(
"half",
"fp16")
1524 .
Case(
"neon",
"neon")
1525 .
Case(
"vfpv3",
"vfp3")
1526 .
Case(
"vfpv3d16",
"d16")
1527 .
Case(
"vfpv4",
"vfp4")
1528 .
Case(
"idiva",
"hwdiv-arm")
1529 .
Case(
"idivt",
"hwdiv")
1533 #if defined(__aarch64__) 1536 if (CPUFeatures[
I] ==
"aes")
1538 else if (CPUFeatures[
I] ==
"pmull")
1539 crypto |= CAP_PMULL;
1540 else if (CPUFeatures[
I] ==
"sha1")
1542 else if (CPUFeatures[
I] ==
"sha2")
1546 if (LLVMFeatureStr !=
"")
1547 Features[LLVMFeatureStr] =
true;
1550 #if defined(__aarch64__) 1552 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1553 Features[
"crypto"] =
true;
1558 #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64)) 1560 if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
1561 Features[
"neon"] =
true;
1562 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
1563 Features[
"crc"] =
true;
1564 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
1565 Features[
"crypto"] =
true;
1574 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
Represents either an error or a value T.
static std::unique_ptr< llvm::MemoryBuffer > LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent()
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
This class represents lattice values for constants.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent)
Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
StringRef getHostCPUNameForS390x(StringRef ProcCpuinfoContent)
StringSwitch & Case(StringLiteral S, T Value)
int getHostNumPhysicalCores()
Get the number of physical cores (as opposed to logical cores returned from thread::hardware_concurre...
LLVM_NODISCARD R Default(T Value)
static bool startswith(StringRef Magic, const char(&S)[N])
bool isArch32Bit() const
Test whether the architecture is 32-bit.
const std::string & str() const
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
A switch()-like statement whose cases are string literals.
llvm::Triple get32BitArchVariant() const
Form a triple with a 32-bit variant of the current architecture.
StringRef getHostCPUNameForBPF()
The instances of the Type class are immutable: once they are created, they are never changed...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::error_code getError() const
#define LLVM_ATTRIBUTE_UNUSED
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
std::string normalize() const
Return the normalized form of this triple's string.
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
bool isArch64Bit() const
Test whether the architecture is 64-bit.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
StringRef getHostCPUNameForARM(StringRef ProcCpuinfoContent)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
llvm::Triple get64BitArchVariant() const
Form a triple with a 64-bit variant of the current architecture.
StringRef - Represent a constant reference to a string, i.e.
bool getHostCPUFeatures(StringMap< bool > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
static int computeHostNumPhysicalCores()