1 //===-- Host.cpp - Implement OS Host Detection ------------------*- 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 detection.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/TargetParser/Host.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/TargetParser/Triple.h"
22 #include "llvm/TargetParser/X86TargetParser.h"
25 // Include the platform-specific parts of this class.
27 #include "Unix/Host.inc"
31 #include "Windows/Host.inc"
37 #include "llvm/Support/BCD.h"
39 #if defined(__APPLE__)
40 #include <mach/host_info.h>
41 #include <mach/mach.h>
42 #include <mach/mach_host.h>
43 #include <mach/machine.h>
44 #include <sys/param.h>
45 #include <sys/sysctl.h>
48 #include <sys/systemcfg.h>
50 #if defined(__sun__) && defined(__svr4__)
54 #define DEBUG_TYPE "host-detection"
56 //===----------------------------------------------------------------------===//
58 // Implementations of the CPU detection routines
60 //===----------------------------------------------------------------------===//
64 static std::unique_ptr
<llvm::MemoryBuffer
>
65 LLVM_ATTRIBUTE_UNUSED
getProcCpuinfoContent() {
66 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> Text
=
67 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
68 if (std::error_code EC
= Text
.getError()) {
69 llvm::errs() << "Can't read "
70 << "/proc/cpuinfo: " << EC
.message() << "\n";
73 return std::move(*Text
);
76 StringRef
sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent
) {
77 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
78 // and so we must use an operating-system interface to determine the current
79 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
80 const char *generic
= "generic";
82 // The cpu line is second (after the 'processor: 0' line), so if this
83 // buffer is too small then something has changed (or is wrong).
84 StringRef::const_iterator CPUInfoStart
= ProcCpuinfoContent
.begin();
85 StringRef::const_iterator CPUInfoEnd
= ProcCpuinfoContent
.end();
87 StringRef::const_iterator CIP
= CPUInfoStart
;
89 StringRef::const_iterator CPUStart
= nullptr;
92 // We need to find the first line which starts with cpu, spaces, and a colon.
93 // After the colon, there may be some additional spaces and then the cpu type.
94 while (CIP
< CPUInfoEnd
&& CPUStart
== nullptr) {
95 if (CIP
< CPUInfoEnd
&& *CIP
== '\n')
98 if (CIP
< CPUInfoEnd
&& *CIP
== 'c') {
100 if (CIP
< CPUInfoEnd
&& *CIP
== 'p') {
102 if (CIP
< CPUInfoEnd
&& *CIP
== 'u') {
104 while (CIP
< CPUInfoEnd
&& (*CIP
== ' ' || *CIP
== '\t'))
107 if (CIP
< CPUInfoEnd
&& *CIP
== ':') {
109 while (CIP
< CPUInfoEnd
&& (*CIP
== ' ' || *CIP
== '\t'))
112 if (CIP
< CPUInfoEnd
) {
114 while (CIP
< CPUInfoEnd
&& (*CIP
!= ' ' && *CIP
!= '\t' &&
115 *CIP
!= ',' && *CIP
!= '\n'))
117 CPULen
= CIP
- CPUStart
;
124 if (CPUStart
== nullptr)
125 while (CIP
< CPUInfoEnd
&& *CIP
!= '\n')
129 if (CPUStart
== nullptr)
132 return StringSwitch
<const char *>(StringRef(CPUStart
, CPULen
))
133 .Case("604e", "604e")
135 .Case("7400", "7400")
136 .Case("7410", "7400")
137 .Case("7447", "7400")
138 .Case("7455", "7450")
140 .Case("POWER4", "970")
141 .Case("PPC970FX", "970")
142 .Case("PPC970MP", "970")
144 .Case("POWER5", "g5")
146 .Case("POWER6", "pwr6")
147 .Case("POWER7", "pwr7")
148 .Case("POWER8", "pwr8")
149 .Case("POWER8E", "pwr8")
150 .Case("POWER8NVL", "pwr8")
151 .Case("POWER9", "pwr9")
152 .Case("POWER10", "pwr10")
153 // FIXME: If we get a simulator or machine with the capabilities of
154 // mcpu=future, we should revisit this and add the name reported by the
155 // simulator/machine.
159 StringRef
sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent
) {
160 // The cpuid register on arm is not accessible from user space. On Linux,
161 // it is exposed through the /proc/cpuinfo file.
163 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
165 SmallVector
<StringRef
, 32> Lines
;
166 ProcCpuinfoContent
.split(Lines
, "\n");
168 // Look for the CPU implementer line.
169 StringRef Implementer
;
172 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
173 if (Lines
[I
].starts_with("CPU implementer"))
174 Implementer
= Lines
[I
].substr(15).ltrim("\t :");
175 if (Lines
[I
].starts_with("Hardware"))
176 Hardware
= Lines
[I
].substr(8).ltrim("\t :");
177 if (Lines
[I
].starts_with("CPU part"))
178 Part
= Lines
[I
].substr(8).ltrim("\t :");
181 if (Implementer
== "0x41") { // ARM Ltd.
182 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
183 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
184 if (Hardware
.ends_with("MSM8994") || Hardware
.ends_with("MSM8996"))
188 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
189 // values correspond to the "Part number" in the CP15/c0 register. The
190 // contents are specified in the various processor manuals.
191 // This corresponds to the Main ID Register in Technical Reference Manuals.
192 // and is used in programs like sys-utils
193 return StringSwitch
<const char *>(Part
)
194 .Case("0x926", "arm926ej-s")
195 .Case("0xb02", "mpcore")
196 .Case("0xb36", "arm1136j-s")
197 .Case("0xb56", "arm1156t2-s")
198 .Case("0xb76", "arm1176jz-s")
199 .Case("0xc08", "cortex-a8")
200 .Case("0xc09", "cortex-a9")
201 .Case("0xc0f", "cortex-a15")
202 .Case("0xc20", "cortex-m0")
203 .Case("0xc23", "cortex-m3")
204 .Case("0xc24", "cortex-m4")
205 .Case("0xd24", "cortex-m52")
206 .Case("0xd22", "cortex-m55")
207 .Case("0xd02", "cortex-a34")
208 .Case("0xd04", "cortex-a35")
209 .Case("0xd03", "cortex-a53")
210 .Case("0xd05", "cortex-a55")
211 .Case("0xd46", "cortex-a510")
212 .Case("0xd80", "cortex-a520")
213 .Case("0xd07", "cortex-a57")
214 .Case("0xd08", "cortex-a72")
215 .Case("0xd09", "cortex-a73")
216 .Case("0xd0a", "cortex-a75")
217 .Case("0xd0b", "cortex-a76")
218 .Case("0xd0d", "cortex-a77")
219 .Case("0xd41", "cortex-a78")
220 .Case("0xd47", "cortex-a710")
221 .Case("0xd4d", "cortex-a715")
222 .Case("0xd81", "cortex-a720")
223 .Case("0xd44", "cortex-x1")
224 .Case("0xd4c", "cortex-x1c")
225 .Case("0xd48", "cortex-x2")
226 .Case("0xd4e", "cortex-x3")
227 .Case("0xd82", "cortex-x4")
228 .Case("0xd0c", "neoverse-n1")
229 .Case("0xd49", "neoverse-n2")
230 .Case("0xd40", "neoverse-v1")
231 .Case("0xd4f", "neoverse-v2")
235 if (Implementer
== "0x42" || Implementer
== "0x43") { // Broadcom | Cavium.
236 return StringSwitch
<const char *>(Part
)
237 .Case("0x516", "thunderx2t99")
238 .Case("0x0516", "thunderx2t99")
239 .Case("0xaf", "thunderx2t99")
240 .Case("0x0af", "thunderx2t99")
241 .Case("0xa1", "thunderxt88")
242 .Case("0x0a1", "thunderxt88")
246 if (Implementer
== "0x46") { // Fujitsu Ltd.
247 return StringSwitch
<const char *>(Part
)
248 .Case("0x001", "a64fx")
252 if (Implementer
== "0x4e") { // NVIDIA Corporation
253 return StringSwitch
<const char *>(Part
)
254 .Case("0x004", "carmel")
258 if (Implementer
== "0x48") // HiSilicon Technologies, Inc.
259 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
260 // values correspond to the "Part number" in the CP15/c0 register. The
261 // contents are specified in the various processor manuals.
262 return StringSwitch
<const char *>(Part
)
263 .Case("0xd01", "tsv110")
266 if (Implementer
== "0x51") // Qualcomm Technologies, Inc.
267 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
268 // values correspond to the "Part number" in the CP15/c0 register. The
269 // contents are specified in the various processor manuals.
270 return StringSwitch
<const char *>(Part
)
271 .Case("0x06f", "krait") // APQ8064
272 .Case("0x201", "kryo")
273 .Case("0x205", "kryo")
274 .Case("0x211", "kryo")
275 .Case("0x800", "cortex-a73") // Kryo 2xx Gold
276 .Case("0x801", "cortex-a73") // Kryo 2xx Silver
277 .Case("0x802", "cortex-a75") // Kryo 3xx Gold
278 .Case("0x803", "cortex-a75") // Kryo 3xx Silver
279 .Case("0x804", "cortex-a76") // Kryo 4xx Gold
280 .Case("0x805", "cortex-a76") // Kryo 4xx/5xx Silver
281 .Case("0xc00", "falkor")
282 .Case("0xc01", "saphira")
284 if (Implementer
== "0x53") { // Samsung Electronics Co., Ltd.
285 // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
286 // any predictive pattern across variants and parts.
287 unsigned Variant
= 0, Part
= 0;
289 // Look for the CPU variant line, whose value is a 1 digit hexadecimal
290 // number, corresponding to the Variant bits in the CP15/C0 register.
292 if (I
.consume_front("CPU variant"))
293 I
.ltrim("\t :").getAsInteger(0, Variant
);
295 // Look for the CPU part line, whose value is a 3 digit hexadecimal
296 // number, corresponding to the PartNum bits in the CP15/C0 register.
298 if (I
.consume_front("CPU part"))
299 I
.ltrim("\t :").getAsInteger(0, Part
);
301 unsigned Exynos
= (Variant
<< 12) | Part
;
304 // Default by falling through to Exynos M3.
313 if (Implementer
== "0x6d") { // Microsoft Corporation.
314 // The Microsoft Azure Cobalt 100 CPU is handled as a Neoverse N2.
315 return StringSwitch
<const char *>(Part
)
316 .Case("0xd49", "neoverse-n2")
320 if (Implementer
== "0xc0") { // Ampere Computing
321 return StringSwitch
<const char *>(Part
)
322 .Case("0xac3", "ampere1")
323 .Case("0xac4", "ampere1a")
324 .Case("0xac5", "ampere1b")
332 StringRef
getCPUNameFromS390Model(unsigned int Id
, bool HaveVectorSupport
) {
334 case 2064: // z900 not supported by LLVM
336 case 2084: // z990 not supported by LLVM
338 case 2094: // z9-109 not supported by LLVM
352 return HaveVectorSupport
? "z13" : "zEC12";
355 return HaveVectorSupport
? "z14" : "zEC12";
358 return HaveVectorSupport
? "z15" : "zEC12";
362 return HaveVectorSupport
? "z16" : "zEC12";
365 } // end anonymous namespace
367 StringRef
sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent
) {
368 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
370 // The "processor 0:" line comes after a fair amount of other information,
371 // including a cache breakdown, but this should be plenty.
372 SmallVector
<StringRef
, 32> Lines
;
373 ProcCpuinfoContent
.split(Lines
, "\n");
375 // Look for the CPU features.
376 SmallVector
<StringRef
, 32> CPUFeatures
;
377 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
378 if (Lines
[I
].starts_with("features")) {
379 size_t Pos
= Lines
[I
].find(':');
380 if (Pos
!= StringRef::npos
) {
381 Lines
[I
].drop_front(Pos
+ 1).split(CPUFeatures
, ' ');
386 // We need to check for the presence of vector support independently of
387 // the machine type, since we may only use the vector register set when
388 // supported by the kernel (and hypervisor).
389 bool HaveVectorSupport
= false;
390 for (unsigned I
= 0, E
= CPUFeatures
.size(); I
!= E
; ++I
) {
391 if (CPUFeatures
[I
] == "vx")
392 HaveVectorSupport
= true;
395 // Now check the processor machine type.
396 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
397 if (Lines
[I
].starts_with("processor ")) {
398 size_t Pos
= Lines
[I
].find("machine = ");
399 if (Pos
!= StringRef::npos
) {
400 Pos
+= sizeof("machine = ") - 1;
402 if (!Lines
[I
].drop_front(Pos
).getAsInteger(10, Id
))
403 return getCPUNameFromS390Model(Id
, HaveVectorSupport
);
412 StringRef
sys::detail::getHostCPUNameForRISCV(StringRef ProcCpuinfoContent
) {
413 // There are 24 lines in /proc/cpuinfo
414 SmallVector
<StringRef
> Lines
;
415 ProcCpuinfoContent
.split(Lines
, "\n");
417 // Look for uarch line to determine cpu name
419 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
420 if (Lines
[I
].starts_with("uarch")) {
421 UArch
= Lines
[I
].substr(5).ltrim("\t :");
426 return StringSwitch
<const char *>(UArch
)
427 .Case("sifive,u74-mc", "sifive-u74")
428 .Case("sifive,bullet0", "sifive-u74")
432 StringRef
sys::detail::getHostCPUNameForBPF() {
433 #if !defined(__linux__) || !defined(__x86_64__)
436 uint8_t v3_insns
[40] __attribute__ ((aligned (8))) =
437 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
438 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
439 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
440 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
441 /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
442 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
443 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
444 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
445 /* BPF_EXIT_INSN() */
446 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
448 uint8_t v2_insns
[40] __attribute__ ((aligned (8))) =
449 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
450 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
451 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
452 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
453 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
454 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
455 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
456 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
457 /* BPF_EXIT_INSN() */
458 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
460 struct bpf_prog_load_attr
{
468 uint32_t kern_version
;
471 attr
.prog_type
= 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
473 attr
.insns
= (uint64_t)v3_insns
;
474 attr
.license
= (uint64_t)"DUMMY";
476 int fd
= syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr
,
483 /* Clear the whole attr in case its content changed by syscall. */
484 memset(&attr
, 0, sizeof(attr
));
485 attr
.prog_type
= 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
487 attr
.insns
= (uint64_t)v2_insns
;
488 attr
.license
= (uint64_t)"DUMMY";
489 fd
= syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr
, sizeof(attr
));
498 #if defined(__i386__) || defined(_M_IX86) || \
499 defined(__x86_64__) || defined(_M_X64)
501 // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
502 // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
503 // support. Consequently, for i386, the presence of CPUID is checked first
504 // via the corresponding eflags bit.
505 // Removal of cpuid.h header motivated by PR30384
506 // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
507 // or test-suite, but are used in external projects e.g. libstdcxx
508 static bool isCpuIdSupported() {
509 #if defined(__GNUC__) || defined(__clang__)
510 #if defined(__i386__)
511 int __cpuid_supported
;
514 " movl %%eax,%%ecx\n"
515 " xorl $0x00200000,%%eax\n"
521 " cmpl %%eax,%%ecx\n"
525 : "=r"(__cpuid_supported
)
528 if (!__cpuid_supported
)
536 /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
537 /// the specified arguments. If we can't run cpuid on the host, return true.
538 static bool getX86CpuIDAndInfo(unsigned value
, unsigned *rEAX
, unsigned *rEBX
,
539 unsigned *rECX
, unsigned *rEDX
) {
540 #if defined(__GNUC__) || defined(__clang__)
541 #if defined(__x86_64__)
542 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
543 // FIXME: should we save this for Clang?
544 __asm__("movq\t%%rbx, %%rsi\n\t"
546 "xchgq\t%%rbx, %%rsi\n\t"
547 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
550 #elif defined(__i386__)
551 __asm__("movl\t%%ebx, %%esi\n\t"
553 "xchgl\t%%ebx, %%esi\n\t"
554 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
560 #elif defined(_MSC_VER)
561 // The MSVC intrinsic is portable across x86 and x64.
563 __cpuid(registers
, value
);
564 *rEAX
= registers
[0];
565 *rEBX
= registers
[1];
566 *rECX
= registers
[2];
567 *rEDX
= registers
[3];
579 VendorSignatures
getVendorSignature(unsigned *MaxLeaf
) {
580 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
581 if (MaxLeaf
== nullptr)
586 if (!isCpuIdSupported())
587 return VendorSignatures::UNKNOWN
;
589 if (getX86CpuIDAndInfo(0, MaxLeaf
, &EBX
, &ECX
, &EDX
) || *MaxLeaf
< 1)
590 return VendorSignatures::UNKNOWN
;
593 if (EBX
== 0x756e6547 && EDX
== 0x49656e69 && ECX
== 0x6c65746e)
594 return VendorSignatures::GENUINE_INTEL
;
597 if (EBX
== 0x68747541 && EDX
== 0x69746e65 && ECX
== 0x444d4163)
598 return VendorSignatures::AUTHENTIC_AMD
;
600 return VendorSignatures::UNKNOWN
;
604 } // namespace detail
608 using namespace llvm::sys::detail::x86
;
610 /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
611 /// the 4 values in the specified arguments. If we can't run cpuid on the host,
613 static bool getX86CpuIDAndInfoEx(unsigned value
, unsigned subleaf
,
614 unsigned *rEAX
, unsigned *rEBX
, unsigned *rECX
,
616 #if defined(__GNUC__) || defined(__clang__)
617 #if defined(__x86_64__)
618 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
619 // FIXME: should we save this for Clang?
620 __asm__("movq\t%%rbx, %%rsi\n\t"
622 "xchgq\t%%rbx, %%rsi\n\t"
623 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
624 : "a"(value
), "c"(subleaf
));
626 #elif defined(__i386__)
627 __asm__("movl\t%%ebx, %%esi\n\t"
629 "xchgl\t%%ebx, %%esi\n\t"
630 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
631 : "a"(value
), "c"(subleaf
));
636 #elif defined(_MSC_VER)
638 __cpuidex(registers
, value
, subleaf
);
639 *rEAX
= registers
[0];
640 *rEBX
= registers
[1];
641 *rECX
= registers
[2];
642 *rEDX
= registers
[3];
649 // Read control register 0 (XCR0). Used to detect features such as AVX.
650 static bool getX86XCR0(unsigned *rEAX
, unsigned *rEDX
) {
651 #if defined(__GNUC__) || defined(__clang__)
652 // Check xgetbv; this uses a .byte sequence instead of the instruction
653 // directly because older assemblers do not include support for xgetbv and
654 // there is no easy way to conditionally compile based on the assembler used.
655 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX
), "=d"(*rEDX
) : "c"(0));
657 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
658 unsigned long long Result
= _xgetbv(_XCR_XFEATURE_ENABLED_MASK
);
660 *rEDX
= Result
>> 32;
667 static void detectX86FamilyModel(unsigned EAX
, unsigned *Family
,
669 *Family
= (EAX
>> 8) & 0xf; // Bits 8 - 11
670 *Model
= (EAX
>> 4) & 0xf; // Bits 4 - 7
671 if (*Family
== 6 || *Family
== 0xf) {
673 // Examine extended family ID if family ID is F.
674 *Family
+= (EAX
>> 20) & 0xff; // Bits 20 - 27
675 // Examine extended model ID if family ID is 6 or F.
676 *Model
+= ((EAX
>> 16) & 0xf) << 4; // Bits 16 - 19
681 getIntelProcessorTypeAndSubtype(unsigned Family
, unsigned Model
,
682 const unsigned *Features
,
683 unsigned *Type
, unsigned *Subtype
) {
684 auto testFeature
= [&](unsigned F
) {
685 return (Features
[F
/ 32] & (1U << (F
% 32))) != 0;
698 if (testFeature(X86::FEATURE_MMX
)) {
706 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
707 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
708 // mobile processor, Intel Core 2 Extreme processor, Intel
709 // Pentium Dual-Core processor, Intel Xeon processor, model
710 // 0Fh. All processors are manufactured using the 65 nm process.
711 case 0x16: // Intel Celeron processor model 16h. All processors are
712 // manufactured using the 65 nm process
714 *Type
= X86::INTEL_CORE2
;
716 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
717 // 17h. All processors are manufactured using the 45 nm process.
719 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
720 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
721 // the 45 nm process.
723 *Type
= X86::INTEL_CORE2
;
725 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
726 // processors are manufactured using the 45 nm process.
727 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
728 // As found in a Summer 2010 model iMac.
730 case 0x2e: // Nehalem EX
732 *Type
= X86::INTEL_COREI7
;
733 *Subtype
= X86::INTEL_COREI7_NEHALEM
;
735 case 0x25: // Intel Core i7, laptop version.
736 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
737 // processors are manufactured using the 32 nm process.
738 case 0x2f: // Westmere EX
740 *Type
= X86::INTEL_COREI7
;
741 *Subtype
= X86::INTEL_COREI7_WESTMERE
;
743 case 0x2a: // Intel Core i7 processor. All processors are manufactured
744 // using the 32 nm process.
747 *Type
= X86::INTEL_COREI7
;
748 *Subtype
= X86::INTEL_COREI7_SANDYBRIDGE
;
751 case 0x3e: // Ivy Bridge EP
753 *Type
= X86::INTEL_COREI7
;
754 *Subtype
= X86::INTEL_COREI7_IVYBRIDGE
;
763 *Type
= X86::INTEL_COREI7
;
764 *Subtype
= X86::INTEL_COREI7_HASWELL
;
773 *Type
= X86::INTEL_COREI7
;
774 *Subtype
= X86::INTEL_COREI7_BROADWELL
;
778 case 0x4e: // Skylake mobile
779 case 0x5e: // Skylake desktop
780 case 0x8e: // Kaby Lake mobile
781 case 0x9e: // Kaby Lake desktop
782 case 0xa5: // Comet Lake-H/S
783 case 0xa6: // Comet Lake-U
785 *Type
= X86::INTEL_COREI7
;
786 *Subtype
= X86::INTEL_COREI7_SKYLAKE
;
792 *Type
= X86::INTEL_COREI7
;
793 *Subtype
= X86::INTEL_COREI7_ROCKETLAKE
;
798 *Type
= X86::INTEL_COREI7
;
799 if (testFeature(X86::FEATURE_AVX512BF16
)) {
801 *Subtype
= X86::INTEL_COREI7_COOPERLAKE
;
802 } else if (testFeature(X86::FEATURE_AVX512VNNI
)) {
804 *Subtype
= X86::INTEL_COREI7_CASCADELAKE
;
806 CPU
= "skylake-avx512";
807 *Subtype
= X86::INTEL_COREI7_SKYLAKE_AVX512
;
814 *Type
= X86::INTEL_COREI7
;
815 *Subtype
= X86::INTEL_COREI7_CANNONLAKE
;
821 CPU
= "icelake-client";
822 *Type
= X86::INTEL_COREI7
;
823 *Subtype
= X86::INTEL_COREI7_ICELAKE_CLIENT
;
830 *Type
= X86::INTEL_COREI7
;
831 *Subtype
= X86::INTEL_COREI7_TIGERLAKE
;
847 *Type
= X86::INTEL_COREI7
;
848 *Subtype
= X86::INTEL_COREI7_ALDERLAKE
;
854 *Type
= X86::INTEL_COREI7
;
855 *Subtype
= X86::INTEL_COREI7_ARROWLAKE
;
863 *Type
= X86::INTEL_COREI7
;
864 *Subtype
= X86::INTEL_COREI7_ARROWLAKE_S
;
870 *Type
= X86::INTEL_COREI7
;
871 *Subtype
= X86::INTEL_COREI7_PANTHERLAKE
;
876 CPU
= "graniterapids";
877 *Type
= X86::INTEL_COREI7
;
878 *Subtype
= X86::INTEL_COREI7_GRANITERAPIDS
;
883 CPU
= "graniterapids-d";
884 *Type
= X86::INTEL_COREI7
;
885 *Subtype
= X86::INTEL_COREI7_GRANITERAPIDS_D
;
891 CPU
= "icelake-server";
892 *Type
= X86::INTEL_COREI7
;
893 *Subtype
= X86::INTEL_COREI7_ICELAKE_SERVER
;
900 CPU
= "sapphirerapids";
901 *Type
= X86::INTEL_COREI7
;
902 *Subtype
= X86::INTEL_COREI7_SAPPHIRERAPIDS
;
905 case 0x1c: // Most 45 nm Intel Atom processors
906 case 0x26: // 45 nm Atom Lincroft
907 case 0x27: // 32 nm Atom Medfield
908 case 0x35: // 32 nm Atom Midview
909 case 0x36: // 32 nm Atom Midview
911 *Type
= X86::INTEL_BONNELL
;
914 // Atom Silvermont codes from the Intel software optimization guide.
920 case 0x4c: // really airmont
922 *Type
= X86::INTEL_SILVERMONT
;
925 case 0x5c: // Apollo Lake
926 case 0x5f: // Denverton
928 *Type
= X86::INTEL_GOLDMONT
;
931 CPU
= "goldmont-plus";
932 *Type
= X86::INTEL_GOLDMONT_PLUS
;
935 case 0x8a: // Lakefield
936 case 0x96: // Elkhart Lake
937 case 0x9c: // Jasper Lake
939 *Type
= X86::INTEL_TREMONT
;
944 CPU
= "sierraforest";
945 *Type
= X86::INTEL_SIERRAFOREST
;
951 *Type
= X86::INTEL_GRANDRIDGE
;
956 CPU
= "clearwaterforest";
957 *Type
= X86::INTEL_CLEARWATERFOREST
;
960 // Xeon Phi (Knights Landing + Knights Mill):
963 *Type
= X86::INTEL_KNL
;
967 *Type
= X86::INTEL_KNM
;
970 default: // Unknown family 6 CPU, try to guess.
971 // Don't both with Type/Subtype here, they aren't used by the caller.
972 // They're used above to keep the code in sync with compiler-rt.
973 // TODO detect tigerlake host from model
974 if (testFeature(X86::FEATURE_AVX512VP2INTERSECT
)) {
976 } else if (testFeature(X86::FEATURE_AVX512VBMI2
)) {
977 CPU
= "icelake-client";
978 } else if (testFeature(X86::FEATURE_AVX512VBMI
)) {
980 } else if (testFeature(X86::FEATURE_AVX512BF16
)) {
982 } else if (testFeature(X86::FEATURE_AVX512VNNI
)) {
984 } else if (testFeature(X86::FEATURE_AVX512VL
)) {
985 CPU
= "skylake-avx512";
986 } else if (testFeature(X86::FEATURE_AVX512ER
)) {
988 } else if (testFeature(X86::FEATURE_CLFLUSHOPT
)) {
989 if (testFeature(X86::FEATURE_SHA
))
993 } else if (testFeature(X86::FEATURE_ADX
)) {
995 } else if (testFeature(X86::FEATURE_AVX2
)) {
997 } else if (testFeature(X86::FEATURE_AVX
)) {
999 } else if (testFeature(X86::FEATURE_SSE4_2
)) {
1000 if (testFeature(X86::FEATURE_MOVBE
))
1004 } else if (testFeature(X86::FEATURE_SSE4_1
)) {
1006 } else if (testFeature(X86::FEATURE_SSSE3
)) {
1007 if (testFeature(X86::FEATURE_MOVBE
))
1011 } else if (testFeature(X86::FEATURE_64BIT
)) {
1013 } else if (testFeature(X86::FEATURE_SSE3
)) {
1015 } else if (testFeature(X86::FEATURE_SSE2
)) {
1017 } else if (testFeature(X86::FEATURE_SSE
)) {
1019 } else if (testFeature(X86::FEATURE_MMX
)) {
1028 if (testFeature(X86::FEATURE_64BIT
)) {
1032 if (testFeature(X86::FEATURE_SSE3
)) {
1047 getAMDProcessorTypeAndSubtype(unsigned Family
, unsigned Model
,
1048 const unsigned *Features
,
1049 unsigned *Type
, unsigned *Subtype
) {
1050 auto testFeature
= [&](unsigned F
) {
1051 return (Features
[F
/ 32] & (1U << (F
% 32))) != 0;
1080 if (testFeature(X86::FEATURE_SSE
)) {
1087 if (testFeature(X86::FEATURE_SSE3
)) {
1095 *Type
= X86::AMDFAM10H
; // "amdfam10"
1098 *Subtype
= X86::AMDFAM10H_BARCELONA
;
1101 *Subtype
= X86::AMDFAM10H_SHANGHAI
;
1104 *Subtype
= X86::AMDFAM10H_ISTANBUL
;
1110 *Type
= X86::AMD_BTVER1
;
1114 *Type
= X86::AMDFAM15H
;
1115 if (Model
>= 0x60 && Model
<= 0x7f) {
1117 *Subtype
= X86::AMDFAM15H_BDVER4
;
1118 break; // 60h-7Fh: Excavator
1120 if (Model
>= 0x30 && Model
<= 0x3f) {
1122 *Subtype
= X86::AMDFAM15H_BDVER3
;
1123 break; // 30h-3Fh: Steamroller
1125 if ((Model
>= 0x10 && Model
<= 0x1f) || Model
== 0x02) {
1127 *Subtype
= X86::AMDFAM15H_BDVER2
;
1128 break; // 02h, 10h-1Fh: Piledriver
1130 if (Model
<= 0x0f) {
1131 *Subtype
= X86::AMDFAM15H_BDVER1
;
1132 break; // 00h-0Fh: Bulldozer
1137 *Type
= X86::AMD_BTVER2
;
1141 *Type
= X86::AMDFAM17H
;
1142 if ((Model
>= 0x30 && Model
<= 0x3f) || (Model
== 0x47) ||
1143 (Model
>= 0x60 && Model
<= 0x67) || (Model
>= 0x68 && Model
<= 0x6f) ||
1144 (Model
>= 0x70 && Model
<= 0x7f) || (Model
>= 0x84 && Model
<= 0x87) ||
1145 (Model
>= 0x90 && Model
<= 0x97) || (Model
>= 0x98 && Model
<= 0x9f) ||
1146 (Model
>= 0xa0 && Model
<= 0xaf)) {
1147 // Family 17h Models 30h-3Fh (Starship) Zen 2
1148 // Family 17h Models 47h (Cardinal) Zen 2
1149 // Family 17h Models 60h-67h (Renoir) Zen 2
1150 // Family 17h Models 68h-6Fh (Lucienne) Zen 2
1151 // Family 17h Models 70h-7Fh (Matisse) Zen 2
1152 // Family 17h Models 84h-87h (ProjectX) Zen 2
1153 // Family 17h Models 90h-97h (VanGogh) Zen 2
1154 // Family 17h Models 98h-9Fh (Mero) Zen 2
1155 // Family 17h Models A0h-AFh (Mendocino) Zen 2
1157 *Subtype
= X86::AMDFAM17H_ZNVER2
;
1160 if ((Model
>= 0x10 && Model
<= 0x1f) || (Model
>= 0x20 && Model
<= 0x2f)) {
1161 // Family 17h Models 10h-1Fh (Raven1) Zen
1162 // Family 17h Models 10h-1Fh (Picasso) Zen+
1163 // Family 17h Models 20h-2Fh (Raven2 x86) Zen
1164 *Subtype
= X86::AMDFAM17H_ZNVER1
;
1170 *Type
= X86::AMDFAM19H
;
1171 if (Model
<= 0x0f || (Model
>= 0x20 && Model
<= 0x2f) ||
1172 (Model
>= 0x30 && Model
<= 0x3f) || (Model
>= 0x40 && Model
<= 0x4f) ||
1173 (Model
>= 0x50 && Model
<= 0x5f)) {
1174 // Family 19h Models 00h-0Fh (Genesis, Chagall) Zen 3
1175 // Family 19h Models 20h-2Fh (Vermeer) Zen 3
1176 // Family 19h Models 30h-3Fh (Badami) Zen 3
1177 // Family 19h Models 40h-4Fh (Rembrandt) Zen 3+
1178 // Family 19h Models 50h-5Fh (Cezanne) Zen 3
1179 *Subtype
= X86::AMDFAM19H_ZNVER3
;
1182 if ((Model
>= 0x10 && Model
<= 0x1f) || (Model
>= 0x60 && Model
<= 0x6f) ||
1183 (Model
>= 0x70 && Model
<= 0x77) || (Model
>= 0x78 && Model
<= 0x7f) ||
1184 (Model
>= 0xa0 && Model
<= 0xaf)) {
1185 // Family 19h Models 10h-1Fh (Stones; Storm Peak) Zen 4
1186 // Family 19h Models 60h-6Fh (Raphael) Zen 4
1187 // Family 19h Models 70h-77h (Phoenix, Hawkpoint1) Zen 4
1188 // Family 19h Models 78h-7Fh (Phoenix 2, Hawkpoint2) Zen 4
1189 // Family 19h Models A0h-AFh (Stones-Dense) Zen 4
1191 *Subtype
= X86::AMDFAM19H_ZNVER4
;
1196 break; // Unknown AMD CPU.
1202 static void getAvailableFeatures(unsigned ECX
, unsigned EDX
, unsigned MaxLeaf
,
1203 unsigned *Features
) {
1206 auto setFeature
= [&](unsigned F
) {
1207 Features
[F
/ 32] |= 1U << (F
% 32);
1210 if ((EDX
>> 15) & 1)
1211 setFeature(X86::FEATURE_CMOV
);
1212 if ((EDX
>> 23) & 1)
1213 setFeature(X86::FEATURE_MMX
);
1214 if ((EDX
>> 25) & 1)
1215 setFeature(X86::FEATURE_SSE
);
1216 if ((EDX
>> 26) & 1)
1217 setFeature(X86::FEATURE_SSE2
);
1220 setFeature(X86::FEATURE_SSE3
);
1222 setFeature(X86::FEATURE_PCLMUL
);
1224 setFeature(X86::FEATURE_SSSE3
);
1225 if ((ECX
>> 12) & 1)
1226 setFeature(X86::FEATURE_FMA
);
1227 if ((ECX
>> 19) & 1)
1228 setFeature(X86::FEATURE_SSE4_1
);
1229 if ((ECX
>> 20) & 1) {
1230 setFeature(X86::FEATURE_SSE4_2
);
1231 setFeature(X86::FEATURE_CRC32
);
1233 if ((ECX
>> 23) & 1)
1234 setFeature(X86::FEATURE_POPCNT
);
1235 if ((ECX
>> 25) & 1)
1236 setFeature(X86::FEATURE_AES
);
1238 if ((ECX
>> 22) & 1)
1239 setFeature(X86::FEATURE_MOVBE
);
1241 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1242 // indicates that the AVX registers will be saved and restored on context
1243 // switch, then we have full AVX support.
1244 const unsigned AVXBits
= (1 << 27) | (1 << 28);
1245 bool HasAVX
= ((ECX
& AVXBits
) == AVXBits
) && !getX86XCR0(&EAX
, &EDX
) &&
1246 ((EAX
& 0x6) == 0x6);
1247 #if defined(__APPLE__)
1248 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1249 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1251 bool HasAVX512Save
= true;
1253 // AVX512 requires additional context to be saved by the OS.
1254 bool HasAVX512Save
= HasAVX
&& ((EAX
& 0xe0) == 0xe0);
1258 setFeature(X86::FEATURE_AVX
);
1261 MaxLeaf
>= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
1263 if (HasLeaf7
&& ((EBX
>> 3) & 1))
1264 setFeature(X86::FEATURE_BMI
);
1265 if (HasLeaf7
&& ((EBX
>> 5) & 1) && HasAVX
)
1266 setFeature(X86::FEATURE_AVX2
);
1267 if (HasLeaf7
&& ((EBX
>> 8) & 1))
1268 setFeature(X86::FEATURE_BMI2
);
1269 if (HasLeaf7
&& ((EBX
>> 16) & 1) && HasAVX512Save
)
1270 setFeature(X86::FEATURE_AVX512F
);
1271 if (HasLeaf7
&& ((EBX
>> 17) & 1) && HasAVX512Save
)
1272 setFeature(X86::FEATURE_AVX512DQ
);
1273 if (HasLeaf7
&& ((EBX
>> 19) & 1))
1274 setFeature(X86::FEATURE_ADX
);
1275 if (HasLeaf7
&& ((EBX
>> 21) & 1) && HasAVX512Save
)
1276 setFeature(X86::FEATURE_AVX512IFMA
);
1277 if (HasLeaf7
&& ((EBX
>> 23) & 1))
1278 setFeature(X86::FEATURE_CLFLUSHOPT
);
1279 if (HasLeaf7
&& ((EBX
>> 26) & 1) && HasAVX512Save
)
1280 setFeature(X86::FEATURE_AVX512PF
);
1281 if (HasLeaf7
&& ((EBX
>> 27) & 1) && HasAVX512Save
)
1282 setFeature(X86::FEATURE_AVX512ER
);
1283 if (HasLeaf7
&& ((EBX
>> 28) & 1) && HasAVX512Save
)
1284 setFeature(X86::FEATURE_AVX512CD
);
1285 if (HasLeaf7
&& ((EBX
>> 29) & 1))
1286 setFeature(X86::FEATURE_SHA
);
1287 if (HasLeaf7
&& ((EBX
>> 30) & 1) && HasAVX512Save
)
1288 setFeature(X86::FEATURE_AVX512BW
);
1289 if (HasLeaf7
&& ((EBX
>> 31) & 1) && HasAVX512Save
)
1290 setFeature(X86::FEATURE_AVX512VL
);
1292 if (HasLeaf7
&& ((ECX
>> 1) & 1) && HasAVX512Save
)
1293 setFeature(X86::FEATURE_AVX512VBMI
);
1294 if (HasLeaf7
&& ((ECX
>> 6) & 1) && HasAVX512Save
)
1295 setFeature(X86::FEATURE_AVX512VBMI2
);
1296 if (HasLeaf7
&& ((ECX
>> 8) & 1))
1297 setFeature(X86::FEATURE_GFNI
);
1298 if (HasLeaf7
&& ((ECX
>> 10) & 1) && HasAVX
)
1299 setFeature(X86::FEATURE_VPCLMULQDQ
);
1300 if (HasLeaf7
&& ((ECX
>> 11) & 1) && HasAVX512Save
)
1301 setFeature(X86::FEATURE_AVX512VNNI
);
1302 if (HasLeaf7
&& ((ECX
>> 12) & 1) && HasAVX512Save
)
1303 setFeature(X86::FEATURE_AVX512BITALG
);
1304 if (HasLeaf7
&& ((ECX
>> 14) & 1) && HasAVX512Save
)
1305 setFeature(X86::FEATURE_AVX512VPOPCNTDQ
);
1307 if (HasLeaf7
&& ((EDX
>> 2) & 1) && HasAVX512Save
)
1308 setFeature(X86::FEATURE_AVX5124VNNIW
);
1309 if (HasLeaf7
&& ((EDX
>> 3) & 1) && HasAVX512Save
)
1310 setFeature(X86::FEATURE_AVX5124FMAPS
);
1311 if (HasLeaf7
&& ((EDX
>> 8) & 1) && HasAVX512Save
)
1312 setFeature(X86::FEATURE_AVX512VP2INTERSECT
);
1314 // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
1315 // return all 0s for invalid subleaves so check the limit.
1316 bool HasLeaf7Subleaf1
=
1317 HasLeaf7
&& EAX
>= 1 &&
1318 !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1319 if (HasLeaf7Subleaf1
&& ((EAX
>> 5) & 1) && HasAVX512Save
)
1320 setFeature(X86::FEATURE_AVX512BF16
);
1322 unsigned MaxExtLevel
;
1323 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel
, &EBX
, &ECX
, &EDX
);
1325 bool HasExtLeaf1
= MaxExtLevel
>= 0x80000001 &&
1326 !getX86CpuIDAndInfo(0x80000001, &EAX
, &EBX
, &ECX
, &EDX
);
1327 if (HasExtLeaf1
&& ((ECX
>> 6) & 1))
1328 setFeature(X86::FEATURE_SSE4_A
);
1329 if (HasExtLeaf1
&& ((ECX
>> 11) & 1))
1330 setFeature(X86::FEATURE_XOP
);
1331 if (HasExtLeaf1
&& ((ECX
>> 16) & 1))
1332 setFeature(X86::FEATURE_FMA4
);
1334 if (HasExtLeaf1
&& ((EDX
>> 29) & 1))
1335 setFeature(X86::FEATURE_64BIT
);
1338 StringRef
sys::getHostCPUName() {
1339 unsigned MaxLeaf
= 0;
1340 const VendorSignatures Vendor
= getVendorSignature(&MaxLeaf
);
1341 if (Vendor
== VendorSignatures::UNKNOWN
)
1344 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
1345 getX86CpuIDAndInfo(0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1347 unsigned Family
= 0, Model
= 0;
1348 unsigned Features
[(X86::CPU_FEATURE_MAX
+ 31) / 32] = {0};
1349 detectX86FamilyModel(EAX
, &Family
, &Model
);
1350 getAvailableFeatures(ECX
, EDX
, MaxLeaf
, Features
);
1352 // These aren't consumed in this file, but we try to keep some source code the
1353 // same or similar to compiler-rt.
1355 unsigned Subtype
= 0;
1359 if (Vendor
== VendorSignatures::GENUINE_INTEL
) {
1360 CPU
= getIntelProcessorTypeAndSubtype(Family
, Model
, Features
, &Type
,
1362 } else if (Vendor
== VendorSignatures::AUTHENTIC_AMD
) {
1363 CPU
= getAMDProcessorTypeAndSubtype(Family
, Model
, Features
, &Type
,
1373 #elif defined(__APPLE__) && defined(__powerpc__)
1374 StringRef
sys::getHostCPUName() {
1375 host_basic_info_data_t hostInfo
;
1376 mach_msg_type_number_t infoCount
;
1378 infoCount
= HOST_BASIC_INFO_COUNT
;
1379 mach_port_t hostPort
= mach_host_self();
1380 host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&hostInfo
,
1382 mach_port_deallocate(mach_task_self(), hostPort
);
1384 if (hostInfo
.cpu_type
!= CPU_TYPE_POWERPC
)
1387 switch (hostInfo
.cpu_subtype
) {
1388 case CPU_SUBTYPE_POWERPC_601
:
1390 case CPU_SUBTYPE_POWERPC_602
:
1392 case CPU_SUBTYPE_POWERPC_603
:
1394 case CPU_SUBTYPE_POWERPC_603e
:
1396 case CPU_SUBTYPE_POWERPC_603ev
:
1398 case CPU_SUBTYPE_POWERPC_604
:
1400 case CPU_SUBTYPE_POWERPC_604e
:
1402 case CPU_SUBTYPE_POWERPC_620
:
1404 case CPU_SUBTYPE_POWERPC_750
:
1406 case CPU_SUBTYPE_POWERPC_7400
:
1408 case CPU_SUBTYPE_POWERPC_7450
:
1410 case CPU_SUBTYPE_POWERPC_970
:
1417 #elif defined(__linux__) && defined(__powerpc__)
1418 StringRef
sys::getHostCPUName() {
1419 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1420 StringRef Content
= P
? P
->getBuffer() : "";
1421 return detail::getHostCPUNameForPowerPC(Content
);
1423 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1424 StringRef
sys::getHostCPUName() {
1425 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1426 StringRef Content
= P
? P
->getBuffer() : "";
1427 return detail::getHostCPUNameForARM(Content
);
1429 #elif defined(__linux__) && defined(__s390x__)
1430 StringRef
sys::getHostCPUName() {
1431 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1432 StringRef Content
= P
? P
->getBuffer() : "";
1433 return detail::getHostCPUNameForS390x(Content
);
1435 #elif defined(__MVS__)
1436 StringRef
sys::getHostCPUName() {
1437 // Get pointer to Communications Vector Table (CVT).
1438 // The pointer is located at offset 16 of the Prefixed Save Area (PSA).
1439 // It is stored as 31 bit pointer and will be zero-extended to 64 bit.
1440 int *StartToCVTOffset
= reinterpret_cast<int *>(0x10);
1441 // Since its stored as a 31-bit pointer, get the 4 bytes from the start
1443 int ReadValue
= *StartToCVTOffset
;
1444 // Explicitly clear the high order bit.
1445 ReadValue
= (ReadValue
& 0x7FFFFFFF);
1446 char *CVT
= reinterpret_cast<char *>(ReadValue
);
1447 // The model number is located in the CVT prefix at offset -6 and stored as
1448 // signless packed decimal.
1449 uint16_t Id
= *(uint16_t *)&CVT
[-6];
1450 // Convert number to integer.
1451 Id
= decodePackedBCD
<uint16_t>(Id
, false);
1452 // Check for vector support. It's stored in field CVTFLAG5 (offset 244),
1453 // bit CVTVEF (X'80'). The facilities list is part of the PSA but the vector
1454 // extension can only be used if bit CVTVEF is on.
1455 bool HaveVectorSupport
= CVT
[244] & 0x80;
1456 return getCPUNameFromS390Model(Id
, HaveVectorSupport
);
1458 #elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1459 #define CPUFAMILY_ARM_SWIFT 0x1e2d6381
1460 #define CPUFAMILY_ARM_CYCLONE 0x37a09642
1461 #define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
1462 #define CPUFAMILY_ARM_TWISTER 0x92fb37c8
1463 #define CPUFAMILY_ARM_HURRICANE 0x67ceee93
1464 #define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
1465 #define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
1466 #define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
1467 #define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
1469 StringRef
sys::getHostCPUName() {
1471 size_t Length
= sizeof(Family
);
1472 sysctlbyname("hw.cpufamily", &Family
, &Length
, NULL
, 0);
1475 case CPUFAMILY_ARM_SWIFT
:
1477 case CPUFAMILY_ARM_CYCLONE
:
1479 case CPUFAMILY_ARM_TYPHOON
:
1481 case CPUFAMILY_ARM_TWISTER
:
1483 case CPUFAMILY_ARM_HURRICANE
:
1485 case CPUFAMILY_ARM_MONSOON_MISTRAL
:
1487 case CPUFAMILY_ARM_VORTEX_TEMPEST
:
1489 case CPUFAMILY_ARM_LIGHTNING_THUNDER
:
1491 case CPUFAMILY_ARM_FIRESTORM_ICESTORM
:
1494 // Default to the newest CPU we know about.
1499 StringRef
sys::getHostCPUName() {
1500 switch (_system_configuration
.implementation
) {
1502 if (_system_configuration
.version
== PV_4_3
)
1506 if (_system_configuration
.version
== PV_5
)
1510 if (_system_configuration
.version
== PV_6_Compat
)
1519 // TODO: simplify this once the macro is available in all OS levels.
1530 #elif defined(__loongarch__)
1531 StringRef
sys::getHostCPUName() {
1532 // Use processor id to detect cpu name.
1533 uint32_t processor_id
;
1534 __asm__("cpucfg %[prid], $zero\n\t" : [prid
] "=r"(processor_id
));
1535 // Refer PRID_SERIES_MASK in linux kernel: arch/loongarch/include/asm/cpu.h.
1536 switch (processor_id
& 0xf000) {
1537 case 0xc000: // Loongson 64bit, 4-issue
1545 #elif defined(__riscv)
1546 StringRef
sys::getHostCPUName() {
1547 #if defined(__linux__)
1548 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1549 StringRef Content
= P
? P
->getBuffer() : "";
1550 return detail::getHostCPUNameForRISCV(Content
);
1552 #if __riscv_xlen == 64
1553 return "generic-rv64";
1554 #elif __riscv_xlen == 32
1555 return "generic-rv32";
1557 #error "Unhandled value of __riscv_xlen"
1561 #elif defined(__sparc__)
1562 #if defined(__linux__)
1563 StringRef
sys::detail::getHostCPUNameForSPARC(StringRef ProcCpuinfoContent
) {
1564 SmallVector
<StringRef
> Lines
;
1565 ProcCpuinfoContent
.split(Lines
, "\n");
1567 // Look for cpu line to determine cpu name
1569 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
1570 if (Lines
[I
].starts_with("cpu")) {
1571 Cpu
= Lines
[I
].substr(5).ltrim("\t :");
1576 return StringSwitch
<const char *>(Cpu
)
1577 .StartsWith("SuperSparc", "supersparc")
1578 .StartsWith("HyperSparc", "hypersparc")
1579 .StartsWith("SpitFire", "ultrasparc")
1580 .StartsWith("BlackBird", "ultrasparc")
1581 .StartsWith("Sabre", " ultrasparc")
1582 .StartsWith("Hummingbird", "ultrasparc")
1583 .StartsWith("Cheetah", "ultrasparc3")
1584 .StartsWith("Jalapeno", "ultrasparc3")
1585 .StartsWith("Jaguar", "ultrasparc3")
1586 .StartsWith("Panther", "ultrasparc3")
1587 .StartsWith("Serrano", "ultrasparc3")
1588 .StartsWith("UltraSparc T1", "niagara")
1589 .StartsWith("UltraSparc T2", "niagara2")
1590 .StartsWith("UltraSparc T3", "niagara3")
1591 .StartsWith("UltraSparc T4", "niagara4")
1592 .StartsWith("UltraSparc T5", "niagara4")
1593 .StartsWith("LEON", "leon3")
1594 // niagara7/m8 not supported by LLVM yet.
1595 .StartsWith("SPARC-M7", "niagara4" /* "niagara7" */)
1596 .StartsWith("SPARC-S7", "niagara4" /* "niagara7" */)
1597 .StartsWith("SPARC-M8", "niagara4" /* "m8" */)
1598 .Default("generic");
1602 StringRef
sys::getHostCPUName() {
1603 #if defined(__linux__)
1604 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1605 StringRef Content
= P
? P
->getBuffer() : "";
1606 return detail::getHostCPUNameForSPARC(Content
);
1607 #elif defined(__sun__) && defined(__svr4__)
1611 kstat_named_t
*brand
= NULL
;
1615 ksp
= kstat_lookup(kc
, const_cast<char *>("cpu_info"), -1, NULL
);
1616 if (ksp
!= NULL
&& kstat_read(kc
, ksp
, NULL
) != -1 &&
1617 ksp
->ks_type
== KSTAT_TYPE_NAMED
)
1619 (kstat_named_t
*)kstat_data_lookup(ksp
, const_cast<char *>("brand"));
1620 if (brand
!= NULL
&& brand
->data_type
== KSTAT_DATA_STRING
)
1621 buf
= KSTAT_NAMED_STR_PTR(brand
);
1625 return StringSwitch
<const char *>(buf
)
1626 .Case("TMS390S10", "supersparc") // Texas Instruments microSPARC I
1627 .Case("TMS390Z50", "supersparc") // Texas Instruments SuperSPARC I
1629 "supersparc") // Texas Instruments SuperSPARC I with SuperCache
1630 .Case("MB86904", "supersparc") // Fujitsu microSPARC II
1631 .Case("MB86907", "supersparc") // Fujitsu TurboSPARC
1632 .Case("RT623", "hypersparc") // Ross hyperSPARC
1633 .Case("RT625", "hypersparc")
1634 .Case("RT626", "hypersparc")
1635 .Case("UltraSPARC-I", "ultrasparc")
1636 .Case("UltraSPARC-II", "ultrasparc")
1637 .Case("UltraSPARC-IIe", "ultrasparc")
1638 .Case("UltraSPARC-IIi", "ultrasparc")
1639 .Case("SPARC64-III", "ultrasparc")
1640 .Case("SPARC64-IV", "ultrasparc")
1641 .Case("UltraSPARC-III", "ultrasparc3")
1642 .Case("UltraSPARC-III+", "ultrasparc3")
1643 .Case("UltraSPARC-IIIi", "ultrasparc3")
1644 .Case("UltraSPARC-IIIi+", "ultrasparc3")
1645 .Case("UltraSPARC-IV", "ultrasparc3")
1646 .Case("UltraSPARC-IV+", "ultrasparc3")
1647 .Case("SPARC64-V", "ultrasparc3")
1648 .Case("SPARC64-VI", "ultrasparc3")
1649 .Case("SPARC64-VII", "ultrasparc3")
1650 .Case("UltraSPARC-T1", "niagara")
1651 .Case("UltraSPARC-T2", "niagara2")
1652 .Case("UltraSPARC-T2", "niagara2")
1653 .Case("UltraSPARC-T2+", "niagara2")
1654 .Case("SPARC-T3", "niagara3")
1655 .Case("SPARC-T4", "niagara4")
1656 .Case("SPARC-T5", "niagara4")
1657 // niagara7/m8 not supported by LLVM yet.
1658 .Case("SPARC-M7", "niagara4" /* "niagara7" */)
1659 .Case("SPARC-S7", "niagara4" /* "niagara7" */)
1660 .Case("SPARC-M8", "niagara4" /* "m8" */)
1661 .Default("generic");
1667 StringRef
sys::getHostCPUName() { return "generic"; }
1673 VendorSignatures
getVendorSignature(unsigned *MaxLeaf
) {
1674 return VendorSignatures::UNKNOWN
;
1678 } // namespace detail
1683 #if defined(__i386__) || defined(_M_IX86) || \
1684 defined(__x86_64__) || defined(_M_X64)
1685 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1686 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
1689 if (getX86CpuIDAndInfo(0, &MaxLevel
, &EBX
, &ECX
, &EDX
) || MaxLevel
< 1)
1692 getX86CpuIDAndInfo(1, &EAX
, &EBX
, &ECX
, &EDX
);
1694 Features
["cx8"] = (EDX
>> 8) & 1;
1695 Features
["cmov"] = (EDX
>> 15) & 1;
1696 Features
["mmx"] = (EDX
>> 23) & 1;
1697 Features
["fxsr"] = (EDX
>> 24) & 1;
1698 Features
["sse"] = (EDX
>> 25) & 1;
1699 Features
["sse2"] = (EDX
>> 26) & 1;
1701 Features
["sse3"] = (ECX
>> 0) & 1;
1702 Features
["pclmul"] = (ECX
>> 1) & 1;
1703 Features
["ssse3"] = (ECX
>> 9) & 1;
1704 Features
["cx16"] = (ECX
>> 13) & 1;
1705 Features
["sse4.1"] = (ECX
>> 19) & 1;
1706 Features
["sse4.2"] = (ECX
>> 20) & 1;
1707 Features
["crc32"] = Features
["sse4.2"];
1708 Features
["movbe"] = (ECX
>> 22) & 1;
1709 Features
["popcnt"] = (ECX
>> 23) & 1;
1710 Features
["aes"] = (ECX
>> 25) & 1;
1711 Features
["rdrnd"] = (ECX
>> 30) & 1;
1713 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1714 // indicates that the AVX registers will be saved and restored on context
1715 // switch, then we have full AVX support.
1716 bool HasXSave
= ((ECX
>> 27) & 1) && !getX86XCR0(&EAX
, &EDX
);
1717 bool HasAVXSave
= HasXSave
&& ((ECX
>> 28) & 1) && ((EAX
& 0x6) == 0x6);
1718 #if defined(__APPLE__)
1719 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1720 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1722 bool HasAVX512Save
= true;
1724 // AVX512 requires additional context to be saved by the OS.
1725 bool HasAVX512Save
= HasAVXSave
&& ((EAX
& 0xe0) == 0xe0);
1727 // AMX requires additional context to be saved by the OS.
1728 const unsigned AMXBits
= (1 << 17) | (1 << 18);
1729 bool HasAMXSave
= HasXSave
&& ((EAX
& AMXBits
) == AMXBits
);
1731 Features
["avx"] = HasAVXSave
;
1732 Features
["fma"] = ((ECX
>> 12) & 1) && HasAVXSave
;
1733 // Only enable XSAVE if OS has enabled support for saving YMM state.
1734 Features
["xsave"] = ((ECX
>> 26) & 1) && HasAVXSave
;
1735 Features
["f16c"] = ((ECX
>> 29) & 1) && HasAVXSave
;
1737 unsigned MaxExtLevel
;
1738 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel
, &EBX
, &ECX
, &EDX
);
1740 bool HasExtLeaf1
= MaxExtLevel
>= 0x80000001 &&
1741 !getX86CpuIDAndInfo(0x80000001, &EAX
, &EBX
, &ECX
, &EDX
);
1742 Features
["sahf"] = HasExtLeaf1
&& ((ECX
>> 0) & 1);
1743 Features
["lzcnt"] = HasExtLeaf1
&& ((ECX
>> 5) & 1);
1744 Features
["sse4a"] = HasExtLeaf1
&& ((ECX
>> 6) & 1);
1745 Features
["prfchw"] = HasExtLeaf1
&& ((ECX
>> 8) & 1);
1746 Features
["xop"] = HasExtLeaf1
&& ((ECX
>> 11) & 1) && HasAVXSave
;
1747 Features
["lwp"] = HasExtLeaf1
&& ((ECX
>> 15) & 1);
1748 Features
["fma4"] = HasExtLeaf1
&& ((ECX
>> 16) & 1) && HasAVXSave
;
1749 Features
["tbm"] = HasExtLeaf1
&& ((ECX
>> 21) & 1);
1750 Features
["mwaitx"] = HasExtLeaf1
&& ((ECX
>> 29) & 1);
1752 Features
["64bit"] = HasExtLeaf1
&& ((EDX
>> 29) & 1);
1754 // Miscellaneous memory related features, detected by
1755 // using the 0x80000008 leaf of the CPUID instruction
1756 bool HasExtLeaf8
= MaxExtLevel
>= 0x80000008 &&
1757 !getX86CpuIDAndInfo(0x80000008, &EAX
, &EBX
, &ECX
, &EDX
);
1758 Features
["clzero"] = HasExtLeaf8
&& ((EBX
>> 0) & 1);
1759 Features
["rdpru"] = HasExtLeaf8
&& ((EBX
>> 4) & 1);
1760 Features
["wbnoinvd"] = HasExtLeaf8
&& ((EBX
>> 9) & 1);
1763 MaxLevel
>= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
1765 Features
["fsgsbase"] = HasLeaf7
&& ((EBX
>> 0) & 1);
1766 Features
["sgx"] = HasLeaf7
&& ((EBX
>> 2) & 1);
1767 Features
["bmi"] = HasLeaf7
&& ((EBX
>> 3) & 1);
1768 // AVX2 is only supported if we have the OS save support from AVX.
1769 Features
["avx2"] = HasLeaf7
&& ((EBX
>> 5) & 1) && HasAVXSave
;
1770 Features
["bmi2"] = HasLeaf7
&& ((EBX
>> 8) & 1);
1771 Features
["invpcid"] = HasLeaf7
&& ((EBX
>> 10) & 1);
1772 Features
["rtm"] = HasLeaf7
&& ((EBX
>> 11) & 1);
1773 // AVX512 is only supported if the OS supports the context save for it.
1774 Features
["avx512f"] = HasLeaf7
&& ((EBX
>> 16) & 1) && HasAVX512Save
;
1775 Features
["avx512dq"] = HasLeaf7
&& ((EBX
>> 17) & 1) && HasAVX512Save
;
1776 Features
["rdseed"] = HasLeaf7
&& ((EBX
>> 18) & 1);
1777 Features
["adx"] = HasLeaf7
&& ((EBX
>> 19) & 1);
1778 Features
["avx512ifma"] = HasLeaf7
&& ((EBX
>> 21) & 1) && HasAVX512Save
;
1779 Features
["clflushopt"] = HasLeaf7
&& ((EBX
>> 23) & 1);
1780 Features
["clwb"] = HasLeaf7
&& ((EBX
>> 24) & 1);
1781 Features
["avx512pf"] = HasLeaf7
&& ((EBX
>> 26) & 1) && HasAVX512Save
;
1782 Features
["avx512er"] = HasLeaf7
&& ((EBX
>> 27) & 1) && HasAVX512Save
;
1783 Features
["avx512cd"] = HasLeaf7
&& ((EBX
>> 28) & 1) && HasAVX512Save
;
1784 Features
["sha"] = HasLeaf7
&& ((EBX
>> 29) & 1);
1785 Features
["avx512bw"] = HasLeaf7
&& ((EBX
>> 30) & 1) && HasAVX512Save
;
1786 Features
["avx512vl"] = HasLeaf7
&& ((EBX
>> 31) & 1) && HasAVX512Save
;
1788 Features
["prefetchwt1"] = HasLeaf7
&& ((ECX
>> 0) & 1);
1789 Features
["avx512vbmi"] = HasLeaf7
&& ((ECX
>> 1) & 1) && HasAVX512Save
;
1790 Features
["pku"] = HasLeaf7
&& ((ECX
>> 4) & 1);
1791 Features
["waitpkg"] = HasLeaf7
&& ((ECX
>> 5) & 1);
1792 Features
["avx512vbmi2"] = HasLeaf7
&& ((ECX
>> 6) & 1) && HasAVX512Save
;
1793 Features
["shstk"] = HasLeaf7
&& ((ECX
>> 7) & 1);
1794 Features
["gfni"] = HasLeaf7
&& ((ECX
>> 8) & 1);
1795 Features
["vaes"] = HasLeaf7
&& ((ECX
>> 9) & 1) && HasAVXSave
;
1796 Features
["vpclmulqdq"] = HasLeaf7
&& ((ECX
>> 10) & 1) && HasAVXSave
;
1797 Features
["avx512vnni"] = HasLeaf7
&& ((ECX
>> 11) & 1) && HasAVX512Save
;
1798 Features
["avx512bitalg"] = HasLeaf7
&& ((ECX
>> 12) & 1) && HasAVX512Save
;
1799 Features
["avx512vpopcntdq"] = HasLeaf7
&& ((ECX
>> 14) & 1) && HasAVX512Save
;
1800 Features
["rdpid"] = HasLeaf7
&& ((ECX
>> 22) & 1);
1801 Features
["kl"] = HasLeaf7
&& ((ECX
>> 23) & 1); // key locker
1802 Features
["cldemote"] = HasLeaf7
&& ((ECX
>> 25) & 1);
1803 Features
["movdiri"] = HasLeaf7
&& ((ECX
>> 27) & 1);
1804 Features
["movdir64b"] = HasLeaf7
&& ((ECX
>> 28) & 1);
1805 Features
["enqcmd"] = HasLeaf7
&& ((ECX
>> 29) & 1);
1807 Features
["uintr"] = HasLeaf7
&& ((EDX
>> 5) & 1);
1808 Features
["avx512vp2intersect"] =
1809 HasLeaf7
&& ((EDX
>> 8) & 1) && HasAVX512Save
;
1810 Features
["serialize"] = HasLeaf7
&& ((EDX
>> 14) & 1);
1811 Features
["tsxldtrk"] = HasLeaf7
&& ((EDX
>> 16) & 1);
1812 // There are two CPUID leafs which information associated with the pconfig
1814 // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
1815 // bit of EDX), while the EAX=0x1b leaf returns information on the
1816 // availability of specific pconfig leafs.
1817 // The target feature here only refers to the the first of these two.
1818 // Users might need to check for the availability of specific pconfig
1819 // leaves using cpuid, since that information is ignored while
1820 // detecting features using the "-march=native" flag.
1821 // For more info, see X86 ISA docs.
1822 Features
["pconfig"] = HasLeaf7
&& ((EDX
>> 18) & 1);
1823 Features
["amx-bf16"] = HasLeaf7
&& ((EDX
>> 22) & 1) && HasAMXSave
;
1824 Features
["avx512fp16"] = HasLeaf7
&& ((EDX
>> 23) & 1) && HasAVX512Save
;
1825 Features
["amx-tile"] = HasLeaf7
&& ((EDX
>> 24) & 1) && HasAMXSave
;
1826 Features
["amx-int8"] = HasLeaf7
&& ((EDX
>> 25) & 1) && HasAMXSave
;
1827 // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
1828 // return all 0s for invalid subleaves so check the limit.
1829 bool HasLeaf7Subleaf1
=
1830 HasLeaf7
&& EAX
>= 1 &&
1831 !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1832 Features
["sha512"] = HasLeaf7Subleaf1
&& ((EAX
>> 0) & 1);
1833 Features
["sm3"] = HasLeaf7Subleaf1
&& ((EAX
>> 1) & 1);
1834 Features
["sm4"] = HasLeaf7Subleaf1
&& ((EAX
>> 2) & 1);
1835 Features
["raoint"] = HasLeaf7Subleaf1
&& ((EAX
>> 3) & 1);
1836 Features
["avxvnni"] = HasLeaf7Subleaf1
&& ((EAX
>> 4) & 1) && HasAVXSave
;
1837 Features
["avx512bf16"] = HasLeaf7Subleaf1
&& ((EAX
>> 5) & 1) && HasAVX512Save
;
1838 Features
["amx-fp16"] = HasLeaf7Subleaf1
&& ((EAX
>> 21) & 1) && HasAMXSave
;
1839 Features
["cmpccxadd"] = HasLeaf7Subleaf1
&& ((EAX
>> 7) & 1);
1840 Features
["hreset"] = HasLeaf7Subleaf1
&& ((EAX
>> 22) & 1);
1841 Features
["avxifma"] = HasLeaf7Subleaf1
&& ((EAX
>> 23) & 1) && HasAVXSave
;
1842 Features
["avxvnniint8"] = HasLeaf7Subleaf1
&& ((EDX
>> 4) & 1) && HasAVXSave
;
1843 Features
["avxneconvert"] = HasLeaf7Subleaf1
&& ((EDX
>> 5) & 1) && HasAVXSave
;
1844 Features
["amx-complex"] = HasLeaf7Subleaf1
&& ((EDX
>> 8) & 1) && HasAMXSave
;
1845 Features
["avxvnniint16"] = HasLeaf7Subleaf1
&& ((EDX
>> 10) & 1) && HasAVXSave
;
1846 Features
["prefetchi"] = HasLeaf7Subleaf1
&& ((EDX
>> 14) & 1);
1847 Features
["usermsr"] = HasLeaf7Subleaf1
&& ((EDX
>> 15) & 1);
1848 Features
["avx10.1-256"] = HasLeaf7Subleaf1
&& ((EDX
>> 19) & 1);
1850 bool HasLeafD
= MaxLevel
>= 0xd &&
1851 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1853 // Only enable XSAVE if OS has enabled support for saving YMM state.
1854 Features
["xsaveopt"] = HasLeafD
&& ((EAX
>> 0) & 1) && HasAVXSave
;
1855 Features
["xsavec"] = HasLeafD
&& ((EAX
>> 1) & 1) && HasAVXSave
;
1856 Features
["xsaves"] = HasLeafD
&& ((EAX
>> 3) & 1) && HasAVXSave
;
1858 bool HasLeaf14
= MaxLevel
>= 0x14 &&
1859 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
1861 Features
["ptwrite"] = HasLeaf14
&& ((EBX
>> 4) & 1);
1864 MaxLevel
>= 0x19 && !getX86CpuIDAndInfo(0x19, &EAX
, &EBX
, &ECX
, &EDX
);
1865 Features
["widekl"] = HasLeaf7
&& HasLeaf19
&& ((EBX
>> 2) & 1);
1868 MaxLevel
>= 0x24 && !getX86CpuIDAndInfo(0x24, &EAX
, &EBX
, &ECX
, &EDX
);
1869 Features
["avx10.1-512"] =
1870 Features
["avx10.1-256"] && HasLeaf24
&& ((EBX
>> 18) & 1);
1874 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1875 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1876 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1880 SmallVector
<StringRef
, 32> Lines
;
1881 P
->getBuffer().split(Lines
, "\n");
1883 SmallVector
<StringRef
, 32> CPUFeatures
;
1885 // Look for the CPU features.
1886 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
1887 if (Lines
[I
].starts_with("Features")) {
1888 Lines
[I
].split(CPUFeatures
, ' ');
1892 #if defined(__aarch64__)
1893 // Keep track of which crypto features we have seen
1894 enum { CAP_AES
= 0x1, CAP_PMULL
= 0x2, CAP_SHA1
= 0x4, CAP_SHA2
= 0x8 };
1895 uint32_t crypto
= 0;
1898 for (unsigned I
= 0, E
= CPUFeatures
.size(); I
!= E
; ++I
) {
1899 StringRef LLVMFeatureStr
= StringSwitch
<StringRef
>(CPUFeatures
[I
])
1900 #if defined(__aarch64__)
1901 .Case("asimd", "neon")
1902 .Case("fp", "fp-armv8")
1903 .Case("crc32", "crc")
1904 .Case("atomics", "lse")
1906 .Case("sve2", "sve2")
1908 .Case("half", "fp16")
1909 .Case("neon", "neon")
1910 .Case("vfpv3", "vfp3")
1911 .Case("vfpv3d16", "vfp3d16")
1912 .Case("vfpv4", "vfp4")
1913 .Case("idiva", "hwdiv-arm")
1914 .Case("idivt", "hwdiv")
1918 #if defined(__aarch64__)
1919 // We need to check crypto separately since we need all of the crypto
1920 // extensions to enable the subtarget feature
1921 if (CPUFeatures
[I
] == "aes")
1923 else if (CPUFeatures
[I
] == "pmull")
1924 crypto
|= CAP_PMULL
;
1925 else if (CPUFeatures
[I
] == "sha1")
1927 else if (CPUFeatures
[I
] == "sha2")
1931 if (LLVMFeatureStr
!= "")
1932 Features
[LLVMFeatureStr
] = true;
1935 #if defined(__aarch64__)
1936 // If we have all crypto bits we can add the feature
1937 if (crypto
== (CAP_AES
| CAP_PMULL
| CAP_SHA1
| CAP_SHA2
))
1938 Features
["crypto"] = true;
1943 #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
1944 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1945 if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE
))
1946 Features
["neon"] = true;
1947 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE
))
1948 Features
["crc"] = true;
1949 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE
))
1950 Features
["crypto"] = true;
1954 #elif defined(__linux__) && defined(__loongarch__)
1955 #include <sys/auxv.h>
1956 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1957 unsigned long hwcap
= getauxval(AT_HWCAP
);
1958 bool HasFPU
= hwcap
& (1UL << 3); // HWCAP_LOONGARCH_FPU
1959 uint32_t cpucfg2
= 0x2;
1960 __asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2
] "+r"(cpucfg2
));
1962 Features
["f"] = HasFPU
&& (cpucfg2
& (1U << 1)); // CPUCFG.2.FP_SP
1963 Features
["d"] = HasFPU
&& (cpucfg2
& (1U << 2)); // CPUCFG.2.FP_DP
1965 Features
["lsx"] = hwcap
& (1UL << 4); // HWCAP_LOONGARCH_LSX
1966 Features
["lasx"] = hwcap
& (1UL << 5); // HWCAP_LOONGARCH_LASX
1967 Features
["lvz"] = hwcap
& (1UL << 9); // HWCAP_LOONGARCH_LVZ
1972 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) { return false; }
1976 /// \returns the \p triple, but with the Host's arch spliced in.
1977 static Triple
withHostArch(Triple T
) {
1978 #if defined(__arm__)
1979 T
.setArch(Triple::arm
);
1980 T
.setArchName("arm");
1981 #elif defined(__arm64e__)
1982 T
.setArch(Triple::aarch64
, Triple::AArch64SubArch_arm64e
);
1983 T
.setArchName("arm64e");
1984 #elif defined(__aarch64__)
1985 T
.setArch(Triple::aarch64
);
1986 T
.setArchName("arm64");
1987 #elif defined(__x86_64h__)
1988 T
.setArch(Triple::x86_64
);
1989 T
.setArchName("x86_64h");
1990 #elif defined(__x86_64__)
1991 T
.setArch(Triple::x86_64
);
1992 T
.setArchName("x86_64");
1993 #elif defined(__i386__)
1994 T
.setArch(Triple::x86
);
1995 T
.setArchName("i386");
1996 #elif defined(__powerpc__)
1997 T
.setArch(Triple::ppc
);
1998 T
.setArchName("powerpc");
2000 # error "Unimplemented host arch fixup"
2006 std::string
sys::getProcessTriple() {
2007 std::string TargetTripleString
= updateTripleOSVersion(LLVM_HOST_TRIPLE
);
2008 Triple
PT(Triple::normalize(TargetTripleString
));
2011 /// In Universal builds, LLVM_HOST_TRIPLE will have the wrong arch in one of
2012 /// the slices. This fixes that up.
2013 PT
= withHostArch(PT
);
2016 if (sizeof(void *) == 8 && PT
.isArch32Bit())
2017 PT
= PT
.get64BitArchVariant();
2018 if (sizeof(void *) == 4 && PT
.isArch64Bit())
2019 PT
= PT
.get32BitArchVariant();
2024 void sys::printDefaultTargetAndDetectedCPU(raw_ostream
&OS
) {
2025 #if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
2026 std::string CPU
= std::string(sys::getHostCPUName());
2027 if (CPU
== "generic")
2029 OS
<< " Default target: " << sys::getDefaultTargetTriple() << '\n'
2030 << " Host CPU: " << CPU
<< '\n';