21 #include "llvm/Config/llvm-config.h" 39 #if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) 40 #include <mach/host_info.h> 41 #include <mach/mach.h> 42 #include <mach/mach_host.h> 43 #include <mach/machine.h> 46 #define DEBUG_TYPE "host-detection" 56 static std::unique_ptr<llvm::MemoryBuffer>
60 if (std::error_code EC = Text.
getError()) {
62 <<
"/proc/cpuinfo: " << EC.message() <<
"\n";
65 return std::move(*Text);
72 const char *
generic =
"generic";
86 while (CIP < CPUInfoEnd && CPUStart == 0) {
87 if (CIP < CPUInfoEnd && *CIP ==
'\n')
90 if (CIP < CPUInfoEnd && *CIP ==
'c') {
92 if (CIP < CPUInfoEnd && *CIP ==
'p') {
94 if (CIP < CPUInfoEnd && *CIP ==
'u') {
96 while (CIP < CPUInfoEnd && (*CIP ==
' ' || *CIP ==
'\t'))
99 if (CIP < CPUInfoEnd && *CIP ==
':') {
101 while (CIP < CPUInfoEnd && (*CIP ==
' ' || *CIP ==
'\t'))
104 if (CIP < CPUInfoEnd) {
106 while (CIP < CPUInfoEnd && (*CIP !=
' ' && *CIP !=
'\t' &&
107 *CIP !=
',' && *CIP !=
'\n'))
109 CPULen = CIP - CPUStart;
117 while (CIP < CPUInfoEnd && *CIP !=
'\n')
125 .Case(
"604e",
"604e")
127 .
Case(
"7400",
"7400")
128 .
Case(
"7410",
"7400")
129 .
Case(
"7447",
"7400")
130 .
Case(
"7455",
"7450")
132 .
Case(
"POWER4",
"970")
133 .
Case(
"PPC970FX",
"970")
134 .
Case(
"PPC970MP",
"970")
136 .
Case(
"POWER5",
"g5")
138 .
Case(
"POWER6",
"pwr6")
139 .
Case(
"POWER7",
"pwr7")
140 .
Case(
"POWER8",
"pwr8")
141 .
Case(
"POWER8E",
"pwr8")
142 .
Case(
"POWER8NVL",
"pwr8")
143 .
Case(
"POWER9",
"pwr9")
154 ProcCpuinfoContent.
split(Lines,
"\n");
159 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I) {
161 Implementer = Lines[
I].substr(15).ltrim(
"\t :");
163 Hardware = Lines[
I].substr(8).ltrim(
"\t :");
166 if (Implementer ==
"0x41") {
174 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
180 .Case(
"0x926",
"arm926ej-s")
181 .
Case(
"0xb02",
"mpcore")
182 .
Case(
"0xb36",
"arm1136j-s")
183 .
Case(
"0xb56",
"arm1156t2-s")
184 .
Case(
"0xb76",
"arm1176jz-s")
185 .
Case(
"0xc08",
"cortex-a8")
186 .
Case(
"0xc09",
"cortex-a9")
187 .
Case(
"0xc0f",
"cortex-a15")
188 .
Case(
"0xc20",
"cortex-m0")
189 .
Case(
"0xc23",
"cortex-m3")
190 .
Case(
"0xc24",
"cortex-m4")
191 .
Case(
"0xd04",
"cortex-a35")
192 .
Case(
"0xd03",
"cortex-a53")
193 .
Case(
"0xd07",
"cortex-a57")
194 .
Case(
"0xd08",
"cortex-a72")
195 .
Case(
"0xd09",
"cortex-a73")
199 if (Implementer ==
"0x51")
201 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
207 .Case(
"0x06f",
"krait")
208 .
Case(
"0x201",
"kryo")
209 .
Case(
"0x205",
"kryo")
210 .
Case(
"0x211",
"kryo")
211 .
Case(
"0x800",
"cortex-a73")
212 .
Case(
"0x801",
"cortex-a73")
213 .
Case(
"0xc00",
"falkor")
214 .
Case(
"0xc01",
"saphira")
217 if (Implementer ==
"0x53") {
220 unsigned Variant = 0, Part = 0;
225 if (
I.consume_front(
"CPU variant"))
226 I.ltrim(
"\t :").getAsInteger(0, Variant);
231 if (
I.consume_front(
"CPU part"))
232 I.ltrim(
"\t :").getAsInteger(0, Part);
234 unsigned Exynos = (Variant << 12) | Part;
257 ProcCpuinfoContent.
split(Lines,
"\n");
261 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
263 size_t Pos = Lines[
I].find(
":");
265 Lines[
I].drop_front(Pos + 1).split(CPUFeatures,
' ');
273 bool HaveVectorSupport =
false;
274 for (
unsigned I = 0,
E = CPUFeatures.
size();
I !=
E; ++
I) {
275 if (CPUFeatures[
I] ==
"vx")
276 HaveVectorSupport =
true;
280 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I) {
282 size_t Pos = Lines[
I].find(
"machine = ");
284 Pos +=
sizeof(
"machine = ") - 1;
286 if (!Lines[
I].drop_front(Pos).getAsInteger(10, Id)) {
287 if (Id >= 3906 && HaveVectorSupport)
289 if (Id >= 2964 && HaveVectorSupport)
305 #if !defined(__linux__) || !defined(__x86_64__) 308 uint8_t insns[40] __attribute__ ((
aligned (8))) =
310 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
312 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
314 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
316 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
318 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
320 struct bpf_prog_load_attr {
333 attr.insns = (uint64_t)insns;
334 attr.license = (uint64_t)
"DUMMY";
336 int fd = syscall(321 , 5 , &attr,
sizeof(attr));
345 #if defined(__i386__) || defined(_M_IX86) || \ 346 defined(__x86_64__) || defined(_M_X64) 348 enum VendorSignatures {
349 SIG_INTEL = 0x756e6547 ,
360 static bool isCpuIdSupported() {
361 #if defined(__GNUC__) || defined(__clang__) 362 #if defined(__i386__) 363 int __cpuid_supported;
366 " movl %%eax,%%ecx\n" 367 " xorl $0x00200000,%%eax\n" 373 " cmpl %%eax,%%ecx\n" 377 :
"=r"(__cpuid_supported)
380 if (!__cpuid_supported)
390 static bool getX86CpuIDAndInfo(
unsigned value,
unsigned *rEAX,
unsigned *rEBX,
391 unsigned *rECX,
unsigned *rEDX) {
392 #if defined(__GNUC__) || defined(__clang__) 393 #if defined(__x86_64__) 396 __asm__(
"movq\t%%rbx, %%rsi\n\t" 398 "xchgq\t%%rbx, %%rsi\n\t" 399 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
402 #elif defined(__i386__) 403 __asm__(
"movl\t%%ebx, %%esi\n\t" 405 "xchgl\t%%ebx, %%esi\n\t" 406 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
412 #elif defined(_MSC_VER) 415 __cpuid(registers, value);
416 *rEAX = registers[0];
417 *rEBX = registers[1];
418 *rECX = registers[2];
419 *rEDX = registers[3];
429 static bool getX86CpuIDAndInfoEx(
unsigned value,
unsigned subleaf,
430 unsigned *rEAX,
unsigned *rEBX,
unsigned *rECX,
432 #if defined(__GNUC__) || defined(__clang__) 433 #if defined(__x86_64__) 436 __asm__(
"movq\t%%rbx, %%rsi\n\t" 438 "xchgq\t%%rbx, %%rsi\n\t" 439 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
440 :
"a"(value),
"c"(subleaf));
442 #elif defined(__i386__) 443 __asm__(
"movl\t%%ebx, %%esi\n\t" 445 "xchgl\t%%ebx, %%esi\n\t" 446 :
"=a"(*rEAX),
"=S"(*rEBX),
"=c"(*rECX),
"=d"(*rEDX)
447 :
"a"(value),
"c"(subleaf));
452 #elif defined(_MSC_VER) 454 __cpuidex(registers, value, subleaf);
455 *rEAX = registers[0];
456 *rEBX = registers[1];
457 *rECX = registers[2];
458 *rEDX = registers[3];
466 static bool getX86XCR0(
unsigned *rEAX,
unsigned *rEDX) {
467 #if defined(__GNUC__) || defined(__clang__) 471 __asm__(
".byte 0x0f, 0x01, 0xd0" :
"=a"(*rEAX),
"=d"(*rEDX) :
"c"(0));
473 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) 474 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
476 *rEDX = Result >> 32;
483 static void detectX86FamilyModel(
unsigned EAX,
unsigned *Family,
485 *Family = (EAX >> 8) & 0xf;
486 *Model = (EAX >> 4) & 0xf;
487 if (*Family == 6 || *Family == 0xf) {
490 *Family += (EAX >> 20) & 0xff;
492 *Model += ((EAX >> 16) & 0xf) << 4;
497 getIntelProcessorTypeAndSubtype(
unsigned Family,
unsigned Model,
498 unsigned Brand_id,
unsigned Features,
499 unsigned Features2,
unsigned *
Type,
505 *Type = X86::INTEL_i386;
508 *Type = X86::INTEL_i486;
511 if (Features & (1 << X86::FEATURE_MMX)) {
512 *Type = X86::INTEL_PENTIUM_MMX;
515 *Type = X86::INTEL_PENTIUM;
520 *Type = X86::INTEL_PENTIUM_PRO;
527 *Type = X86::INTEL_PENTIUM_II;
535 *Type = X86::INTEL_PENTIUM_III;
542 *Type = X86::INTEL_PENTIUM_M;
546 *Type = X86::INTEL_CORE_DUO;
555 *Type = X86::INTEL_CORE2;
556 *Subtype = X86::INTEL_CORE2_65;
564 *Type = X86::INTEL_CORE2;
565 *Subtype = X86::INTEL_CORE2_45;
573 *Type = X86::INTEL_COREI7;
574 *Subtype = X86::INTEL_COREI7_NEHALEM;
580 *Type = X86::INTEL_COREI7;
581 *Subtype = X86::INTEL_COREI7_WESTMERE;
586 *Type = X86::INTEL_COREI7;
587 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
591 *Type = X86::INTEL_COREI7;
592 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
600 *Type = X86::INTEL_COREI7;
601 *Subtype = X86::INTEL_COREI7_HASWELL;
609 *Type = X86::INTEL_COREI7;
610 *Subtype = X86::INTEL_COREI7_BROADWELL;
618 *Type = X86::INTEL_COREI7;
619 *Subtype = X86::INTEL_COREI7_SKYLAKE;
624 *Type = X86::INTEL_COREI7;
625 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
630 *Type = X86::INTEL_COREI7;
631 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
639 *Type = X86::INTEL_BONNELL;
649 *Type = X86::INTEL_SILVERMONT;
654 *Type = X86::INTEL_GOLDMONT;
657 *Type = X86::INTEL_GOLDMONT_PLUS;
660 *Type = X86::INTEL_KNL;
663 *Type = X86::INTEL_KNM;
667 if (Features & (1 << X86::FEATURE_AVX512VBMI)) {
668 *Type = X86::INTEL_COREI7;
669 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
673 if (Features & (1 << X86::FEATURE_AVX512VL)) {
674 *Type = X86::INTEL_COREI7;
675 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
679 if (Features & (1 << X86::FEATURE_AVX512ER)) {
680 *Type = X86::INTEL_KNL;
684 if (Features2 & (1 << (X86::FEATURE_CLFLUSHOPT - 32))) {
685 if (Features2 & (1 << (X86::FEATURE_SHA - 32))) {
686 *Type = X86::INTEL_GOLDMONT;
688 *Type = X86::INTEL_COREI7;
689 *Subtype = X86::INTEL_COREI7_SKYLAKE;
693 if (Features2 & (1 << (X86::FEATURE_ADX - 32))) {
694 *Type = X86::INTEL_COREI7;
695 *Subtype = X86::INTEL_COREI7_BROADWELL;
698 if (Features & (1 << X86::FEATURE_AVX2)) {
699 *Type = X86::INTEL_COREI7;
700 *Subtype = X86::INTEL_COREI7_HASWELL;
703 if (Features & (1 << X86::FEATURE_AVX)) {
704 *Type = X86::INTEL_COREI7;
705 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
708 if (Features & (1 << X86::FEATURE_SSE4_2)) {
709 if (Features2 & (1 << (X86::FEATURE_MOVBE - 32))) {
710 *Type = X86::INTEL_SILVERMONT;
712 *Type = X86::INTEL_COREI7;
713 *Subtype = X86::INTEL_COREI7_NEHALEM;
717 if (Features & (1 << X86::FEATURE_SSE4_1)) {
718 *Type = X86::INTEL_CORE2;
719 *Subtype = X86::INTEL_CORE2_45;
722 if (Features & (1 << X86::FEATURE_SSSE3)) {
723 if (Features2 & (1 << (X86::FEATURE_MOVBE - 32))) {
724 *Type = X86::INTEL_BONNELL;
726 *Type = X86::INTEL_CORE2;
727 *Subtype = X86::INTEL_CORE2_65;
731 if (Features2 & (1 << (X86::FEATURE_EM64T - 32))) {
732 *Type = X86::INTEL_CORE2;
733 *Subtype = X86::INTEL_CORE2_65;
736 if (Features & (1 << X86::FEATURE_SSE3)) {
737 *Type = X86::INTEL_CORE_DUO;
740 if (Features & (1 << X86::FEATURE_SSE2)) {
741 *Type = X86::INTEL_PENTIUM_M;
744 if (Features & (1 << X86::FEATURE_SSE)) {
745 *Type = X86::INTEL_PENTIUM_III;
748 if (Features & (1 << X86::FEATURE_MMX)) {
749 *Type = X86::INTEL_PENTIUM_II;
752 *Type = X86::INTEL_PENTIUM_PRO;
757 if (Features2 & (1 << (X86::FEATURE_EM64T - 32))) {
758 *Type = X86::INTEL_NOCONA;
761 if (Features & (1 << X86::FEATURE_SSE3)) {
762 *Type = X86::INTEL_PRESCOTT;
765 *Type = X86::INTEL_PENTIUM_IV;
773 static void getAMDProcessorTypeAndSubtype(
unsigned Family,
unsigned Model,
774 unsigned Features,
unsigned *Type,
781 *Type = X86::AMD_i486;
784 *Type = X86::AMDPENTIUM;
788 *Subtype = X86::AMDPENTIUM_K6;
791 *Subtype = X86::AMDPENTIUM_K62;
795 *Subtype = X86::AMDPENTIUM_K63;
798 *Subtype = X86::AMDPENTIUM_GEODE;
803 if (Features & (1 << X86::FEATURE_SSE)) {
804 *Type = X86::AMD_ATHLON_XP;
807 *Type = X86::AMD_ATHLON;
810 if (Features & (1 << X86::FEATURE_SSE3)) {
811 *Type = X86::AMD_K8SSE3;
817 *Type = X86::AMDFAM10H;
820 *Subtype = X86::AMDFAM10H_BARCELONA;
823 *Subtype = X86::AMDFAM10H_SHANGHAI;
826 *Subtype = X86::AMDFAM10H_ISTANBUL;
831 *Type = X86::AMD_BTVER1;
834 *Type = X86::AMDFAM15H;
835 if (Model >= 0x60 && Model <= 0x7f) {
836 *Subtype = X86::AMDFAM15H_BDVER4;
839 if (Model >= 0x30 && Model <= 0x3f) {
840 *Subtype = X86::AMDFAM15H_BDVER3;
843 if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
844 *Subtype = X86::AMDFAM15H_BDVER2;
848 *Subtype = X86::AMDFAM15H_BDVER1;
853 *Type = X86::AMD_BTVER2;
856 *Type = X86::AMDFAM17H;
857 *Subtype = X86::AMDFAM17H_ZNVER1;
864 static void getAvailableFeatures(
unsigned ECX,
unsigned EDX,
unsigned MaxLeaf,
865 unsigned *FeaturesOut,
866 unsigned *Features2Out) {
867 unsigned Features = 0;
868 unsigned Features2 = 0;
872 Features |= 1 << X86::FEATURE_CMOV;
874 Features |= 1 << X86::FEATURE_MMX;
876 Features |= 1 << X86::FEATURE_SSE;
878 Features |= 1 << X86::FEATURE_SSE2;
881 Features |= 1 << X86::FEATURE_SSE3;
883 Features |= 1 << X86::FEATURE_PCLMUL;
885 Features |= 1 << X86::FEATURE_SSSE3;
887 Features |= 1 << X86::FEATURE_FMA;
889 Features |= 1 << X86::FEATURE_SSE4_1;
891 Features |= 1 << X86::FEATURE_SSE4_2;
893 Features |= 1 << X86::FEATURE_POPCNT;
895 Features |= 1 << X86::FEATURE_AES;
898 Features2 |= 1 << (X86::FEATURE_MOVBE - 32);
903 const unsigned AVXBits = (1 << 27) | (1 << 28);
904 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
905 ((EAX & 0x6) == 0x6);
906 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
909 Features |= 1 << X86::FEATURE_AVX;
912 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
914 if (HasLeaf7 && ((EBX >> 3) & 1))
915 Features |= 1 << X86::FEATURE_BMI;
916 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
917 Features |= 1 << X86::FEATURE_AVX2;
918 if (HasLeaf7 && ((EBX >> 9) & 1))
919 Features |= 1 << X86::FEATURE_BMI2;
920 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
921 Features |= 1 << X86::FEATURE_AVX512F;
922 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
923 Features |= 1 << X86::FEATURE_AVX512DQ;
924 if (HasLeaf7 && ((EBX >> 19) & 1))
925 Features2 |= 1 << (X86::FEATURE_ADX - 32);
926 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
927 Features |= 1 << X86::FEATURE_AVX512IFMA;
928 if (HasLeaf7 && ((EBX >> 23) & 1))
929 Features2 |= 1 << (X86::FEATURE_CLFLUSHOPT - 32);
930 if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
931 Features |= 1 << X86::FEATURE_AVX512PF;
932 if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
933 Features |= 1 << X86::FEATURE_AVX512ER;
934 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
935 Features |= 1 << X86::FEATURE_AVX512CD;
936 if (HasLeaf7 && ((EBX >> 29) & 1))
937 Features2 |= 1 << (X86::FEATURE_SHA - 32);
938 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
939 Features |= 1 << X86::FEATURE_AVX512BW;
940 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
941 Features |= 1 << X86::FEATURE_AVX512VL;
943 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
944 Features |= 1 << X86::FEATURE_AVX512VBMI;
945 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
946 Features |= 1 << X86::FEATURE_AVX512VPOPCNTDQ;
948 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
949 Features |= 1 << X86::FEATURE_AVX5124VNNIW;
950 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
951 Features |= 1 << X86::FEATURE_AVX5124FMAPS;
953 unsigned MaxExtLevel;
954 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
956 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
957 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
958 if (HasExtLeaf1 && ((ECX >> 6) & 1))
959 Features |= 1 << X86::FEATURE_SSE4_A;
960 if (HasExtLeaf1 && ((ECX >> 11) & 1))
961 Features |= 1 << X86::FEATURE_XOP;
962 if (HasExtLeaf1 && ((ECX >> 16) & 1))
963 Features |= 1 << X86::FEATURE_FMA4;
965 if (HasExtLeaf1 && ((EDX >> 29) & 1))
966 Features2 |= 1 << (X86::FEATURE_EM64T - 32);
969 *Features2Out = Features2;
973 unsigned EAX = 0,
EBX = 0, ECX = 0, EDX = 0;
974 unsigned MaxLeaf, Vendor;
976 #if defined(__GNUC__) || defined(__clang__) 981 if(!isCpuIdSupported())
984 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
986 getX86CpuIDAndInfo(0x1, &EAX, &
EBX, &ECX, &EDX);
988 unsigned Brand_id =
EBX & 0xff;
989 unsigned Family = 0, Model = 0;
990 unsigned Features = 0, Features2 = 0;
991 detectX86FamilyModel(EAX, &Family, &Model);
992 getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2);
995 unsigned Subtype = 0;
997 if (Vendor == SIG_INTEL) {
998 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features,
999 Features2, &Type, &Subtype);
1000 }
else if (Vendor == SIG_AMD) {
1001 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
1005 #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \ 1006 if (Subtype == X86::ENUM) \ 1008 #include "llvm/Support/X86TargetParser.def" 1011 #define X86_CPU_TYPE(ARCHNAME, ENUM) \ 1012 if (Type == X86::ENUM) \ 1014 #include "llvm/Support/X86TargetParser.def" 1019 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) 1021 host_basic_info_data_t hostInfo;
1022 mach_msg_type_number_t infoCount;
1024 infoCount = HOST_BASIC_INFO_COUNT;
1025 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
1031 switch (hostInfo.cpu_subtype) {
1061 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__)) 1064 StringRef Content = P ? P->getBuffer() :
"";
1067 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) 1070 StringRef Content = P ? P->getBuffer() :
"";
1073 #elif defined(__linux__) && defined(__s390x__) 1076 StringRef Content = P ? P->getBuffer() :
"";
1083 #if defined(__linux__) && defined(__x86_64__) 1092 if (std::error_code EC = Text.
getError()) {
1094 <<
"/proc/cpuinfo: " << EC.message() <<
"\n";
1098 (*Text)->getBuffer().split(strs,
"\n", -1,
1100 int CurPhysicalId = -1;
1103 for (
auto &Line : strs) {
1105 if (!Line.startswith(
"physical id") && !Line.startswith(
"core id"))
1107 std::pair<StringRef, StringRef>
Data = Line.split(
':');
1108 auto Name = Data.first.trim();
1109 auto Val = Data.second.trim();
1110 if (
Name ==
"physical id") {
1111 assert(CurPhysicalId == -1 &&
1112 "Expected a core id before seeing another physical id");
1113 Val.getAsInteger(10, CurPhysicalId);
1115 if (
Name ==
"core id") {
1116 assert(CurCoreId == -1 &&
1117 "Expected a physical id before seeing another core id");
1118 Val.getAsInteger(10, CurCoreId);
1120 if (CurPhysicalId != -1 && CurCoreId != -1) {
1121 UniqueItems.
insert(std::make_pair(CurPhysicalId, CurCoreId));
1126 return UniqueItems.
size();
1128 #elif defined(__APPLE__) && defined(__x86_64__) 1129 #include <sys/param.h> 1130 #include <sys/sysctl.h> 1135 size_t len =
sizeof(
count);
1136 sysctlbyname(
"hw.physicalcpu", &count, &len, NULL, 0);
1140 nm[1] = HW_AVAILCPU;
1141 sysctl(nm, 2, &count, &len, NULL, 0);
1157 #if defined(__i386__) || defined(_M_IX86) || \ 1158 defined(__x86_64__) || defined(_M_X64) 1160 unsigned EAX = 0,
EBX = 0, ECX = 0, EDX = 0;
1167 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1171 getX86CpuIDAndInfo(1, &EAX, &
EBX, &ECX, &EDX);
1173 Features[
"cmov"] = (EDX >> 15) & 1;
1174 Features[
"mmx"] = (EDX >> 23) & 1;
1175 Features[
"sse"] = (EDX >> 25) & 1;
1176 Features[
"sse2"] = (EDX >> 26) & 1;
1178 Features[
"sse3"] = (ECX >> 0) & 1;
1179 Features[
"pclmul"] = (ECX >> 1) & 1;
1180 Features[
"ssse3"] = (ECX >> 9) & 1;
1181 Features[
"cx16"] = (ECX >> 13) & 1;
1182 Features[
"sse4.1"] = (ECX >> 19) & 1;
1183 Features[
"sse4.2"] = (ECX >> 20) & 1;
1184 Features[
"movbe"] = (ECX >> 22) & 1;
1185 Features[
"popcnt"] = (ECX >> 23) & 1;
1186 Features[
"aes"] = (ECX >> 25) & 1;
1187 Features[
"rdrnd"] = (ECX >> 30) & 1;
1192 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1193 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1195 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1197 Features[
"avx"] = HasAVXSave;
1198 Features[
"fma"] = ((ECX >> 12) & 1) && HasAVXSave;
1200 Features[
"xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
1201 Features[
"f16c"] = ((ECX >> 29) & 1) && HasAVXSave;
1203 unsigned MaxExtLevel;
1204 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &
EBX, &ECX, &EDX);
1206 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1207 !getX86CpuIDAndInfo(0x80000001, &EAX, &
EBX, &ECX, &EDX);
1208 Features[
"sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1);
1209 Features[
"lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1210 Features[
"sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1211 Features[
"prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1212 Features[
"xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1213 Features[
"lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
1214 Features[
"fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1215 Features[
"tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1216 Features[
"mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1220 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1221 !getX86CpuIDAndInfo(0x80000008, &EAX, &
EBX, &ECX, &EDX);
1222 Features[
"clzero"] = HasExtLeaf8 && ((
EBX >> 0) & 1);
1223 Features[
"wbnoinvd"] = HasExtLeaf8 && ((
EBX >> 9) & 1);
1226 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &
EBX, &ECX, &EDX);
1228 Features[
"fsgsbase"] = HasLeaf7 && ((
EBX >> 0) & 1);
1229 Features[
"sgx"] = HasLeaf7 && ((
EBX >> 2) & 1);
1230 Features[
"bmi"] = HasLeaf7 && ((
EBX >> 3) & 1);
1232 Features[
"avx2"] = HasLeaf7 && ((
EBX >> 5) & 1) && HasAVXSave;
1233 Features[
"bmi2"] = HasLeaf7 && ((
EBX >> 8) & 1);
1234 Features[
"invpcid"] = HasLeaf7 && ((
EBX >> 10) & 1);
1235 Features[
"rtm"] = HasLeaf7 && ((
EBX >> 11) & 1);
1237 Features[
"avx512f"] = HasLeaf7 && ((
EBX >> 16) & 1) && HasAVX512Save;
1238 Features[
"avx512dq"] = HasLeaf7 && ((
EBX >> 17) & 1) && HasAVX512Save;
1239 Features[
"rdseed"] = HasLeaf7 && ((
EBX >> 18) & 1);
1240 Features[
"adx"] = HasLeaf7 && ((
EBX >> 19) & 1);
1241 Features[
"avx512ifma"] = HasLeaf7 && ((
EBX >> 21) & 1) && HasAVX512Save;
1242 Features[
"clflushopt"] = HasLeaf7 && ((
EBX >> 23) & 1);
1243 Features[
"clwb"] = HasLeaf7 && ((
EBX >> 24) & 1);
1244 Features[
"avx512pf"] = HasLeaf7 && ((
EBX >> 26) & 1) && HasAVX512Save;
1245 Features[
"avx512er"] = HasLeaf7 && ((
EBX >> 27) & 1) && HasAVX512Save;
1246 Features[
"avx512cd"] = HasLeaf7 && ((
EBX >> 28) & 1) && HasAVX512Save;
1247 Features[
"sha"] = HasLeaf7 && ((
EBX >> 29) & 1);
1248 Features[
"avx512bw"] = HasLeaf7 && ((
EBX >> 30) & 1) && HasAVX512Save;
1249 Features[
"avx512vl"] = HasLeaf7 && ((
EBX >> 31) & 1) && HasAVX512Save;
1251 Features[
"prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1);
1252 Features[
"avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
1253 Features[
"pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1254 Features[
"waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1);
1255 Features[
"avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
1256 Features[
"shstk"] = HasLeaf7 && ((ECX >> 7) & 1);
1257 Features[
"gfni"] = HasLeaf7 && ((ECX >> 8) & 1);
1258 Features[
"vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
1259 Features[
"vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1260 Features[
"avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
1261 Features[
"avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
1262 Features[
"avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
1263 Features[
"rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);
1264 Features[
"cldemote"] = HasLeaf7 && ((ECX >> 25) & 1);
1265 Features[
"movdiri"] = HasLeaf7 && ((ECX >> 27) & 1);
1266 Features[
"movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1);
1278 Features[
"pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
1280 bool HasLeafD = MaxLevel >= 0xd &&
1281 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &
EBX, &ECX, &EDX);
1284 Features[
"xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1285 Features[
"xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1286 Features[
"xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
1288 bool HasLeaf14 = MaxLevel >= 0x14 &&
1289 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &
EBX, &ECX, &EDX);
1291 Features[
"ptwrite"] = HasLeaf14 && ((
EBX >> 4) & 1);
1295 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) 1302 P->getBuffer().split(Lines,
"\n");
1307 for (
unsigned I = 0,
E = Lines.
size();
I !=
E; ++
I)
1309 Lines[
I].split(CPUFeatures,
' ');
1313 #if defined(__aarch64__) 1315 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1319 for (
unsigned I = 0,
E = CPUFeatures.
size();
I !=
E; ++
I) {
1321 #
if defined(__aarch64__)
1322 .
Case(
"asimd",
"neon")
1323 .
Case(
"fp",
"fp-armv8")
1324 .
Case(
"crc32",
"crc")
1326 .
Case(
"half",
"fp16")
1327 .
Case(
"neon",
"neon")
1328 .
Case(
"vfpv3",
"vfp3")
1329 .
Case(
"vfpv3d16",
"d16")
1330 .
Case(
"vfpv4",
"vfp4")
1331 .
Case(
"idiva",
"hwdiv-arm")
1332 .
Case(
"idivt",
"hwdiv")
1336 #if defined(__aarch64__) 1339 if (CPUFeatures[
I] ==
"aes")
1341 else if (CPUFeatures[
I] ==
"pmull")
1342 crypto |= CAP_PMULL;
1343 else if (CPUFeatures[
I] ==
"sha1")
1345 else if (CPUFeatures[
I] ==
"sha2")
1349 if (LLVMFeatureStr !=
"")
1350 Features[LLVMFeatureStr] =
true;
1353 #if defined(__aarch64__) 1355 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1356 Features[
"crypto"] =
true;
1366 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.
Compute iterated dominance frontiers using a linear time algorithm.
StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent)
Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
StringRef getHostCPUNameForS390x(StringRef ProcCpuinfoContent)
int getHostNumPhysicalCores()
Get the number of physical cores (as opposed to logical cores returned from thread::hardware_concurre...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE 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
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...
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
#define LLVM_ATTRIBUTE_UNUSED
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())
const FeatureBitset Features
llvm::Triple get64BitArchVariant() const
Form a triple with a 64-bit variant of the current architecture.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
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()