LLVM 19.0.0git
Host.cpp
Go to the documentation of this file.
1//===-- Host.cpp - Implement OS Host Detection ------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the operating system Host detection.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/ADT/StringMap.h"
16#include "llvm/ADT/StringRef.h"
18#include "llvm/Config/llvm-config.h"
23#include <string.h>
24
25// Include the platform-specific parts of this class.
26#ifdef LLVM_ON_UNIX
27#include "Unix/Host.inc"
28#include <sched.h>
29#endif
30#ifdef _WIN32
31#include "Windows/Host.inc"
32#endif
33#ifdef _MSC_VER
34#include <intrin.h>
35#endif
36#ifdef __MVS__
37#include "llvm/Support/BCD.h"
38#endif
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>
46#endif
47#ifdef _AIX
48#include <sys/systemcfg.h>
49#endif
50#if defined(__sun__) && defined(__svr4__)
51#include <kstat.h>
52#endif
53
54#define DEBUG_TYPE "host-detection"
55
56//===----------------------------------------------------------------------===//
57//
58// Implementations of the CPU detection routines
59//
60//===----------------------------------------------------------------------===//
61
62using namespace llvm;
63
64static std::unique_ptr<llvm::MemoryBuffer>
68 if (std::error_code EC = Text.getError()) {
69 llvm::errs() << "Can't read "
70 << "/proc/cpuinfo: " << EC.message() << "\n";
71 return nullptr;
72 }
73 return std::move(*Text);
74}
75
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";
81
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();
86
87 StringRef::const_iterator CIP = CPUInfoStart;
88
89 StringRef::const_iterator CPUStart = nullptr;
90 size_t CPULen = 0;
91
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')
96 ++CIP;
97
98 if (CIP < CPUInfoEnd && *CIP == 'c') {
99 ++CIP;
100 if (CIP < CPUInfoEnd && *CIP == 'p') {
101 ++CIP;
102 if (CIP < CPUInfoEnd && *CIP == 'u') {
103 ++CIP;
104 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
105 ++CIP;
106
107 if (CIP < CPUInfoEnd && *CIP == ':') {
108 ++CIP;
109 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
110 ++CIP;
111
112 if (CIP < CPUInfoEnd) {
113 CPUStart = CIP;
114 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
115 *CIP != ',' && *CIP != '\n'))
116 ++CIP;
117 CPULen = CIP - CPUStart;
118 }
119 }
120 }
121 }
122 }
123
124 if (CPUStart == nullptr)
125 while (CIP < CPUInfoEnd && *CIP != '\n')
126 ++CIP;
127 }
128
129 if (CPUStart == nullptr)
130 return generic;
131
132 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
133 .Case("604e", "604e")
134 .Case("604", "604")
135 .Case("7400", "7400")
136 .Case("7410", "7400")
137 .Case("7447", "7400")
138 .Case("7455", "7450")
139 .Case("G4", "g4")
140 .Case("POWER4", "970")
141 .Case("PPC970FX", "970")
142 .Case("PPC970MP", "970")
143 .Case("G5", "g5")
144 .Case("POWER5", "g5")
145 .Case("A2", "a2")
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.
156 .Default(generic);
157}
158
160 // The cpuid register on arm is not accessible from user space. On Linux,
161 // it is exposed through the /proc/cpuinfo file.
162
163 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
164 // in all cases.
166 ProcCpuinfoContent.split(Lines, "\n");
167
168 // Look for the CPU implementer line.
169 StringRef Implementer;
170 StringRef Hardware;
171 StringRef Part;
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 :");
179 }
180
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"))
185 return "cortex-a53";
186
187
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("0xc05", "cortex-a5")
200 .Case("0xc07", "cortex-a7")
201 .Case("0xc08", "cortex-a8")
202 .Case("0xc09", "cortex-a9")
203 .Case("0xc0f", "cortex-a15")
204 .Case("0xc0e", "cortex-a17")
205 .Case("0xc20", "cortex-m0")
206 .Case("0xc23", "cortex-m3")
207 .Case("0xc24", "cortex-m4")
208 .Case("0xc27", "cortex-m7")
209 .Case("0xd20", "cortex-m23")
210 .Case("0xd21", "cortex-m33")
211 .Case("0xd24", "cortex-m52")
212 .Case("0xd22", "cortex-m55")
213 .Case("0xd23", "cortex-m85")
214 .Case("0xc18", "cortex-r8")
215 .Case("0xd13", "cortex-r52")
216 .Case("0xd15", "cortex-r82")
217 .Case("0xd14", "cortex-r82ae")
218 .Case("0xd02", "cortex-a34")
219 .Case("0xd04", "cortex-a35")
220 .Case("0xd03", "cortex-a53")
221 .Case("0xd05", "cortex-a55")
222 .Case("0xd46", "cortex-a510")
223 .Case("0xd80", "cortex-a520")
224 .Case("0xd88", "cortex-a520ae")
225 .Case("0xd07", "cortex-a57")
226 .Case("0xd06", "cortex-a65")
227 .Case("0xd43", "cortex-a65ae")
228 .Case("0xd08", "cortex-a72")
229 .Case("0xd09", "cortex-a73")
230 .Case("0xd0a", "cortex-a75")
231 .Case("0xd0b", "cortex-a76")
232 .Case("0xd0e", "cortex-a76ae")
233 .Case("0xd0d", "cortex-a77")
234 .Case("0xd41", "cortex-a78")
235 .Case("0xd42", "cortex-a78ae")
236 .Case("0xd4b", "cortex-a78c")
237 .Case("0xd47", "cortex-a710")
238 .Case("0xd4d", "cortex-a715")
239 .Case("0xd81", "cortex-a720")
240 .Case("0xd89", "cortex-a720ae")
241 .Case("0xd44", "cortex-x1")
242 .Case("0xd4c", "cortex-x1c")
243 .Case("0xd48", "cortex-x2")
244 .Case("0xd4e", "cortex-x3")
245 .Case("0xd82", "cortex-x4")
246 .Case("0xd4a", "neoverse-e1")
247 .Case("0xd0c", "neoverse-n1")
248 .Case("0xd49", "neoverse-n2")
249 .Case("0xd8e", "neoverse-n3")
250 .Case("0xd40", "neoverse-v1")
251 .Case("0xd4f", "neoverse-v2")
252 .Case("0xd84", "neoverse-v3")
253 .Case("0xd83", "neoverse-v3ae")
254 .Default("generic");
255 }
256
257 if (Implementer == "0x42" || Implementer == "0x43") { // Broadcom | Cavium.
258 return StringSwitch<const char *>(Part)
259 .Case("0x516", "thunderx2t99")
260 .Case("0x0516", "thunderx2t99")
261 .Case("0xaf", "thunderx2t99")
262 .Case("0x0af", "thunderx2t99")
263 .Case("0xa1", "thunderxt88")
264 .Case("0x0a1", "thunderxt88")
265 .Default("generic");
266 }
267
268 if (Implementer == "0x46") { // Fujitsu Ltd.
269 return StringSwitch<const char *>(Part)
270 .Case("0x001", "a64fx")
271 .Default("generic");
272 }
273
274 if (Implementer == "0x4e") { // NVIDIA Corporation
275 return StringSwitch<const char *>(Part)
276 .Case("0x004", "carmel")
277 .Default("generic");
278 }
279
280 if (Implementer == "0x48") // HiSilicon Technologies, Inc.
281 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
282 // values correspond to the "Part number" in the CP15/c0 register. The
283 // contents are specified in the various processor manuals.
284 return StringSwitch<const char *>(Part)
285 .Case("0xd01", "tsv110")
286 .Default("generic");
287
288 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
289 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
290 // values correspond to the "Part number" in the CP15/c0 register. The
291 // contents are specified in the various processor manuals.
292 return StringSwitch<const char *>(Part)
293 .Case("0x06f", "krait") // APQ8064
294 .Case("0x201", "kryo")
295 .Case("0x205", "kryo")
296 .Case("0x211", "kryo")
297 .Case("0x800", "cortex-a73") // Kryo 2xx Gold
298 .Case("0x801", "cortex-a73") // Kryo 2xx Silver
299 .Case("0x802", "cortex-a75") // Kryo 3xx Gold
300 .Case("0x803", "cortex-a75") // Kryo 3xx Silver
301 .Case("0x804", "cortex-a76") // Kryo 4xx Gold
302 .Case("0x805", "cortex-a76") // Kryo 4xx/5xx Silver
303 .Case("0xc00", "falkor")
304 .Case("0xc01", "saphira")
305 .Default("generic");
306 if (Implementer == "0x53") { // Samsung Electronics Co., Ltd.
307 // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
308 // any predictive pattern across variants and parts.
309 unsigned Variant = 0, Part = 0;
310
311 // Look for the CPU variant line, whose value is a 1 digit hexadecimal
312 // number, corresponding to the Variant bits in the CP15/C0 register.
313 for (auto I : Lines)
314 if (I.consume_front("CPU variant"))
315 I.ltrim("\t :").getAsInteger(0, Variant);
316
317 // Look for the CPU part line, whose value is a 3 digit hexadecimal
318 // number, corresponding to the PartNum bits in the CP15/C0 register.
319 for (auto I : Lines)
320 if (I.consume_front("CPU part"))
321 I.ltrim("\t :").getAsInteger(0, Part);
322
323 unsigned Exynos = (Variant << 12) | Part;
324 switch (Exynos) {
325 default:
326 // Default by falling through to Exynos M3.
327 [[fallthrough]];
328 case 0x1002:
329 return "exynos-m3";
330 case 0x1003:
331 return "exynos-m4";
332 }
333 }
334
335 if (Implementer == "0x6d") { // Microsoft Corporation.
336 // The Microsoft Azure Cobalt 100 CPU is handled as a Neoverse N2.
337 return StringSwitch<const char *>(Part)
338 .Case("0xd49", "neoverse-n2")
339 .Default("generic");
340 }
341
342 if (Implementer == "0xc0") { // Ampere Computing
343 return StringSwitch<const char *>(Part)
344 .Case("0xac3", "ampere1")
345 .Case("0xac4", "ampere1a")
346 .Case("0xac5", "ampere1b")
347 .Default("generic");
348 }
349
350 return "generic";
351}
352
353namespace {
354StringRef getCPUNameFromS390Model(unsigned int Id, bool HaveVectorSupport) {
355 switch (Id) {
356 case 2064: // z900 not supported by LLVM
357 case 2066:
358 case 2084: // z990 not supported by LLVM
359 case 2086:
360 case 2094: // z9-109 not supported by LLVM
361 case 2096:
362 return "generic";
363 case 2097:
364 case 2098:
365 return "z10";
366 case 2817:
367 case 2818:
368 return "z196";
369 case 2827:
370 case 2828:
371 return "zEC12";
372 case 2964:
373 case 2965:
374 return HaveVectorSupport? "z13" : "zEC12";
375 case 3906:
376 case 3907:
377 return HaveVectorSupport? "z14" : "zEC12";
378 case 8561:
379 case 8562:
380 return HaveVectorSupport? "z15" : "zEC12";
381 case 3931:
382 case 3932:
383 default:
384 return HaveVectorSupport? "z16" : "zEC12";
385 }
386}
387} // end anonymous namespace
388
390 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
391
392 // The "processor 0:" line comes after a fair amount of other information,
393 // including a cache breakdown, but this should be plenty.
395 ProcCpuinfoContent.split(Lines, "\n");
396
397 // Look for the CPU features.
398 SmallVector<StringRef, 32> CPUFeatures;
399 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
400 if (Lines[I].starts_with("features")) {
401 size_t Pos = Lines[I].find(':');
402 if (Pos != StringRef::npos) {
403 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
404 break;
405 }
406 }
407
408 // We need to check for the presence of vector support independently of
409 // the machine type, since we may only use the vector register set when
410 // supported by the kernel (and hypervisor).
411 bool HaveVectorSupport = false;
412 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
413 if (CPUFeatures[I] == "vx")
414 HaveVectorSupport = true;
415 }
416
417 // Now check the processor machine type.
418 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
419 if (Lines[I].starts_with("processor ")) {
420 size_t Pos = Lines[I].find("machine = ");
421 if (Pos != StringRef::npos) {
422 Pos += sizeof("machine = ") - 1;
423 unsigned int Id;
424 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id))
425 return getCPUNameFromS390Model(Id, HaveVectorSupport);
426 }
427 break;
428 }
429 }
430
431 return "generic";
432}
433
435 // There are 24 lines in /proc/cpuinfo
437 ProcCpuinfoContent.split(Lines, "\n");
438
439 // Look for uarch line to determine cpu name
440 StringRef UArch;
441 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
442 if (Lines[I].starts_with("uarch")) {
443 UArch = Lines[I].substr(5).ltrim("\t :");
444 break;
445 }
446 }
447
448 return StringSwitch<const char *>(UArch)
449 .Case("sifive,u74-mc", "sifive-u74")
450 .Case("sifive,bullet0", "sifive-u74")
451 .Default("generic");
452}
453
455#if !defined(__linux__) || !defined(__x86_64__)
456 return "generic";
457#else
458 uint8_t v3_insns[40] __attribute__ ((aligned (8))) =
459 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
460 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
461 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
462 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
463 /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
464 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
465 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
466 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
467 /* BPF_EXIT_INSN() */
468 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
469
470 uint8_t v2_insns[40] __attribute__ ((aligned (8))) =
471 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
472 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
473 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
474 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
475 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
476 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
477 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
478 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
479 /* BPF_EXIT_INSN() */
480 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
481
482 struct bpf_prog_load_attr {
483 uint32_t prog_type;
484 uint32_t insn_cnt;
485 uint64_t insns;
486 uint64_t license;
487 uint32_t log_level;
488 uint32_t log_size;
489 uint64_t log_buf;
490 uint32_t kern_version;
491 uint32_t prog_flags;
492 } attr = {};
493 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
494 attr.insn_cnt = 5;
495 attr.insns = (uint64_t)v3_insns;
496 attr.license = (uint64_t)"DUMMY";
497
498 int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr,
499 sizeof(attr));
500 if (fd >= 0) {
501 close(fd);
502 return "v3";
503 }
504
505 /* Clear the whole attr in case its content changed by syscall. */
506 memset(&attr, 0, sizeof(attr));
507 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
508 attr.insn_cnt = 5;
509 attr.insns = (uint64_t)v2_insns;
510 attr.license = (uint64_t)"DUMMY";
511 fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr));
512 if (fd >= 0) {
513 close(fd);
514 return "v2";
515 }
516 return "v1";
517#endif
518}
519
520#if defined(__i386__) || defined(_M_IX86) || \
521 defined(__x86_64__) || defined(_M_X64)
522
523// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
524// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
525// support. Consequently, for i386, the presence of CPUID is checked first
526// via the corresponding eflags bit.
527// Removal of cpuid.h header motivated by PR30384
528// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
529// or test-suite, but are used in external projects e.g. libstdcxx
530static bool isCpuIdSupported() {
531#if defined(__GNUC__) || defined(__clang__)
532#if defined(__i386__)
533 int __cpuid_supported;
534 __asm__(" pushfl\n"
535 " popl %%eax\n"
536 " movl %%eax,%%ecx\n"
537 " xorl $0x00200000,%%eax\n"
538 " pushl %%eax\n"
539 " popfl\n"
540 " pushfl\n"
541 " popl %%eax\n"
542 " movl $0,%0\n"
543 " cmpl %%eax,%%ecx\n"
544 " je 1f\n"
545 " movl $1,%0\n"
546 "1:"
547 : "=r"(__cpuid_supported)
548 :
549 : "eax", "ecx");
550 if (!__cpuid_supported)
551 return false;
552#endif
553 return true;
554#endif
555 return true;
556}
557
558/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
559/// the specified arguments. If we can't run cpuid on the host, return true.
560static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
561 unsigned *rECX, unsigned *rEDX) {
562#if defined(__GNUC__) || defined(__clang__)
563#if defined(__x86_64__)
564 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
565 // FIXME: should we save this for Clang?
566 __asm__("movq\t%%rbx, %%rsi\n\t"
567 "cpuid\n\t"
568 "xchgq\t%%rbx, %%rsi\n\t"
569 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
570 : "a"(value));
571 return false;
572#elif defined(__i386__)
573 __asm__("movl\t%%ebx, %%esi\n\t"
574 "cpuid\n\t"
575 "xchgl\t%%ebx, %%esi\n\t"
576 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
577 : "a"(value));
578 return false;
579#else
580 return true;
581#endif
582#elif defined(_MSC_VER)
583 // The MSVC intrinsic is portable across x86 and x64.
584 int registers[4];
585 __cpuid(registers, value);
586 *rEAX = registers[0];
587 *rEBX = registers[1];
588 *rECX = registers[2];
589 *rEDX = registers[3];
590 return false;
591#else
592 return true;
593#endif
594}
595
596namespace llvm {
597namespace sys {
598namespace detail {
599namespace x86 {
600
601VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
602 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
603 if (MaxLeaf == nullptr)
604 MaxLeaf = &EAX;
605 else
606 *MaxLeaf = 0;
607
608 if (!isCpuIdSupported())
609 return VendorSignatures::UNKNOWN;
610
611 if (getX86CpuIDAndInfo(0, MaxLeaf, &EBX, &ECX, &EDX) || *MaxLeaf < 1)
612 return VendorSignatures::UNKNOWN;
613
614 // "Genu ineI ntel"
615 if (EBX == 0x756e6547 && EDX == 0x49656e69 && ECX == 0x6c65746e)
616 return VendorSignatures::GENUINE_INTEL;
617
618 // "Auth enti cAMD"
619 if (EBX == 0x68747541 && EDX == 0x69746e65 && ECX == 0x444d4163)
620 return VendorSignatures::AUTHENTIC_AMD;
621
622 return VendorSignatures::UNKNOWN;
623}
624
625} // namespace x86
626} // namespace detail
627} // namespace sys
628} // namespace llvm
629
630using namespace llvm::sys::detail::x86;
631
632/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
633/// the 4 values in the specified arguments. If we can't run cpuid on the host,
634/// return true.
635static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
636 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
637 unsigned *rEDX) {
638#if defined(__GNUC__) || defined(__clang__)
639#if defined(__x86_64__)
640 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
641 // FIXME: should we save this for Clang?
642 __asm__("movq\t%%rbx, %%rsi\n\t"
643 "cpuid\n\t"
644 "xchgq\t%%rbx, %%rsi\n\t"
645 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
646 : "a"(value), "c"(subleaf));
647 return false;
648#elif defined(__i386__)
649 __asm__("movl\t%%ebx, %%esi\n\t"
650 "cpuid\n\t"
651 "xchgl\t%%ebx, %%esi\n\t"
652 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
653 : "a"(value), "c"(subleaf));
654 return false;
655#else
656 return true;
657#endif
658#elif defined(_MSC_VER)
659 int registers[4];
660 __cpuidex(registers, value, subleaf);
661 *rEAX = registers[0];
662 *rEBX = registers[1];
663 *rECX = registers[2];
664 *rEDX = registers[3];
665 return false;
666#else
667 return true;
668#endif
669}
670
671// Read control register 0 (XCR0). Used to detect features such as AVX.
672static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
673#if defined(__GNUC__) || defined(__clang__)
674 // Check xgetbv; this uses a .byte sequence instead of the instruction
675 // directly because older assemblers do not include support for xgetbv and
676 // there is no easy way to conditionally compile based on the assembler used.
677 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
678 return false;
679#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
680 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
681 *rEAX = Result;
682 *rEDX = Result >> 32;
683 return false;
684#else
685 return true;
686#endif
687}
688
689static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
690 unsigned *Model) {
691 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
692 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
693 if (*Family == 6 || *Family == 0xf) {
694 if (*Family == 0xf)
695 // Examine extended family ID if family ID is F.
696 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
697 // Examine extended model ID if family ID is 6 or F.
698 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
699 }
700}
701
702static StringRef
703getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
704 const unsigned *Features,
705 unsigned *Type, unsigned *Subtype) {
706 auto testFeature = [&](unsigned F) {
707 return (Features[F / 32] & (1U << (F % 32))) != 0;
708 };
709
710 StringRef CPU;
711
712 switch (Family) {
713 case 3:
714 CPU = "i386";
715 break;
716 case 4:
717 CPU = "i486";
718 break;
719 case 5:
720 if (testFeature(X86::FEATURE_MMX)) {
721 CPU = "pentium-mmx";
722 break;
723 }
724 CPU = "pentium";
725 break;
726 case 6:
727 switch (Model) {
728 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
729 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
730 // mobile processor, Intel Core 2 Extreme processor, Intel
731 // Pentium Dual-Core processor, Intel Xeon processor, model
732 // 0Fh. All processors are manufactured using the 65 nm process.
733 case 0x16: // Intel Celeron processor model 16h. All processors are
734 // manufactured using the 65 nm process
735 CPU = "core2";
736 *Type = X86::INTEL_CORE2;
737 break;
738 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
739 // 17h. All processors are manufactured using the 45 nm process.
740 //
741 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
742 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
743 // the 45 nm process.
744 CPU = "penryn";
745 *Type = X86::INTEL_CORE2;
746 break;
747 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
748 // processors are manufactured using the 45 nm process.
749 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
750 // As found in a Summer 2010 model iMac.
751 case 0x1f:
752 case 0x2e: // Nehalem EX
753 CPU = "nehalem";
754 *Type = X86::INTEL_COREI7;
755 *Subtype = X86::INTEL_COREI7_NEHALEM;
756 break;
757 case 0x25: // Intel Core i7, laptop version.
758 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
759 // processors are manufactured using the 32 nm process.
760 case 0x2f: // Westmere EX
761 CPU = "westmere";
762 *Type = X86::INTEL_COREI7;
763 *Subtype = X86::INTEL_COREI7_WESTMERE;
764 break;
765 case 0x2a: // Intel Core i7 processor. All processors are manufactured
766 // using the 32 nm process.
767 case 0x2d:
768 CPU = "sandybridge";
769 *Type = X86::INTEL_COREI7;
770 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
771 break;
772 case 0x3a:
773 case 0x3e: // Ivy Bridge EP
774 CPU = "ivybridge";
775 *Type = X86::INTEL_COREI7;
776 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
777 break;
778
779 // Haswell:
780 case 0x3c:
781 case 0x3f:
782 case 0x45:
783 case 0x46:
784 CPU = "haswell";
785 *Type = X86::INTEL_COREI7;
786 *Subtype = X86::INTEL_COREI7_HASWELL;
787 break;
788
789 // Broadwell:
790 case 0x3d:
791 case 0x47:
792 case 0x4f:
793 case 0x56:
794 CPU = "broadwell";
795 *Type = X86::INTEL_COREI7;
796 *Subtype = X86::INTEL_COREI7_BROADWELL;
797 break;
798
799 // Skylake:
800 case 0x4e: // Skylake mobile
801 case 0x5e: // Skylake desktop
802 case 0x8e: // Kaby Lake mobile
803 case 0x9e: // Kaby Lake desktop
804 case 0xa5: // Comet Lake-H/S
805 case 0xa6: // Comet Lake-U
806 CPU = "skylake";
807 *Type = X86::INTEL_COREI7;
808 *Subtype = X86::INTEL_COREI7_SKYLAKE;
809 break;
810
811 // Rocketlake:
812 case 0xa7:
813 CPU = "rocketlake";
814 *Type = X86::INTEL_COREI7;
815 *Subtype = X86::INTEL_COREI7_ROCKETLAKE;
816 break;
817
818 // Skylake Xeon:
819 case 0x55:
820 *Type = X86::INTEL_COREI7;
821 if (testFeature(X86::FEATURE_AVX512BF16)) {
822 CPU = "cooperlake";
823 *Subtype = X86::INTEL_COREI7_COOPERLAKE;
824 } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
825 CPU = "cascadelake";
826 *Subtype = X86::INTEL_COREI7_CASCADELAKE;
827 } else {
828 CPU = "skylake-avx512";
829 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
830 }
831 break;
832
833 // Cannonlake:
834 case 0x66:
835 CPU = "cannonlake";
836 *Type = X86::INTEL_COREI7;
837 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
838 break;
839
840 // Icelake:
841 case 0x7d:
842 case 0x7e:
843 CPU = "icelake-client";
844 *Type = X86::INTEL_COREI7;
845 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
846 break;
847
848 // Tigerlake:
849 case 0x8c:
850 case 0x8d:
851 CPU = "tigerlake";
852 *Type = X86::INTEL_COREI7;
853 *Subtype = X86::INTEL_COREI7_TIGERLAKE;
854 break;
855
856 // Alderlake:
857 case 0x97:
858 case 0x9a:
859 // Gracemont
860 case 0xbe:
861 // Raptorlake:
862 case 0xb7:
863 case 0xba:
864 case 0xbf:
865 // Meteorlake:
866 case 0xaa:
867 case 0xac:
868 CPU = "alderlake";
869 *Type = X86::INTEL_COREI7;
870 *Subtype = X86::INTEL_COREI7_ALDERLAKE;
871 break;
872
873 // Arrowlake:
874 case 0xc5:
875 CPU = "arrowlake";
876 *Type = X86::INTEL_COREI7;
877 *Subtype = X86::INTEL_COREI7_ARROWLAKE;
878 break;
879
880 // Arrowlake S:
881 case 0xc6:
882 // Lunarlake:
883 case 0xbd:
884 CPU = "arrowlake-s";
885 *Type = X86::INTEL_COREI7;
886 *Subtype = X86::INTEL_COREI7_ARROWLAKE_S;
887 break;
888
889 // Pantherlake:
890 case 0xcc:
891 CPU = "pantherlake";
892 *Type = X86::INTEL_COREI7;
893 *Subtype = X86::INTEL_COREI7_PANTHERLAKE;
894 break;
895
896 // Graniterapids:
897 case 0xad:
898 CPU = "graniterapids";
899 *Type = X86::INTEL_COREI7;
900 *Subtype = X86::INTEL_COREI7_GRANITERAPIDS;
901 break;
902
903 // Granite Rapids D:
904 case 0xae:
905 CPU = "graniterapids-d";
906 *Type = X86::INTEL_COREI7;
907 *Subtype = X86::INTEL_COREI7_GRANITERAPIDS_D;
908 break;
909
910 // Icelake Xeon:
911 case 0x6a:
912 case 0x6c:
913 CPU = "icelake-server";
914 *Type = X86::INTEL_COREI7;
915 *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER;
916 break;
917
918 // Emerald Rapids:
919 case 0xcf:
920 // Sapphire Rapids:
921 case 0x8f:
922 CPU = "sapphirerapids";
923 *Type = X86::INTEL_COREI7;
924 *Subtype = X86::INTEL_COREI7_SAPPHIRERAPIDS;
925 break;
926
927 case 0x1c: // Most 45 nm Intel Atom processors
928 case 0x26: // 45 nm Atom Lincroft
929 case 0x27: // 32 nm Atom Medfield
930 case 0x35: // 32 nm Atom Midview
931 case 0x36: // 32 nm Atom Midview
932 CPU = "bonnell";
933 *Type = X86::INTEL_BONNELL;
934 break;
935
936 // Atom Silvermont codes from the Intel software optimization guide.
937 case 0x37:
938 case 0x4a:
939 case 0x4d:
940 case 0x5a:
941 case 0x5d:
942 case 0x4c: // really airmont
943 CPU = "silvermont";
944 *Type = X86::INTEL_SILVERMONT;
945 break;
946 // Goldmont:
947 case 0x5c: // Apollo Lake
948 case 0x5f: // Denverton
949 CPU = "goldmont";
950 *Type = X86::INTEL_GOLDMONT;
951 break;
952 case 0x7a:
953 CPU = "goldmont-plus";
954 *Type = X86::INTEL_GOLDMONT_PLUS;
955 break;
956 case 0x86:
957 case 0x8a: // Lakefield
958 case 0x96: // Elkhart Lake
959 case 0x9c: // Jasper Lake
960 CPU = "tremont";
961 *Type = X86::INTEL_TREMONT;
962 break;
963
964 // Sierraforest:
965 case 0xaf:
966 CPU = "sierraforest";
967 *Type = X86::INTEL_SIERRAFOREST;
968 break;
969
970 // Grandridge:
971 case 0xb6:
972 CPU = "grandridge";
973 *Type = X86::INTEL_GRANDRIDGE;
974 break;
975
976 // Clearwaterforest:
977 case 0xdd:
978 CPU = "clearwaterforest";
979 *Type = X86::INTEL_CLEARWATERFOREST;
980 break;
981
982 // Xeon Phi (Knights Landing + Knights Mill):
983 case 0x57:
984 CPU = "knl";
985 *Type = X86::INTEL_KNL;
986 break;
987 case 0x85:
988 CPU = "knm";
989 *Type = X86::INTEL_KNM;
990 break;
991
992 default: // Unknown family 6 CPU, try to guess.
993 // Don't both with Type/Subtype here, they aren't used by the caller.
994 // They're used above to keep the code in sync with compiler-rt.
995 // TODO detect tigerlake host from model
996 if (testFeature(X86::FEATURE_AVX512VP2INTERSECT)) {
997 CPU = "tigerlake";
998 } else if (testFeature(X86::FEATURE_AVX512VBMI2)) {
999 CPU = "icelake-client";
1000 } else if (testFeature(X86::FEATURE_AVX512VBMI)) {
1001 CPU = "cannonlake";
1002 } else if (testFeature(X86::FEATURE_AVX512BF16)) {
1003 CPU = "cooperlake";
1004 } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
1005 CPU = "cascadelake";
1006 } else if (testFeature(X86::FEATURE_AVX512VL)) {
1007 CPU = "skylake-avx512";
1008 } else if (testFeature(X86::FEATURE_AVX512ER)) {
1009 CPU = "knl";
1010 } else if (testFeature(X86::FEATURE_CLFLUSHOPT)) {
1011 if (testFeature(X86::FEATURE_SHA))
1012 CPU = "goldmont";
1013 else
1014 CPU = "skylake";
1015 } else if (testFeature(X86::FEATURE_ADX)) {
1016 CPU = "broadwell";
1017 } else if (testFeature(X86::FEATURE_AVX2)) {
1018 CPU = "haswell";
1019 } else if (testFeature(X86::FEATURE_AVX)) {
1020 CPU = "sandybridge";
1021 } else if (testFeature(X86::FEATURE_SSE4_2)) {
1022 if (testFeature(X86::FEATURE_MOVBE))
1023 CPU = "silvermont";
1024 else
1025 CPU = "nehalem";
1026 } else if (testFeature(X86::FEATURE_SSE4_1)) {
1027 CPU = "penryn";
1028 } else if (testFeature(X86::FEATURE_SSSE3)) {
1029 if (testFeature(X86::FEATURE_MOVBE))
1030 CPU = "bonnell";
1031 else
1032 CPU = "core2";
1033 } else if (testFeature(X86::FEATURE_64BIT)) {
1034 CPU = "core2";
1035 } else if (testFeature(X86::FEATURE_SSE3)) {
1036 CPU = "yonah";
1037 } else if (testFeature(X86::FEATURE_SSE2)) {
1038 CPU = "pentium-m";
1039 } else if (testFeature(X86::FEATURE_SSE)) {
1040 CPU = "pentium3";
1041 } else if (testFeature(X86::FEATURE_MMX)) {
1042 CPU = "pentium2";
1043 } else {
1044 CPU = "pentiumpro";
1045 }
1046 break;
1047 }
1048 break;
1049 case 15: {
1050 if (testFeature(X86::FEATURE_64BIT)) {
1051 CPU = "nocona";
1052 break;
1053 }
1054 if (testFeature(X86::FEATURE_SSE3)) {
1055 CPU = "prescott";
1056 break;
1057 }
1058 CPU = "pentium4";
1059 break;
1060 }
1061 default:
1062 break; // Unknown.
1063 }
1064
1065 return CPU;
1066}
1067
1068static StringRef
1069getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
1070 const unsigned *Features,
1071 unsigned *Type, unsigned *Subtype) {
1072 auto testFeature = [&](unsigned F) {
1073 return (Features[F / 32] & (1U << (F % 32))) != 0;
1074 };
1075
1076 StringRef CPU;
1077
1078 switch (Family) {
1079 case 4:
1080 CPU = "i486";
1081 break;
1082 case 5:
1083 CPU = "pentium";
1084 switch (Model) {
1085 case 6:
1086 case 7:
1087 CPU = "k6";
1088 break;
1089 case 8:
1090 CPU = "k6-2";
1091 break;
1092 case 9:
1093 case 13:
1094 CPU = "k6-3";
1095 break;
1096 case 10:
1097 CPU = "geode";
1098 break;
1099 }
1100 break;
1101 case 6:
1102 if (testFeature(X86::FEATURE_SSE)) {
1103 CPU = "athlon-xp";
1104 break;
1105 }
1106 CPU = "athlon";
1107 break;
1108 case 15:
1109 if (testFeature(X86::FEATURE_SSE3)) {
1110 CPU = "k8-sse3";
1111 break;
1112 }
1113 CPU = "k8";
1114 break;
1115 case 16:
1116 CPU = "amdfam10";
1117 *Type = X86::AMDFAM10H; // "amdfam10"
1118 switch (Model) {
1119 case 2:
1120 *Subtype = X86::AMDFAM10H_BARCELONA;
1121 break;
1122 case 4:
1123 *Subtype = X86::AMDFAM10H_SHANGHAI;
1124 break;
1125 case 8:
1126 *Subtype = X86::AMDFAM10H_ISTANBUL;
1127 break;
1128 }
1129 break;
1130 case 20:
1131 CPU = "btver1";
1132 *Type = X86::AMD_BTVER1;
1133 break;
1134 case 21:
1135 CPU = "bdver1";
1136 *Type = X86::AMDFAM15H;
1137 if (Model >= 0x60 && Model <= 0x7f) {
1138 CPU = "bdver4";
1139 *Subtype = X86::AMDFAM15H_BDVER4;
1140 break; // 60h-7Fh: Excavator
1141 }
1142 if (Model >= 0x30 && Model <= 0x3f) {
1143 CPU = "bdver3";
1144 *Subtype = X86::AMDFAM15H_BDVER3;
1145 break; // 30h-3Fh: Steamroller
1146 }
1147 if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
1148 CPU = "bdver2";
1149 *Subtype = X86::AMDFAM15H_BDVER2;
1150 break; // 02h, 10h-1Fh: Piledriver
1151 }
1152 if (Model <= 0x0f) {
1153 *Subtype = X86::AMDFAM15H_BDVER1;
1154 break; // 00h-0Fh: Bulldozer
1155 }
1156 break;
1157 case 22:
1158 CPU = "btver2";
1159 *Type = X86::AMD_BTVER2;
1160 break;
1161 case 23:
1162 CPU = "znver1";
1163 *Type = X86::AMDFAM17H;
1164 if ((Model >= 0x30 && Model <= 0x3f) || (Model == 0x47) ||
1165 (Model >= 0x60 && Model <= 0x67) || (Model >= 0x68 && Model <= 0x6f) ||
1166 (Model >= 0x70 && Model <= 0x7f) || (Model >= 0x84 && Model <= 0x87) ||
1167 (Model >= 0x90 && Model <= 0x97) || (Model >= 0x98 && Model <= 0x9f) ||
1168 (Model >= 0xa0 && Model <= 0xaf)) {
1169 // Family 17h Models 30h-3Fh (Starship) Zen 2
1170 // Family 17h Models 47h (Cardinal) Zen 2
1171 // Family 17h Models 60h-67h (Renoir) Zen 2
1172 // Family 17h Models 68h-6Fh (Lucienne) Zen 2
1173 // Family 17h Models 70h-7Fh (Matisse) Zen 2
1174 // Family 17h Models 84h-87h (ProjectX) Zen 2
1175 // Family 17h Models 90h-97h (VanGogh) Zen 2
1176 // Family 17h Models 98h-9Fh (Mero) Zen 2
1177 // Family 17h Models A0h-AFh (Mendocino) Zen 2
1178 CPU = "znver2";
1179 *Subtype = X86::AMDFAM17H_ZNVER2;
1180 break;
1181 }
1182 if ((Model >= 0x10 && Model <= 0x1f) || (Model >= 0x20 && Model <= 0x2f)) {
1183 // Family 17h Models 10h-1Fh (Raven1) Zen
1184 // Family 17h Models 10h-1Fh (Picasso) Zen+
1185 // Family 17h Models 20h-2Fh (Raven2 x86) Zen
1186 *Subtype = X86::AMDFAM17H_ZNVER1;
1187 break;
1188 }
1189 break;
1190 case 25:
1191 CPU = "znver3";
1192 *Type = X86::AMDFAM19H;
1193 if (Model <= 0x0f || (Model >= 0x20 && Model <= 0x2f) ||
1194 (Model >= 0x30 && Model <= 0x3f) || (Model >= 0x40 && Model <= 0x4f) ||
1195 (Model >= 0x50 && Model <= 0x5f)) {
1196 // Family 19h Models 00h-0Fh (Genesis, Chagall) Zen 3
1197 // Family 19h Models 20h-2Fh (Vermeer) Zen 3
1198 // Family 19h Models 30h-3Fh (Badami) Zen 3
1199 // Family 19h Models 40h-4Fh (Rembrandt) Zen 3+
1200 // Family 19h Models 50h-5Fh (Cezanne) Zen 3
1201 *Subtype = X86::AMDFAM19H_ZNVER3;
1202 break;
1203 }
1204 if ((Model >= 0x10 && Model <= 0x1f) || (Model >= 0x60 && Model <= 0x6f) ||
1205 (Model >= 0x70 && Model <= 0x77) || (Model >= 0x78 && Model <= 0x7f) ||
1206 (Model >= 0xa0 && Model <= 0xaf)) {
1207 // Family 19h Models 10h-1Fh (Stones; Storm Peak) Zen 4
1208 // Family 19h Models 60h-6Fh (Raphael) Zen 4
1209 // Family 19h Models 70h-77h (Phoenix, Hawkpoint1) Zen 4
1210 // Family 19h Models 78h-7Fh (Phoenix 2, Hawkpoint2) Zen 4
1211 // Family 19h Models A0h-AFh (Stones-Dense) Zen 4
1212 CPU = "znver4";
1213 *Subtype = X86::AMDFAM19H_ZNVER4;
1214 break; // "znver4"
1215 }
1216 break;
1217 default:
1218 break; // Unknown AMD CPU.
1219 }
1220
1221 return CPU;
1222}
1223
1224static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
1225 unsigned *Features) {
1226 unsigned EAX, EBX;
1227
1228 auto setFeature = [&](unsigned F) {
1229 Features[F / 32] |= 1U << (F % 32);
1230 };
1231
1232 if ((EDX >> 15) & 1)
1233 setFeature(X86::FEATURE_CMOV);
1234 if ((EDX >> 23) & 1)
1235 setFeature(X86::FEATURE_MMX);
1236 if ((EDX >> 25) & 1)
1237 setFeature(X86::FEATURE_SSE);
1238 if ((EDX >> 26) & 1)
1239 setFeature(X86::FEATURE_SSE2);
1240
1241 if ((ECX >> 0) & 1)
1242 setFeature(X86::FEATURE_SSE3);
1243 if ((ECX >> 1) & 1)
1244 setFeature(X86::FEATURE_PCLMUL);
1245 if ((ECX >> 9) & 1)
1246 setFeature(X86::FEATURE_SSSE3);
1247 if ((ECX >> 12) & 1)
1248 setFeature(X86::FEATURE_FMA);
1249 if ((ECX >> 19) & 1)
1250 setFeature(X86::FEATURE_SSE4_1);
1251 if ((ECX >> 20) & 1) {
1252 setFeature(X86::FEATURE_SSE4_2);
1253 setFeature(X86::FEATURE_CRC32);
1254 }
1255 if ((ECX >> 23) & 1)
1256 setFeature(X86::FEATURE_POPCNT);
1257 if ((ECX >> 25) & 1)
1258 setFeature(X86::FEATURE_AES);
1259
1260 if ((ECX >> 22) & 1)
1261 setFeature(X86::FEATURE_MOVBE);
1262
1263 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1264 // indicates that the AVX registers will be saved and restored on context
1265 // switch, then we have full AVX support.
1266 const unsigned AVXBits = (1 << 27) | (1 << 28);
1267 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
1268 ((EAX & 0x6) == 0x6);
1269#if defined(__APPLE__)
1270 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1271 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1272 // set right now.
1273 bool HasAVX512Save = true;
1274#else
1275 // AVX512 requires additional context to be saved by the OS.
1276 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
1277#endif
1278
1279 if (HasAVX)
1280 setFeature(X86::FEATURE_AVX);
1281
1282 bool HasLeaf7 =
1283 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1284
1285 if (HasLeaf7 && ((EBX >> 3) & 1))
1286 setFeature(X86::FEATURE_BMI);
1287 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
1288 setFeature(X86::FEATURE_AVX2);
1289 if (HasLeaf7 && ((EBX >> 8) & 1))
1290 setFeature(X86::FEATURE_BMI2);
1291 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save) {
1292 setFeature(X86::FEATURE_AVX512F);
1293 setFeature(X86::FEATURE_EVEX512);
1294 }
1295 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
1296 setFeature(X86::FEATURE_AVX512DQ);
1297 if (HasLeaf7 && ((EBX >> 19) & 1))
1298 setFeature(X86::FEATURE_ADX);
1299 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
1300 setFeature(X86::FEATURE_AVX512IFMA);
1301 if (HasLeaf7 && ((EBX >> 23) & 1))
1302 setFeature(X86::FEATURE_CLFLUSHOPT);
1303 if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
1304 setFeature(X86::FEATURE_AVX512PF);
1305 if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
1306 setFeature(X86::FEATURE_AVX512ER);
1307 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
1308 setFeature(X86::FEATURE_AVX512CD);
1309 if (HasLeaf7 && ((EBX >> 29) & 1))
1310 setFeature(X86::FEATURE_SHA);
1311 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
1312 setFeature(X86::FEATURE_AVX512BW);
1313 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
1314 setFeature(X86::FEATURE_AVX512VL);
1315
1316 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
1317 setFeature(X86::FEATURE_AVX512VBMI);
1318 if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
1319 setFeature(X86::FEATURE_AVX512VBMI2);
1320 if (HasLeaf7 && ((ECX >> 8) & 1))
1321 setFeature(X86::FEATURE_GFNI);
1322 if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
1323 setFeature(X86::FEATURE_VPCLMULQDQ);
1324 if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
1325 setFeature(X86::FEATURE_AVX512VNNI);
1326 if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
1327 setFeature(X86::FEATURE_AVX512BITALG);
1328 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
1329 setFeature(X86::FEATURE_AVX512VPOPCNTDQ);
1330
1331 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
1332 setFeature(X86::FEATURE_AVX5124VNNIW);
1333 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
1334 setFeature(X86::FEATURE_AVX5124FMAPS);
1335 if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)
1336 setFeature(X86::FEATURE_AVX512VP2INTERSECT);
1337
1338 // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
1339 // return all 0s for invalid subleaves so check the limit.
1340 bool HasLeaf7Subleaf1 =
1341 HasLeaf7 && EAX >= 1 &&
1342 !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1343 if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
1344 setFeature(X86::FEATURE_AVX512BF16);
1345
1346 unsigned MaxExtLevel;
1347 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1348
1349 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1350 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1351 if (HasExtLeaf1 && ((ECX >> 6) & 1))
1352 setFeature(X86::FEATURE_SSE4_A);
1353 if (HasExtLeaf1 && ((ECX >> 11) & 1))
1354 setFeature(X86::FEATURE_XOP);
1355 if (HasExtLeaf1 && ((ECX >> 16) & 1))
1356 setFeature(X86::FEATURE_FMA4);
1357
1358 if (HasExtLeaf1 && ((EDX >> 29) & 1))
1359 setFeature(X86::FEATURE_64BIT);
1360}
1361
1363 unsigned MaxLeaf = 0;
1364 const VendorSignatures Vendor = getVendorSignature(&MaxLeaf);
1365 if (Vendor == VendorSignatures::UNKNOWN)
1366 return "generic";
1367
1368 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1369 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
1370
1371 unsigned Family = 0, Model = 0;
1372 unsigned Features[(X86::CPU_FEATURE_MAX + 31) / 32] = {0};
1373 detectX86FamilyModel(EAX, &Family, &Model);
1374 getAvailableFeatures(ECX, EDX, MaxLeaf, Features);
1375
1376 // These aren't consumed in this file, but we try to keep some source code the
1377 // same or similar to compiler-rt.
1378 unsigned Type = 0;
1379 unsigned Subtype = 0;
1380
1381 StringRef CPU;
1382
1383 if (Vendor == VendorSignatures::GENUINE_INTEL) {
1384 CPU = getIntelProcessorTypeAndSubtype(Family, Model, Features, &Type,
1385 &Subtype);
1386 } else if (Vendor == VendorSignatures::AUTHENTIC_AMD) {
1387 CPU = getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type,
1388 &Subtype);
1389 }
1390
1391 if (!CPU.empty())
1392 return CPU;
1393
1394 return "generic";
1395}
1396
1397#elif defined(__APPLE__) && defined(__powerpc__)
1399 host_basic_info_data_t hostInfo;
1400 mach_msg_type_number_t infoCount;
1401
1402 infoCount = HOST_BASIC_INFO_COUNT;
1403 mach_port_t hostPort = mach_host_self();
1404 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1405 &infoCount);
1406 mach_port_deallocate(mach_task_self(), hostPort);
1407
1408 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1409 return "generic";
1410
1411 switch (hostInfo.cpu_subtype) {
1413 return "601";
1415 return "602";
1417 return "603";
1419 return "603e";
1421 return "603ev";
1423 return "604";
1425 return "604e";
1427 return "620";
1429 return "750";
1431 return "7400";
1433 return "7450";
1435 return "970";
1436 default:;
1437 }
1438
1439 return "generic";
1440}
1441#elif defined(__linux__) && defined(__powerpc__)
1443 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1444 StringRef Content = P ? P->getBuffer() : "";
1445 return detail::getHostCPUNameForPowerPC(Content);
1446}
1447#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1449 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1450 StringRef Content = P ? P->getBuffer() : "";
1451 return detail::getHostCPUNameForARM(Content);
1452}
1453#elif defined(__linux__) && defined(__s390x__)
1455 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1456 StringRef Content = P ? P->getBuffer() : "";
1457 return detail::getHostCPUNameForS390x(Content);
1458}
1459#elif defined(__MVS__)
1461 // Get pointer to Communications Vector Table (CVT).
1462 // The pointer is located at offset 16 of the Prefixed Save Area (PSA).
1463 // It is stored as 31 bit pointer and will be zero-extended to 64 bit.
1464 int *StartToCVTOffset = reinterpret_cast<int *>(0x10);
1465 // Since its stored as a 31-bit pointer, get the 4 bytes from the start
1466 // of address.
1467 int ReadValue = *StartToCVTOffset;
1468 // Explicitly clear the high order bit.
1469 ReadValue = (ReadValue & 0x7FFFFFFF);
1470 char *CVT = reinterpret_cast<char *>(ReadValue);
1471 // The model number is located in the CVT prefix at offset -6 and stored as
1472 // signless packed decimal.
1473 uint16_t Id = *(uint16_t *)&CVT[-6];
1474 // Convert number to integer.
1475 Id = decodePackedBCD<uint16_t>(Id, false);
1476 // Check for vector support. It's stored in field CVTFLAG5 (offset 244),
1477 // bit CVTVEF (X'80'). The facilities list is part of the PSA but the vector
1478 // extension can only be used if bit CVTVEF is on.
1479 bool HaveVectorSupport = CVT[244] & 0x80;
1480 return getCPUNameFromS390Model(Id, HaveVectorSupport);
1481}
1482#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1483#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
1484#define CPUFAMILY_ARM_CYCLONE 0x37a09642
1485#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
1486#define CPUFAMILY_ARM_TWISTER 0x92fb37c8
1487#define CPUFAMILY_ARM_HURRICANE 0x67ceee93
1488#define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
1489#define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
1490#define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
1491#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
1492#define CPUFAMILY_ARM_BLIZZARD_AVALANCHE 0xda33d83d
1493#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
1494
1496 uint32_t Family;
1497 size_t Length = sizeof(Family);
1498 sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
1499
1500 switch (Family) {
1501 case CPUFAMILY_ARM_SWIFT:
1502 return "swift";
1503 case CPUFAMILY_ARM_CYCLONE:
1504 return "apple-a7";
1505 case CPUFAMILY_ARM_TYPHOON:
1506 return "apple-a8";
1507 case CPUFAMILY_ARM_TWISTER:
1508 return "apple-a9";
1509 case CPUFAMILY_ARM_HURRICANE:
1510 return "apple-a10";
1511 case CPUFAMILY_ARM_MONSOON_MISTRAL:
1512 return "apple-a11";
1513 case CPUFAMILY_ARM_VORTEX_TEMPEST:
1514 return "apple-a12";
1515 case CPUFAMILY_ARM_LIGHTNING_THUNDER:
1516 return "apple-a13";
1517 case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1518 return "apple-m1";
1519 case CPUFAMILY_ARM_BLIZZARD_AVALANCHE:
1520 return "apple-m2";
1521 case CPUFAMILY_ARM_EVEREST_SAWTOOTH:
1522 return "apple-m3";
1523 default:
1524 // Default to the newest CPU we know about.
1525 return "apple-m3";
1526 }
1527}
1528#elif defined(_AIX)
1530 switch (_system_configuration.implementation) {
1531 case POWER_4:
1532 if (_system_configuration.version == PV_4_3)
1533 return "970";
1534 return "pwr4";
1535 case POWER_5:
1536 if (_system_configuration.version == PV_5)
1537 return "pwr5";
1538 return "pwr5x";
1539 case POWER_6:
1540 if (_system_configuration.version == PV_6_Compat)
1541 return "pwr6";
1542 return "pwr6x";
1543 case POWER_7:
1544 return "pwr7";
1545 case POWER_8:
1546 return "pwr8";
1547 case POWER_9:
1548 return "pwr9";
1549// TODO: simplify this once the macro is available in all OS levels.
1550#ifdef POWER_10
1551 case POWER_10:
1552#else
1553 case 0x40000:
1554#endif
1555 return "pwr10";
1556 default:
1557 return "generic";
1558 }
1559}
1560#elif defined(__loongarch__)
1562 // Use processor id to detect cpu name.
1563 uint32_t processor_id;
1564 __asm__("cpucfg %[prid], $zero\n\t" : [prid] "=r"(processor_id));
1565 // Refer PRID_SERIES_MASK in linux kernel: arch/loongarch/include/asm/cpu.h.
1566 switch (processor_id & 0xf000) {
1567 case 0xc000: // Loongson 64bit, 4-issue
1568 return "la464";
1569 // TODO: Others.
1570 default:
1571 break;
1572 }
1573 return "generic";
1574}
1575#elif defined(__riscv)
1577#if defined(__linux__)
1578 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1579 StringRef Content = P ? P->getBuffer() : "";
1580 return detail::getHostCPUNameForRISCV(Content);
1581#else
1582#if __riscv_xlen == 64
1583 return "generic-rv64";
1584#elif __riscv_xlen == 32
1585 return "generic-rv32";
1586#else
1587#error "Unhandled value of __riscv_xlen"
1588#endif
1589#endif
1590}
1591#elif defined(__sparc__)
1592#if defined(__linux__)
1595 ProcCpuinfoContent.split(Lines, "\n");
1596
1597 // Look for cpu line to determine cpu name
1598 StringRef Cpu;
1599 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
1600 if (Lines[I].starts_with("cpu")) {
1601 Cpu = Lines[I].substr(5).ltrim("\t :");
1602 break;
1603 }
1604 }
1605
1606 return StringSwitch<const char *>(Cpu)
1607 .StartsWith("SuperSparc", "supersparc")
1608 .StartsWith("HyperSparc", "hypersparc")
1609 .StartsWith("SpitFire", "ultrasparc")
1610 .StartsWith("BlackBird", "ultrasparc")
1611 .StartsWith("Sabre", " ultrasparc")
1612 .StartsWith("Hummingbird", "ultrasparc")
1613 .StartsWith("Cheetah", "ultrasparc3")
1614 .StartsWith("Jalapeno", "ultrasparc3")
1615 .StartsWith("Jaguar", "ultrasparc3")
1616 .StartsWith("Panther", "ultrasparc3")
1617 .StartsWith("Serrano", "ultrasparc3")
1618 .StartsWith("UltraSparc T1", "niagara")
1619 .StartsWith("UltraSparc T2", "niagara2")
1620 .StartsWith("UltraSparc T3", "niagara3")
1621 .StartsWith("UltraSparc T4", "niagara4")
1622 .StartsWith("UltraSparc T5", "niagara4")
1623 .StartsWith("LEON", "leon3")
1624 // niagara7/m8 not supported by LLVM yet.
1625 .StartsWith("SPARC-M7", "niagara4" /* "niagara7" */)
1626 .StartsWith("SPARC-S7", "niagara4" /* "niagara7" */)
1627 .StartsWith("SPARC-M8", "niagara4" /* "m8" */)
1628 .Default("generic");
1629}
1630#endif
1631
1633#if defined(__linux__)
1634 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1635 StringRef Content = P ? P->getBuffer() : "";
1636 return detail::getHostCPUNameForSPARC(Content);
1637#elif defined(__sun__) && defined(__svr4__)
1638 char *buf = NULL;
1639 kstat_ctl_t *kc;
1640 kstat_t *ksp;
1641 kstat_named_t *brand = NULL;
1642
1643 kc = kstat_open();
1644 if (kc != NULL) {
1645 ksp = kstat_lookup(kc, const_cast<char *>("cpu_info"), -1, NULL);
1646 if (ksp != NULL && kstat_read(kc, ksp, NULL) != -1 &&
1647 ksp->ks_type == KSTAT_TYPE_NAMED)
1648 brand =
1649 (kstat_named_t *)kstat_data_lookup(ksp, const_cast<char *>("brand"));
1650 if (brand != NULL && brand->data_type == KSTAT_DATA_STRING)
1651 buf = KSTAT_NAMED_STR_PTR(brand);
1652 }
1653 kstat_close(kc);
1654
1655 return StringSwitch<const char *>(buf)
1656 .Case("TMS390S10", "supersparc") // Texas Instruments microSPARC I
1657 .Case("TMS390Z50", "supersparc") // Texas Instruments SuperSPARC I
1658 .Case("TMS390Z55",
1659 "supersparc") // Texas Instruments SuperSPARC I with SuperCache
1660 .Case("MB86904", "supersparc") // Fujitsu microSPARC II
1661 .Case("MB86907", "supersparc") // Fujitsu TurboSPARC
1662 .Case("RT623", "hypersparc") // Ross hyperSPARC
1663 .Case("RT625", "hypersparc")
1664 .Case("RT626", "hypersparc")
1665 .Case("UltraSPARC-I", "ultrasparc")
1666 .Case("UltraSPARC-II", "ultrasparc")
1667 .Case("UltraSPARC-IIe", "ultrasparc")
1668 .Case("UltraSPARC-IIi", "ultrasparc")
1669 .Case("SPARC64-III", "ultrasparc")
1670 .Case("SPARC64-IV", "ultrasparc")
1671 .Case("UltraSPARC-III", "ultrasparc3")
1672 .Case("UltraSPARC-III+", "ultrasparc3")
1673 .Case("UltraSPARC-IIIi", "ultrasparc3")
1674 .Case("UltraSPARC-IIIi+", "ultrasparc3")
1675 .Case("UltraSPARC-IV", "ultrasparc3")
1676 .Case("UltraSPARC-IV+", "ultrasparc3")
1677 .Case("SPARC64-V", "ultrasparc3")
1678 .Case("SPARC64-VI", "ultrasparc3")
1679 .Case("SPARC64-VII", "ultrasparc3")
1680 .Case("UltraSPARC-T1", "niagara")
1681 .Case("UltraSPARC-T2", "niagara2")
1682 .Case("UltraSPARC-T2", "niagara2")
1683 .Case("UltraSPARC-T2+", "niagara2")
1684 .Case("SPARC-T3", "niagara3")
1685 .Case("SPARC-T4", "niagara4")
1686 .Case("SPARC-T5", "niagara4")
1687 // niagara7/m8 not supported by LLVM yet.
1688 .Case("SPARC-M7", "niagara4" /* "niagara7" */)
1689 .Case("SPARC-S7", "niagara4" /* "niagara7" */)
1690 .Case("SPARC-M8", "niagara4" /* "m8" */)
1691 .Default("generic");
1692#else
1693 return "generic";
1694#endif
1695}
1696#else
1697StringRef sys::getHostCPUName() { return "generic"; }
1698namespace llvm {
1699namespace sys {
1700namespace detail {
1701namespace x86 {
1702
1705}
1706
1707} // namespace x86
1708} // namespace detail
1709} // namespace sys
1710} // namespace llvm
1711#endif
1712
1713#if defined(__i386__) || defined(_M_IX86) || \
1714 defined(__x86_64__) || defined(_M_X64)
1716 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1717 unsigned MaxLevel;
1718
1719 if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
1720 return false;
1721
1722 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1723
1724 Features["cx8"] = (EDX >> 8) & 1;
1725 Features["cmov"] = (EDX >> 15) & 1;
1726 Features["mmx"] = (EDX >> 23) & 1;
1727 Features["fxsr"] = (EDX >> 24) & 1;
1728 Features["sse"] = (EDX >> 25) & 1;
1729 Features["sse2"] = (EDX >> 26) & 1;
1730
1731 Features["sse3"] = (ECX >> 0) & 1;
1732 Features["pclmul"] = (ECX >> 1) & 1;
1733 Features["ssse3"] = (ECX >> 9) & 1;
1734 Features["cx16"] = (ECX >> 13) & 1;
1735 Features["sse4.1"] = (ECX >> 19) & 1;
1736 Features["sse4.2"] = (ECX >> 20) & 1;
1737 Features["crc32"] = Features["sse4.2"];
1738 Features["movbe"] = (ECX >> 22) & 1;
1739 Features["popcnt"] = (ECX >> 23) & 1;
1740 Features["aes"] = (ECX >> 25) & 1;
1741 Features["rdrnd"] = (ECX >> 30) & 1;
1742
1743 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1744 // indicates that the AVX registers will be saved and restored on context
1745 // switch, then we have full AVX support.
1746 bool HasXSave = ((ECX >> 27) & 1) && !getX86XCR0(&EAX, &EDX);
1747 bool HasAVXSave = HasXSave && ((ECX >> 28) & 1) && ((EAX & 0x6) == 0x6);
1748#if defined(__APPLE__)
1749 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1750 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1751 // set right now.
1752 bool HasAVX512Save = true;
1753#else
1754 // AVX512 requires additional context to be saved by the OS.
1755 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1756#endif
1757 // AMX requires additional context to be saved by the OS.
1758 const unsigned AMXBits = (1 << 17) | (1 << 18);
1759 bool HasAMXSave = HasXSave && ((EAX & AMXBits) == AMXBits);
1760
1761 Features["avx"] = HasAVXSave;
1762 Features["fma"] = ((ECX >> 12) & 1) && HasAVXSave;
1763 // Only enable XSAVE if OS has enabled support for saving YMM state.
1764 Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
1765 Features["f16c"] = ((ECX >> 29) & 1) && HasAVXSave;
1766
1767 unsigned MaxExtLevel;
1768 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1769
1770 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1771 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1772 Features["sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1);
1773 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1774 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1775 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1776 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1777 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
1778 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1779 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1780 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1781
1782 Features["64bit"] = HasExtLeaf1 && ((EDX >> 29) & 1);
1783
1784 // Miscellaneous memory related features, detected by
1785 // using the 0x80000008 leaf of the CPUID instruction
1786 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1787 !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);
1788 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1789 Features["rdpru"] = HasExtLeaf8 && ((EBX >> 4) & 1);
1790 Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1);
1791
1792 bool HasLeaf7 =
1793 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1794
1795 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1796 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1797 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1798 // AVX2 is only supported if we have the OS save support from AVX.
1799 Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave;
1800 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1801 Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1);
1802 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1803 // AVX512 is only supported if the OS supports the context save for it.
1804 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1805 Features["evex512"] = Features["avx512f"];
1806 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1807 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1808 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1809 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1810 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1811 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1812 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1813 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1814 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1815 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1816 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1817 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1818
1819 Features["prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1);
1820 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
1821 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1822 Features["waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1);
1823 Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
1824 Features["shstk"] = HasLeaf7 && ((ECX >> 7) & 1);
1825 Features["gfni"] = HasLeaf7 && ((ECX >> 8) & 1);
1826 Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
1827 Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1828 Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
1829 Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
1830 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
1831 Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);
1832 Features["kl"] = HasLeaf7 && ((ECX >> 23) & 1); // key locker
1833 Features["cldemote"] = HasLeaf7 && ((ECX >> 25) & 1);
1834 Features["movdiri"] = HasLeaf7 && ((ECX >> 27) & 1);
1835 Features["movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1);
1836 Features["enqcmd"] = HasLeaf7 && ((ECX >> 29) & 1);
1837
1838 Features["uintr"] = HasLeaf7 && ((EDX >> 5) & 1);
1839 Features["avx512vp2intersect"] =
1840 HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save;
1841 Features["serialize"] = HasLeaf7 && ((EDX >> 14) & 1);
1842 Features["tsxldtrk"] = HasLeaf7 && ((EDX >> 16) & 1);
1843 // There are two CPUID leafs which information associated with the pconfig
1844 // instruction:
1845 // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
1846 // bit of EDX), while the EAX=0x1b leaf returns information on the
1847 // availability of specific pconfig leafs.
1848 // The target feature here only refers to the the first of these two.
1849 // Users might need to check for the availability of specific pconfig
1850 // leaves using cpuid, since that information is ignored while
1851 // detecting features using the "-march=native" flag.
1852 // For more info, see X86 ISA docs.
1853 Features["pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
1854 Features["amx-bf16"] = HasLeaf7 && ((EDX >> 22) & 1) && HasAMXSave;
1855 Features["avx512fp16"] = HasLeaf7 && ((EDX >> 23) & 1) && HasAVX512Save;
1856 Features["amx-tile"] = HasLeaf7 && ((EDX >> 24) & 1) && HasAMXSave;
1857 Features["amx-int8"] = HasLeaf7 && ((EDX >> 25) & 1) && HasAMXSave;
1858 // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
1859 // return all 0s for invalid subleaves so check the limit.
1860 bool HasLeaf7Subleaf1 =
1861 HasLeaf7 && EAX >= 1 &&
1862 !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1863 Features["sha512"] = HasLeaf7Subleaf1 && ((EAX >> 0) & 1);
1864 Features["sm3"] = HasLeaf7Subleaf1 && ((EAX >> 1) & 1);
1865 Features["sm4"] = HasLeaf7Subleaf1 && ((EAX >> 2) & 1);
1866 Features["raoint"] = HasLeaf7Subleaf1 && ((EAX >> 3) & 1);
1867 Features["avxvnni"] = HasLeaf7Subleaf1 && ((EAX >> 4) & 1) && HasAVXSave;
1868 Features["avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save;
1869 Features["amx-fp16"] = HasLeaf7Subleaf1 && ((EAX >> 21) & 1) && HasAMXSave;
1870 Features["cmpccxadd"] = HasLeaf7Subleaf1 && ((EAX >> 7) & 1);
1871 Features["hreset"] = HasLeaf7Subleaf1 && ((EAX >> 22) & 1);
1872 Features["avxifma"] = HasLeaf7Subleaf1 && ((EAX >> 23) & 1) && HasAVXSave;
1873 Features["avxvnniint8"] = HasLeaf7Subleaf1 && ((EDX >> 4) & 1) && HasAVXSave;
1874 Features["avxneconvert"] = HasLeaf7Subleaf1 && ((EDX >> 5) & 1) && HasAVXSave;
1875 Features["amx-complex"] = HasLeaf7Subleaf1 && ((EDX >> 8) & 1) && HasAMXSave;
1876 Features["avxvnniint16"] = HasLeaf7Subleaf1 && ((EDX >> 10) & 1) && HasAVXSave;
1877 Features["prefetchi"] = HasLeaf7Subleaf1 && ((EDX >> 14) & 1);
1878 Features["usermsr"] = HasLeaf7Subleaf1 && ((EDX >> 15) & 1);
1879 Features["avx10.1-256"] = HasLeaf7Subleaf1 && ((EDX >> 19) & 1);
1880 bool HasAPXF = HasLeaf7Subleaf1 && ((EDX >> 21) & 1);
1881 Features["egpr"] = HasAPXF;
1882 Features["push2pop2"] = HasAPXF;
1883 Features["ppx"] = HasAPXF;
1884 Features["ndd"] = HasAPXF;
1885 Features["ccmp"] = HasAPXF;
1886 Features["cf"] = HasAPXF;
1887
1888 bool HasLeafD = MaxLevel >= 0xd &&
1889 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1890
1891 // Only enable XSAVE if OS has enabled support for saving YMM state.
1892 Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1893 Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1894 Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
1895
1896 bool HasLeaf14 = MaxLevel >= 0x14 &&
1897 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX);
1898
1899 Features["ptwrite"] = HasLeaf14 && ((EBX >> 4) & 1);
1900
1901 bool HasLeaf19 =
1902 MaxLevel >= 0x19 && !getX86CpuIDAndInfo(0x19, &EAX, &EBX, &ECX, &EDX);
1903 Features["widekl"] = HasLeaf7 && HasLeaf19 && ((EBX >> 2) & 1);
1904
1905 bool HasLeaf24 =
1906 MaxLevel >= 0x24 && !getX86CpuIDAndInfo(0x24, &EAX, &EBX, &ECX, &EDX);
1907 Features["avx10.1-512"] =
1908 Features["avx10.1-256"] && HasLeaf24 && ((EBX >> 18) & 1);
1909
1910 return true;
1911}
1912#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1914 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1915 if (!P)
1916 return false;
1917
1919 P->getBuffer().split(Lines, "\n");
1920
1922
1923 // Look for the CPU features.
1924 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1925 if (Lines[I].starts_with("Features")) {
1926 Lines[I].split(CPUFeatures, ' ');
1927 break;
1928 }
1929
1930#if defined(__aarch64__)
1931 // Keep track of which crypto features we have seen
1932 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1933 uint32_t crypto = 0;
1934#endif
1935
1936 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1937 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1938#if defined(__aarch64__)
1939 .Case("asimd", "neon")
1940 .Case("fp", "fp-armv8")
1941 .Case("crc32", "crc")
1942 .Case("atomics", "lse")
1943 .Case("sve", "sve")
1944 .Case("sve2", "sve2")
1945#else
1946 .Case("half", "fp16")
1947 .Case("neon", "neon")
1948 .Case("vfpv3", "vfp3")
1949 .Case("vfpv3d16", "vfp3d16")
1950 .Case("vfpv4", "vfp4")
1951 .Case("idiva", "hwdiv-arm")
1952 .Case("idivt", "hwdiv")
1953#endif
1954 .Default("");
1955
1956#if defined(__aarch64__)
1957 // We need to check crypto separately since we need all of the crypto
1958 // extensions to enable the subtarget feature
1959 if (CPUFeatures[I] == "aes")
1960 crypto |= CAP_AES;
1961 else if (CPUFeatures[I] == "pmull")
1962 crypto |= CAP_PMULL;
1963 else if (CPUFeatures[I] == "sha1")
1964 crypto |= CAP_SHA1;
1965 else if (CPUFeatures[I] == "sha2")
1966 crypto |= CAP_SHA2;
1967#endif
1968
1969 if (LLVMFeatureStr != "")
1970 Features[LLVMFeatureStr] = true;
1971 }
1972
1973#if defined(__aarch64__)
1974 // If we have all crypto bits we can add the feature
1975 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1976 Features["crypto"] = true;
1977#endif
1978
1979 return true;
1980}
1981#elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
1983 if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
1984 Features["neon"] = true;
1985 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
1986 Features["crc"] = true;
1987 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
1988 Features["crypto"] = true;
1989
1990 return true;
1991}
1992#elif defined(__linux__) && defined(__loongarch__)
1993#include <sys/auxv.h>
1995 unsigned long hwcap = getauxval(AT_HWCAP);
1996 bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
1997 uint32_t cpucfg2 = 0x2;
1998 __asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2));
1999
2000 Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
2001 Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
2002
2003 Features["lsx"] = hwcap & (1UL << 4); // HWCAP_LOONGARCH_LSX
2004 Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
2005 Features["lvz"] = hwcap & (1UL << 9); // HWCAP_LOONGARCH_LVZ
2006
2007 return true;
2008}
2009#else
2010bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
2011#endif
2012
2013#if __APPLE__
2014/// \returns the \p triple, but with the Host's arch spliced in.
2015static Triple withHostArch(Triple T) {
2016#if defined(__arm__)
2017 T.setArch(Triple::arm);
2018 T.setArchName("arm");
2019#elif defined(__arm64e__)
2021 T.setArchName("arm64e");
2022#elif defined(__aarch64__)
2023 T.setArch(Triple::aarch64);
2024 T.setArchName("arm64");
2025#elif defined(__x86_64h__)
2026 T.setArch(Triple::x86_64);
2027 T.setArchName("x86_64h");
2028#elif defined(__x86_64__)
2029 T.setArch(Triple::x86_64);
2030 T.setArchName("x86_64");
2031#elif defined(__i386__)
2032 T.setArch(Triple::x86);
2033 T.setArchName("i386");
2034#elif defined(__powerpc__)
2035 T.setArch(Triple::ppc);
2036 T.setArchName("powerpc");
2037#else
2038# error "Unimplemented host arch fixup"
2039#endif
2040 return T;
2041}
2042#endif
2043
2045 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
2046 Triple PT(Triple::normalize(TargetTripleString));
2047
2048#if __APPLE__
2049 /// In Universal builds, LLVM_HOST_TRIPLE will have the wrong arch in one of
2050 /// the slices. This fixes that up.
2051 PT = withHostArch(PT);
2052#endif
2053
2054 if (sizeof(void *) == 8 && PT.isArch32Bit())
2055 PT = PT.get64BitArchVariant();
2056 if (sizeof(void *) == 4 && PT.isArch64Bit())
2057 PT = PT.get32BitArchVariant();
2058
2059 return PT.str();
2060}
2061
2063#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
2064 std::string CPU = std::string(sys::getHostCPUName());
2065 if (CPU == "generic")
2066 CPU = "(unknown)";
2067 OS << " Default target: " << sys::getDefaultTargetTriple() << '\n'
2068 << " Host CPU: " << CPU << '\n';
2069#endif
2070}
This file defines the StringMap class.
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:203
Given that RA is a live value
T Content
static std::unique_ptr< llvm::MemoryBuffer > LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent()
Definition: Host.cpp:65
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
raw_pwrite_stream & OS
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
Represents either an error or a value T.
Definition: ErrorOr.h:56
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:127
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:692
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
iterator begin() const
Definition: StringRef.h:111
iterator end() const
Definition: StringRef.h:113
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:269
static constexpr size_t npos
Definition: StringRef.h:52
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
StringSwitch & StartsWith(StringLiteral S, T Value)
Definition: StringSwitch.h:83
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::Triple get32BitArchVariant() const
Form a triple with a 32-bit variant of the current architecture.
Definition: Triple.cpp:1642
std::string normalize() const
Return the normalized form of this triple's string.
Definition: Triple.h:366
llvm::Triple get64BitArchVariant() const
Form a triple with a 64-bit variant of the current architecture.
Definition: Triple.cpp:1723
const std::string & str() const
Definition: Triple.h:436
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1630
@ AArch64SubArch_arm64e
Definition: Triple.h:149
bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition: Triple.cpp:1634
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ CPU_SUBTYPE_POWERPC_970
Definition: MachO.h:1663
@ CPU_SUBTYPE_POWERPC_604e
Definition: MachO.h:1658
@ CPU_SUBTYPE_POWERPC_603e
Definition: MachO.h:1655
@ CPU_SUBTYPE_POWERPC_7400
Definition: MachO.h:1661
@ CPU_SUBTYPE_POWERPC_604
Definition: MachO.h:1657
@ CPU_SUBTYPE_POWERPC_750
Definition: MachO.h:1660
@ CPU_SUBTYPE_POWERPC_601
Definition: MachO.h:1652
@ CPU_SUBTYPE_POWERPC_620
Definition: MachO.h:1659
@ CPU_SUBTYPE_POWERPC_603ev
Definition: MachO.h:1656
@ CPU_SUBTYPE_POWERPC_603
Definition: MachO.h:1654
@ CPU_SUBTYPE_POWERPC_7450
Definition: MachO.h:1662
@ CPU_SUBTYPE_POWERPC_602
Definition: MachO.h:1653
Helper functions to extract CPU details from CPUID on x86.
Definition: Host.h:72
VendorSignatures getVendorSignature(unsigned *MaxLeaf=nullptr)
Returns the host CPU's vendor.
Definition: Host.cpp:1703
StringRef getHostCPUNameForS390x(StringRef ProcCpuinfoContent)
Definition: Host.cpp:389
StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent)
Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
Definition: Host.cpp:76
StringRef getHostCPUNameForBPF()
Definition: Host.cpp:454
StringRef getHostCPUNameForARM(StringRef ProcCpuinfoContent)
Definition: Host.cpp:159
StringRef getHostCPUNameForRISCV(StringRef ProcCpuinfoContent)
Definition: Host.cpp:434
StringRef getHostCPUNameForSPARC(StringRef ProcCpuinfoContent)
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1697
void printDefaultTargetAndDetectedCPU(raw_ostream &OS)
This is a function compatible with cl::AddExtraVersionPrinter, which adds info about the current targ...
Definition: Host.cpp:2062
bool getHostCPUFeatures(StringMap< bool, MallocAllocator > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:2044
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:456
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.