1 //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the operating system Host concept.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Host.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/X86TargetParser.h"
25 #include "llvm/Support/raw_ostream.h"
29 // Include the platform-specific parts of this class.
31 #include "Unix/Host.inc"
35 #include "Windows/Host.inc"
40 #if defined(__APPLE__) && (!defined(__x86_64__))
41 #include <mach/host_info.h>
42 #include <mach/mach.h>
43 #include <mach/mach_host.h>
44 #include <mach/machine.h>
47 #define DEBUG_TYPE "host-detection"
49 //===----------------------------------------------------------------------===//
51 // Implementations of the CPU detection routines
53 //===----------------------------------------------------------------------===//
57 static std::unique_ptr<llvm::MemoryBuffer>
58 LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent() {
59 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
60 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
61 if (std::error_code EC = Text.getError()) {
62 llvm::errs() << "Can't read "
63 << "/proc/cpuinfo: " << EC.message() << "\n";
66 return std::move(*Text);
69 StringRef sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent) {
70 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
71 // and so we must use an operating-system interface to determine the current
72 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
73 const char *generic = "generic";
75 // The cpu line is second (after the 'processor: 0' line), so if this
76 // buffer is too small then something has changed (or is wrong).
77 StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin();
78 StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end();
80 StringRef::const_iterator CIP = CPUInfoStart;
82 StringRef::const_iterator CPUStart = 0;
85 // We need to find the first line which starts with cpu, spaces, and a colon.
86 // After the colon, there may be some additional spaces and then the cpu type.
87 while (CIP < CPUInfoEnd && CPUStart == 0) {
88 if (CIP < CPUInfoEnd && *CIP == '\n')
91 if (CIP < CPUInfoEnd && *CIP == 'c') {
93 if (CIP < CPUInfoEnd && *CIP == 'p') {
95 if (CIP < CPUInfoEnd && *CIP == 'u') {
97 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
100 if (CIP < CPUInfoEnd && *CIP == ':') {
102 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
105 if (CIP < CPUInfoEnd) {
107 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
108 *CIP != ',' && *CIP != '\n'))
110 CPULen = CIP - CPUStart;
118 while (CIP < CPUInfoEnd && *CIP != '\n')
125 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
126 .Case("604e", "604e")
128 .Case("7400", "7400")
129 .Case("7410", "7400")
130 .Case("7447", "7400")
131 .Case("7455", "7450")
133 .Case("POWER4", "970")
134 .Case("PPC970FX", "970")
135 .Case("PPC970MP", "970")
137 .Case("POWER5", "g5")
139 .Case("POWER6", "pwr6")
140 .Case("POWER7", "pwr7")
141 .Case("POWER8", "pwr8")
142 .Case("POWER8E", "pwr8")
143 .Case("POWER8NVL", "pwr8")
144 .Case("POWER9", "pwr9")
145 .Case("POWER10", "pwr10")
146 // FIXME: If we get a simulator or machine with the capabilities of
147 // mcpu=future, we should revisit this and add the name reported by the
148 // simulator/machine.
152 StringRef sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent) {
153 // The cpuid register on arm is not accessible from user space. On Linux,
154 // it is exposed through the /proc/cpuinfo file.
156 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
158 SmallVector<StringRef, 32> Lines;
159 ProcCpuinfoContent.split(Lines, "\n");
161 // Look for the CPU implementer line.
162 StringRef Implementer;
165 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
166 if (Lines[I].startswith("CPU implementer"))
167 Implementer = Lines[I].substr(15).ltrim("\t :");
168 if (Lines[I].startswith("Hardware"))
169 Hardware = Lines[I].substr(8).ltrim("\t :");
170 if (Lines[I].startswith("CPU part"))
171 Part = Lines[I].substr(8).ltrim("\t :");
174 if (Implementer == "0x41") { // ARM Ltd.
175 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
176 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
177 if (Hardware.endswith("MSM8994") || Hardware.endswith("MSM8996"))
181 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
182 // values correspond to the "Part number" in the CP15/c0 register. The
183 // contents are specified in the various processor manuals.
184 // This corresponds to the Main ID Register in Technical Reference Manuals.
185 // and is used in programs like sys-utils
186 return StringSwitch<const char *>(Part)
187 .Case("0x926", "arm926ej-s")
188 .Case("0xb02", "mpcore")
189 .Case("0xb36", "arm1136j-s")
190 .Case("0xb56", "arm1156t2-s")
191 .Case("0xb76", "arm1176jz-s")
192 .Case("0xc08", "cortex-a8")
193 .Case("0xc09", "cortex-a9")
194 .Case("0xc0f", "cortex-a15")
195 .Case("0xc20", "cortex-m0")
196 .Case("0xc23", "cortex-m3")
197 .Case("0xc24", "cortex-m4")
198 .Case("0xd22", "cortex-m55")
199 .Case("0xd02", "cortex-a34")
200 .Case("0xd04", "cortex-a35")
201 .Case("0xd03", "cortex-a53")
202 .Case("0xd07", "cortex-a57")
203 .Case("0xd08", "cortex-a72")
204 .Case("0xd09", "cortex-a73")
205 .Case("0xd0a", "cortex-a75")
206 .Case("0xd0b", "cortex-a76")
207 .Case("0xd0d", "cortex-a77")
208 .Case("0xd41", "cortex-a78")
209 .Case("0xd44", "cortex-x1")
210 .Case("0xd0c", "neoverse-n1")
211 .Case("0xd49", "neoverse-n2")
215 if (Implementer == "0x42" || Implementer == "0x43") { // Broadcom | Cavium.
216 return StringSwitch<const char *>(Part)
217 .Case("0x516", "thunderx2t99")
218 .Case("0x0516", "thunderx2t99")
219 .Case("0xaf", "thunderx2t99")
220 .Case("0x0af", "thunderx2t99")
221 .Case("0xa1", "thunderxt88")
222 .Case("0x0a1", "thunderxt88")
226 if (Implementer == "0x46") { // Fujitsu Ltd.
227 return StringSwitch<const char *>(Part)
228 .Case("0x001", "a64fx")
232 if (Implementer == "0x4e") { // NVIDIA Corporation
233 return StringSwitch<const char *>(Part)
234 .Case("0x004", "carmel")
238 if (Implementer == "0x48") // HiSilicon Technologies, Inc.
239 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
240 // values correspond to the "Part number" in the CP15/c0 register. The
241 // contents are specified in the various processor manuals.
242 return StringSwitch<const char *>(Part)
243 .Case("0xd01", "tsv110")
246 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
247 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
248 // values correspond to the "Part number" in the CP15/c0 register. The
249 // contents are specified in the various processor manuals.
250 return StringSwitch<const char *>(Part)
251 .Case("0x06f", "krait") // APQ8064
252 .Case("0x201", "kryo")
253 .Case("0x205", "kryo")
254 .Case("0x211", "kryo")
255 .Case("0x800", "cortex-a73") // Kryo 2xx Gold
256 .Case("0x801", "cortex-a73") // Kryo 2xx Silver
257 .Case("0x802", "cortex-a75") // Kryo 3xx Gold
258 .Case("0x803", "cortex-a75") // Kryo 3xx Silver
259 .Case("0x804", "cortex-a76") // Kryo 4xx Gold
260 .Case("0x805", "cortex-a76") // Kryo 4xx/5xx Silver
261 .Case("0xc00", "falkor")
262 .Case("0xc01", "saphira")
264 if (Implementer == "0x53") { // Samsung Electronics Co., Ltd.
265 // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
266 // any predictive pattern across variants and parts.
267 unsigned Variant = 0, Part = 0;
269 // Look for the CPU variant line, whose value is a 1 digit hexadecimal
270 // number, corresponding to the Variant bits in the CP15/C0 register.
272 if (I.consume_front("CPU variant"))
273 I.ltrim("\t :").getAsInteger(0, Variant);
275 // Look for the CPU part line, whose value is a 3 digit hexadecimal
276 // number, corresponding to the PartNum bits in the CP15/C0 register.
278 if (I.consume_front("CPU part"))
279 I.ltrim("\t :").getAsInteger(0, Part);
281 unsigned Exynos = (Variant << 12) | Part;
284 // Default by falling through to Exynos M3.
296 StringRef sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent) {
297 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
299 // The "processor 0:" line comes after a fair amount of other information,
300 // including a cache breakdown, but this should be plenty.
301 SmallVector<StringRef, 32> Lines;
302 ProcCpuinfoContent.split(Lines, "\n");
304 // Look for the CPU features.
305 SmallVector<StringRef, 32> CPUFeatures;
306 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
307 if (Lines[I].startswith("features")) {
308 size_t Pos = Lines[I].find(':');
309 if (Pos != StringRef::npos) {
310 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
315 // We need to check for the presence of vector support independently of
316 // the machine type, since we may only use the vector register set when
317 // supported by the kernel (and hypervisor).
318 bool HaveVectorSupport = false;
319 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
320 if (CPUFeatures[I] == "vx")
321 HaveVectorSupport = true;
324 // Now check the processor machine type.
325 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
326 if (Lines[I].startswith("processor ")) {
327 size_t Pos = Lines[I].find("machine = ");
328 if (Pos != StringRef::npos) {
329 Pos += sizeof("machine = ") - 1;
331 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
332 if (Id >= 8561 && HaveVectorSupport)
334 if (Id >= 3906 && HaveVectorSupport)
336 if (Id >= 2964 && HaveVectorSupport)
351 StringRef sys::detail::getHostCPUNameForBPF() {
352 #if !defined(__linux__) || !defined(__x86_64__)
355 uint8_t v3_insns[40] __attribute__ ((aligned (8))) =
356 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
357 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
358 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
359 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
360 /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
361 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
362 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
363 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
364 /* BPF_EXIT_INSN() */
365 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
367 uint8_t v2_insns[40] __attribute__ ((aligned (8))) =
368 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
369 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
370 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
371 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
372 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
373 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
374 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
375 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
376 /* BPF_EXIT_INSN() */
377 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
379 struct bpf_prog_load_attr {
387 uint32_t kern_version;
390 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
392 attr.insns = (uint64_t)v3_insns;
393 attr.license = (uint64_t)"DUMMY";
395 int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr,
402 /* Clear the whole attr in case its content changed by syscall. */
403 memset(&attr, 0, sizeof(attr));
404 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
406 attr.insns = (uint64_t)v2_insns;
407 attr.license = (uint64_t)"DUMMY";
408 fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr));
417 #if defined(__i386__) || defined(_M_IX86) || \
418 defined(__x86_64__) || defined(_M_X64)
420 enum VendorSignatures {
421 SIG_INTEL = 0x756e6547 /* Genu */,
422 SIG_AMD = 0x68747541 /* Auth */
425 // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
426 // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
427 // support. Consequently, for i386, the presence of CPUID is checked first
428 // via the corresponding eflags bit.
429 // Removal of cpuid.h header motivated by PR30384
430 // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
431 // or test-suite, but are used in external projects e.g. libstdcxx
432 static bool isCpuIdSupported() {
433 #if defined(__GNUC__) || defined(__clang__)
434 #if defined(__i386__)
435 int __cpuid_supported;
438 " movl %%eax,%%ecx\n"
439 " xorl $0x00200000,%%eax\n"
445 " cmpl %%eax,%%ecx\n"
449 : "=r"(__cpuid_supported)
452 if (!__cpuid_supported)
460 /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
461 /// the specified arguments. If we can't run cpuid on the host, return true.
462 static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
463 unsigned *rECX, unsigned *rEDX) {
464 #if defined(__GNUC__) || defined(__clang__)
465 #if defined(__x86_64__)
466 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
467 // FIXME: should we save this for Clang?
468 __asm__("movq\t%%rbx, %%rsi\n\t"
470 "xchgq\t%%rbx, %%rsi\n\t"
471 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
474 #elif defined(__i386__)
475 __asm__("movl\t%%ebx, %%esi\n\t"
477 "xchgl\t%%ebx, %%esi\n\t"
478 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
484 #elif defined(_MSC_VER)
485 // The MSVC intrinsic is portable across x86 and x64.
487 __cpuid(registers, value);
488 *rEAX = registers[0];
489 *rEBX = registers[1];
490 *rECX = registers[2];
491 *rEDX = registers[3];
498 /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
499 /// the 4 values in the specified arguments. If we can't run cpuid on the host,
501 static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
502 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
504 #if defined(__GNUC__) || defined(__clang__)
505 #if defined(__x86_64__)
506 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
507 // FIXME: should we save this for Clang?
508 __asm__("movq\t%%rbx, %%rsi\n\t"
510 "xchgq\t%%rbx, %%rsi\n\t"
511 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
512 : "a"(value), "c"(subleaf));
514 #elif defined(__i386__)
515 __asm__("movl\t%%ebx, %%esi\n\t"
517 "xchgl\t%%ebx, %%esi\n\t"
518 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
519 : "a"(value), "c"(subleaf));
524 #elif defined(_MSC_VER)
526 __cpuidex(registers, value, subleaf);
527 *rEAX = registers[0];
528 *rEBX = registers[1];
529 *rECX = registers[2];
530 *rEDX = registers[3];
537 // Read control register 0 (XCR0). Used to detect features such as AVX.
538 static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
539 #if defined(__GNUC__) || defined(__clang__)
540 // Check xgetbv; this uses a .byte sequence instead of the instruction
541 // directly because older assemblers do not include support for xgetbv and
542 // there is no easy way to conditionally compile based on the assembler used.
543 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
545 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
546 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
548 *rEDX = Result >> 32;
555 static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
557 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
558 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
559 if (*Family == 6 || *Family == 0xf) {
561 // Examine extended family ID if family ID is F.
562 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
563 // Examine extended model ID if family ID is 6 or F.
564 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
569 getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
570 const unsigned *Features,
571 unsigned *Type, unsigned *Subtype) {
572 auto testFeature = [&](unsigned F) {
573 return (Features[F / 32] & (1U << (F % 32))) != 0;
586 if (testFeature(X86::FEATURE_MMX)) {
594 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
595 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
596 // mobile processor, Intel Core 2 Extreme processor, Intel
597 // Pentium Dual-Core processor, Intel Xeon processor, model
598 // 0Fh. All processors are manufactured using the 65 nm process.
599 case 0x16: // Intel Celeron processor model 16h. All processors are
600 // manufactured using the 65 nm process
602 *Type = X86::INTEL_CORE2;
604 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
605 // 17h. All processors are manufactured using the 45 nm process.
607 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
608 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
609 // the 45 nm process.
611 *Type = X86::INTEL_CORE2;
613 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
614 // processors are manufactured using the 45 nm process.
615 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
616 // As found in a Summer 2010 model iMac.
618 case 0x2e: // Nehalem EX
620 *Type = X86::INTEL_COREI7;
621 *Subtype = X86::INTEL_COREI7_NEHALEM;
623 case 0x25: // Intel Core i7, laptop version.
624 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
625 // processors are manufactured using the 32 nm process.
626 case 0x2f: // Westmere EX
628 *Type = X86::INTEL_COREI7;
629 *Subtype = X86::INTEL_COREI7_WESTMERE;
631 case 0x2a: // Intel Core i7 processor. All processors are manufactured
632 // using the 32 nm process.
635 *Type = X86::INTEL_COREI7;
636 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
639 case 0x3e: // Ivy Bridge EP
641 *Type = X86::INTEL_COREI7;
642 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
651 *Type = X86::INTEL_COREI7;
652 *Subtype = X86::INTEL_COREI7_HASWELL;
661 *Type = X86::INTEL_COREI7;
662 *Subtype = X86::INTEL_COREI7_BROADWELL;
666 case 0x4e: // Skylake mobile
667 case 0x5e: // Skylake desktop
668 case 0x8e: // Kaby Lake mobile
669 case 0x9e: // Kaby Lake desktop
670 case 0xa5: // Comet Lake-H/S
671 case 0xa6: // Comet Lake-U
673 *Type = X86::INTEL_COREI7;
674 *Subtype = X86::INTEL_COREI7_SKYLAKE;
679 *Type = X86::INTEL_COREI7;
680 if (testFeature(X86::FEATURE_AVX512BF16)) {
682 *Subtype = X86::INTEL_COREI7_COOPERLAKE;
683 } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
685 *Subtype = X86::INTEL_COREI7_CASCADELAKE;
687 CPU = "skylake-avx512";
688 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
695 *Type = X86::INTEL_COREI7;
696 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
702 CPU = "icelake-client";
703 *Type = X86::INTEL_COREI7;
704 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
710 CPU = "icelake-server";
711 *Type = X86::INTEL_COREI7;
712 *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER;
717 CPU = "sapphirerapids";
718 *Type = X86::INTEL_COREI7;
719 *Subtype = X86::INTEL_COREI7_SAPPHIRERAPIDS;
722 case 0x1c: // Most 45 nm Intel Atom processors
723 case 0x26: // 45 nm Atom Lincroft
724 case 0x27: // 32 nm Atom Medfield
725 case 0x35: // 32 nm Atom Midview
726 case 0x36: // 32 nm Atom Midview
728 *Type = X86::INTEL_BONNELL;
731 // Atom Silvermont codes from the Intel software optimization guide.
737 case 0x4c: // really airmont
739 *Type = X86::INTEL_SILVERMONT;
742 case 0x5c: // Apollo Lake
743 case 0x5f: // Denverton
745 *Type = X86::INTEL_GOLDMONT;
748 CPU = "goldmont-plus";
749 *Type = X86::INTEL_GOLDMONT_PLUS;
753 *Type = X86::INTEL_TREMONT;
756 // Xeon Phi (Knights Landing + Knights Mill):
759 *Type = X86::INTEL_KNL;
763 *Type = X86::INTEL_KNM;
766 default: // Unknown family 6 CPU, try to guess.
767 // Don't both with Type/Subtype here, they aren't used by the caller.
768 // They're used above to keep the code in sync with compiler-rt.
769 // TODO detect tigerlake host from model
770 if (testFeature(X86::FEATURE_AVX512VP2INTERSECT)) {
772 } else if (testFeature(X86::FEATURE_AVX512VBMI2)) {
773 CPU = "icelake-client";
774 } else if (testFeature(X86::FEATURE_AVX512VBMI)) {
776 } else if (testFeature(X86::FEATURE_AVX512BF16)) {
778 } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
780 } else if (testFeature(X86::FEATURE_AVX512VL)) {
781 CPU = "skylake-avx512";
782 } else if (testFeature(X86::FEATURE_AVX512ER)) {
784 } else if (testFeature(X86::FEATURE_CLFLUSHOPT)) {
785 if (testFeature(X86::FEATURE_SHA))
789 } else if (testFeature(X86::FEATURE_ADX)) {
791 } else if (testFeature(X86::FEATURE_AVX2)) {
793 } else if (testFeature(X86::FEATURE_AVX)) {
795 } else if (testFeature(X86::FEATURE_SSE4_2)) {
796 if (testFeature(X86::FEATURE_MOVBE))
800 } else if (testFeature(X86::FEATURE_SSE4_1)) {
802 } else if (testFeature(X86::FEATURE_SSSE3)) {
803 if (testFeature(X86::FEATURE_MOVBE))
807 } else if (testFeature(X86::FEATURE_64BIT)) {
809 } else if (testFeature(X86::FEATURE_SSE3)) {
811 } else if (testFeature(X86::FEATURE_SSE2)) {
813 } else if (testFeature(X86::FEATURE_SSE)) {
815 } else if (testFeature(X86::FEATURE_MMX)) {
824 if (testFeature(X86::FEATURE_64BIT)) {
828 if (testFeature(X86::FEATURE_SSE3)) {
843 getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
844 const unsigned *Features,
845 unsigned *Type, unsigned *Subtype) {
846 auto testFeature = [&](unsigned F) {
847 return (Features[F / 32] & (1U << (F % 32))) != 0;
876 if (testFeature(X86::FEATURE_SSE)) {
883 if (testFeature(X86::FEATURE_SSE3)) {
891 *Type = X86::AMDFAM10H; // "amdfam10"
894 *Subtype = X86::AMDFAM10H_BARCELONA;
897 *Subtype = X86::AMDFAM10H_SHANGHAI;
900 *Subtype = X86::AMDFAM10H_ISTANBUL;
906 *Type = X86::AMD_BTVER1;
910 *Type = X86::AMDFAM15H;
911 if (Model >= 0x60 && Model <= 0x7f) {
913 *Subtype = X86::AMDFAM15H_BDVER4;
914 break; // 60h-7Fh: Excavator
916 if (Model >= 0x30 && Model <= 0x3f) {
918 *Subtype = X86::AMDFAM15H_BDVER3;
919 break; // 30h-3Fh: Steamroller
921 if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
923 *Subtype = X86::AMDFAM15H_BDVER2;
924 break; // 02h, 10h-1Fh: Piledriver
927 *Subtype = X86::AMDFAM15H_BDVER1;
928 break; // 00h-0Fh: Bulldozer
933 *Type = X86::AMD_BTVER2;
937 *Type = X86::AMDFAM17H;
938 if ((Model >= 0x30 && Model <= 0x3f) || Model == 0x71) {
940 *Subtype = X86::AMDFAM17H_ZNVER2;
941 break; // 30h-3fh, 71h: Zen2
944 *Subtype = X86::AMDFAM17H_ZNVER1;
945 break; // 00h-0Fh: Zen1
950 *Type = X86::AMDFAM19H;
952 *Subtype = X86::AMDFAM19H_ZNVER3;
953 break; // 00h-0Fh: Zen3
957 break; // Unknown AMD CPU.
963 static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
964 unsigned *Features) {
967 auto setFeature = [&](unsigned F) {
968 Features[F / 32] |= 1U << (F % 32);
972 setFeature(X86::FEATURE_CMOV);
974 setFeature(X86::FEATURE_MMX);
976 setFeature(X86::FEATURE_SSE);
978 setFeature(X86::FEATURE_SSE2);
981 setFeature(X86::FEATURE_SSE3);
983 setFeature(X86::FEATURE_PCLMUL);
985 setFeature(X86::FEATURE_SSSE3);
987 setFeature(X86::FEATURE_FMA);
989 setFeature(X86::FEATURE_SSE4_1);
991 setFeature(X86::FEATURE_SSE4_2);
993 setFeature(X86::FEATURE_POPCNT);
995 setFeature(X86::FEATURE_AES);
998 setFeature(X86::FEATURE_MOVBE);
1000 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1001 // indicates that the AVX registers will be saved and restored on context
1002 // switch, then we have full AVX support.
1003 const unsigned AVXBits = (1 << 27) | (1 << 28);
1004 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
1005 ((EAX & 0x6) == 0x6);
1006 #if defined(__APPLE__)
1007 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1008 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1010 bool HasAVX512Save = true;
1012 // AVX512 requires additional context to be saved by the OS.
1013 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
1017 setFeature(X86::FEATURE_AVX);
1020 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1022 if (HasLeaf7 && ((EBX >> 3) & 1))
1023 setFeature(X86::FEATURE_BMI);
1024 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
1025 setFeature(X86::FEATURE_AVX2);
1026 if (HasLeaf7 && ((EBX >> 8) & 1))
1027 setFeature(X86::FEATURE_BMI2);
1028 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
1029 setFeature(X86::FEATURE_AVX512F);
1030 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
1031 setFeature(X86::FEATURE_AVX512DQ);
1032 if (HasLeaf7 && ((EBX >> 19) & 1))
1033 setFeature(X86::FEATURE_ADX);
1034 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
1035 setFeature(X86::FEATURE_AVX512IFMA);
1036 if (HasLeaf7 && ((EBX >> 23) & 1))
1037 setFeature(X86::FEATURE_CLFLUSHOPT);
1038 if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
1039 setFeature(X86::FEATURE_AVX512PF);
1040 if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
1041 setFeature(X86::FEATURE_AVX512ER);
1042 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
1043 setFeature(X86::FEATURE_AVX512CD);
1044 if (HasLeaf7 && ((EBX >> 29) & 1))
1045 setFeature(X86::FEATURE_SHA);
1046 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
1047 setFeature(X86::FEATURE_AVX512BW);
1048 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
1049 setFeature(X86::FEATURE_AVX512VL);
1051 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
1052 setFeature(X86::FEATURE_AVX512VBMI);
1053 if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
1054 setFeature(X86::FEATURE_AVX512VBMI2);
1055 if (HasLeaf7 && ((ECX >> 8) & 1))
1056 setFeature(X86::FEATURE_GFNI);
1057 if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
1058 setFeature(X86::FEATURE_VPCLMULQDQ);
1059 if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
1060 setFeature(X86::FEATURE_AVX512VNNI);
1061 if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
1062 setFeature(X86::FEATURE_AVX512BITALG);
1063 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
1064 setFeature(X86::FEATURE_AVX512VPOPCNTDQ);
1066 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
1067 setFeature(X86::FEATURE_AVX5124VNNIW);
1068 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
1069 setFeature(X86::FEATURE_AVX5124FMAPS);
1070 if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)
1071 setFeature(X86::FEATURE_AVX512VP2INTERSECT);
1073 bool HasLeaf7Subleaf1 =
1074 MaxLeaf >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1075 if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
1076 setFeature(X86::FEATURE_AVX512BF16);
1078 unsigned MaxExtLevel;
1079 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1081 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1082 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1083 if (HasExtLeaf1 && ((ECX >> 6) & 1))
1084 setFeature(X86::FEATURE_SSE4_A);
1085 if (HasExtLeaf1 && ((ECX >> 11) & 1))
1086 setFeature(X86::FEATURE_XOP);
1087 if (HasExtLeaf1 && ((ECX >> 16) & 1))
1088 setFeature(X86::FEATURE_FMA4);
1090 if (HasExtLeaf1 && ((EDX >> 29) & 1))
1091 setFeature(X86::FEATURE_64BIT);
1094 StringRef sys::getHostCPUName() {
1095 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1096 unsigned MaxLeaf, Vendor;
1098 if (!isCpuIdSupported())
1101 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
1103 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
1105 unsigned Family = 0, Model = 0;
1106 unsigned Features[(X86::CPU_FEATURE_MAX + 31) / 32] = {0};
1107 detectX86FamilyModel(EAX, &Family, &Model);
1108 getAvailableFeatures(ECX, EDX, MaxLeaf, Features);
1110 // These aren't consumed in this file, but we try to keep some source code the
1111 // same or similar to compiler-rt.
1113 unsigned Subtype = 0;
1117 if (Vendor == SIG_INTEL) {
1118 CPU = getIntelProcessorTypeAndSubtype(Family, Model, Features, &Type,
1120 } else if (Vendor == SIG_AMD) {
1121 CPU = getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type,
1131 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1132 StringRef sys::getHostCPUName() {
1133 host_basic_info_data_t hostInfo;
1134 mach_msg_type_number_t infoCount;
1136 infoCount = HOST_BASIC_INFO_COUNT;
1137 mach_port_t hostPort = mach_host_self();
1138 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1140 mach_port_deallocate(mach_task_self(), hostPort);
1142 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1145 switch (hostInfo.cpu_subtype) {
1146 case CPU_SUBTYPE_POWERPC_601:
1148 case CPU_SUBTYPE_POWERPC_602:
1150 case CPU_SUBTYPE_POWERPC_603:
1152 case CPU_SUBTYPE_POWERPC_603e:
1154 case CPU_SUBTYPE_POWERPC_603ev:
1156 case CPU_SUBTYPE_POWERPC_604:
1158 case CPU_SUBTYPE_POWERPC_604e:
1160 case CPU_SUBTYPE_POWERPC_620:
1162 case CPU_SUBTYPE_POWERPC_750:
1164 case CPU_SUBTYPE_POWERPC_7400:
1166 case CPU_SUBTYPE_POWERPC_7450:
1168 case CPU_SUBTYPE_POWERPC_970:
1175 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1176 StringRef sys::getHostCPUName() {
1177 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1178 StringRef Content = P ? P->getBuffer() : "";
1179 return detail::getHostCPUNameForPowerPC(Content);
1181 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1182 StringRef sys::getHostCPUName() {
1183 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1184 StringRef Content = P ? P->getBuffer() : "";
1185 return detail::getHostCPUNameForARM(Content);
1187 #elif defined(__linux__) && defined(__s390x__)
1188 StringRef sys::getHostCPUName() {
1189 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1190 StringRef Content = P ? P->getBuffer() : "";
1191 return detail::getHostCPUNameForS390x(Content);
1193 #elif defined(__APPLE__) && defined(__aarch64__)
1194 StringRef sys::getHostCPUName() {
1197 #elif defined(__APPLE__) && defined(__arm__)
1198 StringRef sys::getHostCPUName() {
1199 host_basic_info_data_t hostInfo;
1200 mach_msg_type_number_t infoCount;
1202 infoCount = HOST_BASIC_INFO_COUNT;
1203 mach_port_t hostPort = mach_host_self();
1204 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1206 mach_port_deallocate(mach_task_self(), hostPort);
1208 if (hostInfo.cpu_type != CPU_TYPE_ARM) {
1209 assert(false && "CPUType not equal to ARM should not be possible on ARM");
1212 switch (hostInfo.cpu_subtype) {
1213 case CPU_SUBTYPE_ARM_V7S:
1221 StringRef sys::getHostCPUName() { return "generic"; }
1224 #if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
1225 // On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1226 // using the number of unique physical/core id pairs. The following
1227 // implementation reads the /proc/cpuinfo format on an x86_64 system.
1228 int computeHostNumPhysicalCores() {
1229 // Enabled represents the number of physical id/core id pairs with at least
1230 // one processor id enabled by the CPU affinity mask.
1231 cpu_set_t Affinity, Enabled;
1232 if (sched_getaffinity(0, sizeof(Affinity), &Affinity) != 0)
1236 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1237 // mmapped because it appears to have 0 size.
1238 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1239 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1240 if (std::error_code EC = Text.getError()) {
1241 llvm::errs() << "Can't read "
1242 << "/proc/cpuinfo: " << EC.message() << "\n";
1245 SmallVector<StringRef, 8> strs;
1246 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1247 /*KeepEmpty=*/false);
1248 int CurProcessor = -1;
1249 int CurPhysicalId = -1;
1250 int CurSiblings = -1;
1252 for (StringRef Line : strs) {
1253 std::pair<StringRef, StringRef> Data = Line.split(':');
1254 auto Name = Data.first.trim();
1255 auto Val = Data.second.trim();
1256 // These fields are available if the kernel is configured with CONFIG_SMP.
1257 if (Name == "processor")
1258 Val.getAsInteger(10, CurProcessor);
1259 else if (Name == "physical id")
1260 Val.getAsInteger(10, CurPhysicalId);
1261 else if (Name == "siblings")
1262 Val.getAsInteger(10, CurSiblings);
1263 else if (Name == "core id") {
1264 Val.getAsInteger(10, CurCoreId);
1265 // The processor id corresponds to an index into cpu_set_t.
1266 if (CPU_ISSET(CurProcessor, &Affinity))
1267 CPU_SET(CurPhysicalId * CurSiblings + CurCoreId, &Enabled);
1270 return CPU_COUNT(&Enabled);
1272 #elif defined(__linux__) && defined(__powerpc__)
1273 int computeHostNumPhysicalCores() {
1275 if (sched_getaffinity(0, sizeof(Affinity), &Affinity) == 0)
1276 return CPU_COUNT(&Affinity);
1278 // The call to sched_getaffinity() may have failed because the Affinity
1279 // mask is too small for the number of CPU's on the system (i.e. the
1280 // system has more than 1024 CPUs). Allocate a mask large enough for
1281 // twice as many CPUs.
1282 cpu_set_t *DynAffinity;
1283 DynAffinity = CPU_ALLOC(2048);
1284 if (sched_getaffinity(0, CPU_ALLOC_SIZE(2048), DynAffinity) == 0) {
1285 int NumCPUs = CPU_COUNT(DynAffinity);
1286 CPU_FREE(DynAffinity);
1291 #elif defined(__linux__) && defined(__s390x__)
1292 int computeHostNumPhysicalCores() { return sysconf(_SC_NPROCESSORS_ONLN); }
1293 #elif defined(__APPLE__) && defined(__x86_64__)
1294 #include <sys/param.h>
1295 #include <sys/sysctl.h>
1297 // Gets the number of *physical cores* on the machine.
1298 int computeHostNumPhysicalCores() {
1300 size_t len = sizeof(count);
1301 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1305 nm[1] = HW_AVAILCPU;
1306 sysctl(nm, 2, &count, &len, NULL, 0);
1312 #elif defined(__MVS__)
1313 int computeHostNumPhysicalCores() {
1315 // Byte offset of the pointer to the Communications Vector Table (CVT) in
1316 // the Prefixed Save Area (PSA). The table entry is a 31-bit pointer and
1317 // will be zero-extended to uintptr_t.
1319 // Byte offset of the pointer to the Common System Data Area (CSD) in the
1320 // CVT. The table entry is a 31-bit pointer and will be zero-extended to
1323 // Byte offset to the number of live CPs in the LPAR, stored as a signed
1324 // 32-bit value in the table.
1325 CSD_NUMBER_ONLINE_STANDARD_CPS = 264,
1328 char *CVT = reinterpret_cast<char *>(
1329 static_cast<uintptr_t>(reinterpret_cast<unsigned int &>(PSA[FLCCVT])));
1330 char *CSD = reinterpret_cast<char *>(
1331 static_cast<uintptr_t>(reinterpret_cast<unsigned int &>(CVT[CVTCSD])));
1332 return reinterpret_cast<int &>(CSD[CSD_NUMBER_ONLINE_STANDARD_CPS]);
1334 #elif defined(_WIN32) && LLVM_ENABLE_THREADS != 0
1335 // Defined in llvm/lib/Support/Windows/Threading.inc
1336 int computeHostNumPhysicalCores();
1338 // On other systems, return -1 to indicate unknown.
1339 static int computeHostNumPhysicalCores() { return -1; }
1342 int sys::getHostNumPhysicalCores() {
1343 static int NumCores = computeHostNumPhysicalCores();
1347 #if defined(__i386__) || defined(_M_IX86) || \
1348 defined(__x86_64__) || defined(_M_X64)
1349 bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1350 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1353 if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
1356 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1358 Features["cx8"] = (EDX >> 8) & 1;
1359 Features["cmov"] = (EDX >> 15) & 1;
1360 Features["mmx"] = (EDX >> 23) & 1;
1361 Features["fxsr"] = (EDX >> 24) & 1;
1362 Features["sse"] = (EDX >> 25) & 1;
1363 Features["sse2"] = (EDX >> 26) & 1;
1365 Features["sse3"] = (ECX >> 0) & 1;
1366 Features["pclmul"] = (ECX >> 1) & 1;
1367 Features["ssse3"] = (ECX >> 9) & 1;
1368 Features["cx16"] = (ECX >> 13) & 1;
1369 Features["sse4.1"] = (ECX >> 19) & 1;
1370 Features["sse4.2"] = (ECX >> 20) & 1;
1371 Features["movbe"] = (ECX >> 22) & 1;
1372 Features["popcnt"] = (ECX >> 23) & 1;
1373 Features["aes"] = (ECX >> 25) & 1;
1374 Features["rdrnd"] = (ECX >> 30) & 1;
1376 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1377 // indicates that the AVX registers will be saved and restored on context
1378 // switch, then we have full AVX support.
1379 bool HasXSave = ((ECX >> 27) & 1) && !getX86XCR0(&EAX, &EDX);
1380 bool HasAVXSave = HasXSave && ((ECX >> 28) & 1) && ((EAX & 0x6) == 0x6);
1381 #if defined(__APPLE__)
1382 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1383 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1385 bool HasAVX512Save = true;
1387 // AVX512 requires additional context to be saved by the OS.
1388 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1390 // AMX requires additional context to be saved by the OS.
1391 const unsigned AMXBits = (1 << 17) | (1 << 18);
1392 bool HasAMXSave = HasXSave && ((EAX & AMXBits) == AMXBits);
1394 Features["avx"] = HasAVXSave;
1395 Features["fma"] = ((ECX >> 12) & 1) && HasAVXSave;
1396 // Only enable XSAVE if OS has enabled support for saving YMM state.
1397 Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
1398 Features["f16c"] = ((ECX >> 29) & 1) && HasAVXSave;
1400 unsigned MaxExtLevel;
1401 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1403 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1404 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1405 Features["sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1);
1406 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1407 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1408 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1409 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1410 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
1411 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1412 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1413 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1415 Features["64bit"] = HasExtLeaf1 && ((EDX >> 29) & 1);
1417 // Miscellaneous memory related features, detected by
1418 // using the 0x80000008 leaf of the CPUID instruction
1419 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1420 !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);
1421 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1422 Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1);
1425 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1427 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1428 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1429 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1430 // AVX2 is only supported if we have the OS save support from AVX.
1431 Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave;
1432 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1433 Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1);
1434 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1435 // AVX512 is only supported if the OS supports the context save for it.
1436 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1437 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1438 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1439 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1440 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1441 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1442 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1443 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1444 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1445 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1446 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1447 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1448 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1450 Features["prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1);
1451 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
1452 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1453 Features["waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1);
1454 Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
1455 Features["shstk"] = HasLeaf7 && ((ECX >> 7) & 1);
1456 Features["gfni"] = HasLeaf7 && ((ECX >> 8) & 1);
1457 Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
1458 Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1459 Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
1460 Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
1461 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
1462 Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);
1463 Features["kl"] = HasLeaf7 && ((ECX >> 23) & 1); // key locker
1464 Features["cldemote"] = HasLeaf7 && ((ECX >> 25) & 1);
1465 Features["movdiri"] = HasLeaf7 && ((ECX >> 27) & 1);
1466 Features["movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1);
1467 Features["enqcmd"] = HasLeaf7 && ((ECX >> 29) & 1);
1469 Features["uintr"] = HasLeaf7 && ((EDX >> 5) & 1);
1470 Features["avx512vp2intersect"] =
1471 HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save;
1472 Features["serialize"] = HasLeaf7 && ((EDX >> 14) & 1);
1473 Features["tsxldtrk"] = HasLeaf7 && ((EDX >> 16) & 1);
1474 // There are two CPUID leafs which information associated with the pconfig
1476 // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
1477 // bit of EDX), while the EAX=0x1b leaf returns information on the
1478 // availability of specific pconfig leafs.
1479 // The target feature here only refers to the the first of these two.
1480 // Users might need to check for the availability of specific pconfig
1481 // leaves using cpuid, since that information is ignored while
1482 // detecting features using the "-march=native" flag.
1483 // For more info, see X86 ISA docs.
1484 Features["pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
1485 Features["amx-bf16"] = HasLeaf7 && ((EDX >> 22) & 1) && HasAMXSave;
1486 Features["amx-tile"] = HasLeaf7 && ((EDX >> 24) & 1) && HasAMXSave;
1487 Features["amx-int8"] = HasLeaf7 && ((EDX >> 25) & 1) && HasAMXSave;
1488 bool HasLeaf7Subleaf1 =
1489 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1490 Features["avxvnni"] = HasLeaf7Subleaf1 && ((EAX >> 4) & 1) && HasAVXSave;
1491 Features["avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save;
1492 Features["hreset"] = HasLeaf7Subleaf1 && ((EAX >> 22) & 1);
1494 bool HasLeafD = MaxLevel >= 0xd &&
1495 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1497 // Only enable XSAVE if OS has enabled support for saving YMM state.
1498 Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1499 Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1500 Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
1502 bool HasLeaf14 = MaxLevel >= 0x14 &&
1503 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX);
1505 Features["ptwrite"] = HasLeaf14 && ((EBX >> 4) & 1);
1508 MaxLevel >= 0x19 && !getX86CpuIDAndInfo(0x19, &EAX, &EBX, &ECX, &EDX);
1509 Features["widekl"] = HasLeaf7 && HasLeaf19 && ((EBX >> 2) & 1);
1513 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1514 bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1515 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1519 SmallVector<StringRef, 32> Lines;
1520 P->getBuffer().split(Lines, "\n");
1522 SmallVector<StringRef, 32> CPUFeatures;
1524 // Look for the CPU features.
1525 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1526 if (Lines[I].startswith("Features")) {
1527 Lines[I].split(CPUFeatures, ' ');
1531 #if defined(__aarch64__)
1532 // Keep track of which crypto features we have seen
1533 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1534 uint32_t crypto = 0;
1537 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1538 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1539 #if defined(__aarch64__)
1540 .Case("asimd", "neon")
1541 .Case("fp", "fp-armv8")
1542 .Case("crc32", "crc")
1544 .Case("half", "fp16")
1545 .Case("neon", "neon")
1546 .Case("vfpv3", "vfp3")
1547 .Case("vfpv3d16", "d16")
1548 .Case("vfpv4", "vfp4")
1549 .Case("idiva", "hwdiv-arm")
1550 .Case("idivt", "hwdiv")
1554 #if defined(__aarch64__)
1555 // We need to check crypto separately since we need all of the crypto
1556 // extensions to enable the subtarget feature
1557 if (CPUFeatures[I] == "aes")
1559 else if (CPUFeatures[I] == "pmull")
1560 crypto |= CAP_PMULL;
1561 else if (CPUFeatures[I] == "sha1")
1563 else if (CPUFeatures[I] == "sha2")
1567 if (LLVMFeatureStr != "")
1568 Features[LLVMFeatureStr] = true;
1571 #if defined(__aarch64__)
1572 // If we have all crypto bits we can add the feature
1573 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1574 Features["crypto"] = true;
1579 #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
1580 bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1581 if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
1582 Features["neon"] = true;
1583 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
1584 Features["crc"] = true;
1585 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
1586 Features["crypto"] = true;
1591 bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1594 std::string sys::getProcessTriple() {
1595 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1596 Triple PT(Triple::normalize(TargetTripleString));
1598 if (sizeof(void *) == 8 && PT.isArch32Bit())
1599 PT = PT.get64BitArchVariant();
1600 if (sizeof(void *) == 4 && PT.isArch64Bit())
1601 PT = PT.get32BitArchVariant();