LLVM 24.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
14#include "llvm/ADT/Bitfields.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
21#include "llvm/Config/llvm-config.h"
27#include <string.h>
28
29// Include the platform-specific parts of this class.
30#ifdef LLVM_ON_UNIX
31#include "Unix/Host.inc"
32#include <sched.h>
33#endif
34#ifdef _WIN32
35#include "Windows/Host.inc"
36#endif
37#ifdef _MSC_VER
38#include <intrin.h>
39#endif
40#ifdef __MVS__
41#include "llvm/Support/BCD.h"
42#endif
43#if defined(__APPLE__)
44#include <mach/host_info.h>
45#include <mach/mach.h>
46#include <mach/mach_host.h>
47#include <mach/machine.h>
48#include <sys/param.h>
49#include <sys/sysctl.h>
50#endif
51#ifdef _AIX
52#include <sys/systemcfg.h>
53#endif
54#if defined(__sun__) && defined(__svr4__)
55#include <kstat.h>
56#endif
57#if defined(__GNUC__) || defined(__clang__)
58#if (defined(__i386__) || defined(__x86_64__)) && !defined(_MSC_VER)
59#include <cpuid.h>
60#endif
61#endif
62
63#define DEBUG_TYPE "host-detection"
64
65//===----------------------------------------------------------------------===//
66//
67// Implementations of the CPU detection routines
68//
69//===----------------------------------------------------------------------===//
70
71using namespace llvm;
72
73[[maybe_unused]] static std::unique_ptr<llvm::MemoryBuffer>
75 const char *CPUInfoFile = "/proc/cpuinfo";
76 if (const char *CpuinfoIntercept = std::getenv("LLVM_CPUINFO"))
77 CPUInfoFile = CpuinfoIntercept;
80
81 if (std::error_code EC = Text.getError()) {
82 llvm::errs() << "Can't read " << CPUInfoFile << ": " << EC.message()
83 << "\n";
84 return nullptr;
85 }
86 return std::move(*Text);
87}
88
90 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
91 // and so we must use an operating-system interface to determine the current
92 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
93 const char *generic = "generic";
94
95 // The cpu line is second (after the 'processor: 0' line), so if this
96 // buffer is too small then something has changed (or is wrong).
97 StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin();
98 StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end();
99
100 StringRef::const_iterator CIP = CPUInfoStart;
101
102 StringRef::const_iterator CPUStart = nullptr;
103 size_t CPULen = 0;
104
105 // We need to find the first line which starts with cpu, spaces, and a colon.
106 // After the colon, there may be some additional spaces and then the cpu type.
107 while (CIP < CPUInfoEnd && CPUStart == nullptr) {
108 if (CIP < CPUInfoEnd && *CIP == '\n')
109 ++CIP;
110
111 if (CIP < CPUInfoEnd && *CIP == 'c') {
112 ++CIP;
113 if (CIP < CPUInfoEnd && *CIP == 'p') {
114 ++CIP;
115 if (CIP < CPUInfoEnd && *CIP == 'u') {
116 ++CIP;
117 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
118 ++CIP;
119
120 if (CIP < CPUInfoEnd && *CIP == ':') {
121 ++CIP;
122 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
123 ++CIP;
124
125 if (CIP < CPUInfoEnd) {
126 CPUStart = CIP;
127 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
128 *CIP != ',' && *CIP != '\n'))
129 ++CIP;
130 CPULen = CIP - CPUStart;
131 }
132 }
133 }
134 }
135 }
136
137 if (CPUStart == nullptr)
138 while (CIP < CPUInfoEnd && *CIP != '\n')
139 ++CIP;
140 }
141
142 if (CPUStart == nullptr)
143 return generic;
144
145 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
146 // Linux's cpu_name, cut at the first space or comma.
147 .Case("603", "603")
148 .Case("603e", "603e")
149 // 603e cores. The e300c2 has no FPU, so it stays generic rather than
150 // taking 603e's fres and frsqrte.
151 .Cases({"82xx", "G2_LE", "e300c1", "e300c3", "e300c4"}, "603e")
152 .Case("603ev", "603ev")
153 .Case("604", "604")
154 .Cases({"604e", "604r", "604ev"}, "604e")
155 .Cases(
156 {"740/750", "745/755", "750CX", "750CXe", "750CL", "750FX", "750GX"},
157 "750")
158 .Cases({"7400", "7410"}, "7400")
159 .Cases({"7450", "7455", "7447/7457", "7447A", "7448"}, "7450")
160 .Case("G4", "g4")
161 .Case("POWER4", "970")
162 .Case("PPC970FX", "970")
163 .Case("PPC970MP", "970")
164 .Case("G5", "g5")
165 .Case("POWER5", "g5")
166 .Case("A2", "a2")
167 .Case("POWER6", "pwr6")
168 .Case("POWER7", "pwr7")
169 .Case("POWER8", "pwr8")
170 .Case("POWER8E", "pwr8")
171 .Case("POWER8NVL", "pwr8")
172 .Case("POWER9", "pwr9")
173 .Case("POWER10", "pwr10")
174 .Case("POWER11", "pwr11")
175 // FIXME: If we get a simulator or machine with the capabilities of
176 // mcpu=future, we should revisit this and add the name reported by the
177 // simulator/machine.
178 .Default(generic);
179}
180
183 StringRef Part, ArrayRef<StringRef> Parts,
184 function_ref<unsigned()> GetVariant) {
185
186 auto MatchBigLittle = [](auto const &Parts, StringRef Big, StringRef Little) {
187 if (Parts.size() == 2)
188 return (Parts[0] == Big && Parts[1] == Little) ||
189 (Parts[1] == Big && Parts[0] == Little);
190 return false;
191 };
192
193 if (Implementer == "0x41") { // ARM Ltd.
194 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
195 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
196 if (Hardware.ends_with("MSM8994") || Hardware.ends_with("MSM8996"))
197 return "cortex-a53";
198
199 // Detect big.LITTLE systems.
200 if (MatchBigLittle(Parts, "0xd85", "0xd87"))
201 return "cortex-x925";
202
203 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
204 // values correspond to the "Part number" in the CP15/c0 register. The
205 // contents are specified in the various processor manuals.
206 // This corresponds to the Main ID Register in Technical Reference Manuals.
207 // and is used in programs like sys-utils
208 return StringSwitch<const char *>(Part)
209 .Case("0x926", "arm926ej-s")
210 .Case("0xb02", "mpcore")
211 .Case("0xb36", "arm1136j-s")
212 .Case("0xb56", "arm1156t2-s")
213 .Case("0xb76", "arm1176jz-s")
214 .Case("0xd8a", "c1-nano")
215 .Case("0xd90", "c1-premium")
216 .Case("0xd8b", "c1-pro")
217 .Case("0xd8c", "c1-ultra")
218 .Case("0xd96", "c2-ultra")
219 .Case("0xc05", "cortex-a5")
220 .Case("0xc07", "cortex-a7")
221 .Case("0xc08", "cortex-a8")
222 .Case("0xc09", "cortex-a9")
223 .Case("0xc0f", "cortex-a15")
224 .Case("0xc0e", "cortex-a17")
225 .Case("0xc20", "cortex-m0")
226 .Case("0xc23", "cortex-m3")
227 .Case("0xc24", "cortex-m4")
228 .Case("0xc27", "cortex-m7")
229 .Case("0xd20", "cortex-m23")
230 .Case("0xd21", "cortex-m33")
231 .Case("0xd24", "cortex-m52")
232 .Case("0xd22", "cortex-m55")
233 .Case("0xd23", "cortex-m85")
234 .Case("0xc18", "cortex-r8")
235 .Case("0xd13", "cortex-r52")
236 .Case("0xd16", "cortex-r52plus")
237 .Case("0xd15", "cortex-r82")
238 .Case("0xd14", "cortex-r82ae")
239 .Case("0xd02", "cortex-a34")
240 .Case("0xd04", "cortex-a35")
241 .Case("0xd8f", "cortex-a320")
242 .Case("0xd03", "cortex-a53")
243 .Case("0xd05", "cortex-a55")
244 .Case("0xd46", "cortex-a510")
245 .Case("0xd80", "cortex-a520")
246 .Case("0xd88", "cortex-a520ae")
247 .Case("0xd07", "cortex-a57")
248 .Case("0xd06", "cortex-a65")
249 .Case("0xd43", "cortex-a65ae")
250 .Case("0xd08", "cortex-a72")
251 .Case("0xd09", "cortex-a73")
252 .Case("0xd0a", "cortex-a75")
253 .Case("0xd0b", "cortex-a76")
254 .Case("0xd0e", "cortex-a76ae")
255 .Case("0xd0d", "cortex-a77")
256 .Case("0xd41", "cortex-a78")
257 .Case("0xd42", "cortex-a78ae")
258 .Case("0xd4b", "cortex-a78c")
259 .Case("0xd47", "cortex-a710")
260 .Case("0xd4d", "cortex-a715")
261 .Case("0xd81", "cortex-a720")
262 .Case("0xd89", "cortex-a720ae")
263 .Case("0xd87", "cortex-a725")
264 .Case("0xd44", "cortex-x1")
265 .Case("0xd4c", "cortex-x1c")
266 .Case("0xd48", "cortex-x2")
267 .Case("0xd4e", "cortex-x3")
268 .Case("0xd82", "cortex-x4")
269 .Case("0xd85", "cortex-x925")
270 .Case("0xd4a", "neoverse-e1")
271 .Case("0xd0c", "neoverse-n1")
272 .Case("0xd49", "neoverse-n2")
273 .Case("0xd8e", "neoverse-n3")
274 .Case("0xd40", "neoverse-v1")
275 .Case("0xd4f", "neoverse-v2")
276 .Case("0xd84", "neoverse-v3")
277 .Case("0xd83", "neoverse-v3ae")
278 .Default("generic");
279 }
280
281 if (Implementer == "0x42" || Implementer == "0x43") { // Broadcom | Cavium.
282 return StringSwitch<const char *>(Part)
283 .Case("0x516", "thunderx2t99")
284 .Case("0x0516", "thunderx2t99")
285 .Case("0xaf", "thunderx2t99")
286 .Case("0x0af", "thunderx2t99")
287 .Case("0xa1", "thunderxt88")
288 .Case("0x0a1", "thunderxt88")
289 .Default("generic");
290 }
291
292 if (Implementer == "0x46") { // Fujitsu Ltd.
293 return StringSwitch<const char *>(Part)
294 .Case("0x001", "a64fx")
295 .Case("0x003", "fujitsu-monaka")
296 .Default("generic");
297 }
298
299 if (Implementer == "0x4e") { // NVIDIA Corporation
300 return StringSwitch<const char *>(Part)
301 .Case("0x004", "carmel")
302 .Case("0x10", "olympus")
303 .Case("0x010", "olympus")
304 .Case("0x11", "rigel")
305 .Case("0x011", "rigel")
306 .Default("generic");
307 }
308
309 if (Implementer == "0x48") // HiSilicon Technologies, Inc.
310 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
311 // values correspond to the "Part number" in the CP15/c0 register. The
312 // contents are specified in the various processor manuals.
313 return StringSwitch<const char *>(Part)
314 .Case("0xd01", "tsv110")
315 .Case("0xd06", "hip12")
316 .Default("generic");
317
318 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
319 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
320 // values correspond to the "Part number" in the CP15/c0 register. The
321 // contents are specified in the various processor manuals.
322 return StringSwitch<const char *>(Part)
323 .Case("0x06f", "krait") // APQ8064
324 .Case("0x201", "kryo")
325 .Case("0x205", "kryo")
326 .Case("0x211", "kryo")
327 .Case("0x800", "cortex-a73") // Kryo 2xx Gold
328 .Case("0x801", "cortex-a73") // Kryo 2xx Silver
329 .Case("0x802", "cortex-a75") // Kryo 3xx Gold
330 .Case("0x803", "cortex-a75") // Kryo 3xx Silver
331 .Case("0x804", "cortex-a76") // Kryo 4xx Gold
332 .Case("0x805", "cortex-a76") // Kryo 4xx/5xx Silver
333 .Case("0xc00", "falkor")
334 .Case("0xc01", "saphira")
335 .Case("0x001", "oryon-1")
336 .Default("generic");
337 if (Implementer == "0x53") { // Samsung Electronics Co., Ltd.
338 // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
339 // any predictive pattern across variants and parts.
340
341 // Look for the CPU variant line, whose value is a 1 digit hexadecimal
342 // number, corresponding to the Variant bits in the CP15/C0 register.
343 unsigned Variant = GetVariant();
344
345 // Convert the CPU part line, whose value is a 3 digit hexadecimal number,
346 // corresponding to the PartNum bits in the CP15/C0 register.
347 unsigned PartAsInt;
348 Part.getAsInteger(0, PartAsInt);
349
350 unsigned Exynos = (Variant << 12) | PartAsInt;
351 switch (Exynos) {
352 default:
353 // Default by falling through to Exynos M3.
354 [[fallthrough]];
355 case 0x1002:
356 return "exynos-m3";
357 case 0x1003:
358 return "exynos-m4";
359 }
360 }
361
362 if (Implementer == "0x61") { // Apple
363 return StringSwitch<const char *>(Part)
364 .Case("0x020", "apple-m1")
365 .Case("0x021", "apple-m1")
366 .Case("0x022", "apple-m1")
367 .Case("0x023", "apple-m1")
368 .Case("0x024", "apple-m1")
369 .Case("0x025", "apple-m1")
370 .Case("0x028", "apple-m1")
371 .Case("0x029", "apple-m1")
372 .Case("0x030", "apple-m2")
373 .Case("0x031", "apple-m2")
374 .Case("0x032", "apple-m2")
375 .Case("0x033", "apple-m2")
376 .Case("0x034", "apple-m2")
377 .Case("0x035", "apple-m2")
378 .Case("0x038", "apple-m2")
379 .Case("0x039", "apple-m2")
380 .Case("0x049", "apple-m3")
381 .Case("0x048", "apple-m3")
382 .Default("generic");
383 }
384
385 if (Implementer == "0x63") { // Arm China.
386 return StringSwitch<const char *>(Part)
387 .Case("0x132", "star-mc1")
388 .Case("0xd25", "star-mc3")
389 .Default("generic");
390 }
391
392 if (Implementer == "0x6d") { // Microsoft Corporation.
393 // The Microsoft Azure Cobalt 100 CPU is handled as a Neoverse N2.
394 return StringSwitch<const char *>(Part)
395 .Case("0xd49", "neoverse-n2")
396 .Default("generic");
397 }
398
399 if (Implementer == "0xc0") { // Ampere Computing
400 return StringSwitch<const char *>(Part)
401 .Case("0xac3", "ampere1")
402 .Case("0xac4", "ampere1a")
403 .Case("0xac5", "ampere1b")
404 .Case("0xac7", "ampere1c")
405 .Default("generic");
406 }
407
408 return "generic";
409}
410
412 // The cpuid register on arm is not accessible from user space. On Linux,
413 // it is exposed through the /proc/cpuinfo file.
414
415 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
416 // in all cases.
418 ProcCpuinfoContent.split(Lines, '\n');
419
420 // Look for the CPU implementer and hardware lines, and store the CPU part
421 // numbers found.
422 StringRef Implementer;
423 StringRef Hardware;
425 for (StringRef Line : Lines) {
426 if (Line.consume_front("CPU implementer"))
427 Implementer = Line.ltrim("\t :");
428 else if (Line.consume_front("Hardware"))
429 Hardware = Line.ltrim("\t :");
430 else if (Line.consume_front("CPU part"))
431 Parts.emplace_back(Line.ltrim("\t :"));
432 }
433
434 // Last `Part' seen, in case we don't analyse all `Parts' parsed.
435 StringRef Part = Parts.empty() ? StringRef() : Parts.back();
436
437 // Remove duplicate `Parts'.
438 llvm::sort(Parts);
439 Parts.erase(llvm::unique(Parts), Parts.end());
440
441 auto GetVariant = [&]() {
442 unsigned Variant = 0;
443 for (auto I : Lines)
444 if (I.consume_front("CPU variant"))
445 I.ltrim("\t :").getAsInteger(0, Variant);
446 return Variant;
447 };
448
449 return getHostCPUNameForARMFromComponents(Implementer, Hardware, Part, Parts,
450 GetVariant);
451}
452
454 ArrayRef<uint64_t> UniqueCpuInfos) {
455 // On Windows, the registry provides cached copied of the MIDR_EL1 register.
457 using Implementer = Bitfield::Element<uint16_t, 24, 8>;
459
460 SmallVector<std::string> PartsHolder;
461 PartsHolder.reserve(UniqueCpuInfos.size());
462 for (auto Info : UniqueCpuInfos)
463 PartsHolder.push_back("0x" + utohexstr(Bitfield::get<PartNum>(Info),
464 /*LowerCase*/ true,
465 /*Width*/ 3));
466
468 Parts.reserve(PartsHolder.size());
469 for (const auto &Part : PartsHolder)
470 Parts.push_back(Part);
471
473 "0x" + utohexstr(Bitfield::get<Implementer>(PrimaryCpuInfo),
474 /*LowerCase*/ true,
475 /*Width*/ 2),
476 /*Hardware*/ "",
477 "0x" + utohexstr(Bitfield::get<PartNum>(PrimaryCpuInfo),
478 /*LowerCase*/ true,
479 /*Width*/ 3),
480 Parts, [=]() { return Bitfield::get<Variant>(PrimaryCpuInfo); });
481}
482
483namespace {
484StringRef getCPUNameFromS390Model(unsigned int Id, bool HaveVectorSupport) {
485 switch (Id) {
486 case 2064: // z900 not supported by LLVM
487 case 2066:
488 case 2084: // z990 not supported by LLVM
489 case 2086:
490 case 2094: // z9-109 not supported by LLVM
491 case 2096:
492 return "generic";
493 case 2097:
494 case 2098:
495 return "z10";
496 case 2817:
497 case 2818:
498 return "z196";
499 case 2827:
500 case 2828:
501 return "zEC12";
502 case 2964:
503 case 2965:
504 return HaveVectorSupport? "z13" : "zEC12";
505 case 3906:
506 case 3907:
507 return HaveVectorSupport? "z14" : "zEC12";
508 case 8561:
509 case 8562:
510 return HaveVectorSupport? "z15" : "zEC12";
511 case 3931:
512 case 3932:
513 return HaveVectorSupport? "z16" : "zEC12";
514 case 9175:
515 case 9176:
516 default:
517 return HaveVectorSupport? "z17" : "zEC12";
518 }
519}
520} // end anonymous namespace
521
523 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
524
525 // The "processor 0:" line comes after a fair amount of other information,
526 // including a cache breakdown, but this should be plenty.
528 ProcCpuinfoContent.split(Lines, '\n');
529
530 // Look for the CPU features.
531 SmallVector<StringRef, 32> CPUFeatures;
532 for (StringRef Line : Lines)
533 if (Line.starts_with("features")) {
534 size_t Pos = Line.find(':');
535 if (Pos != StringRef::npos) {
536 Line.drop_front(Pos + 1).split(CPUFeatures, ' ');
537 break;
538 }
539 }
540
541 // We need to check for the presence of vector support independently of
542 // the machine type, since we may only use the vector register set when
543 // supported by the kernel (and hypervisor).
544 bool HaveVectorSupport = llvm::is_contained(CPUFeatures, "vx");
545
546 // Now check the processor machine type.
547 for (StringRef Line : Lines) {
548 if (Line.starts_with("processor ")) {
549 size_t Pos = Line.find("machine = ");
550 if (Pos != StringRef::npos) {
551 Pos += sizeof("machine = ") - 1;
552 unsigned int Id;
553 if (!Line.drop_front(Pos).getAsInteger(10, Id))
554 return getCPUNameFromS390Model(Id, HaveVectorSupport);
555 }
556 break;
557 }
558 }
559
560 return "generic";
561}
562
564 // There are 24 lines in /proc/cpuinfo
566 ProcCpuinfoContent.split(Lines, '\n');
567
568 // Look for uarch line to determine cpu name
569 StringRef UArch;
570 for (StringRef Line : Lines) {
571 if (Line.starts_with("uarch")) {
572 UArch = Line.substr(5).ltrim("\t :");
573 break;
574 }
575 }
576
577 return StringSwitch<const char *>(UArch)
578 .Case("eswin,eic770x", "sifive-p550")
579 .Case("sifive,u74-mc", "sifive-u74")
580 .Case("sifive,bullet0", "sifive-u74")
581 .Case("spacemit,x60", "spacemit-x60")
582 .Case("spacemit,x100", "spacemit-x100")
583 .Case("spacemit,a100", "spacemit-a100")
584 .Default("");
585}
586
588#if !defined(__linux__) || !defined(__x86_64__)
589 return "generic";
590#else
591 uint8_t v3_insns[40] __attribute__ ((aligned (8))) =
592 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
593 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
594 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
595 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
596 /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
597 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
598 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
599 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
600 /* BPF_EXIT_INSN() */
601 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
602
603 uint8_t v2_insns[40] __attribute__ ((aligned (8))) =
604 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
605 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
606 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
607 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
608 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
609 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
610 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
611 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
612 /* BPF_EXIT_INSN() */
613 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
614
615 struct bpf_prog_load_attr {
616 uint32_t prog_type;
617 uint32_t insn_cnt;
618 uint64_t insns;
619 uint64_t license;
620 uint32_t log_level;
621 uint32_t log_size;
622 uint64_t log_buf;
623 uint32_t kern_version;
624 uint32_t prog_flags;
625 } attr = {};
626 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
627 attr.insn_cnt = 5;
628 attr.insns = (uint64_t)v3_insns;
629 attr.license = (uint64_t)"DUMMY";
630
631 int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr,
632 sizeof(attr));
633 if (fd >= 0) {
634 close(fd);
635 return "v3";
636 }
637
638 /* Clear the whole attr in case its content changed by syscall. */
639 memset(&attr, 0, sizeof(attr));
640 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
641 attr.insn_cnt = 5;
642 attr.insns = (uint64_t)v2_insns;
643 attr.license = (uint64_t)"DUMMY";
644 fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr));
645 if (fd >= 0) {
646 close(fd);
647 return "v2";
648 }
649 return "v1";
650#endif
651}
652
653#if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \
654 defined(_M_X64)) && \
655 !defined(_M_ARM64EC)
656
657/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
658/// the specified arguments. If we can't run cpuid on the host, return true.
659static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
660 unsigned *rECX, unsigned *rEDX) {
661#if (defined(__i386__) || defined(__x86_64__)) && !defined(_MSC_VER)
662 return !__get_cpuid(value, rEAX, rEBX, rECX, rEDX);
663#elif defined(_MSC_VER)
664 // The MSVC intrinsic is portable across x86 and x64.
665 int registers[4];
666 __cpuid(registers, value);
667 *rEAX = registers[0];
668 *rEBX = registers[1];
669 *rECX = registers[2];
670 *rEDX = registers[3];
671 return false;
672#else
673 return true;
674#endif
675}
676
677namespace llvm {
678namespace sys {
679namespace detail {
680namespace x86 {
681
682VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
683 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
684 if (MaxLeaf == nullptr)
685 MaxLeaf = &EAX;
686 else
687 *MaxLeaf = 0;
688
689 if (getX86CpuIDAndInfo(0, MaxLeaf, &EBX, &ECX, &EDX) || *MaxLeaf < 1)
690 return VendorSignatures::UNKNOWN;
691
692 // "Genu ineI ntel"
693 if (EBX == 0x756e6547 && EDX == 0x49656e69 && ECX == 0x6c65746e)
694 return VendorSignatures::GENUINE_INTEL;
695
696 // "Auth enti cAMD"
697 if (EBX == 0x68747541 && EDX == 0x69746e65 && ECX == 0x444d4163)
698 return VendorSignatures::AUTHENTIC_AMD;
699
700 // "Hygo nGen uine"
701 if (EBX == 0x6f677948 && EDX == 0x6e65476e && ECX == 0x656e6975)
702 return VendorSignatures::HYGON_GENUINE;
703
704 return VendorSignatures::UNKNOWN;
705}
706
707} // namespace x86
708} // namespace detail
709} // namespace sys
710} // namespace llvm
711
712using namespace llvm::sys::detail::x86;
713
714/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
715/// the 4 values in the specified arguments. If we can't run cpuid on the host,
716/// return true.
717static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
718 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
719 unsigned *rEDX) {
720 // TODO(boomanaiden154): When the minimum toolchain versions for gcc and clang
721 // are such that __cpuidex is defined within cpuid.h for both, we can remove
722 // the __get_cpuid_count function and share the MSVC implementation between
723 // all three.
724#if (defined(__i386__) || defined(__x86_64__)) && !defined(_MSC_VER)
725 return !__get_cpuid_count(value, subleaf, rEAX, rEBX, rECX, rEDX);
726#elif defined(_MSC_VER)
727 int registers[4];
728 __cpuidex(registers, value, subleaf);
729 *rEAX = registers[0];
730 *rEBX = registers[1];
731 *rECX = registers[2];
732 *rEDX = registers[3];
733 return false;
734#else
735 return true;
736#endif
737}
738
739// Read control register 0 (XCR0). Used to detect features such as AVX.
740static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
741 // TODO(boomanaiden154): When the minimum toolchain versions for gcc and clang
742 // are such that _xgetbv is supported by both, we can unify the implementation
743 // with MSVC and remove all inline assembly.
744#if defined(__GNUC__) || defined(__clang__)
745 // Check xgetbv; this uses a .byte sequence instead of the instruction
746 // directly because older assemblers do not include support for xgetbv and
747 // there is no easy way to conditionally compile based on the assembler used.
748 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
749 return false;
750#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
751 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
752 *rEAX = Result;
753 *rEDX = Result >> 32;
754 return false;
755#else
756 return true;
757#endif
758}
759
760static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
761 unsigned *Model) {
762 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
763 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
764 if (*Family == 6 || *Family == 0xf) {
765 if (*Family == 0xf)
766 // Examine extended family ID if family ID is F.
767 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
768 // Examine extended model ID if family ID is 6 or F.
769 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
770 }
771}
772
773#define testFeature(F) (Features[F / 32] & (1 << (F % 32))) != 0
774
775static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
776 unsigned Model,
777 const unsigned *Features,
778 unsigned *Type,
779 unsigned *Subtype) {
780 StringRef CPU;
781
782 switch (Family) {
783 case 0x3:
784 CPU = "i386";
785 break;
786 case 0x4:
787 CPU = "i486";
788 break;
789 case 0x5:
790 if (testFeature(X86::FEATURE_MMX)) {
791 CPU = "pentium-mmx";
792 break;
793 }
794 CPU = "pentium";
795 break;
796 case 0x6:
797 switch (Model) {
798 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
799 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
800 // mobile processor, Intel Core 2 Extreme processor, Intel
801 // Pentium Dual-Core processor, Intel Xeon processor, model
802 // 0Fh. All processors are manufactured using the 65 nm process.
803 case 0x16: // Intel Celeron processor model 16h. All processors are
804 // manufactured using the 65 nm process
805 CPU = "core2";
806 *Type = X86::INTEL_CORE2;
807 break;
808 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
809 // 17h. All processors are manufactured using the 45 nm process.
810 //
811 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
812 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
813 // the 45 nm process.
814 CPU = "penryn";
815 *Type = X86::INTEL_CORE2;
816 break;
817 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
818 // processors are manufactured using the 45 nm process.
819 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
820 // As found in a Summer 2010 model iMac.
821 case 0x1f:
822 case 0x2e: // Nehalem EX
823 CPU = "nehalem";
824 *Type = X86::INTEL_COREI7;
825 *Subtype = X86::INTEL_COREI7_NEHALEM;
826 break;
827 case 0x25: // Intel Core i7, laptop version.
828 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
829 // processors are manufactured using the 32 nm process.
830 case 0x2f: // Westmere EX
831 CPU = "westmere";
832 *Type = X86::INTEL_COREI7;
833 *Subtype = X86::INTEL_COREI7_WESTMERE;
834 break;
835 case 0x2a: // Intel Core i7 processor. All processors are manufactured
836 // using the 32 nm process.
837 case 0x2d:
838 CPU = "sandybridge";
839 *Type = X86::INTEL_COREI7;
840 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
841 break;
842 case 0x3a:
843 case 0x3e: // Ivy Bridge EP
844 CPU = "ivybridge";
845 *Type = X86::INTEL_COREI7;
846 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
847 break;
848
849 // Haswell:
850 case 0x3c:
851 case 0x3f:
852 case 0x45:
853 case 0x46:
854 CPU = "haswell";
855 *Type = X86::INTEL_COREI7;
856 *Subtype = X86::INTEL_COREI7_HASWELL;
857 break;
858
859 // Broadwell:
860 case 0x3d:
861 case 0x47:
862 case 0x4f:
863 case 0x56:
864 CPU = "broadwell";
865 *Type = X86::INTEL_COREI7;
866 *Subtype = X86::INTEL_COREI7_BROADWELL;
867 break;
868
869 // Skylake:
870 case 0x4e: // Skylake mobile
871 case 0x5e: // Skylake desktop
872 case 0x8e: // Kaby Lake mobile
873 case 0x9e: // Kaby Lake desktop
874 case 0xa5: // Comet Lake-H/S
875 case 0xa6: // Comet Lake-U
876 CPU = "skylake";
877 *Type = X86::INTEL_COREI7;
878 *Subtype = X86::INTEL_COREI7_SKYLAKE;
879 break;
880
881 // Rocketlake:
882 case 0xa7:
883 CPU = "rocketlake";
884 *Type = X86::INTEL_COREI7;
885 *Subtype = X86::INTEL_COREI7_ROCKETLAKE;
886 break;
887
888 // Skylake Xeon:
889 case 0x55:
890 *Type = X86::INTEL_COREI7;
891 if (testFeature(X86::FEATURE_AVX512BF16)) {
892 CPU = "cooperlake";
893 *Subtype = X86::INTEL_COREI7_COOPERLAKE;
894 } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
895 CPU = "cascadelake";
896 *Subtype = X86::INTEL_COREI7_CASCADELAKE;
897 } else {
898 CPU = "skylake-avx512";
899 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
900 }
901 break;
902
903 // Cannonlake:
904 case 0x66:
905 CPU = "cannonlake";
906 *Type = X86::INTEL_COREI7;
907 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
908 break;
909
910 // Icelake:
911 case 0x7d:
912 case 0x7e:
913 CPU = "icelake-client";
914 *Type = X86::INTEL_COREI7;
915 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
916 break;
917
918 // Tigerlake:
919 case 0x8c:
920 case 0x8d:
921 CPU = "tigerlake";
922 *Type = X86::INTEL_COREI7;
923 *Subtype = X86::INTEL_COREI7_TIGERLAKE;
924 break;
925
926 // Alderlake:
927 case 0x97:
928 case 0x9a:
929 CPU = "alderlake";
930 *Type = X86::INTEL_COREI7;
931 *Subtype = X86::INTEL_COREI7_ALDERLAKE;
932 break;
933
934 // Gracemont
935 case 0xbe:
936 CPU = "gracemont";
937 *Type = X86::INTEL_COREI7;
938 *Subtype = X86::INTEL_COREI7_ALDERLAKE;
939 break;
940
941 // Raptorlake:
942 case 0xb7:
943 case 0xba:
944 case 0xbf:
945 CPU = "raptorlake";
946 *Type = X86::INTEL_COREI7;
947 *Subtype = X86::INTEL_COREI7_ALDERLAKE;
948 break;
949
950 // Meteorlake:
951 case 0xaa:
952 case 0xac:
953 CPU = "meteorlake";
954 *Type = X86::INTEL_COREI7;
955 *Subtype = X86::INTEL_COREI7_ALDERLAKE;
956 break;
957
958 // Arrowlake:
959 case 0xc5:
960 // Arrowlake U:
961 case 0xb5:
962 CPU = "arrowlake";
963 *Type = X86::INTEL_COREI7;
964 *Subtype = X86::INTEL_COREI7_ARROWLAKE;
965 break;
966
967 // Arrowlake S:
968 case 0xc6:
969 CPU = "arrowlake-s";
970 *Type = X86::INTEL_COREI7;
971 *Subtype = X86::INTEL_COREI7_ARROWLAKE_S;
972 break;
973
974 // Lunarlake:
975 case 0xbd:
976 CPU = "lunarlake";
977 *Type = X86::INTEL_COREI7;
978 *Subtype = X86::INTEL_COREI7_ARROWLAKE_S;
979 break;
980
981 // Pantherlake:
982 case 0xcc:
983 CPU = "pantherlake";
984 *Type = X86::INTEL_COREI7;
985 *Subtype = X86::INTEL_COREI7_PANTHERLAKE;
986 break;
987
988 // Wildcatlake:
989 case 0xd5:
990 CPU = "wildcatlake";
991 *Type = X86::INTEL_COREI7;
992 *Subtype = X86::INTEL_COREI7_PANTHERLAKE;
993 break;
994
995 // Graniterapids:
996 case 0xad:
997 CPU = "graniterapids";
998 *Type = X86::INTEL_COREI7;
999 *Subtype = X86::INTEL_COREI7_GRANITERAPIDS;
1000 break;
1001
1002 // Granite Rapids D:
1003 case 0xae:
1004 CPU = "graniterapids-d";
1005 *Type = X86::INTEL_COREI7;
1006 *Subtype = X86::INTEL_COREI7_GRANITERAPIDS_D;
1007 break;
1008
1009 // Icelake Xeon:
1010 case 0x6a:
1011 case 0x6c:
1012 CPU = "icelake-server";
1013 *Type = X86::INTEL_COREI7;
1014 *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER;
1015 break;
1016
1017 // Emerald Rapids:
1018 case 0xcf:
1019 CPU = "emeraldrapids";
1020 *Type = X86::INTEL_COREI7;
1021 *Subtype = X86::INTEL_COREI7_SAPPHIRERAPIDS;
1022 break;
1023
1024 // Sapphire Rapids:
1025 case 0x8f:
1026 CPU = "sapphirerapids";
1027 *Type = X86::INTEL_COREI7;
1028 *Subtype = X86::INTEL_COREI7_SAPPHIRERAPIDS;
1029 break;
1030
1031 case 0x1c: // Most 45 nm Intel Atom processors
1032 case 0x26: // 45 nm Atom Lincroft
1033 case 0x27: // 32 nm Atom Medfield
1034 case 0x35: // 32 nm Atom Midview
1035 case 0x36: // 32 nm Atom Midview
1036 CPU = "bonnell";
1037 *Type = X86::INTEL_BONNELL;
1038 break;
1039
1040 // Atom Silvermont codes from the Intel software optimization guide.
1041 case 0x37:
1042 case 0x4a:
1043 case 0x4d:
1044 case 0x5a:
1045 case 0x5d:
1046 case 0x4c: // really airmont
1047 CPU = "silvermont";
1048 *Type = X86::INTEL_SILVERMONT;
1049 break;
1050 // Goldmont:
1051 case 0x5c: // Apollo Lake
1052 case 0x5f: // Denverton
1053 CPU = "goldmont";
1054 *Type = X86::INTEL_GOLDMONT;
1055 break;
1056 case 0x7a:
1057 CPU = "goldmont-plus";
1058 *Type = X86::INTEL_GOLDMONT_PLUS;
1059 break;
1060 case 0x86:
1061 case 0x8a: // Lakefield
1062 case 0x96: // Elkhart Lake
1063 case 0x9c: // Jasper Lake
1064 CPU = "tremont";
1065 *Type = X86::INTEL_TREMONT;
1066 break;
1067
1068 // Sierraforest:
1069 case 0xaf:
1070 CPU = "sierraforest";
1071 *Type = X86::INTEL_SIERRAFOREST;
1072 break;
1073
1074 // Grandridge:
1075 case 0xb6:
1076 CPU = "grandridge";
1077 *Type = X86::INTEL_GRANDRIDGE;
1078 break;
1079
1080 // Clearwaterforest:
1081 case 0xdd:
1082 CPU = "clearwaterforest";
1083 *Type = X86::INTEL_CLEARWATERFOREST;
1084 break;
1085
1086 // Xeon Phi (Knights Landing + Knights Mill):
1087 case 0x57:
1088 CPU = "knl";
1089 *Type = X86::INTEL_KNL;
1090 break;
1091 case 0x85:
1092 CPU = "knm";
1093 *Type = X86::INTEL_KNM;
1094 break;
1095
1096 default: // Unknown family 6 CPU, try to guess.
1097 // Don't both with Type/Subtype here, they aren't used by the caller.
1098 // They're used above to keep the code in sync with compiler-rt.
1099 // TODO detect tigerlake host from model
1100 if (testFeature(X86::FEATURE_AVX512VP2INTERSECT)) {
1101 CPU = "tigerlake";
1102 } else if (testFeature(X86::FEATURE_AVX512VBMI2)) {
1103 CPU = "icelake-client";
1104 } else if (testFeature(X86::FEATURE_AVX512VBMI)) {
1105 CPU = "cannonlake";
1106 } else if (testFeature(X86::FEATURE_AVX512BF16)) {
1107 CPU = "cooperlake";
1108 } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
1109 CPU = "cascadelake";
1110 } else if (testFeature(X86::FEATURE_AVX512VL)) {
1111 CPU = "skylake-avx512";
1112 } else if (testFeature(X86::FEATURE_CLFLUSHOPT)) {
1113 if (testFeature(X86::FEATURE_SHA))
1114 CPU = "goldmont";
1115 else
1116 CPU = "skylake";
1117 } else if (testFeature(X86::FEATURE_ADX)) {
1118 CPU = "broadwell";
1119 } else if (testFeature(X86::FEATURE_AVX2)) {
1120 CPU = "haswell";
1121 } else if (testFeature(X86::FEATURE_AVX)) {
1122 CPU = "sandybridge";
1123 } else if (testFeature(X86::FEATURE_SSE4_2)) {
1124 if (testFeature(X86::FEATURE_MOVBE))
1125 CPU = "silvermont";
1126 else
1127 CPU = "nehalem";
1128 } else if (testFeature(X86::FEATURE_SSE4_1)) {
1129 CPU = "penryn";
1130 } else if (testFeature(X86::FEATURE_SSSE3)) {
1131 if (testFeature(X86::FEATURE_MOVBE))
1132 CPU = "bonnell";
1133 else
1134 CPU = "core2";
1135 } else if (testFeature(X86::FEATURE_64BIT)) {
1136 CPU = "core2";
1137 } else if (testFeature(X86::FEATURE_SSE3)) {
1138 CPU = "yonah";
1139 } else if (testFeature(X86::FEATURE_SSE2)) {
1140 CPU = "pentium-m";
1141 } else if (testFeature(X86::FEATURE_SSE)) {
1142 CPU = "pentium3";
1143 } else if (testFeature(X86::FEATURE_MMX)) {
1144 CPU = "pentium2";
1145 } else {
1146 CPU = "pentiumpro";
1147 }
1148 break;
1149 }
1150 break;
1151 case 0xf: {
1152 if (testFeature(X86::FEATURE_64BIT)) {
1153 CPU = "nocona";
1154 break;
1155 }
1156 if (testFeature(X86::FEATURE_SSE3)) {
1157 CPU = "prescott";
1158 break;
1159 }
1160 CPU = "pentium4";
1161 break;
1162 }
1163 case 0x13:
1164 switch (Model) {
1165 // Diamond Rapids:
1166 case 0x01:
1167 CPU = "diamondrapids";
1168 *Type = X86::INTEL_COREI7;
1169 *Subtype = X86::INTEL_COREI7_DIAMONDRAPIDS;
1170 break;
1171
1172 default: // Unknown family 19 CPU.
1173 break;
1174 }
1175 break;
1176 case 0x12:
1177 switch (Model) {
1178 // Novalake:
1179 case 0x1:
1180 case 0x3:
1181 CPU = "novalake";
1182 *Type = X86::INTEL_COREI7;
1183 *Subtype = X86::INTEL_COREI7_NOVALAKE;
1184 break;
1185 default: // Unknown family 0x12 CPU.
1186 break;
1187 }
1188 break;
1189
1190 default:
1191 break; // Unknown.
1192 }
1193
1194 return CPU;
1195}
1196
1197static const char *getAMDProcessorTypeAndSubtype(unsigned Family,
1198 unsigned Model,
1199 const unsigned *Features,
1200 unsigned *Type,
1201 unsigned *Subtype) {
1202 const char *CPU = nullptr;
1203
1204 switch (Family) {
1205 case 4:
1206 CPU = "i486";
1207 break;
1208 case 5:
1209 CPU = "pentium";
1210 switch (Model) {
1211 case 6:
1212 case 7:
1213 CPU = "k6";
1214 break;
1215 case 8:
1216 CPU = "k6-2";
1217 break;
1218 case 9:
1219 case 13:
1220 CPU = "k6-3";
1221 break;
1222 case 10:
1223 CPU = "geode";
1224 break;
1225 }
1226 break;
1227 case 6:
1228 if (testFeature(X86::FEATURE_SSE)) {
1229 CPU = "athlon-xp";
1230 break;
1231 }
1232 CPU = "athlon";
1233 break;
1234 case 15:
1235 if (testFeature(X86::FEATURE_SSE3)) {
1236 CPU = "k8-sse3";
1237 break;
1238 }
1239 CPU = "k8";
1240 break;
1241 case 16:
1242 case 18:
1243 CPU = "amdfam10";
1244 *Type = X86::AMDFAM10H; // "amdfam10"
1245 switch (Model) {
1246 case 2:
1247 *Subtype = X86::AMDFAM10H_BARCELONA;
1248 break;
1249 case 4:
1250 *Subtype = X86::AMDFAM10H_SHANGHAI;
1251 break;
1252 case 8:
1253 *Subtype = X86::AMDFAM10H_ISTANBUL;
1254 break;
1255 }
1256 break;
1257 case 20:
1258 CPU = "btver1";
1259 *Type = X86::AMD_BTVER1;
1260 break;
1261 case 21:
1262 CPU = "bdver1";
1263 *Type = X86::AMDFAM15H;
1264 if (Model >= 0x60 && Model <= 0x7f) {
1265 CPU = "bdver4";
1266 *Subtype = X86::AMDFAM15H_BDVER4;
1267 break; // 60h-7Fh: Excavator
1268 }
1269 if (Model >= 0x30 && Model <= 0x3f) {
1270 CPU = "bdver3";
1271 *Subtype = X86::AMDFAM15H_BDVER3;
1272 break; // 30h-3Fh: Steamroller
1273 }
1274 if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
1275 CPU = "bdver2";
1276 *Subtype = X86::AMDFAM15H_BDVER2;
1277 break; // 02h, 10h-1Fh: Piledriver
1278 }
1279 if (Model <= 0x0f) {
1280 *Subtype = X86::AMDFAM15H_BDVER1;
1281 break; // 00h-0Fh: Bulldozer
1282 }
1283 break;
1284 case 22:
1285 CPU = "btver2";
1286 *Type = X86::AMD_BTVER2;
1287 break;
1288 case 23:
1289 CPU = "znver1";
1290 *Type = X86::AMDFAM17H;
1291 if ((Model >= 0x30 && Model <= 0x3f) || (Model == 0x47) ||
1292 (Model >= 0x60 && Model <= 0x67) || (Model >= 0x68 && Model <= 0x6f) ||
1293 (Model >= 0x70 && Model <= 0x7f) || (Model >= 0x84 && Model <= 0x87) ||
1294 (Model >= 0x90 && Model <= 0x97) || (Model >= 0x98 && Model <= 0x9f) ||
1295 (Model >= 0xa0 && Model <= 0xaf)) {
1296 // Family 17h Models 30h-3Fh (Starship) Zen 2
1297 // Family 17h Models 47h (Cardinal) Zen 2
1298 // Family 17h Models 60h-67h (Renoir) Zen 2
1299 // Family 17h Models 68h-6Fh (Lucienne) Zen 2
1300 // Family 17h Models 70h-7Fh (Matisse) Zen 2
1301 // Family 17h Models 84h-87h (ProjectX) Zen 2
1302 // Family 17h Models 90h-97h (VanGogh) Zen 2
1303 // Family 17h Models 98h-9Fh (Mero) Zen 2
1304 // Family 17h Models A0h-AFh (Mendocino) Zen 2
1305 CPU = "znver2";
1306 *Subtype = X86::AMDFAM17H_ZNVER2;
1307 break;
1308 }
1309 if ((Model >= 0x10 && Model <= 0x1f) || (Model >= 0x20 && Model <= 0x2f)) {
1310 // Family 17h Models 10h-1Fh (Raven1) Zen
1311 // Family 17h Models 10h-1Fh (Picasso) Zen+
1312 // Family 17h Models 20h-2Fh (Raven2 x86) Zen
1313 *Subtype = X86::AMDFAM17H_ZNVER1;
1314 break;
1315 }
1316 break;
1317 case 25:
1318 CPU = "znver3";
1319 *Type = X86::AMDFAM19H;
1320 if (Model <= 0x0f || (Model >= 0x20 && Model <= 0x2f) ||
1321 (Model >= 0x30 && Model <= 0x3f) || (Model >= 0x40 && Model <= 0x4f) ||
1322 (Model >= 0x50 && Model <= 0x5f)) {
1323 // Family 19h Models 00h-0Fh (Genesis, Chagall) Zen 3
1324 // Family 19h Models 20h-2Fh (Vermeer) Zen 3
1325 // Family 19h Models 30h-3Fh (Badami) Zen 3
1326 // Family 19h Models 40h-4Fh (Rembrandt) Zen 3+
1327 // Family 19h Models 50h-5Fh (Cezanne) Zen 3
1328 *Subtype = X86::AMDFAM19H_ZNVER3;
1329 break;
1330 }
1331 if ((Model >= 0x10 && Model <= 0x1f) || (Model >= 0x60 && Model <= 0x6f) ||
1332 (Model >= 0x70 && Model <= 0x77) || (Model >= 0x78 && Model <= 0x7f) ||
1333 (Model >= 0xa0 && Model <= 0xaf)) {
1334 // Family 19h Models 10h-1Fh (Stones; Storm Peak) Zen 4
1335 // Family 19h Models 60h-6Fh (Raphael) Zen 4
1336 // Family 19h Models 70h-77h (Phoenix, Hawkpoint1) Zen 4
1337 // Family 19h Models 78h-7Fh (Phoenix 2, Hawkpoint2) Zen 4
1338 // Family 19h Models A0h-AFh (Stones-Dense) Zen 4
1339 CPU = "znver4";
1340 *Subtype = X86::AMDFAM19H_ZNVER4;
1341 break; // "znver4"
1342 }
1343 break; // family 19h
1344 case 26:
1345 CPU = "znver5";
1346 *Type = X86::AMDFAM1AH;
1347 if (Model <= 0x4f || (Model >= 0x60 && Model <= 0x77) ||
1348 (Model >= 0xd0 && Model <= 0xd7)) {
1349 // Models 00h-0Fh (Breithorn).
1350 // Models 10h-1Fh (Breithorn-Dense).
1351 // Models 20h-2Fh (Strix 1).
1352 // Models 30h-37h (Strix 2).
1353 // Models 38h-3Fh (Strix 3).
1354 // Models 40h-4Fh (Granite Ridge).
1355 // Models 60h-6Fh (Krackan1).
1356 // Models 70h-77h (Sarlak).
1357 // Models D0h-D7h (Annapurna).
1358 CPU = "znver5";
1359 *Subtype = X86::AMDFAM1AH_ZNVER5;
1360 break; // "znver5"
1361 }
1362 if ((Model >= 0x50 && Model <= 0x5f) || (Model >= 0x80 && Model <= 0xcf) ||
1363 (Model >= 0xd8 && Model <= 0xe7)) {
1364 CPU = "znver6";
1365 *Subtype = X86::AMDFAM1AH_ZNVER6;
1366 break; // "znver6"
1367 }
1368 break;
1369
1370 default:
1371 break; // Unknown AMD CPU.
1372 }
1373
1374 return CPU;
1375}
1376
1377static StringRef getHygonProcessorTypeAndSubtype(unsigned Family,
1378 unsigned Model,
1379 const unsigned *Features,
1380 unsigned *Type,
1381 unsigned *Subtype) {
1382 StringRef CPU;
1383
1384 switch (Family) {
1385 case 24:
1386 switch (Model) {
1387 case 4:
1388 CPU = "c86-4g-m4";
1389 *Type = X86::HYGONFAM18H;
1390 *Subtype = X86::HYGONFAM18H_C86_4G_M4;
1391 break; // c86-4g-m4
1392 case 6:
1393 CPU = "c86-4g-m6";
1394 *Type = X86::HYGONFAM18H;
1395 *Subtype = X86::HYGONFAM18H_C86_4G_M6;
1396 break; // c86-4g-m6
1397 case 7:
1398 CPU = "c86-4g-m7";
1399 *Type = X86::HYGONFAM18H;
1400 *Subtype = X86::HYGONFAM18H_C86_4G_M7;
1401 break; // c86-4g-m7
1402 case 8:
1403 CPU = "c86-4g-m8";
1404 *Type = X86::HYGONFAM18H;
1405 *Subtype = X86::HYGONFAM18H_C86_4G_M8;
1406 break; // c86-4g-m8
1407 }
1408 break; // Hygon Family 18H
1409 default:
1410 break; // Unknown Hygon CPU.
1411 }
1412
1413 return CPU;
1414}
1415
1416#undef testFeature
1417
1418static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
1419 unsigned *Features) {
1420 unsigned EAX, EBX;
1421
1422 auto setFeature = [&](unsigned F) {
1423 Features[F / 32] |= 1U << (F % 32);
1424 };
1425
1426 if ((EDX >> 15) & 1)
1427 setFeature(X86::FEATURE_CMOV);
1428 if ((EDX >> 23) & 1)
1429 setFeature(X86::FEATURE_MMX);
1430 if ((EDX >> 25) & 1)
1431 setFeature(X86::FEATURE_SSE);
1432 if ((EDX >> 26) & 1)
1433 setFeature(X86::FEATURE_SSE2);
1434
1435 if ((ECX >> 0) & 1)
1436 setFeature(X86::FEATURE_SSE3);
1437 if ((ECX >> 1) & 1)
1438 setFeature(X86::FEATURE_PCLMUL);
1439 if ((ECX >> 9) & 1)
1440 setFeature(X86::FEATURE_SSSE3);
1441 if ((ECX >> 12) & 1)
1442 setFeature(X86::FEATURE_FMA);
1443 if ((ECX >> 19) & 1)
1444 setFeature(X86::FEATURE_SSE4_1);
1445 if ((ECX >> 20) & 1) {
1446 setFeature(X86::FEATURE_SSE4_2);
1447 setFeature(X86::FEATURE_CRC32);
1448 }
1449 if ((ECX >> 23) & 1)
1450 setFeature(X86::FEATURE_POPCNT);
1451 if ((ECX >> 25) & 1)
1452 setFeature(X86::FEATURE_AES);
1453
1454 if ((ECX >> 22) & 1)
1455 setFeature(X86::FEATURE_MOVBE);
1456
1457 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1458 // indicates that the AVX registers will be saved and restored on context
1459 // switch, then we have full AVX support.
1460 const unsigned AVXBits = (1 << 27) | (1 << 28);
1461 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
1462 ((EAX & 0x6) == 0x6);
1463#if defined(__APPLE__)
1464 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1465 // save the AVX512 context if we use AVX512 instructions, even the bit is not
1466 // set right now.
1467 bool HasAVX512Save = true;
1468#else
1469 // AVX512 requires additional context to be saved by the OS.
1470 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
1471#endif
1472
1473 if (HasAVX)
1474 setFeature(X86::FEATURE_AVX);
1475
1476 bool HasLeaf7 =
1477 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1478
1479 if (HasLeaf7 && ((EBX >> 3) & 1))
1480 setFeature(X86::FEATURE_BMI);
1481 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
1482 setFeature(X86::FEATURE_AVX2);
1483 if (HasLeaf7 && ((EBX >> 8) & 1))
1484 setFeature(X86::FEATURE_BMI2);
1485 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save) {
1486 setFeature(X86::FEATURE_AVX512F);
1487 }
1488 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
1489 setFeature(X86::FEATURE_AVX512DQ);
1490 if (HasLeaf7 && ((EBX >> 19) & 1))
1491 setFeature(X86::FEATURE_ADX);
1492 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
1493 setFeature(X86::FEATURE_AVX512IFMA);
1494 if (HasLeaf7 && ((EBX >> 23) & 1))
1495 setFeature(X86::FEATURE_CLFLUSHOPT);
1496 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
1497 setFeature(X86::FEATURE_AVX512CD);
1498 if (HasLeaf7 && ((EBX >> 29) & 1))
1499 setFeature(X86::FEATURE_SHA);
1500 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
1501 setFeature(X86::FEATURE_AVX512BW);
1502 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
1503 setFeature(X86::FEATURE_AVX512VL);
1504
1505 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
1506 setFeature(X86::FEATURE_AVX512VBMI);
1507 if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
1508 setFeature(X86::FEATURE_AVX512VBMI2);
1509 if (HasLeaf7 && ((ECX >> 8) & 1))
1510 setFeature(X86::FEATURE_GFNI);
1511 if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
1512 setFeature(X86::FEATURE_VPCLMULQDQ);
1513 if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
1514 setFeature(X86::FEATURE_AVX512VNNI);
1515 if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
1516 setFeature(X86::FEATURE_AVX512BITALG);
1517 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
1518 setFeature(X86::FEATURE_AVX512VPOPCNTDQ);
1519
1520 if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)
1521 setFeature(X86::FEATURE_AVX512VP2INTERSECT);
1522
1523 // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
1524 // return all 0s for invalid subleaves so check the limit.
1525 bool HasLeaf7Subleaf1 =
1526 HasLeaf7 && EAX >= 1 &&
1527 !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1528 if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
1529 setFeature(X86::FEATURE_AVX512BF16);
1530
1531 unsigned MaxExtLevel;
1532 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1533
1534 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1535 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1536 if (HasExtLeaf1 && ((ECX >> 6) & 1))
1537 setFeature(X86::FEATURE_SSE4_A);
1538 if (HasExtLeaf1 && ((ECX >> 11) & 1))
1539 setFeature(X86::FEATURE_XOP);
1540 if (HasExtLeaf1 && ((ECX >> 16) & 1))
1541 setFeature(X86::FEATURE_FMA4);
1542
1543 if (HasExtLeaf1 && ((EDX >> 29) & 1))
1544 setFeature(X86::FEATURE_64BIT);
1545}
1546
1548 unsigned MaxLeaf = 0;
1549 const VendorSignatures Vendor = getVendorSignature(&MaxLeaf);
1550 if (Vendor == VendorSignatures::UNKNOWN)
1551 return "generic";
1552
1553 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1554 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
1555
1556 unsigned Family = 0, Model = 0;
1557 unsigned Features[(X86::CPU_FEATURE_MAX + 31) / 32] = {0};
1558 detectX86FamilyModel(EAX, &Family, &Model);
1559 getAvailableFeatures(ECX, EDX, MaxLeaf, Features);
1560
1561 // These aren't consumed in this file, but we try to keep some source code the
1562 // same or similar to compiler-rt.
1563 unsigned Type = 0;
1564 unsigned Subtype = 0;
1565
1566 StringRef CPU;
1567
1568 if (Vendor == VendorSignatures::GENUINE_INTEL) {
1569 CPU = getIntelProcessorTypeAndSubtype(Family, Model, Features, &Type,
1570 &Subtype);
1571 } else if (Vendor == VendorSignatures::AUTHENTIC_AMD) {
1572 CPU = getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type,
1573 &Subtype);
1574 } else if (Vendor == VendorSignatures::HYGON_GENUINE) {
1575 CPU = getHygonProcessorTypeAndSubtype(Family, Model, Features, &Type,
1576 &Subtype);
1577 }
1578
1579 if (!CPU.empty())
1580 return CPU;
1581
1582 return "generic";
1583}
1584
1585#elif defined(_M_ARM64) || defined(_M_ARM64EC)
1586
1588 constexpr char CentralProcessorKeyName[] =
1589 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor";
1590 // Sub keys names are simple numbers ("0", "1", etc.) so 10 chars should be
1591 // enough for the slash and name.
1592 constexpr size_t SubKeyNameMaxSize = ARRAYSIZE(CentralProcessorKeyName) + 10;
1593
1595 uint64_t PrimaryCpuInfo;
1596 char PrimaryPartKeyName[SubKeyNameMaxSize];
1597 DWORD PrimaryPartKeyNameSize = 0;
1598 HKEY CentralProcessorKey;
1599 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, CentralProcessorKeyName, 0, KEY_READ,
1600 &CentralProcessorKey) == ERROR_SUCCESS) {
1601 for (unsigned Index = 0; Index < UINT32_MAX; ++Index) {
1602 char SubKeyName[SubKeyNameMaxSize];
1603 DWORD SubKeySize = SubKeyNameMaxSize;
1604 HKEY SubKey;
1605 if ((RegEnumKeyExA(CentralProcessorKey, Index, SubKeyName, &SubKeySize,
1606 nullptr, nullptr, nullptr,
1607 nullptr) == ERROR_SUCCESS) &&
1608 (RegOpenKeyExA(CentralProcessorKey, SubKeyName, 0, KEY_READ,
1609 &SubKey) == ERROR_SUCCESS)) {
1610 // The "CP 4000" registry key contains a cached copy of the MIDR_EL1
1611 // register.
1612 uint64_t RegValue;
1613 DWORD ActualType;
1614 DWORD RegValueSize = sizeof(RegValue);
1615 if ((RegQueryValueExA(SubKey, "CP 4000", nullptr, &ActualType,
1616 (PBYTE)&RegValue,
1617 &RegValueSize) == ERROR_SUCCESS) &&
1618 (ActualType == REG_QWORD) && RegValueSize == sizeof(RegValue)) {
1619 // Assume that the part with the "highest" reg key name is the primary
1620 // part (to match the way that Linux's cpuinfo is written). Win32
1621 // makes no guarantees about the order of sub keys, so we have to
1622 // compare the names.
1623 if (PrimaryPartKeyNameSize < SubKeySize ||
1624 (PrimaryPartKeyNameSize == SubKeySize &&
1625 ::memcmp(SubKeyName, PrimaryPartKeyName, SubKeySize) > 0)) {
1626 PrimaryCpuInfo = RegValue;
1627 ::memcpy(PrimaryPartKeyName, SubKeyName, SubKeySize + 1);
1628 PrimaryPartKeyNameSize = SubKeySize;
1629 }
1630 if (!llvm::is_contained(Values, RegValue)) {
1631 Values.push_back(RegValue);
1632 }
1633 }
1634 RegCloseKey(SubKey);
1635 } else {
1636 // No more sub keys.
1637 break;
1638 }
1639 }
1640 RegCloseKey(CentralProcessorKey);
1641 }
1642
1643 if (Values.empty()) {
1644 return "generic";
1645 }
1646
1647 // Win32 makes no guarantees about the order of sub keys, so sort to ensure
1648 // reproducibility.
1650
1651 return detail::getHostCPUNameForARM(PrimaryCpuInfo, Values);
1652}
1653
1654#elif defined(__APPLE__) && defined(__powerpc__)
1656 host_basic_info_data_t hostInfo;
1657 mach_msg_type_number_t infoCount;
1658
1659 infoCount = HOST_BASIC_INFO_COUNT;
1660 mach_port_t hostPort = mach_host_self();
1661 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1662 &infoCount);
1663 mach_port_deallocate(mach_task_self(), hostPort);
1664
1665 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1666 return "generic";
1667
1668 switch (hostInfo.cpu_subtype) {
1670 return "601";
1672 return "602";
1674 return "603";
1676 return "603e";
1678 return "603ev";
1680 return "604";
1682 return "604e";
1684 return "620";
1686 return "750";
1688 return "7400";
1690 return "7450";
1692 return "970";
1693 default:;
1694 }
1695
1696 return "generic";
1697}
1698#elif defined(__linux__) && defined(__powerpc__)
1700 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1701 StringRef Content = P ? P->getBuffer() : "";
1702 return detail::getHostCPUNameForPowerPC(Content);
1703}
1704#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1706 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1707 StringRef Content = P ? P->getBuffer() : "";
1708 return detail::getHostCPUNameForARM(Content);
1709}
1710#elif defined(__linux__) && defined(__s390x__)
1712 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1713 StringRef Content = P ? P->getBuffer() : "";
1714 return detail::getHostCPUNameForS390x(Content);
1715}
1716#elif defined(__MVS__)
1718 // Get pointer to Communications Vector Table (CVT).
1719 // The pointer is located at offset 16 of the Prefixed Save Area (PSA).
1720 // It is stored as 31 bit pointer and will be zero-extended to 64 bit.
1721 int *StartToCVTOffset = reinterpret_cast<int *>(0x10);
1722 // Since its stored as a 31-bit pointer, get the 4 bytes from the start
1723 // of address.
1724 int ReadValue = *StartToCVTOffset;
1725 // Explicitly clear the high order bit.
1726 ReadValue = (ReadValue & 0x7FFFFFFF);
1727 char *CVT = reinterpret_cast<char *>(ReadValue);
1728 // The model number is located in the CVT prefix at offset -6 and stored as
1729 // signless packed decimal.
1730 uint16_t Id = *(uint16_t *)&CVT[-6];
1731 // Convert number to integer.
1732 Id = decodePackedBCD<uint16_t>(Id, false);
1733 // Check for vector support. It's stored in field CVTFLAG5 (offset 244),
1734 // bit CVTVEF (X'80'). The facilities list is part of the PSA but the vector
1735 // extension can only be used if bit CVTVEF is on.
1736 bool HaveVectorSupport = CVT[244] & 0x80;
1737 return getCPUNameFromS390Model(Id, HaveVectorSupport);
1738}
1739#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1740// Copied from <mach/machine.h> in the macOS SDK.
1741//
1742// Also available here, though usually not as up-to-date:
1743// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/mach/machine.h#L403-L452.
1744#define CPUFAMILY_UNKNOWN 0
1745#define CPUFAMILY_ARM_9 0xe73283ae
1746#define CPUFAMILY_ARM_11 0x8ff620d8
1747#define CPUFAMILY_ARM_XSCALE 0x53b005f5
1748#define CPUFAMILY_ARM_12 0xbd1b0ae9
1749#define CPUFAMILY_ARM_13 0x0cc90e64
1750#define CPUFAMILY_ARM_14 0x96077ef1
1751#define CPUFAMILY_ARM_15 0xa8511bca
1752#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
1753#define CPUFAMILY_ARM_CYCLONE 0x37a09642
1754#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
1755#define CPUFAMILY_ARM_TWISTER 0x92fb37c8
1756#define CPUFAMILY_ARM_HURRICANE 0x67ceee93
1757#define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
1758#define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
1759#define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
1760#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
1761#define CPUFAMILY_ARM_BLIZZARD_AVALANCHE 0xda33d83d
1762#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
1763#define CPUFAMILY_ARM_IBIZA 0xfa33415e
1764#define CPUFAMILY_ARM_PALMA 0x72015832
1765#define CPUFAMILY_ARM_COLL 0x2876f5b5
1766#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
1767#define CPUFAMILY_ARM_DONAN 0x6f5129ac
1768#define CPUFAMILY_ARM_BRAVA 0x17d5b93a
1769#define CPUFAMILY_ARM_TAHITI 0x75d4acb9
1770#define CPUFAMILY_ARM_TUPAI 0x204526d0
1771#define CPUFAMILY_ARM_HIDRA 0x1d5a87e8
1772#define CPUFAMILY_ARM_SOTRA 0xf76c5b1a
1773#define CPUFAMILY_ARM_THERA 0xab345f09
1774#define CPUFAMILY_ARM_TILOS 0x01d7a72b
1775
1777 uint32_t Family;
1778 size_t Length = sizeof(Family);
1779 sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
1780
1781 // This is found by testing on actual hardware, and by looking at:
1782 // https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/arm/cpuid.c#L109-L231.
1783 //
1784 // Another great resource is
1785 // https://github.com/AsahiLinux/docs/wiki/Codenames.
1786 //
1787 // NOTE: We choose to return `apple-mX` instead of `apple-aX`, since the M1,
1788 // M2, M3 etc. aliases are more widely known to users than A14, A15, A16 etc.
1789 // (and this code is basically only used on host macOS anyways).
1790 switch (Family) {
1791 case CPUFAMILY_UNKNOWN:
1792 return "generic";
1793 case CPUFAMILY_ARM_9:
1794 return "arm920t"; // or arm926ej-s
1795 case CPUFAMILY_ARM_11:
1796 return "arm1136jf-s";
1797 case CPUFAMILY_ARM_XSCALE:
1798 return "xscale";
1799 case CPUFAMILY_ARM_12: // Seems unused by the kernel
1800 return "generic";
1801 case CPUFAMILY_ARM_13:
1802 return "cortex-a8";
1803 case CPUFAMILY_ARM_14:
1804 return "cortex-a9";
1805 case CPUFAMILY_ARM_15:
1806 return "cortex-a7";
1807 case CPUFAMILY_ARM_SWIFT:
1808 return "swift";
1809 case CPUFAMILY_ARM_CYCLONE:
1810 return "apple-a7";
1811 case CPUFAMILY_ARM_TYPHOON:
1812 return "apple-a8";
1813 case CPUFAMILY_ARM_TWISTER:
1814 return "apple-a9";
1815 case CPUFAMILY_ARM_HURRICANE:
1816 return "apple-a10";
1817 case CPUFAMILY_ARM_MONSOON_MISTRAL:
1818 return "apple-a11";
1819 case CPUFAMILY_ARM_VORTEX_TEMPEST:
1820 return "apple-a12";
1821 case CPUFAMILY_ARM_LIGHTNING_THUNDER:
1822 return "apple-a13";
1823 case CPUFAMILY_ARM_FIRESTORM_ICESTORM: // A14 / M1
1824 return "apple-m1";
1825 case CPUFAMILY_ARM_BLIZZARD_AVALANCHE: // A15 / M2
1826 return "apple-m2";
1827 case CPUFAMILY_ARM_EVEREST_SAWTOOTH: // A16
1828 case CPUFAMILY_ARM_IBIZA: // M3
1829 case CPUFAMILY_ARM_PALMA: // M3 Max
1830 case CPUFAMILY_ARM_LOBOS: // M3 Pro
1831 return "apple-m3";
1832 case CPUFAMILY_ARM_COLL: // A17 Pro
1833 return "apple-a17";
1834 case CPUFAMILY_ARM_DONAN: // M4
1835 case CPUFAMILY_ARM_BRAVA: // M4 Pro/Max
1836 case CPUFAMILY_ARM_TAHITI: // A18 Pro
1837 case CPUFAMILY_ARM_TUPAI: // A18
1838 return "apple-m4";
1839 case CPUFAMILY_ARM_HIDRA: // M5
1840 case CPUFAMILY_ARM_SOTRA: // M5 Pro/Max
1841 case CPUFAMILY_ARM_THERA: // A19 Pro
1842 case CPUFAMILY_ARM_TILOS: // A19
1843 return "apple-m5";
1844 default:
1845 // Default to the newest CPU we know about.
1846 return "apple-m5";
1847 }
1848}
1849#elif defined(_AIX)
1851 switch (_system_configuration.implementation) {
1852 case POWER_4:
1853 if (_system_configuration.version == PV_4_3)
1854 return "970";
1855 return "pwr4";
1856 case POWER_5:
1857 if (_system_configuration.version == PV_5)
1858 return "pwr5";
1859 return "pwr5x";
1860 case POWER_6:
1861 if (_system_configuration.version == PV_6_Compat)
1862 return "pwr6";
1863 return "pwr6x";
1864 case POWER_7:
1865 return "pwr7";
1866 case POWER_8:
1867 return "pwr8";
1868 case POWER_9:
1869 return "pwr9";
1870// TODO: simplify this once the macro is available in all OS levels.
1871#ifdef POWER_10
1872 case POWER_10:
1873#else
1874 case 0x40000:
1875#endif
1876 return "pwr10";
1877#ifdef POWER_11
1878 case POWER_11:
1879#else
1880 case 0x80000:
1881#endif
1882 return "pwr11";
1883 default:
1884 return "generic";
1885 }
1886}
1887#elif defined(__loongarch__)
1889 // Use processor id to detect cpu name.
1890 uint32_t processor_id;
1891 __asm__("cpucfg %[prid], $zero\n\t" : [prid] "=r"(processor_id));
1892 // Refer PRID_SERIES_MASK in linux kernel: arch/loongarch/include/asm/cpu.h.
1893 switch (processor_id & 0xf000) {
1894 case 0xc000: // Loongson 64bit, 4-issue
1895 return "la464";
1896 case 0xd000: // Loongson 64bit, 6-issue
1897 return "la664";
1898 // TODO: Others.
1899 default:
1900 break;
1901 }
1902 return "generic";
1903}
1904#elif defined(__riscv)
1905#if defined(__linux__)
1906// struct riscv_hwprobe
1907struct RISCVHwProbe {
1908 int64_t Key;
1910};
1911#endif
1912
1914#if defined(__linux__)
1915 cpu_set_t Affinity;
1916 if (sched_getaffinity(0, sizeof(Affinity), &Affinity) == 0) {
1917 // Try the hwprobe way first.
1918 RISCVHwProbe Query[]{{/*RISCV_HWPROBE_KEY_MVENDORID=*/0, 0},
1919 {/*RISCV_HWPROBE_KEY_MARCHID=*/1, 0},
1920 {/*RISCV_HWPROBE_KEY_MIMPID=*/2, 0}};
1921 int Ret = syscall(/*__NR_riscv_hwprobe=*/258, /*pairs=*/Query,
1922 /*pair_count=*/std::size(Query),
1923 /*cpusetsize=*/sizeof(Affinity),
1924 /*cpus=*/&Affinity, /*flags=*/0);
1925 if (Ret == 0) {
1926 RISCV::CPUModel Model{static_cast<uint32_t>(Query[0].Value),
1927 Query[1].Value, Query[2].Value};
1929 if (!Name.empty())
1930 return Name;
1931 }
1932 }
1933
1934 // Then try the cpuinfo way.
1935 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1936 StringRef Content = P ? P->getBuffer() : "";
1938 if (!Name.empty())
1939 return Name;
1940#endif
1941#if __riscv_xlen == 64
1942 return "generic-rv64";
1943#elif __riscv_xlen == 32
1944 return "generic-rv32";
1945#else
1946#error "Unhandled value of __riscv_xlen"
1947#endif
1948}
1949#elif defined(__sparc__)
1950#if defined(__linux__)
1953 ProcCpuinfoContent.split(Lines, '\n');
1954
1955 // Look for cpu line to determine cpu name
1956 StringRef Cpu;
1957 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
1958 if (Lines[I].starts_with("cpu")) {
1959 Cpu = Lines[I].substr(5).ltrim("\t :");
1960 break;
1961 }
1962 }
1963
1964 return StringSwitch<const char *>(Cpu)
1965 .StartsWith("SuperSparc", "supersparc")
1966 .StartsWith("HyperSparc", "hypersparc")
1967 .StartsWith("SpitFire", "ultrasparc")
1968 .StartsWith("BlackBird", "ultrasparc")
1969 .StartsWith("Sabre", " ultrasparc")
1970 .StartsWith("Hummingbird", "ultrasparc")
1971 .StartsWith("Cheetah", "ultrasparc3")
1972 .StartsWith("Jalapeno", "ultrasparc3")
1973 .StartsWith("Jaguar", "ultrasparc3")
1974 .StartsWith("Panther", "ultrasparc3")
1975 .StartsWith("Serrano", "ultrasparc3")
1976 .StartsWith("UltraSparc T1", "niagara")
1977 .StartsWith("UltraSparc T2", "niagara2")
1978 .StartsWith("UltraSparc T3", "niagara3")
1979 .StartsWith("UltraSparc T4", "niagara4")
1980 .StartsWith("UltraSparc T5", "niagara4")
1981 .StartsWith("LEON", "leon3")
1982 // niagara7/m8 not supported by LLVM yet.
1983 .StartsWith("SPARC-M7", "niagara4" /* "niagara7" */)
1984 .StartsWith("SPARC-S7", "niagara4" /* "niagara7" */)
1985 .StartsWith("SPARC-M8", "niagara4" /* "m8" */)
1986 .Default("generic");
1987}
1988#endif
1989
1991#if defined(__linux__)
1992 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1993 StringRef Content = P ? P->getBuffer() : "";
1994 return detail::getHostCPUNameForSPARC(Content);
1995#elif defined(__sun__) && defined(__svr4__)
1996 char *buf = NULL;
1997 kstat_ctl_t *kc;
1998 kstat_t *ksp;
1999 kstat_named_t *brand = NULL;
2000
2001 kc = kstat_open();
2002 if (kc != NULL) {
2003 ksp = kstat_lookup(kc, const_cast<char *>("cpu_info"), -1, NULL);
2004 if (ksp != NULL && kstat_read(kc, ksp, NULL) != -1 &&
2005 ksp->ks_type == KSTAT_TYPE_NAMED)
2006 brand =
2007 (kstat_named_t *)kstat_data_lookup(ksp, const_cast<char *>("brand"));
2008 if (brand != NULL && brand->data_type == KSTAT_DATA_STRING)
2009 buf = KSTAT_NAMED_STR_PTR(brand);
2010 }
2011 kstat_close(kc);
2012
2013 return StringSwitch<const char *>(buf)
2014 .Case("TMS390S10", "supersparc") // Texas Instruments microSPARC I
2015 .Case("TMS390Z50", "supersparc") // Texas Instruments SuperSPARC I
2016 .Case("TMS390Z55",
2017 "supersparc") // Texas Instruments SuperSPARC I with SuperCache
2018 .Case("MB86904", "supersparc") // Fujitsu microSPARC II
2019 .Case("MB86907", "supersparc") // Fujitsu TurboSPARC
2020 .Case("RT623", "hypersparc") // Ross hyperSPARC
2021 .Case("RT625", "hypersparc")
2022 .Case("RT626", "hypersparc")
2023 .Case("UltraSPARC-I", "ultrasparc")
2024 .Case("UltraSPARC-II", "ultrasparc")
2025 .Case("UltraSPARC-IIe", "ultrasparc")
2026 .Case("UltraSPARC-IIi", "ultrasparc")
2027 .Case("SPARC64-III", "ultrasparc")
2028 .Case("SPARC64-IV", "ultrasparc")
2029 .Case("UltraSPARC-III", "ultrasparc3")
2030 .Case("UltraSPARC-III+", "ultrasparc3")
2031 .Case("UltraSPARC-IIIi", "ultrasparc3")
2032 .Case("UltraSPARC-IIIi+", "ultrasparc3")
2033 .Case("UltraSPARC-IV", "ultrasparc3")
2034 .Case("UltraSPARC-IV+", "ultrasparc3")
2035 .Case("SPARC64-V", "ultrasparc3")
2036 .Case("SPARC64-VI", "ultrasparc3")
2037 .Case("SPARC64-VII", "ultrasparc3")
2038 .Case("UltraSPARC-T1", "niagara")
2039 .Case("UltraSPARC-T2", "niagara2")
2040 .Case("UltraSPARC-T2", "niagara2")
2041 .Case("UltraSPARC-T2+", "niagara2")
2042 .Case("SPARC-T3", "niagara3")
2043 .Case("SPARC-T4", "niagara4")
2044 .Case("SPARC-T5", "niagara4")
2045 // niagara7/m8 not supported by LLVM yet.
2046 .Case("SPARC-M7", "niagara4" /* "niagara7" */)
2047 .Case("SPARC-S7", "niagara4" /* "niagara7" */)
2048 .Case("SPARC-M8", "niagara4" /* "m8" */)
2049 .Default("generic");
2050#else
2051 return "generic";
2052#endif
2053}
2054#else
2055StringRef sys::getHostCPUName() { return "generic"; }
2056namespace llvm {
2057namespace sys {
2058namespace detail {
2059namespace x86 {
2060
2063}
2064
2065} // namespace x86
2066} // namespace detail
2067} // namespace sys
2068} // namespace llvm
2069#endif
2070
2073 StringMap<bool> Features;
2075 ProcCpuinfoContent.split(Lines, '\n');
2076
2077 SmallVector<StringRef, 32> CPUFeatures;
2078
2079 // Look for the CPU features.
2080 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
2081 if (Lines[I].starts_with("Features")) {
2082 Lines[I].split(CPUFeatures, ' ');
2083 break;
2084 }
2085
2086#if defined(__aarch64__)
2087 // All of these are "crypto" features, but we must sift out actual features
2088 // as the former meaning of "crypto" as a single feature is no more.
2089 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
2090 uint32_t crypto = 0;
2091#endif
2092
2093 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
2094 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
2095#if defined(__aarch64__)
2096 .Case("asimd", "neon")
2097 .Case("fp", "fp-armv8")
2098 .Case("crc32", "crc")
2099 .Case("atomics", "lse")
2100 .Case("rng", "rand")
2101 .Case("sha3", "sha3")
2102 .Case("sm4", "sm4")
2103 .Case("sve", "sve")
2104 .Case("sve2", "sve2")
2105 .Case("sveaes", "sve-aes")
2106 .Case("svesha3", "sve-sha3")
2107 .Case("svesm4", "sve-sm4")
2108 .Case("sme", "sme")
2109 .Case("smei16i64", "sme-i16i64")
2110 .Case("smef64f64", "sme-f64f64")
2111 .Case("smefa64", "sme-fa64")
2112 .Case("sme2", "sme2")
2113 .Case("sme2p1", "sme2p1")
2114 .Case("smeb16b16", "sme-b16b16")
2115 .Case("smef16f16", "sme-f16f16")
2116 .Case("smef8f16", "sme-f8f16")
2117 .Case("smef8f32", "sme-f8f32")
2118 .Case("smesf8fma", "ssve-fp8fma")
2119 .Case("smesf8dp4", "ssve-fp8dot4")
2120 .Case("smesf8dp2", "ssve-fp8dot2")
2121 .Case("smeaes", "ssve-aes")
2122 .Case("sme2p2", "sme2p2")
2123 .Case("sme2p3", "sme2p3")
2124#else
2125 .Case("half", "fp16")
2126 .Case("neon", "neon")
2127 .Case("vfpv3", "vfp3")
2128 .Case("vfpv3d16", "vfp3d16")
2129 .Case("vfpv4", "vfp4")
2130 .Case("idiva", "hwdiv-arm")
2131 .Case("idivt", "hwdiv")
2132#endif
2133 .Default("");
2134
2135#if defined(__aarch64__)
2136 // We need to check crypto separately since we need all of the crypto
2137 // extensions to enable the subtarget feature
2138 if (CPUFeatures[I] == "aes")
2139 crypto |= CAP_AES;
2140 else if (CPUFeatures[I] == "pmull")
2141 crypto |= CAP_PMULL;
2142 else if (CPUFeatures[I] == "sha1")
2143 crypto |= CAP_SHA1;
2144 else if (CPUFeatures[I] == "sha2")
2145 crypto |= CAP_SHA2;
2146#endif
2147
2148 if (LLVMFeatureStr != "")
2149 Features[LLVMFeatureStr] = true;
2150 }
2151
2152#if defined(__aarch64__)
2153 // LLVM has decided some AArch64 CPUs have all the instructions they _may_
2154 // have, as opposed to all the instructions they _must_ have, so allow runtime
2155 // information to correct us on that.
2156 uint32_t Aes = CAP_AES | CAP_PMULL;
2157 uint32_t Sha2 = CAP_SHA1 | CAP_SHA2;
2158 Features["aes"] = (crypto & Aes) == Aes;
2159 Features["sha2"] = (crypto & Sha2) == Sha2;
2160
2161 // Even if an underlying core supports SVE/SME, it might not be available if
2162 // it's disabled by the OS, or some other layer. Disable SVE/SME if we don't
2163 // detect support at runtime.
2164 if (!Features.contains("sve"))
2165 Features["sve"] = false;
2166 if (!Features.contains("sme"))
2167 Features["sme"] = false;
2168 if (!Features.contains("sme2"))
2169 Features["sme2"] = false;
2170
2171 // Also disable RNG if we can't detect support at runtime.
2172 if (!Features.contains("rand"))
2173 Features["rand"] = false;
2174#endif
2175
2176 return Features;
2177}
2178
2179#if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \
2180 defined(_M_X64)) && \
2181 !defined(_M_ARM64EC)
2183 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
2184 unsigned MaxLevel;
2185 StringMap<bool> Features;
2186
2187 if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
2188 return Features;
2189
2190 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
2191
2192 Features["cx8"] = (EDX >> 8) & 1;
2193 Features["cmov"] = (EDX >> 15) & 1;
2194 Features["mmx"] = (EDX >> 23) & 1;
2195 Features["fxsr"] = (EDX >> 24) & 1;
2196 Features["sse"] = (EDX >> 25) & 1;
2197 Features["sse2"] = (EDX >> 26) & 1;
2198
2199 Features["sse3"] = (ECX >> 0) & 1;
2200 Features["pclmul"] = (ECX >> 1) & 1;
2201 Features["ssse3"] = (ECX >> 9) & 1;
2202 Features["cx16"] = (ECX >> 13) & 1;
2203 Features["sse4.1"] = (ECX >> 19) & 1;
2204 Features["sse4.2"] = (ECX >> 20) & 1;
2205 Features["crc32"] = Features["sse4.2"];
2206 Features["movbe"] = (ECX >> 22) & 1;
2207 Features["popcnt"] = (ECX >> 23) & 1;
2208 Features["aes"] = (ECX >> 25) & 1;
2209 Features["rdrnd"] = (ECX >> 30) & 1;
2210
2211 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
2212 // indicates that the AVX registers will be saved and restored on context
2213 // switch, then we have full AVX support.
2214 bool HasXSave = ((ECX >> 27) & 1) && !getX86XCR0(&EAX, &EDX);
2215 bool HasAVXSave = HasXSave && ((ECX >> 28) & 1) && ((EAX & 0x6) == 0x6);
2216#if defined(__APPLE__)
2217 // Darwin lazily saves the AVX512 context on first use: trust that the OS will
2218 // save the AVX512 context if we use AVX512 instructions, even the bit is not
2219 // set right now.
2220 bool HasAVX512Save = true;
2221#else
2222 // AVX512 requires additional context to be saved by the OS.
2223 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
2224#endif
2225 // AMX requires additional context to be saved by the OS.
2226 const unsigned AMXBits = (1 << 17) | (1 << 18);
2227 bool HasAMXSave = HasXSave && ((EAX & AMXBits) == AMXBits);
2228 // APX requires additional context to be saved by the OS.
2229 bool HasAPXSave = HasXSave && ((EAX >> 19) & 1);
2230
2231 Features["avx"] = HasAVXSave;
2232 Features["fma"] = ((ECX >> 12) & 1) && HasAVXSave;
2233 // Only enable XSAVE if OS has enabled support for saving YMM state.
2234 Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
2235 Features["f16c"] = ((ECX >> 29) & 1) && HasAVXSave;
2236
2237 unsigned MaxExtLevel;
2238 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
2239
2240 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
2241 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
2242 Features["sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1);
2243 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
2244 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
2245 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
2246 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
2247 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
2248 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
2249 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
2250 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
2251
2252 Features["64bit"] = HasExtLeaf1 && ((EDX >> 29) & 1);
2253
2254 // Miscellaneous memory related features, detected by
2255 // using the 0x80000008 leaf of the CPUID instruction
2256 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
2257 !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);
2258 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
2259 Features["rdpru"] = HasExtLeaf8 && ((EBX >> 4) & 1);
2260 Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1);
2261
2262 bool HasExtLeaf21 = MaxExtLevel >= 0x80000021 &&
2263 !getX86CpuIDAndInfo(0x80000021, &EAX, &EBX, &ECX, &EDX);
2264 // AMD cpuid bit for prefetchi is different from Intel
2265 Features["prefetchi"] = HasExtLeaf21 && ((EAX >> 20) & 1);
2266 Features["avx512bmm"] = HasExtLeaf21 && ((EAX >> 23) & 1) && HasAVX512Save;
2267
2268 bool HasLeaf7 =
2269 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
2270
2271 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
2272 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
2273 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
2274 // AVX2 is only supported if we have the OS save support from AVX.
2275 Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave;
2276 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
2277 Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1);
2278 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
2279 // AVX512 is only supported if the OS supports the context save for it.
2280 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
2281 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
2282 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
2283 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
2284 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
2285 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
2286 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
2287 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
2288 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
2289 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
2290 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
2291
2292 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
2293 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
2294 Features["waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1);
2295 Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
2296 Features["shstk"] = HasLeaf7 && ((ECX >> 7) & 1);
2297 Features["gfni"] = HasLeaf7 && ((ECX >> 8) & 1);
2298 Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
2299 Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
2300 Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
2301 Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
2302 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
2303 Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);
2304 Features["kl"] = HasLeaf7 && ((ECX >> 23) & 1); // key locker
2305 Features["cldemote"] = HasLeaf7 && ((ECX >> 25) & 1);
2306 Features["movdiri"] = HasLeaf7 && ((ECX >> 27) & 1);
2307 Features["movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1);
2308 Features["enqcmd"] = HasLeaf7 && ((ECX >> 29) & 1);
2309
2310 Features["uintr"] = HasLeaf7 && ((EDX >> 5) & 1);
2311 Features["avx512vp2intersect"] =
2312 HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save;
2313 Features["serialize"] = HasLeaf7 && ((EDX >> 14) & 1);
2314 Features["tsxldtrk"] = HasLeaf7 && ((EDX >> 16) & 1);
2315 // There are two CPUID leafs which information associated with the pconfig
2316 // instruction:
2317 // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
2318 // bit of EDX), while the EAX=0x1b leaf returns information on the
2319 // availability of specific pconfig leafs.
2320 // The target feature here only refers to the the first of these two.
2321 // Users might need to check for the availability of specific pconfig
2322 // leaves using cpuid, since that information is ignored while
2323 // detecting features using the "-march=native" flag.
2324 // For more info, see X86 ISA docs.
2325 Features["pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
2326 Features["amx-bf16"] = HasLeaf7 && ((EDX >> 22) & 1) && HasAMXSave;
2327 Features["avx512fp16"] = HasLeaf7 && ((EDX >> 23) & 1) && HasAVX512Save;
2328 Features["amx-tile"] = HasLeaf7 && ((EDX >> 24) & 1) && HasAMXSave;
2329 Features["amx-int8"] = HasLeaf7 && ((EDX >> 25) & 1) && HasAMXSave;
2330 // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
2331 // return all 0s for invalid subleaves so check the limit.
2332 bool HasLeaf7Subleaf1 =
2333 HasLeaf7 && EAX >= 1 &&
2334 !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
2335 Features["sha512"] = HasLeaf7Subleaf1 && ((EAX >> 0) & 1);
2336 Features["sm3"] = HasLeaf7Subleaf1 && ((EAX >> 1) & 1);
2337 Features["sm4"] = HasLeaf7Subleaf1 && ((EAX >> 2) & 1);
2338 Features["raoint"] = HasLeaf7Subleaf1 && ((EAX >> 3) & 1);
2339 Features["avxvnni"] = HasLeaf7Subleaf1 && ((EAX >> 4) & 1) && HasAVXSave;
2340 Features["avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save;
2341 Features["amx-fp16"] = HasLeaf7Subleaf1 && ((EAX >> 21) & 1) && HasAMXSave;
2342 Features["cmpccxadd"] = HasLeaf7Subleaf1 && ((EAX >> 7) & 1);
2343 Features["hreset"] = HasLeaf7Subleaf1 && ((EAX >> 22) & 1);
2344 Features["avxifma"] = HasLeaf7Subleaf1 && ((EAX >> 23) & 1) && HasAVXSave;
2345 Features["movrs"] = HasLeaf7Subleaf1 && ((EAX >> 31) & 1);
2346 Features["avxvnniint8"] = HasLeaf7Subleaf1 && ((EDX >> 4) & 1) && HasAVXSave;
2347 Features["avxneconvert"] = HasLeaf7Subleaf1 && ((EDX >> 5) & 1) && HasAVXSave;
2348 Features["amx-complex"] = HasLeaf7Subleaf1 && ((EDX >> 8) & 1) && HasAMXSave;
2349 Features["avxvnniint16"] = HasLeaf7Subleaf1 && ((EDX >> 10) & 1) && HasAVXSave;
2350 Features["prefetchi"] |= HasLeaf7Subleaf1 && ((EDX >> 14) & 1);
2351 Features["usermsr"] = HasLeaf7Subleaf1 && ((EDX >> 15) & 1);
2352 bool HasAVX10 = HasLeaf7Subleaf1 && ((EDX >> 19) & 1);
2353 bool HasAPXF = HasLeaf7Subleaf1 && ((EDX >> 21) & 1) && HasAPXSave;
2354 Features["egpr"] = HasAPXF;
2355 // TODO: We may need to check OS or MSVC version once unwinder opcodes
2356 // support PUSH2/POP2/PPX.
2357 Features["push2pop2"] = HasAPXF;
2358 Features["ppx"] = HasAPXF;
2359 Features["ndd"] = HasAPXF;
2360 Features["ccmp"] = HasAPXF;
2361 Features["nf"] = HasAPXF;
2362 Features["cf"] = HasAPXF;
2363 Features["zu"] = HasAPXF;
2364 Features["jmpabs"] = HasAPXF;
2365
2366 bool HasLeafD = MaxLevel >= 0xd &&
2367 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
2368
2369 // Only enable XSAVE if OS has enabled support for saving YMM state.
2370 Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
2371 Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
2372 Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
2373
2374 bool HasLeaf14 = MaxLevel >= 0x14 &&
2375 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX);
2376
2377 Features["ptwrite"] = HasLeaf14 && ((EBX >> 4) & 1);
2378
2379 bool HasLeaf19 =
2380 MaxLevel >= 0x19 && !getX86CpuIDAndInfo(0x19, &EAX, &EBX, &ECX, &EDX);
2381 Features["widekl"] = HasLeaf7 && HasLeaf19 && ((EBX >> 2) & 1);
2382
2383 bool HasLeaf1E = MaxLevel >= 0x1e &&
2384 !getX86CpuIDAndInfoEx(0x1e, 0x1, &EAX, &EBX, &ECX, &EDX);
2385 Features["amx-fp8"] = HasLeaf1E && ((EAX >> 4) & 1) && HasAMXSave;
2386 Features["amx-avx512"] = HasLeaf1E && ((EAX >> 7) & 1) && HasAMXSave;
2387 Features["amx-movrs"] = HasLeaf1E && ((EAX >> 8) & 1) && HasAMXSave;
2388
2389 bool HasLeaf24 = MaxLevel >= 0x24 &&
2390 !getX86CpuIDAndInfoEx(0x24, 0x0, &EAX, &EBX, &ECX, &EDX);
2391
2392 int AVX10Ver = HasLeaf24 ? (EBX & 0xff) : 0;
2393 Features["avx10.1"] = HasAVX10 && AVX10Ver >= 1;
2394 Features["avx10.2"] = HasAVX10 && AVX10Ver >= 2;
2395
2396 bool HasLeaf24Subleaf1 =
2397 HasLeaf24 && EAX >= 1 &&
2398 !getX86CpuIDAndInfoEx(0x24, 0x1, &EAX, &EBX, &ECX, &EDX);
2399 Features["avx10v2aux"] = HasAVX10 && HasLeaf24Subleaf1 && ((ECX >> 3) & 1);
2400
2401 return Features;
2402}
2403#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
2405 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
2406 if (!P)
2407 return {};
2408
2409 return detail::getHostCPUFeaturesForARM(P->getBuffer());
2410}
2411#elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64) || \
2412 defined(__arm64ec__) || defined(_M_ARM64EC))
2413#ifndef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
2414#define PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE 43
2415#endif
2416#ifndef PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE
2417#define PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE 44
2418#endif
2419#ifndef PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE
2420#define PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE 45
2421#endif
2422#ifndef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
2423#define PF_ARM_SVE_INSTRUCTIONS_AVAILABLE 46
2424#endif
2425#ifndef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
2426#define PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE 47
2427#endif
2428#ifndef PF_ARM_SVE2_1_INSTRUCTIONS_AVAILABLE
2429#define PF_ARM_SVE2_1_INSTRUCTIONS_AVAILABLE 48
2430#endif
2431#ifndef PF_ARM_SVE_PMULL128_INSTRUCTIONS_AVAILABLE
2432#define PF_ARM_SVE_PMULL128_INSTRUCTIONS_AVAILABLE 50
2433#endif
2434#ifndef PF_ARM_SVE_BITPERM_INSTRUCTIONS_AVAILABLE
2435#define PF_ARM_SVE_BITPERM_INSTRUCTIONS_AVAILABLE 51
2436#endif
2437#ifndef PF_ARM_SVE_SHA3_INSTRUCTIONS_AVAILABLE
2438#define PF_ARM_SVE_SHA3_INSTRUCTIONS_AVAILABLE 55
2439#endif
2440#ifndef PF_ARM_SVE_SM4_INSTRUCTIONS_AVAILABLE
2441#define PF_ARM_SVE_SM4_INSTRUCTIONS_AVAILABLE 56
2442#endif
2443#ifndef PF_ARM_SVE_F32MM_INSTRUCTIONS_AVAILABLE
2444#define PF_ARM_SVE_F32MM_INSTRUCTIONS_AVAILABLE 58
2445#endif
2446#ifndef PF_ARM_SVE_F64MM_INSTRUCTIONS_AVAILABLE
2447#define PF_ARM_SVE_F64MM_INSTRUCTIONS_AVAILABLE 59
2448#endif
2449#ifndef PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE
2450#define PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE 66
2451#endif
2452#ifndef PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE
2453#define PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE 67
2454#endif
2455#ifndef PF_ARM_V86_BF16_INSTRUCTIONS_AVAILABLE
2456#define PF_ARM_V86_BF16_INSTRUCTIONS_AVAILABLE 68
2457#endif
2458#ifndef PF_ARM_SME_INSTRUCTIONS_AVAILABLE
2459#define PF_ARM_SME_INSTRUCTIONS_AVAILABLE 70
2460#endif
2461#ifndef PF_ARM_SME2_INSTRUCTIONS_AVAILABLE
2462#define PF_ARM_SME2_INSTRUCTIONS_AVAILABLE 71
2463#endif
2464#ifndef PF_ARM_SME_F64F64_INSTRUCTIONS_AVAILABLE
2465#define PF_ARM_SME_F64F64_INSTRUCTIONS_AVAILABLE 85
2466#endif
2467#ifndef PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE
2468#define PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE 86
2469#endif
2470
2472 StringMap<bool> Features;
2473
2474 // If we're asking the OS at runtime, believe what the OS says
2475 Features["crc"] =
2476 IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE);
2477 Features["lse"] =
2478 IsProcessorFeaturePresent(PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE);
2479 Features["dotprod"] =
2480 IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE);
2481 Features["jsconv"] =
2482 IsProcessorFeaturePresent(PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE);
2483 Features["rcpc"] =
2484 IsProcessorFeaturePresent(PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE);
2485 Features["sve"] =
2486 IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE);
2487 Features["sve2"] =
2488 IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE);
2489 Features["sve2p1"] =
2490 IsProcessorFeaturePresent(PF_ARM_SVE2_1_INSTRUCTIONS_AVAILABLE);
2491 Features["sve-aes"] =
2492 IsProcessorFeaturePresent(PF_ARM_SVE_PMULL128_INSTRUCTIONS_AVAILABLE);
2493 Features["sve-bitperm"] =
2494 IsProcessorFeaturePresent(PF_ARM_SVE_BITPERM_INSTRUCTIONS_AVAILABLE);
2495 Features["sve-sha3"] =
2496 IsProcessorFeaturePresent(PF_ARM_SVE_SHA3_INSTRUCTIONS_AVAILABLE);
2497 Features["sve-sm4"] =
2498 IsProcessorFeaturePresent(PF_ARM_SVE_SM4_INSTRUCTIONS_AVAILABLE);
2499 Features["f32mm"] =
2500 IsProcessorFeaturePresent(PF_ARM_SVE_F32MM_INSTRUCTIONS_AVAILABLE);
2501 Features["f64mm"] =
2502 IsProcessorFeaturePresent(PF_ARM_SVE_F64MM_INSTRUCTIONS_AVAILABLE);
2503 Features["i8mm"] =
2504 IsProcessorFeaturePresent(PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE);
2505 Features["fullfp16"] =
2506 IsProcessorFeaturePresent(PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE);
2507 Features["bf16"] =
2508 IsProcessorFeaturePresent(PF_ARM_V86_BF16_INSTRUCTIONS_AVAILABLE);
2509 Features["sme"] =
2510 IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE);
2511 Features["sme2"] =
2512 IsProcessorFeaturePresent(PF_ARM_SME2_INSTRUCTIONS_AVAILABLE);
2513 Features["sme-i16i64"] =
2514 IsProcessorFeaturePresent(PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE);
2515 Features["sme-f64f64"] =
2516 IsProcessorFeaturePresent(PF_ARM_SME_F64F64_INSTRUCTIONS_AVAILABLE);
2517
2518 // Avoid inferring "crypto" means more than the traditional AES + SHA2
2519 bool TradCrypto =
2520 IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE);
2521 Features["aes"] = TradCrypto;
2522 Features["sha2"] = TradCrypto;
2523
2524 return Features;
2525}
2526#elif defined(__linux__) && defined(__loongarch__)
2527#include <sys/auxv.h>
2529 unsigned long hwcap = getauxval(AT_HWCAP);
2530 bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
2531 uint32_t cpucfg2 = 0x2, cpucfg3 = 0x3;
2532 __asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2));
2533 __asm__("cpucfg %[cpucfg3], %[cpucfg3]\n\t" : [cpucfg3] "+r"(cpucfg3));
2534
2535 StringMap<bool> Features;
2536
2537 Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
2538 Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
2539
2540 Features["lsx"] = hwcap & (1UL << 4); // HWCAP_LOONGARCH_LSX
2541 Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
2542 Features["lvz"] = hwcap & (1UL << 9); // HWCAP_LOONGARCH_LVZ
2543
2544 Features["frecipe"] = cpucfg2 & (1U << 25); // CPUCFG.2.FRECIPE
2545 Features["div32"] = cpucfg2 & (1U << 26); // CPUCFG.2.DIV32
2546 Features["lam-bh"] = cpucfg2 & (1U << 27); // CPUCFG.2.LAM_BH
2547 Features["lamcas"] = cpucfg2 & (1U << 28); // CPUCFG.2.LAMCAS
2548 Features["scq"] = cpucfg2 & (1U << 30); // CPUCFG.2.SCQ
2549
2550 Features["ld-seq-sa"] = cpucfg3 & (1U << 23); // CPUCFG.3.LD_SEQ_SA
2551
2552 // TODO: Need to complete.
2553 // Features["llacq-screl"] = cpucfg2 & (1U << 29); // CPUCFG.2.LLACQ_SCREL
2554 return Features;
2555}
2556#elif defined(__linux__) && defined(__riscv)
2558 RISCVHwProbe Query[]{{/*RISCV_HWPROBE_KEY_BASE_BEHAVIOR=*/3, 0},
2559 {/*RISCV_HWPROBE_KEY_IMA_EXT_0=*/4, 0},
2560 {/*RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF=*/9, 0},
2561 {/*RISCV_HWPROBE_KEY_IMA_EXT_1=*/16, 0}};
2562 int Ret = syscall(/*__NR_riscv_hwprobe=*/258, /*pairs=*/Query,
2563 /*pair_count=*/std::size(Query), /*cpu_count=*/0,
2564 /*cpus=*/0, /*flags=*/0);
2565 if (Ret != 0)
2566 return {};
2567
2568 StringMap<bool> Features;
2569 uint64_t BaseMask = Query[0].Value;
2570 // Check whether RISCV_HWPROBE_BASE_BEHAVIOR_IMA is set.
2571 if (BaseMask & 1) {
2572 Features["i"] = true;
2573 Features["m"] = true;
2574 Features["a"] = true;
2575 }
2576
2577 uint64_t ExtMask = Query[1].Value;
2578 Features["f"] = ExtMask & (1 << 0); // RISCV_HWPROBE_IMA_FD
2579 Features["d"] = ExtMask & (1 << 0); // RISCV_HWPROBE_IMA_FD
2580 Features["c"] = ExtMask & (1 << 1); // RISCV_HWPROBE_IMA_C
2581 Features["v"] = ExtMask & (1 << 2); // RISCV_HWPROBE_IMA_V
2582 Features["zba"] = ExtMask & (1 << 3); // RISCV_HWPROBE_EXT_ZBA
2583 Features["zbb"] = ExtMask & (1 << 4); // RISCV_HWPROBE_EXT_ZBB
2584 Features["zbs"] = ExtMask & (1 << 5); // RISCV_HWPROBE_EXT_ZBS
2585 Features["zicboz"] = ExtMask & (1 << 6); // RISCV_HWPROBE_EXT_ZICBOZ
2586 Features["zbc"] = ExtMask & (1 << 7); // RISCV_HWPROBE_EXT_ZBC
2587 Features["zbkb"] = ExtMask & (1 << 8); // RISCV_HWPROBE_EXT_ZBKB
2588 Features["zbkc"] = ExtMask & (1 << 9); // RISCV_HWPROBE_EXT_ZBKC
2589 Features["zbkx"] = ExtMask & (1 << 10); // RISCV_HWPROBE_EXT_ZBKX
2590 Features["zknd"] = ExtMask & (1 << 11); // RISCV_HWPROBE_EXT_ZKND
2591 Features["zkne"] = ExtMask & (1 << 12); // RISCV_HWPROBE_EXT_ZKNE
2592 Features["zknh"] = ExtMask & (1 << 13); // RISCV_HWPROBE_EXT_ZKNH
2593 Features["zksed"] = ExtMask & (1 << 14); // RISCV_HWPROBE_EXT_ZKSED
2594 Features["zksh"] = ExtMask & (1 << 15); // RISCV_HWPROBE_EXT_ZKSH
2595 Features["zkt"] = ExtMask & (1 << 16); // RISCV_HWPROBE_EXT_ZKT
2596 Features["zvbb"] = ExtMask & (1 << 17); // RISCV_HWPROBE_EXT_ZVBB
2597 Features["zvbc"] = ExtMask & (1 << 18); // RISCV_HWPROBE_EXT_ZVBC
2598 Features["zvkb"] = ExtMask & (1 << 19); // RISCV_HWPROBE_EXT_ZVKB
2599 Features["zvkg"] = ExtMask & (1 << 20); // RISCV_HWPROBE_EXT_ZVKG
2600 Features["zvkned"] = ExtMask & (1 << 21); // RISCV_HWPROBE_EXT_ZVKNED
2601 Features["zvknha"] = ExtMask & (1 << 22); // RISCV_HWPROBE_EXT_ZVKNHA
2602 Features["zvknhb"] = ExtMask & (1 << 23); // RISCV_HWPROBE_EXT_ZVKNHB
2603 Features["zvksed"] = ExtMask & (1 << 24); // RISCV_HWPROBE_EXT_ZVKSED
2604 Features["zvksh"] = ExtMask & (1 << 25); // RISCV_HWPROBE_EXT_ZVKSH
2605 Features["zvkt"] = ExtMask & (1 << 26); // RISCV_HWPROBE_EXT_ZVKT
2606 Features["zfh"] = ExtMask & (1 << 27); // RISCV_HWPROBE_EXT_ZFH
2607 Features["zfhmin"] = ExtMask & (1 << 28); // RISCV_HWPROBE_EXT_ZFHMIN
2608 Features["zihintntl"] = ExtMask & (1 << 29); // RISCV_HWPROBE_EXT_ZIHINTNTL
2609 Features["zvfh"] = ExtMask & (1 << 30); // RISCV_HWPROBE_EXT_ZVFH
2610 Features["zvfhmin"] = ExtMask & (1ULL << 31); // RISCV_HWPROBE_EXT_ZVFHMIN
2611 Features["zfa"] = ExtMask & (1ULL << 32); // RISCV_HWPROBE_EXT_ZFA
2612 Features["ztso"] = ExtMask & (1ULL << 33); // RISCV_HWPROBE_EXT_ZTSO
2613 Features["zacas"] = ExtMask & (1ULL << 34); // RISCV_HWPROBE_EXT_ZACAS
2614 Features["zicond"] = ExtMask & (1ULL << 35); // RISCV_HWPROBE_EXT_ZICOND
2615 Features["zihintpause"] =
2616 ExtMask & (1ULL << 36); // RISCV_HWPROBE_EXT_ZIHINTPAUSE
2617 Features["zve32x"] = ExtMask & (1ULL << 37); // RISCV_HWPROBE_EXT_ZVE32X
2618 Features["zve32f"] = ExtMask & (1ULL << 38); // RISCV_HWPROBE_EXT_ZVE32F
2619 Features["zve64x"] = ExtMask & (1ULL << 39); // RISCV_HWPROBE_EXT_ZVE64X
2620 Features["zve64f"] = ExtMask & (1ULL << 40); // RISCV_HWPROBE_EXT_ZVE64F
2621 Features["zve64d"] = ExtMask & (1ULL << 41); // RISCV_HWPROBE_EXT_ZVE64D
2622 Features["zimop"] = ExtMask & (1ULL << 42); // RISCV_HWPROBE_EXT_ZIMOP
2623 Features["zca"] = ExtMask & (1ULL << 43); // RISCV_HWPROBE_EXT_ZCA
2624 Features["zcb"] = ExtMask & (1ULL << 44); // RISCV_HWPROBE_EXT_ZCB
2625 Features["zcd"] = ExtMask & (1ULL << 45); // RISCV_HWPROBE_EXT_ZCD
2626 Features["zcf"] = ExtMask & (1ULL << 46); // RISCV_HWPROBE_EXT_ZCF
2627 Features["zcmop"] = ExtMask & (1ULL << 47); // RISCV_HWPROBE_EXT_ZCMOP
2628 Features["zawrs"] = ExtMask & (1ULL << 48); // RISCV_HWPROBE_EXT_ZAWRS
2629 Features["supm"] = ExtMask & (1ULL << 49); // RISCV_HWPROBE_EXT_SUPM
2630 Features["zicntr"] = ExtMask & (1ULL << 50); // RISCV_HWPROBE_EXT_ZICNTR
2631 Features["zihpm"] = ExtMask & (1ULL << 51); // RISCV_HWPROBE_EXT_ZIHPM
2632 Features["zfbfmin"] = ExtMask & (1ULL << 52); // RISCV_HWPROBE_EXT_ZFBFMIN
2633 Features["zvfbfmin"] = ExtMask & (1ULL << 53); // RISCV_HWPROBE_EXT_ZVFBFMIN
2634 Features["zvfbfwma"] = ExtMask & (1ULL << 54); // RISCV_HWPROBE_EXT_ZVFBFWMA
2635 Features["zicbom"] = ExtMask & (1ULL << 55); // RISCV_HWPROBE_EXT_ZICBOM
2636 Features["zaamo"] = ExtMask & (1ULL << 56); // RISCV_HWPROBE_EXT_ZAAMO
2637 Features["zalrsc"] = ExtMask & (1ULL << 57); // RISCV_HWPROBE_EXT_ZALRSC
2638 Features["zabha"] = ExtMask & (1ULL << 58); // RISCV_HWPROBE_EXT_ZABHA
2639 Features["zalasr"] = ExtMask & (1ULL << 59); // RISCV_HWPROBE_EXT_ZALASR
2640 Features["zicbop"] = ExtMask & (1ULL << 60); // RISCV_HWPROBE_EXT_ZICBOP
2641 Features["zilsd"] = ExtMask & (1ULL << 61); // RISCV_HWPROBE_EXT_ZILSD
2642 Features["zclsd"] = ExtMask & (1ULL << 62); // RISCV_HWPROBE_EXT_ZCLSD
2643
2644 uint64_t Ext1Mask = Query[3].Value;
2645 Features["zicfiss"] = Ext1Mask & (1ULL << 0); // RISCV_HWPROBE_EXT_ZICFISS
2646
2647 // Check whether the processor supports fast misaligned scalar memory access.
2648 // NOTE: RISCV_HWPROBE_KEY_MISALIGNED_SCALAR_PERF is only available on
2649 // Linux 6.11 or later. If it is not recognized, the key field will be cleared
2650 // to -1.
2651 if (Query[2].Key != -1 &&
2652 Query[2].Value == /*RISCV_HWPROBE_MISALIGNED_SCALAR_FAST=*/3)
2653 Features["unaligned-scalar-mem"] = true;
2654
2655 // Infer Zvl from vlenb CSR.
2656 if (Features["v"] || Features["zve32x"]) {
2657#if __riscv_xlen == 64
2658 uint64_t VLen;
2659#elif __riscv_xlen == 32
2660 uint32_t VLen;
2661#else
2662#error "Unknown XLEN"
2663#endif
2664 // Use the raw CSR number in case assembler doesn't know vlenb.
2665 __asm__ volatile("csrr %0, 0xc22" : "=r"(VLen));
2666 VLen *= 8;
2667 std::string ZvlFeature = (Twine("zvl") + Twine(VLen) + "b").str();
2668 Features[ZvlFeature] = true;
2669 } else {
2670 // The host CPU name may imply a Zvl*b even though vector is unavailable
2671 // (e.g. if the kernel has V disabled). Disabling zvl32b also disables
2672 // every larger Zvl*b, as they all imply it.
2673 Features["zvl32b"] = false;
2674 }
2675
2676 return Features;
2677}
2678#else
2680#endif
2681
2682#if __APPLE__
2683/// \returns the \p triple, but with the Host's arch spliced in.
2684static Triple withHostArch(Triple T) {
2685#if defined(__arm__)
2686 T.setArch(Triple::arm);
2687 T.setArchName("arm");
2688#elif defined(__arm64e__)
2690 T.setArchName("arm64e");
2691#elif defined(__aarch64__)
2692 T.setArch(Triple::aarch64);
2693 T.setArchName("arm64");
2694#elif defined(__x86_64h__)
2695 T.setArch(Triple::x86_64);
2696 T.setArchName("x86_64h");
2697#elif defined(__x86_64__)
2698 T.setArch(Triple::x86_64);
2699 T.setArchName("x86_64");
2700#elif defined(__i386__)
2701 T.setArch(Triple::x86);
2702 T.setArchName("i386");
2703#elif defined(__powerpc__)
2704 T.setArch(Triple::ppc);
2705 T.setArchName("powerpc");
2706#else
2707# error "Unimplemented host arch fixup"
2708#endif
2709 return T;
2710}
2711#endif
2712
2714 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
2715 Triple PT(Triple::normalize(TargetTripleString));
2716
2717#if __APPLE__
2718 /// In Universal builds, LLVM_HOST_TRIPLE will have the wrong arch in one of
2719 /// the slices. This fixes that up.
2720 PT = withHostArch(PT);
2721#endif
2722
2723 if (sizeof(void *) == 8 && PT.isArch32Bit())
2724 PT = PT.get64BitArchVariant();
2725 if (sizeof(void *) == 4 && PT.isArch64Bit())
2726 PT = PT.get32BitArchVariant();
2727
2728 return PT.str();
2729}
2730
2732#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
2733 std::string CPU = std::string(sys::getHostCPUName());
2734 if (CPU == "generic")
2735 CPU = "(unknown)";
2736 OS << " Default target: " << sys::getDefaultTargetTriple() << '\n'
2737 << " Host CPU: " << CPU << '\n';
2738#endif
2739}
This file defines the StringMap class.
uint64_t uint64_t
This file implements methods to test, set and extract typed bits from packed unsigned integers.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
StringRef getHostCPUNameForARMFromComponents(StringRef Implementer, StringRef Hardware, StringRef Part, ArrayRef< StringRef > Parts, function_ref< unsigned()> GetVariant)
Definition Host.cpp:182
static std::unique_ptr< llvm::MemoryBuffer > getProcCpuinfoContent()
Definition Host.cpp:74
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define P(N)
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
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.
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
bool contains(StringRef Key) const
contains - Return true if the element is in the map, false otherwise.
Definition StringMap.h:270
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:728
static constexpr size_t npos
Definition StringRef.h:58
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:482
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:589
iterator begin() const
Definition StringRef.h:114
const char * const_iterator
Definition StringRef.h:61
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition StringRef.h:818
iterator end() const
Definition StringRef.h:116
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition StringRef.h:270
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
LLVM_ABI llvm::Triple get32BitArchVariant() const
Form a triple with a 32-bit variant of the current architecture.
Definition Triple.cpp:1862
LLVM_ABI llvm::Triple get64BitArchVariant() const
Form a triple with a 64-bit variant of the current architecture.
Definition Triple.cpp:1978
static LLVM_ABI std::string normalize(StringRef Str, CanonicalForm Form=CanonicalForm::ANY)
Turn an arbitrary machine specification into the canonical triple form (or something sensible that th...
Definition Triple.cpp:1211
const std::string & str() const
Definition Triple.h:583
LLVM_ABI bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition Triple.cpp:1850
@ AArch64SubArch_arm64e
Definition Triple.h:162
LLVM_ABI bool isArch32Bit() const
Test whether the architecture is 32-bit.
Definition Triple.cpp:1854
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ CPU_SUBTYPE_POWERPC_970
Definition MachO.h:1834
@ CPU_SUBTYPE_POWERPC_604e
Definition MachO.h:1829
@ CPU_SUBTYPE_POWERPC_603e
Definition MachO.h:1826
@ CPU_SUBTYPE_POWERPC_7400
Definition MachO.h:1832
@ CPU_SUBTYPE_POWERPC_604
Definition MachO.h:1828
@ CPU_SUBTYPE_POWERPC_750
Definition MachO.h:1831
@ CPU_SUBTYPE_POWERPC_601
Definition MachO.h:1823
@ CPU_SUBTYPE_POWERPC_620
Definition MachO.h:1830
@ CPU_SUBTYPE_POWERPC_603ev
Definition MachO.h:1827
@ CPU_SUBTYPE_POWERPC_603
Definition MachO.h:1825
@ CPU_SUBTYPE_POWERPC_7450
Definition MachO.h:1833
@ CPU_SUBTYPE_POWERPC_602
Definition MachO.h:1824
LLVM_ABI StringRef getCPUNameFromCPUModel(const CPUModel &Model)
Helper functions to extract CPU details from CPUID on x86.
Definition Host.h:80
LLVM_ABI VendorSignatures getVendorSignature(unsigned *MaxLeaf=nullptr)
Returns the host CPU's vendor.
Definition Host.cpp:2061
LLVM_ABI StringRef getHostCPUNameForSPARC(StringRef ProcCpuinfoContent)
LLVM_ABI StringRef getHostCPUNameForS390x(StringRef ProcCpuinfoContent)
Definition Host.cpp:522
LLVM_ABI StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent)
Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
Definition Host.cpp:89
LLVM_ABI StringRef getHostCPUNameForBPF()
Definition Host.cpp:587
LLVM_ABI StringRef getHostCPUNameForARM(StringRef ProcCpuinfoContent)
Definition Host.cpp:411
LLVM_ABI StringRef getHostCPUNameForRISCV(StringRef ProcCpuinfoContent)
Definition Host.cpp:563
LLVM_ABI StringMap< bool, MallocAllocator > getHostCPUFeaturesForARM(StringRef ProcCpuinfoContent)
Helper function to extract host CPU features from /proc/cpuinfo on ARM and AArch64 linux.
Definition Host.cpp:2072
LLVM_ABI StringMap< bool, MallocAllocator > getHostCPUFeatures()
getHostCPUFeatures - Get the LLVM names for the host CPU features.
Definition Host.cpp:2679
LLVM_ABI StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition Host.cpp:2055
LLVM_ABI void printDefaultTargetAndDetectedCPU(raw_ostream &OS)
This is a function compatible with cl::AddExtraVersionPrinter, which adds info about the current targ...
Definition Host.cpp:2731
LLVM_ABI std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition Host.cpp:2713
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:577
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
int64_t decodePackedBCD(const uint8_t *Ptr, size_t ByteLen, bool IsSigned=true)
Definition BCD.h:26
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2150
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
Describes an element of a Bitfield.
Definition Bitfields.h:176
static Bitfield::Type get(StorageType Packed)
Unpacks the field from the Packed value.
Definition Bitfields.h:207