1 //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the operating system Host concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Host.h"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/raw_ostream.h"
28 // Include the platform-specific parts of this class.
30 #include "Unix/Host.inc"
33 #include "Windows/Host.inc"
38 #if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
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"
47 //===----------------------------------------------------------------------===//
49 // Implementations of the CPU detection routines
51 //===----------------------------------------------------------------------===//
55 #if defined(__linux__)
56 static ssize_t LLVM_ATTRIBUTE_UNUSED readCpuInfo(void *Buf, size_t Size) {
57 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
58 // memory buffer because the 'file' has 0 size (it can be read from only
62 std::error_code EC = sys::fs::openFileForRead("/proc/cpuinfo", FD);
64 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << EC.message() << "\n");
67 int Ret = read(FD, Buf, Size);
68 int CloseStatus = close(FD);
75 #if defined(__i386__) || defined(_M_IX86) || \
76 defined(__x86_64__) || defined(_M_X64)
78 enum VendorSignatures {
79 SIG_INTEL = 0x756e6547 /* Genu */,
80 SIG_AMD = 0x68747541 /* Auth */
83 enum ProcessorVendors {
117 enum ProcessorSubtypes {
118 INTEL_COREI7_NEHALEM = 1,
119 INTEL_COREI7_WESTMERE,
120 INTEL_COREI7_SANDYBRIDGE,
129 INTEL_COREI7_IVYBRIDGE,
130 INTEL_COREI7_HASWELL,
131 INTEL_COREI7_BROADWELL,
132 INTEL_COREI7_SKYLAKE,
133 INTEL_COREI7_SKYLAKE_AVX512,
135 INTEL_ATOM_SILVERMONT,
136 INTEL_KNIGHTS_LANDING,
155 enum ProcessorFeatures {
174 // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
175 // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
176 // support. Consequently, for i386, the presence of CPUID is checked first
177 // via the corresponding eflags bit.
178 // Removal of cpuid.h header motivated by PR30384
179 // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
180 // or test-suite, but are used in external projects e.g. libstdcxx
181 static bool isCpuIdSupported() {
182 #if defined(__GNUC__) || defined(__clang__)
183 #if defined(__i386__)
184 int __cpuid_supported;
187 " movl %%eax,%%ecx\n"
188 " xorl $0x00200000,%%eax\n"
194 " cmpl %%eax,%%ecx\n"
198 : "=r"(__cpuid_supported)
201 if (!__cpuid_supported)
209 /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
210 /// the specified arguments. If we can't run cpuid on the host, return true.
211 static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
212 unsigned *rECX, unsigned *rEDX) {
213 #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
214 #if defined(__GNUC__) || defined(__clang__)
215 #if defined(__x86_64__)
216 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
217 // FIXME: should we save this for Clang?
218 __asm__("movq\t%%rbx, %%rsi\n\t"
220 "xchgq\t%%rbx, %%rsi\n\t"
221 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
223 #elif defined(__i386__)
224 __asm__("movl\t%%ebx, %%esi\n\t"
226 "xchgl\t%%ebx, %%esi\n\t"
227 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
230 assert(0 && "This method is defined only for x86.");
232 #elif defined(_MSC_VER)
233 // The MSVC intrinsic is portable across x86 and x64.
235 __cpuid(registers, value);
236 *rEAX = registers[0];
237 *rEBX = registers[1];
238 *rECX = registers[2];
239 *rEDX = registers[3];
247 /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
248 /// the 4 values in the specified arguments. If we can't run cpuid on the host,
250 static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
251 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
253 #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
254 #if defined(__x86_64__) || defined(_M_X64)
255 #if defined(__GNUC__) || defined(__clang__)
256 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
257 // FIXME: should we save this for Clang?
258 __asm__("movq\t%%rbx, %%rsi\n\t"
260 "xchgq\t%%rbx, %%rsi\n\t"
261 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
262 : "a"(value), "c"(subleaf));
263 #elif defined(_MSC_VER)
265 __cpuidex(registers, value, subleaf);
266 *rEAX = registers[0];
267 *rEBX = registers[1];
268 *rECX = registers[2];
269 *rEDX = registers[3];
271 #elif defined(__i386__) || defined(_M_IX86)
272 #if defined(__GNUC__) || defined(__clang__)
273 __asm__("movl\t%%ebx, %%esi\n\t"
275 "xchgl\t%%ebx, %%esi\n\t"
276 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
277 : "a"(value), "c"(subleaf));
278 #elif defined(_MSC_VER)
284 mov dword ptr [esi],eax
286 mov dword ptr [esi],ebx
288 mov dword ptr [esi],ecx
290 mov dword ptr [esi],edx
294 assert(0 && "This method is defined only for x86.");
302 static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
303 #if defined(__GNUC__) || defined(__clang__)
304 // Check xgetbv; this uses a .byte sequence instead of the instruction
305 // directly because older assemblers do not include support for xgetbv and
306 // there is no easy way to conditionally compile based on the assembler used.
307 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
309 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
310 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
312 *rEDX = Result >> 32;
319 static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
321 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
322 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
323 if (*Family == 6 || *Family == 0xf) {
325 // Examine extended family ID if family ID is F.
326 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
327 // Examine extended model ID if family ID is 6 or F.
328 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
333 getIntelProcessorTypeAndSubtype(unsigned int Family, unsigned int Model,
334 unsigned int Brand_id, unsigned int Features,
335 unsigned *Type, unsigned *Subtype) {
344 case 0: // Intel486 DX processors
345 case 1: // Intel486 DX processors
346 case 2: // Intel486 SX processors
347 case 3: // Intel487 processors, IntelDX2 OverDrive processors,
348 // IntelDX2 processors
349 case 4: // Intel486 SL processor
350 case 5: // IntelSX2 processors
351 case 7: // Write-Back Enhanced IntelDX2 processors
352 case 8: // IntelDX4 OverDrive processors, IntelDX4 processors
360 case 1: // Pentium OverDrive processor for Pentium processor (60, 66),
361 // Pentium processors (60, 66)
362 case 2: // Pentium OverDrive processor for Pentium processor (75, 90,
363 // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
365 case 3: // Pentium OverDrive processors for Intel486 processor-based
367 *Type = INTEL_PENTIUM;
369 case 4: // Pentium OverDrive processor with MMX technology for Pentium
370 // processor (75, 90, 100, 120, 133), Pentium processor with
371 // MMX technology (166, 200)
372 *Type = INTEL_PENTIUM;
373 *Subtype = INTEL_PENTIUM_MMX;
376 *Type = INTEL_PENTIUM;
382 case 0x01: // Pentium Pro processor
383 *Type = INTEL_PENTIUM_PRO;
385 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
387 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
388 // model 05, and Intel Celeron processor, model 05
389 case 0x06: // Celeron processor, model 06
390 *Type = INTEL_PENTIUM_II;
392 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
393 // processor, model 07
394 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
395 // model 08, and Celeron processor, model 08
396 case 0x0a: // Pentium III Xeon processor, model 0Ah
397 case 0x0b: // Pentium III processor, model 0Bh
398 *Type = INTEL_PENTIUM_III;
400 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
401 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
402 // 0Dh. All processors are manufactured using the 90 nm process.
403 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
404 // Integrated Processor with Intel QuickAssist Technology
405 *Type = INTEL_PENTIUM_M;
407 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
408 // 0Eh. All processors are manufactured using the 65 nm process.
409 *Type = INTEL_CORE_DUO;
411 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
412 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
413 // mobile processor, Intel Core 2 Extreme processor, Intel
414 // Pentium Dual-Core processor, Intel Xeon processor, model
415 // 0Fh. All processors are manufactured using the 65 nm process.
416 case 0x16: // Intel Celeron processor model 16h. All processors are
417 // manufactured using the 65 nm process
418 *Type = INTEL_CORE2; // "core2"
419 *Subtype = INTEL_CORE2_65;
421 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
422 // 17h. All processors are manufactured using the 45 nm process.
424 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
425 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
426 // the 45 nm process.
427 *Type = INTEL_CORE2; // "penryn"
428 *Subtype = INTEL_CORE2_45;
430 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
431 // processors are manufactured using the 45 nm process.
432 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
433 // As found in a Summer 2010 model iMac.
435 case 0x2e: // Nehalem EX
436 *Type = INTEL_COREI7; // "nehalem"
437 *Subtype = INTEL_COREI7_NEHALEM;
439 case 0x25: // Intel Core i7, laptop version.
440 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
441 // processors are manufactured using the 32 nm process.
442 case 0x2f: // Westmere EX
443 *Type = INTEL_COREI7; // "westmere"
444 *Subtype = INTEL_COREI7_WESTMERE;
446 case 0x2a: // Intel Core i7 processor. All processors are manufactured
447 // using the 32 nm process.
449 *Type = INTEL_COREI7; //"sandybridge"
450 *Subtype = INTEL_COREI7_SANDYBRIDGE;
453 case 0x3e: // Ivy Bridge EP
454 *Type = INTEL_COREI7; // "ivybridge"
455 *Subtype = INTEL_COREI7_IVYBRIDGE;
463 *Type = INTEL_COREI7; // "haswell"
464 *Subtype = INTEL_COREI7_HASWELL;
472 *Type = INTEL_COREI7; // "broadwell"
473 *Subtype = INTEL_COREI7_BROADWELL;
478 *Type = INTEL_COREI7; // "skylake-avx512"
479 *Subtype = INTEL_COREI7_SKYLAKE_AVX512;
482 *Type = INTEL_COREI7; // "skylake"
483 *Subtype = INTEL_COREI7_SKYLAKE;
486 case 0x1c: // Most 45 nm Intel Atom processors
487 case 0x26: // 45 nm Atom Lincroft
488 case 0x27: // 32 nm Atom Medfield
489 case 0x35: // 32 nm Atom Midview
490 case 0x36: // 32 nm Atom Midview
492 *Subtype = INTEL_ATOM_BONNELL;
495 // Atom Silvermont codes from the Intel software optimization guide.
501 case 0x4c: // really airmont
503 *Subtype = INTEL_ATOM_SILVERMONT;
504 break; // "silvermont"
507 *Type = INTEL_XEONPHI; // knl
508 *Subtype = INTEL_KNIGHTS_LANDING;
511 default: // Unknown family 6 CPU, try to guess.
512 if (Features & (1 << FEATURE_AVX512)) {
513 *Type = INTEL_XEONPHI; // knl
514 *Subtype = INTEL_KNIGHTS_LANDING;
517 if (Features & (1 << FEATURE_ADX)) {
518 *Type = INTEL_COREI7;
519 *Subtype = INTEL_COREI7_BROADWELL;
522 if (Features & (1 << FEATURE_AVX2)) {
523 *Type = INTEL_COREI7;
524 *Subtype = INTEL_COREI7_HASWELL;
527 if (Features & (1 << FEATURE_AVX)) {
528 *Type = INTEL_COREI7;
529 *Subtype = INTEL_COREI7_SANDYBRIDGE;
532 if (Features & (1 << FEATURE_SSE4_2)) {
533 if (Features & (1 << FEATURE_MOVBE)) {
535 *Subtype = INTEL_ATOM_SILVERMONT;
537 *Type = INTEL_COREI7;
538 *Subtype = INTEL_COREI7_NEHALEM;
542 if (Features & (1 << FEATURE_SSE4_1)) {
543 *Type = INTEL_CORE2; // "penryn"
544 *Subtype = INTEL_CORE2_45;
547 if (Features & (1 << FEATURE_SSSE3)) {
548 if (Features & (1 << FEATURE_MOVBE)) {
550 *Subtype = INTEL_ATOM_BONNELL; // "bonnell"
552 *Type = INTEL_CORE2; // "core2"
553 *Subtype = INTEL_CORE2_65;
557 if (Features & (1 << FEATURE_EM64T)) {
558 *Type = INTEL_X86_64;
561 if (Features & (1 << FEATURE_SSE2)) {
562 *Type = INTEL_PENTIUM_M;
565 if (Features & (1 << FEATURE_SSE)) {
566 *Type = INTEL_PENTIUM_III;
569 if (Features & (1 << FEATURE_MMX)) {
570 *Type = INTEL_PENTIUM_II;
573 *Type = INTEL_PENTIUM_PRO;
579 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
580 // model 00h and manufactured using the 0.18 micron process.
581 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
582 // processor MP, and Intel Celeron processor. All processors are
583 // model 01h and manufactured using the 0.18 micron process.
584 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
585 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
586 // processor, and Mobile Intel Celeron processor. All processors
587 // are model 02h and manufactured using the 0.13 micron process.
589 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
592 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
593 // processor. All processors are model 03h and manufactured using
594 // the 90 nm process.
595 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
596 // Pentium D processor, Intel Xeon processor, Intel Xeon
597 // processor MP, Intel Celeron D processor. All processors are
598 // model 04h and manufactured using the 90 nm process.
599 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
600 // Extreme Edition, Intel Xeon processor, Intel Xeon processor
601 // MP, Intel Celeron D processor. All processors are model 06h
602 // and manufactured using the 65 nm process.
604 ((Features & (1 << FEATURE_EM64T)) ? INTEL_NOCONA : INTEL_PRESCOTT);
609 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
619 static void getAMDProcessorTypeAndSubtype(unsigned int Family,
621 unsigned int Features,
624 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
625 // appears to be no way to generate the wide variety of AMD-specific targets
626 // from the information returned from CPUID.
636 *Subtype = AMDPENTIUM_K6;
639 *Subtype = AMDPENTIUM_K62;
643 *Subtype = AMDPENTIUM_K63;
646 *Subtype = AMDPENTIUM_GEODE;
654 *Subtype = AMDATHLON_TBIRD;
655 break; // "athlon-tbird"
659 *Subtype = AMDATHLON_MP;
660 break; // "athlon-mp"
662 *Subtype = AMDATHLON_XP;
663 break; // "athlon-xp"
668 if (Features & (1 << FEATURE_SSE3)) {
669 *Subtype = AMDATHLON_K8SSE3;
674 *Subtype = AMDATHLON_OPTERON;
677 *Subtype = AMDATHLON_FX;
678 break; // "athlon-fx"; also opteron
680 *Subtype = AMDATHLON_64;
685 *Type = AMDFAM10H; // "amdfam10"
688 *Subtype = AMDFAM10H_BARCELONA;
691 *Subtype = AMDFAM10H_SHANGHAI;
694 *Subtype = AMDFAM10H_ISTANBUL;
700 *Subtype = AMD_BTVER1;
705 (1 << FEATURE_AVX))) { // If no AVX support, provide a sane fallback.
706 *Subtype = AMD_BTVER1;
709 if (Model >= 0x50 && Model <= 0x6f) {
710 *Subtype = AMDFAM15H_BDVER4;
711 break; // "bdver4"; 50h-6Fh: Excavator
713 if (Model >= 0x30 && Model <= 0x3f) {
714 *Subtype = AMDFAM15H_BDVER3;
715 break; // "bdver3"; 30h-3Fh: Steamroller
717 if (Model >= 0x10 && Model <= 0x1f) {
718 *Subtype = AMDFAM15H_BDVER2;
719 break; // "bdver2"; 10h-1Fh: Piledriver
722 *Subtype = AMDFAM15H_BDVER1;
723 break; // "bdver1"; 00h-0Fh: Bulldozer
729 (1 << FEATURE_AVX))) { // If no AVX support provide a sane fallback.
730 *Subtype = AMD_BTVER1;
733 *Subtype = AMD_BTVER2;
740 static unsigned getAvailableFeatures(unsigned int ECX, unsigned int EDX,
742 unsigned Features = 0;
743 unsigned int EAX, EBX;
744 Features |= (((EDX >> 23) & 1) << FEATURE_MMX);
745 Features |= (((EDX >> 25) & 1) << FEATURE_SSE);
746 Features |= (((EDX >> 26) & 1) << FEATURE_SSE2);
747 Features |= (((ECX >> 0) & 1) << FEATURE_SSE3);
748 Features |= (((ECX >> 9) & 1) << FEATURE_SSSE3);
749 Features |= (((ECX >> 19) & 1) << FEATURE_SSE4_1);
750 Features |= (((ECX >> 20) & 1) << FEATURE_SSE4_2);
751 Features |= (((ECX >> 22) & 1) << FEATURE_MOVBE);
753 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
754 // indicates that the AVX registers will be saved and restored on context
755 // switch, then we have full AVX support.
756 const unsigned AVXBits = (1 << 27) | (1 << 28);
757 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
758 ((EAX & 0x6) == 0x6);
759 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
761 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
762 bool HasADX = HasLeaf7 && ((EBX >> 19) & 1);
763 bool HasAVX2 = HasAVX && HasLeaf7 && (EBX & 0x20);
764 bool HasAVX512 = HasLeaf7 && HasAVX512Save && ((EBX >> 16) & 1);
765 Features |= (HasAVX << FEATURE_AVX);
766 Features |= (HasAVX2 << FEATURE_AVX2);
767 Features |= (HasAVX512 << FEATURE_AVX512);
768 Features |= (HasAVX512Save << FEATURE_AVX512SAVE);
769 Features |= (HasADX << FEATURE_ADX);
771 getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
772 Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T);
776 StringRef sys::getHostCPUName() {
777 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
778 unsigned MaxLeaf, Vendor;
780 #if defined(__GNUC__) || defined(__clang__)
781 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
782 // and simplify it to not invoke __cpuid (like cpu_model.c in
783 // compiler-rt/lib/builtins/cpu_model.c?
784 // Opting for the second option.
785 if(!isCpuIdSupported())
788 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX))
790 if (getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
793 unsigned Brand_id = EBX & 0xff;
794 unsigned Family = 0, Model = 0;
795 unsigned Features = 0;
796 detectX86FamilyModel(EAX, &Family, &Model);
797 Features = getAvailableFeatures(ECX, EDX, MaxLeaf);
802 if (Vendor == SIG_INTEL) {
803 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, &Type,
811 if (Subtype == INTEL_PENTIUM_MMX)
812 return "pentium-mmx";
814 case INTEL_PENTIUM_PRO:
816 case INTEL_PENTIUM_II:
818 case INTEL_PENTIUM_III:
820 case INTEL_PENTIUM_IV:
822 case INTEL_PENTIUM_M:
837 case INTEL_COREI7_NEHALEM:
839 case INTEL_COREI7_WESTMERE:
841 case INTEL_COREI7_SANDYBRIDGE:
842 return "sandybridge";
843 case INTEL_COREI7_IVYBRIDGE:
845 case INTEL_COREI7_HASWELL:
847 case INTEL_COREI7_BROADWELL:
849 case INTEL_COREI7_SKYLAKE:
851 case INTEL_COREI7_SKYLAKE_AVX512:
852 return "skylake-avx512";
858 case INTEL_ATOM_BONNELL:
860 case INTEL_ATOM_SILVERMONT:
866 return "knl"; /*update for more variants added*/
876 } else if (Vendor == SIG_AMD) {
877 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
889 case AMDPENTIUM_GEODE:
896 case AMDATHLON_TBIRD:
897 return "athlon-tbird";
902 case AMDATHLON_K8SSE3:
904 case AMDATHLON_OPTERON:
914 if(Subtype == AMDFAM10H_BARCELONA)
921 case AMDFAM15H_BDVER1:
923 case AMDFAM15H_BDVER2:
925 case AMDFAM15H_BDVER3:
927 case AMDFAM15H_BDVER4:
950 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
951 StringRef sys::getHostCPUName() {
952 host_basic_info_data_t hostInfo;
953 mach_msg_type_number_t infoCount;
955 infoCount = HOST_BASIC_INFO_COUNT;
956 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
959 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
962 switch (hostInfo.cpu_subtype) {
963 case CPU_SUBTYPE_POWERPC_601:
965 case CPU_SUBTYPE_POWERPC_602:
967 case CPU_SUBTYPE_POWERPC_603:
969 case CPU_SUBTYPE_POWERPC_603e:
971 case CPU_SUBTYPE_POWERPC_603ev:
973 case CPU_SUBTYPE_POWERPC_604:
975 case CPU_SUBTYPE_POWERPC_604e:
977 case CPU_SUBTYPE_POWERPC_620:
979 case CPU_SUBTYPE_POWERPC_750:
981 case CPU_SUBTYPE_POWERPC_7400:
983 case CPU_SUBTYPE_POWERPC_7450:
985 case CPU_SUBTYPE_POWERPC_970:
992 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
993 StringRef sys::getHostCPUName() {
994 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
995 // and so we must use an operating-system interface to determine the current
996 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
997 const char *generic = "generic";
999 // The cpu line is second (after the 'processor: 0' line), so if this
1000 // buffer is too small then something has changed (or is wrong).
1002 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1003 if (CPUInfoSize == -1)
1006 const char *CPUInfoStart = buffer;
1007 const char *CPUInfoEnd = buffer + CPUInfoSize;
1009 const char *CIP = CPUInfoStart;
1011 const char *CPUStart = 0;
1014 // We need to find the first line which starts with cpu, spaces, and a colon.
1015 // After the colon, there may be some additional spaces and then the cpu type.
1016 while (CIP < CPUInfoEnd && CPUStart == 0) {
1017 if (CIP < CPUInfoEnd && *CIP == '\n')
1020 if (CIP < CPUInfoEnd && *CIP == 'c') {
1022 if (CIP < CPUInfoEnd && *CIP == 'p') {
1024 if (CIP < CPUInfoEnd && *CIP == 'u') {
1026 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
1029 if (CIP < CPUInfoEnd && *CIP == ':') {
1031 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
1034 if (CIP < CPUInfoEnd) {
1036 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
1037 *CIP != ',' && *CIP != '\n'))
1039 CPULen = CIP - CPUStart;
1047 while (CIP < CPUInfoEnd && *CIP != '\n')
1054 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
1055 .Case("604e", "604e")
1057 .Case("7400", "7400")
1058 .Case("7410", "7400")
1059 .Case("7447", "7400")
1060 .Case("7455", "7450")
1062 .Case("POWER4", "970")
1063 .Case("PPC970FX", "970")
1064 .Case("PPC970MP", "970")
1066 .Case("POWER5", "g5")
1068 .Case("POWER6", "pwr6")
1069 .Case("POWER7", "pwr7")
1070 .Case("POWER8", "pwr8")
1071 .Case("POWER8E", "pwr8")
1072 .Case("POWER9", "pwr9")
1075 #elif defined(__linux__) && defined(__arm__)
1076 StringRef sys::getHostCPUName() {
1077 // The cpuid register on arm is not accessible from user space. On Linux,
1078 // it is exposed through the /proc/cpuinfo file.
1080 // Read 1024 bytes from /proc/cpuinfo, which should contain the CPU part line
1083 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1084 if (CPUInfoSize == -1)
1087 StringRef Str(buffer, CPUInfoSize);
1089 SmallVector<StringRef, 32> Lines;
1090 Str.split(Lines, "\n");
1092 // Look for the CPU implementer line.
1093 StringRef Implementer;
1094 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1095 if (Lines[I].startswith("CPU implementer"))
1096 Implementer = Lines[I].substr(15).ltrim("\t :");
1098 if (Implementer == "0x41") // ARM Ltd.
1099 // Look for the CPU part line.
1100 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1101 if (Lines[I].startswith("CPU part"))
1102 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
1103 // values correspond to the "Part number" in the CP15/c0 register. The
1104 // contents are specified in the various processor manuals.
1105 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
1106 .Case("0x926", "arm926ej-s")
1107 .Case("0xb02", "mpcore")
1108 .Case("0xb36", "arm1136j-s")
1109 .Case("0xb56", "arm1156t2-s")
1110 .Case("0xb76", "arm1176jz-s")
1111 .Case("0xc08", "cortex-a8")
1112 .Case("0xc09", "cortex-a9")
1113 .Case("0xc0f", "cortex-a15")
1114 .Case("0xc20", "cortex-m0")
1115 .Case("0xc23", "cortex-m3")
1116 .Case("0xc24", "cortex-m4")
1117 .Default("generic");
1119 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
1120 // Look for the CPU part line.
1121 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1122 if (Lines[I].startswith("CPU part"))
1123 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
1124 // values correspond to the "Part number" in the CP15/c0 register. The
1125 // contents are specified in the various processor manuals.
1126 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
1127 .Case("0x06f", "krait") // APQ8064
1128 .Default("generic");
1132 #elif defined(__linux__) && defined(__s390x__)
1133 StringRef sys::getHostCPUName() {
1134 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
1136 // The "processor 0:" line comes after a fair amount of other information,
1137 // including a cache breakdown, but this should be plenty.
1139 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1140 if (CPUInfoSize == -1)
1143 StringRef Str(buffer, CPUInfoSize);
1144 SmallVector<StringRef, 32> Lines;
1145 Str.split(Lines, "\n");
1147 // Look for the CPU features.
1148 SmallVector<StringRef, 32> CPUFeatures;
1149 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1150 if (Lines[I].startswith("features")) {
1151 size_t Pos = Lines[I].find(":");
1152 if (Pos != StringRef::npos) {
1153 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
1158 // We need to check for the presence of vector support independently of
1159 // the machine type, since we may only use the vector register set when
1160 // supported by the kernel (and hypervisor).
1161 bool HaveVectorSupport = false;
1162 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1163 if (CPUFeatures[I] == "vx")
1164 HaveVectorSupport = true;
1167 // Now check the processor machine type.
1168 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
1169 if (Lines[I].startswith("processor ")) {
1170 size_t Pos = Lines[I].find("machine = ");
1171 if (Pos != StringRef::npos) {
1172 Pos += sizeof("machine = ") - 1;
1174 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
1175 if (Id >= 2964 && HaveVectorSupport)
1190 StringRef sys::getHostCPUName() { return "generic"; }
1193 #if defined(__linux__) && defined(__x86_64__)
1194 // On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1195 // using the number of unique physical/core id pairs. The following
1196 // implementation reads the /proc/cpuinfo format on an x86_64 system.
1197 static int computeHostNumPhysicalCores() {
1198 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1199 // mmapped because it appears to have 0 size.
1200 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1201 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1202 if (std::error_code EC = Text.getError()) {
1203 llvm::errs() << "Can't read "
1204 << "/proc/cpuinfo: " << EC.message() << "\n";
1206 SmallVector<StringRef, 8> strs;
1207 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1208 /*KeepEmpty=*/false);
1209 int CurPhysicalId = -1;
1211 SmallSet<std::pair<int, int>, 32> UniqueItems;
1212 for (auto &Line : strs) {
1214 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1216 std::pair<StringRef, StringRef> Data = Line.split(':');
1217 auto Name = Data.first.trim();
1218 auto Val = Data.second.trim();
1219 if (Name == "physical id") {
1220 assert(CurPhysicalId == -1 &&
1221 "Expected a core id before seeing another physical id");
1222 Val.getAsInteger(10, CurPhysicalId);
1224 if (Name == "core id") {
1225 assert(CurCoreId == -1 &&
1226 "Expected a physical id before seeing another core id");
1227 Val.getAsInteger(10, CurCoreId);
1229 if (CurPhysicalId != -1 && CurCoreId != -1) {
1230 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1235 return UniqueItems.size();
1237 #elif defined(__APPLE__) && defined(__x86_64__)
1238 #include <sys/param.h>
1239 #include <sys/sysctl.h>
1241 // Gets the number of *physical cores* on the machine.
1242 static int computeHostNumPhysicalCores() {
1244 size_t len = sizeof(count);
1245 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1249 nm[1] = HW_AVAILCPU;
1250 sysctl(nm, 2, &count, &len, NULL, 0);
1257 // On other systems, return -1 to indicate unknown.
1258 static int computeHostNumPhysicalCores() { return -1; }
1261 int sys::getHostNumPhysicalCores() {
1262 static int NumCores = computeHostNumPhysicalCores();
1266 #if defined(__i386__) || defined(_M_IX86) || \
1267 defined(__x86_64__) || defined(_M_X64)
1268 bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1269 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1276 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1280 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1282 Features["cmov"] = (EDX >> 15) & 1;
1283 Features["mmx"] = (EDX >> 23) & 1;
1284 Features["sse"] = (EDX >> 25) & 1;
1285 Features["sse2"] = (EDX >> 26) & 1;
1286 Features["sse3"] = (ECX >> 0) & 1;
1287 Features["ssse3"] = (ECX >> 9) & 1;
1288 Features["sse4.1"] = (ECX >> 19) & 1;
1289 Features["sse4.2"] = (ECX >> 20) & 1;
1291 Features["pclmul"] = (ECX >> 1) & 1;
1292 Features["cx16"] = (ECX >> 13) & 1;
1293 Features["movbe"] = (ECX >> 22) & 1;
1294 Features["popcnt"] = (ECX >> 23) & 1;
1295 Features["aes"] = (ECX >> 25) & 1;
1296 Features["rdrnd"] = (ECX >> 30) & 1;
1298 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1299 // indicates that the AVX registers will be saved and restored on context
1300 // switch, then we have full AVX support.
1301 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1302 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1303 Features["avx"] = HasAVXSave;
1304 Features["fma"] = HasAVXSave && (ECX >> 12) & 1;
1305 Features["f16c"] = HasAVXSave && (ECX >> 29) & 1;
1307 // Only enable XSAVE if OS has enabled support for saving YMM state.
1308 Features["xsave"] = HasAVXSave && (ECX >> 26) & 1;
1310 // AVX512 requires additional context to be saved by the OS.
1311 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1313 unsigned MaxExtLevel;
1314 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1316 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1317 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1318 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1319 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1320 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1321 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1322 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1323 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1324 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1327 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1329 // AVX2 is only supported if we have the OS save support from AVX.
1330 Features["avx2"] = HasAVXSave && HasLeaf7 && ((EBX >> 5) & 1);
1332 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1333 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1334 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1335 Features["hle"] = HasLeaf7 && ((EBX >> 4) & 1);
1336 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1337 Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1);
1338 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1339 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1340 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1341 Features["smap"] = HasLeaf7 && ((EBX >> 20) & 1);
1342 Features["pcommit"] = HasLeaf7 && ((EBX >> 22) & 1);
1343 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1344 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1345 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1347 // AVX512 is only supported if the OS supports the context save for it.
1348 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1349 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1350 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1351 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1352 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1353 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1354 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1355 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1357 Features["prefetchwt1"] = HasLeaf7 && (ECX & 1);
1358 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
1359 // Enable protection keys
1360 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1362 bool HasLeafD = MaxLevel >= 0xd &&
1363 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1365 // Only enable XSAVE if OS has enabled support for saving YMM state.
1366 Features["xsaveopt"] = HasAVXSave && HasLeafD && ((EAX >> 0) & 1);
1367 Features["xsavec"] = HasAVXSave && HasLeafD && ((EAX >> 1) & 1);
1368 Features["xsaves"] = HasAVXSave && HasLeafD && ((EAX >> 3) & 1);
1372 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1373 bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1374 // Read 1024 bytes from /proc/cpuinfo, which should contain the Features line
1377 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1378 if (CPUInfoSize == -1)
1381 StringRef Str(buffer, CPUInfoSize);
1383 SmallVector<StringRef, 32> Lines;
1384 Str.split(Lines, "\n");
1386 SmallVector<StringRef, 32> CPUFeatures;
1388 // Look for the CPU features.
1389 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1390 if (Lines[I].startswith("Features")) {
1391 Lines[I].split(CPUFeatures, ' ');
1395 #if defined(__aarch64__)
1396 // Keep track of which crypto features we have seen
1397 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1398 uint32_t crypto = 0;
1401 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1402 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1403 #if defined(__aarch64__)
1404 .Case("asimd", "neon")
1405 .Case("fp", "fp-armv8")
1406 .Case("crc32", "crc")
1408 .Case("half", "fp16")
1409 .Case("neon", "neon")
1410 .Case("vfpv3", "vfp3")
1411 .Case("vfpv3d16", "d16")
1412 .Case("vfpv4", "vfp4")
1413 .Case("idiva", "hwdiv-arm")
1414 .Case("idivt", "hwdiv")
1418 #if defined(__aarch64__)
1419 // We need to check crypto separately since we need all of the crypto
1420 // extensions to enable the subtarget feature
1421 if (CPUFeatures[I] == "aes")
1423 else if (CPUFeatures[I] == "pmull")
1424 crypto |= CAP_PMULL;
1425 else if (CPUFeatures[I] == "sha1")
1427 else if (CPUFeatures[I] == "sha2")
1431 if (LLVMFeatureStr != "")
1432 Features[LLVMFeatureStr] = true;
1435 #if defined(__aarch64__)
1436 // If we have all crypto bits we can add the feature
1437 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1438 Features["crypto"] = true;
1444 bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1447 std::string sys::getProcessTriple() {
1448 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
1450 if (sizeof(void *) == 8 && PT.isArch32Bit())
1451 PT = PT.get64BitArchVariant();
1452 if (sizeof(void *) == 4 && PT.isArch64Bit())
1453 PT = PT.get32BitArchVariant();