Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1 | //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the operating system Host concept. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Support/Host.h" |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 14 | #include "llvm/Support/TargetParser.h" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallSet.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | #include "llvm/ADT/Triple.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 20 | #include "llvm/Config/llvm-config.h" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/FileSystem.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <assert.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | // Include the platform-specific parts of this class. |
| 29 | #ifdef LLVM_ON_UNIX |
| 30 | #include "Unix/Host.inc" |
| 31 | #endif |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 32 | #ifdef _WIN32 |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 33 | #include "Windows/Host.inc" |
| 34 | #endif |
| 35 | #ifdef _MSC_VER |
| 36 | #include <intrin.h> |
| 37 | #endif |
Chris Bieneman | 34688fa | 2019-10-30 12:50:04 -0700 | [diff] [blame] | 38 | #if defined(__APPLE__) && (!defined(__x86_64__)) |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 39 | #include <mach/host_info.h> |
| 40 | #include <mach/mach.h> |
| 41 | #include <mach/mach_host.h> |
| 42 | #include <mach/machine.h> |
| 43 | #endif |
| 44 | |
| 45 | #define DEBUG_TYPE "host-detection" |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | // |
| 49 | // Implementations of the CPU detection routines |
| 50 | // |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | using namespace llvm; |
| 54 | |
| 55 | static std::unique_ptr<llvm::MemoryBuffer> |
| 56 | LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent() { |
| 57 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 58 | llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo"); |
| 59 | if (std::error_code EC = Text.getError()) { |
| 60 | llvm::errs() << "Can't read " |
| 61 | << "/proc/cpuinfo: " << EC.message() << "\n"; |
| 62 | return nullptr; |
| 63 | } |
| 64 | return std::move(*Text); |
| 65 | } |
| 66 | |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 67 | StringRef sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 68 | // Access to the Processor Version Register (PVR) on PowerPC is privileged, |
| 69 | // and so we must use an operating-system interface to determine the current |
| 70 | // processor type. On Linux, this is exposed through the /proc/cpuinfo file. |
| 71 | const char *generic = "generic"; |
| 72 | |
| 73 | // The cpu line is second (after the 'processor: 0' line), so if this |
| 74 | // buffer is too small then something has changed (or is wrong). |
| 75 | StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin(); |
| 76 | StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end(); |
| 77 | |
| 78 | StringRef::const_iterator CIP = CPUInfoStart; |
| 79 | |
| 80 | StringRef::const_iterator CPUStart = 0; |
| 81 | size_t CPULen = 0; |
| 82 | |
| 83 | // We need to find the first line which starts with cpu, spaces, and a colon. |
| 84 | // After the colon, there may be some additional spaces and then the cpu type. |
| 85 | while (CIP < CPUInfoEnd && CPUStart == 0) { |
| 86 | if (CIP < CPUInfoEnd && *CIP == '\n') |
| 87 | ++CIP; |
| 88 | |
| 89 | if (CIP < CPUInfoEnd && *CIP == 'c') { |
| 90 | ++CIP; |
| 91 | if (CIP < CPUInfoEnd && *CIP == 'p') { |
| 92 | ++CIP; |
| 93 | if (CIP < CPUInfoEnd && *CIP == 'u') { |
| 94 | ++CIP; |
| 95 | while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t')) |
| 96 | ++CIP; |
| 97 | |
| 98 | if (CIP < CPUInfoEnd && *CIP == ':') { |
| 99 | ++CIP; |
| 100 | while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t')) |
| 101 | ++CIP; |
| 102 | |
| 103 | if (CIP < CPUInfoEnd) { |
| 104 | CPUStart = CIP; |
| 105 | while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' && |
| 106 | *CIP != ',' && *CIP != '\n')) |
| 107 | ++CIP; |
| 108 | CPULen = CIP - CPUStart; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (CPUStart == 0) |
| 116 | while (CIP < CPUInfoEnd && *CIP != '\n') |
| 117 | ++CIP; |
| 118 | } |
| 119 | |
| 120 | if (CPUStart == 0) |
| 121 | return generic; |
| 122 | |
| 123 | return StringSwitch<const char *>(StringRef(CPUStart, CPULen)) |
| 124 | .Case("604e", "604e") |
| 125 | .Case("604", "604") |
| 126 | .Case("7400", "7400") |
| 127 | .Case("7410", "7400") |
| 128 | .Case("7447", "7400") |
| 129 | .Case("7455", "7450") |
| 130 | .Case("G4", "g4") |
| 131 | .Case("POWER4", "970") |
| 132 | .Case("PPC970FX", "970") |
| 133 | .Case("PPC970MP", "970") |
| 134 | .Case("G5", "g5") |
| 135 | .Case("POWER5", "g5") |
| 136 | .Case("A2", "a2") |
| 137 | .Case("POWER6", "pwr6") |
| 138 | .Case("POWER7", "pwr7") |
| 139 | .Case("POWER8", "pwr8") |
| 140 | .Case("POWER8E", "pwr8") |
| 141 | .Case("POWER8NVL", "pwr8") |
| 142 | .Case("POWER9", "pwr9") |
| 143 | .Default(generic); |
| 144 | } |
| 145 | |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 146 | StringRef sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 147 | // The cpuid register on arm is not accessible from user space. On Linux, |
| 148 | // it is exposed through the /proc/cpuinfo file. |
| 149 | |
| 150 | // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line |
| 151 | // in all cases. |
| 152 | SmallVector<StringRef, 32> Lines; |
| 153 | ProcCpuinfoContent.split(Lines, "\n"); |
| 154 | |
| 155 | // Look for the CPU implementer line. |
| 156 | StringRef Implementer; |
| 157 | StringRef Hardware; |
| 158 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) { |
| 159 | if (Lines[I].startswith("CPU implementer")) |
| 160 | Implementer = Lines[I].substr(15).ltrim("\t :"); |
| 161 | if (Lines[I].startswith("Hardware")) |
| 162 | Hardware = Lines[I].substr(8).ltrim("\t :"); |
| 163 | } |
| 164 | |
| 165 | if (Implementer == "0x41") { // ARM Ltd. |
| 166 | // MSM8992/8994 may give cpu part for the core that the kernel is running on, |
| 167 | // which is undeterministic and wrong. Always return cortex-a53 for these SoC. |
| 168 | if (Hardware.endswith("MSM8994") || Hardware.endswith("MSM8996")) |
| 169 | return "cortex-a53"; |
| 170 | |
| 171 | |
| 172 | // Look for the CPU part line. |
| 173 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 174 | if (Lines[I].startswith("CPU part")) |
| 175 | // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The |
| 176 | // values correspond to the "Part number" in the CP15/c0 register. The |
| 177 | // contents are specified in the various processor manuals. |
| 178 | return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) |
| 179 | .Case("0x926", "arm926ej-s") |
| 180 | .Case("0xb02", "mpcore") |
| 181 | .Case("0xb36", "arm1136j-s") |
| 182 | .Case("0xb56", "arm1156t2-s") |
| 183 | .Case("0xb76", "arm1176jz-s") |
| 184 | .Case("0xc08", "cortex-a8") |
| 185 | .Case("0xc09", "cortex-a9") |
| 186 | .Case("0xc0f", "cortex-a15") |
| 187 | .Case("0xc20", "cortex-m0") |
| 188 | .Case("0xc23", "cortex-m3") |
| 189 | .Case("0xc24", "cortex-m4") |
| 190 | .Case("0xd04", "cortex-a35") |
| 191 | .Case("0xd03", "cortex-a53") |
| 192 | .Case("0xd07", "cortex-a57") |
| 193 | .Case("0xd08", "cortex-a72") |
| 194 | .Case("0xd09", "cortex-a73") |
Yi Kong | 432f48f | 2019-06-11 00:05:36 +0000 | [diff] [blame] | 195 | .Case("0xd0a", "cortex-a75") |
| 196 | .Case("0xd0b", "cortex-a76") |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 197 | .Default("generic"); |
| 198 | } |
| 199 | |
Joel Jones | 0a6c000 | 2018-10-05 22:23:21 +0000 | [diff] [blame] | 200 | if (Implementer == "0x42" || Implementer == "0x43") { // Broadcom | Cavium. |
| 201 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) { |
| 202 | if (Lines[I].startswith("CPU part")) { |
| 203 | return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) |
| 204 | .Case("0x516", "thunderx2t99") |
| 205 | .Case("0x0516", "thunderx2t99") |
| 206 | .Case("0xaf", "thunderx2t99") |
| 207 | .Case("0x0af", "thunderx2t99") |
| 208 | .Case("0xa1", "thunderxt88") |
| 209 | .Case("0x0a1", "thunderxt88") |
| 210 | .Default("generic"); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Bryan Chan | 1235539 | 2018-11-09 19:32:08 +0000 | [diff] [blame] | 215 | if (Implementer == "0x48") // HiSilicon Technologies, Inc. |
| 216 | // Look for the CPU part line. |
| 217 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 218 | if (Lines[I].startswith("CPU part")) |
| 219 | // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The |
| 220 | // values correspond to the "Part number" in the CP15/c0 register. The |
| 221 | // contents are specified in the various processor manuals. |
| 222 | return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) |
| 223 | .Case("0xd01", "tsv110") |
| 224 | .Default("generic"); |
| 225 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 226 | if (Implementer == "0x51") // Qualcomm Technologies, Inc. |
| 227 | // Look for the CPU part line. |
| 228 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 229 | if (Lines[I].startswith("CPU part")) |
| 230 | // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The |
| 231 | // values correspond to the "Part number" in the CP15/c0 register. The |
| 232 | // contents are specified in the various processor manuals. |
| 233 | return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) |
| 234 | .Case("0x06f", "krait") // APQ8064 |
| 235 | .Case("0x201", "kryo") |
| 236 | .Case("0x205", "kryo") |
Eli Friedman | bde9fc7 | 2017-09-13 21:48:00 +0000 | [diff] [blame] | 237 | .Case("0x211", "kryo") |
| 238 | .Case("0x800", "cortex-a73") |
| 239 | .Case("0x801", "cortex-a73") |
Yi Kong | 432f48f | 2019-06-11 00:05:36 +0000 | [diff] [blame] | 240 | .Case("0x802", "cortex-a73") |
| 241 | .Case("0x803", "cortex-a73") |
| 242 | .Case("0x804", "cortex-a73") |
| 243 | .Case("0x805", "cortex-a73") |
Balaram Makam | a1e7ecc7 | 2017-09-22 17:46:36 +0000 | [diff] [blame] | 244 | .Case("0xc00", "falkor") |
Chad Rosier | 7107085 | 2017-09-25 14:05:00 +0000 | [diff] [blame] | 245 | .Case("0xc01", "saphira") |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 246 | .Default("generic"); |
| 247 | |
Evandro Menezes | 5d7a9e6 | 2017-12-08 21:09:59 +0000 | [diff] [blame] | 248 | if (Implementer == "0x53") { // Samsung Electronics Co., Ltd. |
| 249 | // The Exynos chips have a convoluted ID scheme that doesn't seem to follow |
| 250 | // any predictive pattern across variants and parts. |
| 251 | unsigned Variant = 0, Part = 0; |
| 252 | |
| 253 | // Look for the CPU variant line, whose value is a 1 digit hexadecimal |
| 254 | // number, corresponding to the Variant bits in the CP15/C0 register. |
| 255 | for (auto I : Lines) |
| 256 | if (I.consume_front("CPU variant")) |
| 257 | I.ltrim("\t :").getAsInteger(0, Variant); |
| 258 | |
| 259 | // Look for the CPU part line, whose value is a 3 digit hexadecimal |
| 260 | // number, corresponding to the PartNum bits in the CP15/C0 register. |
| 261 | for (auto I : Lines) |
| 262 | if (I.consume_front("CPU part")) |
| 263 | I.ltrim("\t :").getAsInteger(0, Part); |
| 264 | |
| 265 | unsigned Exynos = (Variant << 12) | Part; |
| 266 | switch (Exynos) { |
| 267 | default: |
Evandro Menezes | 215da66 | 2019-10-02 16:26:40 -0500 | [diff] [blame] | 268 | // Default by falling through to Exynos M3. |
Evandro Menezes | 5d7a9e6 | 2017-12-08 21:09:59 +0000 | [diff] [blame] | 269 | LLVM_FALLTHROUGH; |
Evandro Menezes | 215da66 | 2019-10-02 16:26:40 -0500 | [diff] [blame] | 270 | case 0x1002: |
| 271 | return "exynos-m3"; |
| 272 | case 0x1003: |
| 273 | return "exynos-m4"; |
Evandro Menezes | 5d7a9e6 | 2017-12-08 21:09:59 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 277 | return "generic"; |
| 278 | } |
| 279 | |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 280 | StringRef sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 281 | // STIDP is a privileged operation, so use /proc/cpuinfo instead. |
| 282 | |
| 283 | // The "processor 0:" line comes after a fair amount of other information, |
| 284 | // including a cache breakdown, but this should be plenty. |
| 285 | SmallVector<StringRef, 32> Lines; |
| 286 | ProcCpuinfoContent.split(Lines, "\n"); |
| 287 | |
| 288 | // Look for the CPU features. |
| 289 | SmallVector<StringRef, 32> CPUFeatures; |
| 290 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 291 | if (Lines[I].startswith("features")) { |
| 292 | size_t Pos = Lines[I].find(":"); |
| 293 | if (Pos != StringRef::npos) { |
| 294 | Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' '); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // We need to check for the presence of vector support independently of |
| 300 | // the machine type, since we may only use the vector register set when |
| 301 | // supported by the kernel (and hypervisor). |
| 302 | bool HaveVectorSupport = false; |
| 303 | for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) { |
| 304 | if (CPUFeatures[I] == "vx") |
| 305 | HaveVectorSupport = true; |
| 306 | } |
| 307 | |
| 308 | // Now check the processor machine type. |
| 309 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) { |
| 310 | if (Lines[I].startswith("processor ")) { |
| 311 | size_t Pos = Lines[I].find("machine = "); |
| 312 | if (Pos != StringRef::npos) { |
| 313 | Pos += sizeof("machine = ") - 1; |
| 314 | unsigned int Id; |
| 315 | if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) { |
Ulrich Weigand | 0f0a8b7 | 2019-07-12 18:13:16 +0000 | [diff] [blame] | 316 | if (Id >= 8561 && HaveVectorSupport) |
Ulrich Weigand | 819c165 | 2019-09-20 23:04:45 +0000 | [diff] [blame] | 317 | return "z15"; |
Ulrich Weigand | 2b3482f | 2017-07-17 17:41:11 +0000 | [diff] [blame] | 318 | if (Id >= 3906 && HaveVectorSupport) |
| 319 | return "z14"; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 320 | if (Id >= 2964 && HaveVectorSupport) |
| 321 | return "z13"; |
| 322 | if (Id >= 2827) |
| 323 | return "zEC12"; |
| 324 | if (Id >= 2817) |
| 325 | return "z196"; |
| 326 | } |
| 327 | } |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return "generic"; |
| 333 | } |
| 334 | |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 335 | StringRef sys::detail::getHostCPUNameForBPF() { |
| 336 | #if !defined(__linux__) || !defined(__x86_64__) |
| 337 | return "generic"; |
| 338 | #else |
Jiong Wang | 66b18e5 | 2019-02-07 10:43:09 +0000 | [diff] [blame] | 339 | uint8_t v3_insns[40] __attribute__ ((aligned (8))) = |
| 340 | /* BPF_MOV64_IMM(BPF_REG_0, 0) */ |
| 341 | { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 342 | /* BPF_MOV64_IMM(BPF_REG_2, 1) */ |
| 343 | 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, |
| 344 | /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */ |
| 345 | 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 346 | /* BPF_MOV64_IMM(BPF_REG_0, 1) */ |
| 347 | 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, |
| 348 | /* BPF_EXIT_INSN() */ |
| 349 | 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; |
| 350 | |
| 351 | uint8_t v2_insns[40] __attribute__ ((aligned (8))) = |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 352 | /* BPF_MOV64_IMM(BPF_REG_0, 0) */ |
| 353 | { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 354 | /* BPF_MOV64_IMM(BPF_REG_2, 1) */ |
| 355 | 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, |
| 356 | /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */ |
| 357 | 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 358 | /* BPF_MOV64_IMM(BPF_REG_0, 1) */ |
| 359 | 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, |
| 360 | /* BPF_EXIT_INSN() */ |
| 361 | 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; |
| 362 | |
| 363 | struct bpf_prog_load_attr { |
| 364 | uint32_t prog_type; |
| 365 | uint32_t insn_cnt; |
| 366 | uint64_t insns; |
| 367 | uint64_t license; |
| 368 | uint32_t log_level; |
| 369 | uint32_t log_size; |
| 370 | uint64_t log_buf; |
| 371 | uint32_t kern_version; |
| 372 | uint32_t prog_flags; |
| 373 | } attr = {}; |
| 374 | attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */ |
| 375 | attr.insn_cnt = 5; |
Jiong Wang | 66b18e5 | 2019-02-07 10:43:09 +0000 | [diff] [blame] | 376 | attr.insns = (uint64_t)v3_insns; |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 377 | attr.license = (uint64_t)"DUMMY"; |
| 378 | |
Jiong Wang | 66b18e5 | 2019-02-07 10:43:09 +0000 | [diff] [blame] | 379 | int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, |
| 380 | sizeof(attr)); |
| 381 | if (fd >= 0) { |
| 382 | close(fd); |
| 383 | return "v3"; |
| 384 | } |
| 385 | |
| 386 | /* Clear the whole attr in case its content changed by syscall. */ |
| 387 | memset(&attr, 0, sizeof(attr)); |
| 388 | attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */ |
| 389 | attr.insn_cnt = 5; |
| 390 | attr.insns = (uint64_t)v2_insns; |
| 391 | attr.license = (uint64_t)"DUMMY"; |
| 392 | fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr)); |
Yonghong Song | c6d2571 | 2017-08-23 16:24:31 +0000 | [diff] [blame] | 393 | if (fd >= 0) { |
| 394 | close(fd); |
| 395 | return "v2"; |
| 396 | } |
| 397 | return "v1"; |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 398 | #endif |
| 399 | } |
| 400 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 401 | #if defined(__i386__) || defined(_M_IX86) || \ |
| 402 | defined(__x86_64__) || defined(_M_X64) |
| 403 | |
| 404 | enum VendorSignatures { |
| 405 | SIG_INTEL = 0x756e6547 /* Genu */, |
| 406 | SIG_AMD = 0x68747541 /* Auth */ |
| 407 | }; |
| 408 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 409 | // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max). |
| 410 | // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID |
| 411 | // support. Consequently, for i386, the presence of CPUID is checked first |
| 412 | // via the corresponding eflags bit. |
| 413 | // Removal of cpuid.h header motivated by PR30384 |
| 414 | // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp |
| 415 | // or test-suite, but are used in external projects e.g. libstdcxx |
| 416 | static bool isCpuIdSupported() { |
| 417 | #if defined(__GNUC__) || defined(__clang__) |
| 418 | #if defined(__i386__) |
| 419 | int __cpuid_supported; |
| 420 | __asm__(" pushfl\n" |
| 421 | " popl %%eax\n" |
| 422 | " movl %%eax,%%ecx\n" |
| 423 | " xorl $0x00200000,%%eax\n" |
| 424 | " pushl %%eax\n" |
| 425 | " popfl\n" |
| 426 | " pushfl\n" |
| 427 | " popl %%eax\n" |
| 428 | " movl $0,%0\n" |
| 429 | " cmpl %%eax,%%ecx\n" |
| 430 | " je 1f\n" |
| 431 | " movl $1,%0\n" |
| 432 | "1:" |
| 433 | : "=r"(__cpuid_supported) |
| 434 | : |
| 435 | : "eax", "ecx"); |
| 436 | if (!__cpuid_supported) |
| 437 | return false; |
| 438 | #endif |
| 439 | return true; |
| 440 | #endif |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in |
| 445 | /// the specified arguments. If we can't run cpuid on the host, return true. |
| 446 | static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX, |
| 447 | unsigned *rECX, unsigned *rEDX) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 448 | #if defined(__GNUC__) || defined(__clang__) |
| 449 | #if defined(__x86_64__) |
| 450 | // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually. |
| 451 | // FIXME: should we save this for Clang? |
| 452 | __asm__("movq\t%%rbx, %%rsi\n\t" |
| 453 | "cpuid\n\t" |
| 454 | "xchgq\t%%rbx, %%rsi\n\t" |
| 455 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 456 | : "a"(value)); |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 457 | return false; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 458 | #elif defined(__i386__) |
| 459 | __asm__("movl\t%%ebx, %%esi\n\t" |
| 460 | "cpuid\n\t" |
| 461 | "xchgl\t%%ebx, %%esi\n\t" |
| 462 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 463 | : "a"(value)); |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 464 | return false; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 465 | #else |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 466 | return true; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 467 | #endif |
| 468 | #elif defined(_MSC_VER) |
| 469 | // The MSVC intrinsic is portable across x86 and x64. |
| 470 | int registers[4]; |
| 471 | __cpuid(registers, value); |
| 472 | *rEAX = registers[0]; |
| 473 | *rEBX = registers[1]; |
| 474 | *rECX = registers[2]; |
| 475 | *rEDX = registers[3]; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 476 | return false; |
| 477 | #else |
| 478 | return true; |
| 479 | #endif |
| 480 | } |
| 481 | |
| 482 | /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return |
| 483 | /// the 4 values in the specified arguments. If we can't run cpuid on the host, |
| 484 | /// return true. |
| 485 | static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf, |
| 486 | unsigned *rEAX, unsigned *rEBX, unsigned *rECX, |
| 487 | unsigned *rEDX) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 488 | #if defined(__GNUC__) || defined(__clang__) |
Craig Topper | 828cf30 | 2017-07-17 05:16:16 +0000 | [diff] [blame] | 489 | #if defined(__x86_64__) |
Craig Topper | ada983a | 2017-07-10 06:09:22 +0000 | [diff] [blame] | 490 | // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually. |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 491 | // FIXME: should we save this for Clang? |
| 492 | __asm__("movq\t%%rbx, %%rsi\n\t" |
| 493 | "cpuid\n\t" |
| 494 | "xchgq\t%%rbx, %%rsi\n\t" |
| 495 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 496 | : "a"(value), "c"(subleaf)); |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 497 | return false; |
Craig Topper | 828cf30 | 2017-07-17 05:16:16 +0000 | [diff] [blame] | 498 | #elif defined(__i386__) |
| 499 | __asm__("movl\t%%ebx, %%esi\n\t" |
| 500 | "cpuid\n\t" |
| 501 | "xchgl\t%%ebx, %%esi\n\t" |
| 502 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 503 | : "a"(value), "c"(subleaf)); |
| 504 | return false; |
| 505 | #else |
| 506 | return true; |
| 507 | #endif |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 508 | #elif defined(_MSC_VER) |
| 509 | int registers[4]; |
| 510 | __cpuidex(registers, value, subleaf); |
| 511 | *rEAX = registers[0]; |
| 512 | *rEBX = registers[1]; |
| 513 | *rECX = registers[2]; |
| 514 | *rEDX = registers[3]; |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 515 | return false; |
| 516 | #else |
| 517 | return true; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 518 | #endif |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Craig Topper | f3af64e | 2017-07-12 06:49:57 +0000 | [diff] [blame] | 521 | // Read control register 0 (XCR0). Used to detect features such as AVX. |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 522 | static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) { |
| 523 | #if defined(__GNUC__) || defined(__clang__) |
| 524 | // Check xgetbv; this uses a .byte sequence instead of the instruction |
| 525 | // directly because older assemblers do not include support for xgetbv and |
| 526 | // there is no easy way to conditionally compile based on the assembler used. |
| 527 | __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0)); |
| 528 | return false; |
| 529 | #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) |
| 530 | unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK); |
| 531 | *rEAX = Result; |
| 532 | *rEDX = Result >> 32; |
| 533 | return false; |
| 534 | #else |
| 535 | return true; |
| 536 | #endif |
| 537 | } |
| 538 | |
| 539 | static void detectX86FamilyModel(unsigned EAX, unsigned *Family, |
| 540 | unsigned *Model) { |
| 541 | *Family = (EAX >> 8) & 0xf; // Bits 8 - 11 |
| 542 | *Model = (EAX >> 4) & 0xf; // Bits 4 - 7 |
| 543 | if (*Family == 6 || *Family == 0xf) { |
| 544 | if (*Family == 0xf) |
| 545 | // Examine extended family ID if family ID is F. |
| 546 | *Family += (EAX >> 20) & 0xff; // Bits 20 - 27 |
| 547 | // Examine extended model ID if family ID is 6 or F. |
| 548 | *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19 |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static void |
Craig Topper | c6bbe4b | 2017-07-08 05:16:14 +0000 | [diff] [blame] | 553 | getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model, |
| 554 | unsigned Brand_id, unsigned Features, |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 555 | unsigned Features2, unsigned Features3, |
| 556 | unsigned *Type, unsigned *Subtype) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 557 | if (Brand_id != 0) |
| 558 | return; |
| 559 | switch (Family) { |
| 560 | case 3: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 561 | *Type = X86::INTEL_i386; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 562 | break; |
| 563 | case 4: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 564 | *Type = X86::INTEL_i486; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 565 | break; |
| 566 | case 5: |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 567 | if (Features & (1 << X86::FEATURE_MMX)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 568 | *Type = X86::INTEL_PENTIUM_MMX; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 569 | break; |
| 570 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 571 | *Type = X86::INTEL_PENTIUM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 572 | break; |
| 573 | case 6: |
| 574 | switch (Model) { |
| 575 | case 0x01: // Pentium Pro processor |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 576 | *Type = X86::INTEL_PENTIUM_PRO; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 577 | break; |
| 578 | case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor, |
| 579 | // model 03 |
| 580 | case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor, |
| 581 | // model 05, and Intel Celeron processor, model 05 |
| 582 | case 0x06: // Celeron processor, model 06 |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 583 | *Type = X86::INTEL_PENTIUM_II; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 584 | break; |
| 585 | case 0x07: // Pentium III processor, model 07, and Pentium III Xeon |
| 586 | // processor, model 07 |
| 587 | case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor, |
| 588 | // model 08, and Celeron processor, model 08 |
| 589 | case 0x0a: // Pentium III Xeon processor, model 0Ah |
| 590 | case 0x0b: // Pentium III processor, model 0Bh |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 591 | *Type = X86::INTEL_PENTIUM_III; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 592 | break; |
| 593 | case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09. |
| 594 | case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model |
| 595 | // 0Dh. All processors are manufactured using the 90 nm process. |
| 596 | case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579 |
| 597 | // Integrated Processor with Intel QuickAssist Technology |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 598 | *Type = X86::INTEL_PENTIUM_M; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 599 | break; |
| 600 | case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model |
| 601 | // 0Eh. All processors are manufactured using the 65 nm process. |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 602 | *Type = X86::INTEL_CORE_DUO; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 603 | break; // yonah |
| 604 | case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile |
| 605 | // processor, Intel Core 2 Quad processor, Intel Core 2 Quad |
| 606 | // mobile processor, Intel Core 2 Extreme processor, Intel |
| 607 | // Pentium Dual-Core processor, Intel Xeon processor, model |
| 608 | // 0Fh. All processors are manufactured using the 65 nm process. |
| 609 | case 0x16: // Intel Celeron processor model 16h. All processors are |
| 610 | // manufactured using the 65 nm process |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 611 | *Type = X86::INTEL_CORE2; // "core2" |
| 612 | *Subtype = X86::INTEL_CORE2_65; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 613 | break; |
| 614 | case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model |
| 615 | // 17h. All processors are manufactured using the 45 nm process. |
| 616 | // |
| 617 | // 45nm: Penryn , Wolfdale, Yorkfield (XE) |
| 618 | case 0x1d: // Intel Xeon processor MP. All processors are manufactured using |
| 619 | // the 45 nm process. |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 620 | *Type = X86::INTEL_CORE2; // "penryn" |
| 621 | *Subtype = X86::INTEL_CORE2_45; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 622 | break; |
| 623 | case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All |
| 624 | // processors are manufactured using the 45 nm process. |
| 625 | case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz. |
| 626 | // As found in a Summer 2010 model iMac. |
| 627 | case 0x1f: |
| 628 | case 0x2e: // Nehalem EX |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 629 | *Type = X86::INTEL_COREI7; // "nehalem" |
| 630 | *Subtype = X86::INTEL_COREI7_NEHALEM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 631 | break; |
| 632 | case 0x25: // Intel Core i7, laptop version. |
| 633 | case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All |
| 634 | // processors are manufactured using the 32 nm process. |
| 635 | case 0x2f: // Westmere EX |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 636 | *Type = X86::INTEL_COREI7; // "westmere" |
| 637 | *Subtype = X86::INTEL_COREI7_WESTMERE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 638 | break; |
| 639 | case 0x2a: // Intel Core i7 processor. All processors are manufactured |
| 640 | // using the 32 nm process. |
| 641 | case 0x2d: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 642 | *Type = X86::INTEL_COREI7; //"sandybridge" |
| 643 | *Subtype = X86::INTEL_COREI7_SANDYBRIDGE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 644 | break; |
| 645 | case 0x3a: |
| 646 | case 0x3e: // Ivy Bridge EP |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 647 | *Type = X86::INTEL_COREI7; // "ivybridge" |
| 648 | *Subtype = X86::INTEL_COREI7_IVYBRIDGE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 649 | break; |
| 650 | |
| 651 | // Haswell: |
| 652 | case 0x3c: |
| 653 | case 0x3f: |
| 654 | case 0x45: |
| 655 | case 0x46: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 656 | *Type = X86::INTEL_COREI7; // "haswell" |
| 657 | *Subtype = X86::INTEL_COREI7_HASWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | // Broadwell: |
| 661 | case 0x3d: |
| 662 | case 0x47: |
| 663 | case 0x4f: |
| 664 | case 0x56: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 665 | *Type = X86::INTEL_COREI7; // "broadwell" |
| 666 | *Subtype = X86::INTEL_COREI7_BROADWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 667 | break; |
| 668 | |
| 669 | // Skylake: |
Craig Topper | c669629 | 2019-05-31 19:18:07 +0000 | [diff] [blame] | 670 | case 0x4e: // Skylake mobile |
| 671 | case 0x5e: // Skylake desktop |
| 672 | case 0x8e: // Kaby Lake mobile |
| 673 | case 0x9e: // Kaby Lake desktop |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 674 | *Type = X86::INTEL_COREI7; // "skylake" |
| 675 | *Subtype = X86::INTEL_COREI7_SKYLAKE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 676 | break; |
| 677 | |
| 678 | // Skylake Xeon: |
| 679 | case 0x55: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 680 | *Type = X86::INTEL_COREI7; |
Craig Topper | 5465875 | 2019-09-04 16:01:43 +0000 | [diff] [blame] | 681 | if (Features2 & (1 << (X86::FEATURE_AVX512BF16 - 32))) |
Pengfei Wang | f8b2893 | 2019-06-07 08:31:35 +0000 | [diff] [blame] | 682 | *Subtype = X86::INTEL_COREI7_COOPERLAKE; // "cooperlake" |
| 683 | else if (Features2 & (1 << (X86::FEATURE_AVX512VNNI - 32))) |
Craig Topper | c669629 | 2019-05-31 19:18:07 +0000 | [diff] [blame] | 684 | *Subtype = X86::INTEL_COREI7_CASCADELAKE; // "cascadelake" |
| 685 | else |
| 686 | *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 687 | break; |
| 688 | |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 689 | // Cannonlake: |
| 690 | case 0x66: |
| 691 | *Type = X86::INTEL_COREI7; |
| 692 | *Subtype = X86::INTEL_COREI7_CANNONLAKE; // "cannonlake" |
| 693 | break; |
| 694 | |
Craig Topper | cac6b76 | 2019-05-20 16:58:23 +0000 | [diff] [blame] | 695 | // Icelake: |
Craig Topper | 2f1895e | 2019-05-22 19:51:35 +0000 | [diff] [blame] | 696 | case 0x7d: |
Craig Topper | cac6b76 | 2019-05-20 16:58:23 +0000 | [diff] [blame] | 697 | case 0x7e: |
| 698 | *Type = X86::INTEL_COREI7; |
| 699 | *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT; // "icelake-client" |
| 700 | break; |
| 701 | |
Craig Topper | 2f1895e | 2019-05-22 19:51:35 +0000 | [diff] [blame] | 702 | // Icelake Xeon: |
| 703 | case 0x6a: |
| 704 | case 0x6c: |
| 705 | *Type = X86::INTEL_COREI7; |
| 706 | *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER; // "icelake-server" |
| 707 | break; |
| 708 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 709 | case 0x1c: // Most 45 nm Intel Atom processors |
| 710 | case 0x26: // 45 nm Atom Lincroft |
| 711 | case 0x27: // 32 nm Atom Medfield |
| 712 | case 0x35: // 32 nm Atom Midview |
| 713 | case 0x36: // 32 nm Atom Midview |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 714 | *Type = X86::INTEL_BONNELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 715 | break; // "bonnell" |
| 716 | |
| 717 | // Atom Silvermont codes from the Intel software optimization guide. |
| 718 | case 0x37: |
| 719 | case 0x4a: |
| 720 | case 0x4d: |
| 721 | case 0x5a: |
| 722 | case 0x5d: |
| 723 | case 0x4c: // really airmont |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 724 | *Type = X86::INTEL_SILVERMONT; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 725 | break; // "silvermont" |
Michael Zuckerman | 4bcb9c3 | 2017-06-29 10:00:33 +0000 | [diff] [blame] | 726 | // Goldmont: |
Craig Topper | 0dadfe3 | 2017-11-15 06:02:43 +0000 | [diff] [blame] | 727 | case 0x5c: // Apollo Lake |
| 728 | case 0x5f: // Denverton |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 729 | *Type = X86::INTEL_GOLDMONT; |
Michael Zuckerman | 4bcb9c3 | 2017-06-29 10:00:33 +0000 | [diff] [blame] | 730 | break; // "goldmont" |
Gabor Buella | 8f1646b | 2018-04-16 07:47:35 +0000 | [diff] [blame] | 731 | case 0x7a: |
| 732 | *Type = X86::INTEL_GOLDMONT_PLUS; |
| 733 | break; |
Craig Topper | cac6b76 | 2019-05-20 16:58:23 +0000 | [diff] [blame] | 734 | case 0x86: |
| 735 | *Type = X86::INTEL_TREMONT; |
| 736 | break; |
Craig Topper | c669629 | 2019-05-31 19:18:07 +0000 | [diff] [blame] | 737 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 738 | case 0x57: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 739 | *Type = X86::INTEL_KNL; // knl |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 740 | break; |
Craig Topper | c669629 | 2019-05-31 19:18:07 +0000 | [diff] [blame] | 741 | |
Craig Topper | 5d69291 | 2017-10-13 18:10:17 +0000 | [diff] [blame] | 742 | case 0x85: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 743 | *Type = X86::INTEL_KNM; // knm |
Craig Topper | 5d69291 | 2017-10-13 18:10:17 +0000 | [diff] [blame] | 744 | break; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 745 | |
| 746 | default: // Unknown family 6 CPU, try to guess. |
Pengfei Wang | e28cbbd | 2019-08-12 01:29:46 +0000 | [diff] [blame] | 747 | // TODO detect tigerlake host |
| 748 | if (Features3 & (1 << (X86::FEATURE_AVX512VP2INTERSECT - 64))) { |
| 749 | *Type = X86::INTEL_COREI7; |
| 750 | *Subtype = X86::INTEL_COREI7_TIGERLAKE; |
| 751 | break; |
| 752 | } |
| 753 | |
Craig Topper | aa3f249 | 2018-11-15 18:11:52 +0000 | [diff] [blame] | 754 | if (Features & (1 << X86::FEATURE_AVX512VBMI2)) { |
| 755 | *Type = X86::INTEL_COREI7; |
| 756 | *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT; |
| 757 | break; |
| 758 | } |
| 759 | |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 760 | if (Features & (1 << X86::FEATURE_AVX512VBMI)) { |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 761 | *Type = X86::INTEL_COREI7; |
| 762 | *Subtype = X86::INTEL_COREI7_CANNONLAKE; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 763 | break; |
| 764 | } |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 765 | |
Craig Topper | 5465875 | 2019-09-04 16:01:43 +0000 | [diff] [blame] | 766 | if (Features2 & (1 << (X86::FEATURE_AVX512BF16 - 32))) { |
Pengfei Wang | f8b2893 | 2019-06-07 08:31:35 +0000 | [diff] [blame] | 767 | *Type = X86::INTEL_COREI7; |
| 768 | *Subtype = X86::INTEL_COREI7_COOPERLAKE; |
| 769 | break; |
| 770 | } |
| 771 | |
Craig Topper | 5fb34b5 | 2018-11-27 18:05:00 +0000 | [diff] [blame] | 772 | if (Features2 & (1 << (X86::FEATURE_AVX512VNNI - 32))) { |
| 773 | *Type = X86::INTEL_COREI7; |
| 774 | *Subtype = X86::INTEL_COREI7_CASCADELAKE; |
| 775 | break; |
| 776 | } |
| 777 | |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 778 | if (Features & (1 << X86::FEATURE_AVX512VL)) { |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 779 | *Type = X86::INTEL_COREI7; |
| 780 | *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; |
| 781 | break; |
| 782 | } |
| 783 | |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 784 | if (Features & (1 << X86::FEATURE_AVX512ER)) { |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 785 | *Type = X86::INTEL_KNL; // knl |
| 786 | break; |
| 787 | } |
| 788 | |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 789 | if (Features3 & (1 << (X86::FEATURE_CLFLUSHOPT - 64))) { |
| 790 | if (Features3 & (1 << (X86::FEATURE_SHA - 64))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 791 | *Type = X86::INTEL_GOLDMONT; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 792 | } else { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 793 | *Type = X86::INTEL_COREI7; |
| 794 | *Subtype = X86::INTEL_COREI7_SKYLAKE; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 795 | } |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 796 | break; |
| 797 | } |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 798 | if (Features3 & (1 << (X86::FEATURE_ADX - 64))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 799 | *Type = X86::INTEL_COREI7; |
| 800 | *Subtype = X86::INTEL_COREI7_BROADWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 801 | break; |
| 802 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 803 | if (Features & (1 << X86::FEATURE_AVX2)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 804 | *Type = X86::INTEL_COREI7; |
| 805 | *Subtype = X86::INTEL_COREI7_HASWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 806 | break; |
| 807 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 808 | if (Features & (1 << X86::FEATURE_AVX)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 809 | *Type = X86::INTEL_COREI7; |
| 810 | *Subtype = X86::INTEL_COREI7_SANDYBRIDGE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 813 | if (Features & (1 << X86::FEATURE_SSE4_2)) { |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 814 | if (Features3 & (1 << (X86::FEATURE_MOVBE - 64))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 815 | *Type = X86::INTEL_SILVERMONT; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 816 | } else { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 817 | *Type = X86::INTEL_COREI7; |
| 818 | *Subtype = X86::INTEL_COREI7_NEHALEM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 819 | } |
| 820 | break; |
| 821 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 822 | if (Features & (1 << X86::FEATURE_SSE4_1)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 823 | *Type = X86::INTEL_CORE2; // "penryn" |
| 824 | *Subtype = X86::INTEL_CORE2_45; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 825 | break; |
| 826 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 827 | if (Features & (1 << X86::FEATURE_SSSE3)) { |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 828 | if (Features3 & (1 << (X86::FEATURE_MOVBE - 64))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 829 | *Type = X86::INTEL_BONNELL; // "bonnell" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 830 | } else { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 831 | *Type = X86::INTEL_CORE2; // "core2" |
| 832 | *Subtype = X86::INTEL_CORE2_65; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 833 | } |
| 834 | break; |
| 835 | } |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 836 | if (Features3 & (1 << (X86::FEATURE_EM64T - 64))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 837 | *Type = X86::INTEL_CORE2; // "core2" |
| 838 | *Subtype = X86::INTEL_CORE2_65; |
Craig Topper | a233e16 | 2017-11-02 19:13:32 +0000 | [diff] [blame] | 839 | break; |
| 840 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 841 | if (Features & (1 << X86::FEATURE_SSE3)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 842 | *Type = X86::INTEL_CORE_DUO; |
Craig Topper | a233e16 | 2017-11-02 19:13:32 +0000 | [diff] [blame] | 843 | break; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 844 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 845 | if (Features & (1 << X86::FEATURE_SSE2)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 846 | *Type = X86::INTEL_PENTIUM_M; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 847 | break; |
| 848 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 849 | if (Features & (1 << X86::FEATURE_SSE)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 850 | *Type = X86::INTEL_PENTIUM_III; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 851 | break; |
| 852 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 853 | if (Features & (1 << X86::FEATURE_MMX)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 854 | *Type = X86::INTEL_PENTIUM_II; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 855 | break; |
| 856 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 857 | *Type = X86::INTEL_PENTIUM_PRO; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 858 | break; |
| 859 | } |
| 860 | break; |
| 861 | case 15: { |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 862 | if (Features3 & (1 << (X86::FEATURE_EM64T - 64))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 863 | *Type = X86::INTEL_NOCONA; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 864 | break; |
| 865 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 866 | if (Features & (1 << X86::FEATURE_SSE3)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 867 | *Type = X86::INTEL_PRESCOTT; |
Craig Topper | 1494915 | 2017-11-02 19:13:34 +0000 | [diff] [blame] | 868 | break; |
| 869 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 870 | *Type = X86::INTEL_PENTIUM_IV; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | default: |
| 874 | break; /*"generic"*/ |
| 875 | } |
| 876 | } |
| 877 | |
Craig Topper | 2ace153 | 2017-07-08 06:44:34 +0000 | [diff] [blame] | 878 | static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model, |
| 879 | unsigned Features, unsigned *Type, |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 880 | unsigned *Subtype) { |
| 881 | // FIXME: this poorly matches the generated SubtargetFeatureKV table. There |
| 882 | // appears to be no way to generate the wide variety of AMD-specific targets |
| 883 | // from the information returned from CPUID. |
| 884 | switch (Family) { |
| 885 | case 4: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 886 | *Type = X86::AMD_i486; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 887 | break; |
| 888 | case 5: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 889 | *Type = X86::AMDPENTIUM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 890 | switch (Model) { |
| 891 | case 6: |
| 892 | case 7: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 893 | *Subtype = X86::AMDPENTIUM_K6; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 894 | break; // "k6" |
| 895 | case 8: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 896 | *Subtype = X86::AMDPENTIUM_K62; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 897 | break; // "k6-2" |
| 898 | case 9: |
| 899 | case 13: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 900 | *Subtype = X86::AMDPENTIUM_K63; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 901 | break; // "k6-3" |
| 902 | case 10: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 903 | *Subtype = X86::AMDPENTIUM_GEODE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 904 | break; // "geode" |
| 905 | } |
| 906 | break; |
| 907 | case 6: |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 908 | if (Features & (1 << X86::FEATURE_SSE)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 909 | *Type = X86::AMD_ATHLON_XP; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 910 | break; // "athlon-xp" |
| 911 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 912 | *Type = X86::AMD_ATHLON; |
Craig Topper | f3de5eb | 2017-07-13 06:34:10 +0000 | [diff] [blame] | 913 | break; // "athlon" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 914 | case 15: |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 915 | if (Features & (1 << X86::FEATURE_SSE3)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 916 | *Type = X86::AMD_K8SSE3; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 917 | break; // "k8-sse3" |
| 918 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 919 | *Type = X86::AMD_K8; |
Craig Topper | f3de5eb | 2017-07-13 06:34:10 +0000 | [diff] [blame] | 920 | break; // "k8" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 921 | case 16: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 922 | *Type = X86::AMDFAM10H; // "amdfam10" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 923 | switch (Model) { |
| 924 | case 2: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 925 | *Subtype = X86::AMDFAM10H_BARCELONA; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 926 | break; |
| 927 | case 4: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 928 | *Subtype = X86::AMDFAM10H_SHANGHAI; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 929 | break; |
| 930 | case 8: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 931 | *Subtype = X86::AMDFAM10H_ISTANBUL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 932 | break; |
| 933 | } |
| 934 | break; |
| 935 | case 20: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 936 | *Type = X86::AMD_BTVER1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 937 | break; // "btver1"; |
| 938 | case 21: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 939 | *Type = X86::AMDFAM15H; |
Craig Topper | 1f9d3c0 | 2017-07-08 06:44:35 +0000 | [diff] [blame] | 940 | if (Model >= 0x60 && Model <= 0x7f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 941 | *Subtype = X86::AMDFAM15H_BDVER4; |
Craig Topper | 3db1170 | 2017-07-12 06:49:56 +0000 | [diff] [blame] | 942 | break; // "bdver4"; 60h-7Fh: Excavator |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 943 | } |
| 944 | if (Model >= 0x30 && Model <= 0x3f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 945 | *Subtype = X86::AMDFAM15H_BDVER3; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 946 | break; // "bdver3"; 30h-3Fh: Steamroller |
| 947 | } |
Roman Lebedev | bc1a924 | 2018-05-01 18:39:31 +0000 | [diff] [blame] | 948 | if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 949 | *Subtype = X86::AMDFAM15H_BDVER2; |
Roman Lebedev | bc1a924 | 2018-05-01 18:39:31 +0000 | [diff] [blame] | 950 | break; // "bdver2"; 02h, 10h-1Fh: Piledriver |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 951 | } |
| 952 | if (Model <= 0x0f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 953 | *Subtype = X86::AMDFAM15H_BDVER1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 954 | break; // "bdver1"; 00h-0Fh: Bulldozer |
| 955 | } |
| 956 | break; |
| 957 | case 22: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 958 | *Type = X86::AMD_BTVER2; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 959 | break; // "btver2" |
| 960 | case 23: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 961 | *Type = X86::AMDFAM17H; |
Craig Topper | ff75bf6 | 2019-11-18 11:31:17 -0800 | [diff] [blame] | 962 | if ((Model >= 0x30 && Model <= 0x3f) || Model == 0x71) { |
Ganesh Gopalasubramanian | e172d700 | 2019-02-26 16:55:10 +0000 | [diff] [blame] | 963 | *Subtype = X86::AMDFAM17H_ZNVER2; |
Craig Topper | ff75bf6 | 2019-11-18 11:31:17 -0800 | [diff] [blame] | 964 | break; // "znver2"; 30h-3fh, 71h: Zen2 |
Ganesh Gopalasubramanian | e172d700 | 2019-02-26 16:55:10 +0000 | [diff] [blame] | 965 | } |
| 966 | if (Model <= 0x0f) { |
| 967 | *Subtype = X86::AMDFAM17H_ZNVER1; |
| 968 | break; // "znver1"; 00h-0Fh: Zen1 |
| 969 | } |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 970 | break; |
| 971 | default: |
| 972 | break; // "generic" |
| 973 | } |
| 974 | } |
| 975 | |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 976 | static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf, |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 977 | unsigned *FeaturesOut, unsigned *Features2Out, |
| 978 | unsigned *Features3Out) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 979 | unsigned Features = 0; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 980 | unsigned Features2 = 0; |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 981 | unsigned Features3 = 0; |
Craig Topper | c6bbe4b | 2017-07-08 05:16:14 +0000 | [diff] [blame] | 982 | unsigned EAX, EBX; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 983 | |
Simon Pilgrim | a7d1a7c | 2018-10-20 13:16:31 +0000 | [diff] [blame] | 984 | auto setFeature = [&](unsigned F) { |
| 985 | if (F < 32) |
Craig Topper | 28659f5 | 2018-11-24 20:26:11 +0000 | [diff] [blame] | 986 | Features |= 1U << (F & 0x1f); |
Simon Pilgrim | a7d1a7c | 2018-10-20 13:16:31 +0000 | [diff] [blame] | 987 | else if (F < 64) |
Craig Topper | 28659f5 | 2018-11-24 20:26:11 +0000 | [diff] [blame] | 988 | Features2 |= 1U << ((F - 32) & 0x1f); |
Simon Pilgrim | a7d1a7c | 2018-10-20 13:16:31 +0000 | [diff] [blame] | 989 | else if (F < 96) |
Craig Topper | 28659f5 | 2018-11-24 20:26:11 +0000 | [diff] [blame] | 990 | Features3 |= 1U << ((F - 64) & 0x1f); |
Simon Pilgrim | a7d1a7c | 2018-10-20 13:16:31 +0000 | [diff] [blame] | 991 | else |
| 992 | llvm_unreachable("Unexpected FeatureBit"); |
| 993 | }; |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 994 | |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 995 | if ((EDX >> 15) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 996 | setFeature(X86::FEATURE_CMOV); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 997 | if ((EDX >> 23) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 998 | setFeature(X86::FEATURE_MMX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 999 | if ((EDX >> 25) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1000 | setFeature(X86::FEATURE_SSE); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1001 | if ((EDX >> 26) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1002 | setFeature(X86::FEATURE_SSE2); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1003 | |
| 1004 | if ((ECX >> 0) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1005 | setFeature(X86::FEATURE_SSE3); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1006 | if ((ECX >> 1) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1007 | setFeature(X86::FEATURE_PCLMUL); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1008 | if ((ECX >> 9) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1009 | setFeature(X86::FEATURE_SSSE3); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1010 | if ((ECX >> 12) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1011 | setFeature(X86::FEATURE_FMA); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1012 | if ((ECX >> 19) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1013 | setFeature(X86::FEATURE_SSE4_1); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1014 | if ((ECX >> 20) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1015 | setFeature(X86::FEATURE_SSE4_2); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1016 | if ((ECX >> 23) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1017 | setFeature(X86::FEATURE_POPCNT); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1018 | if ((ECX >> 25) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1019 | setFeature(X86::FEATURE_AES); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1020 | |
| 1021 | if ((ECX >> 22) & 1) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1022 | setFeature(X86::FEATURE_MOVBE); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1023 | |
| 1024 | // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV |
| 1025 | // indicates that the AVX registers will be saved and restored on context |
| 1026 | // switch, then we have full AVX support. |
| 1027 | const unsigned AVXBits = (1 << 27) | (1 << 28); |
| 1028 | bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) && |
| 1029 | ((EAX & 0x6) == 0x6); |
Florian Hahn | 82921bf | 2019-11-21 09:03:16 +0000 | [diff] [blame^] | 1030 | #if defined(__APPLE__) |
| 1031 | // Darwin lazily saves the AVX512 context on first use: trust that the OS will |
| 1032 | // save the AVX512 context if we use AVX512 instructions, even the bit is not |
| 1033 | // set right now. |
| 1034 | bool HasAVX512Save = true; |
| 1035 | #else |
| 1036 | // AVX512 requires additional context to be saved by the OS. |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1037 | bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0); |
Florian Hahn | 82921bf | 2019-11-21 09:03:16 +0000 | [diff] [blame^] | 1038 | #endif |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (HasAVX) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1041 | setFeature(X86::FEATURE_AVX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1042 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1043 | bool HasLeaf7 = |
| 1044 | MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (HasLeaf7 && ((EBX >> 3) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1047 | setFeature(X86::FEATURE_BMI); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1048 | if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1049 | setFeature(X86::FEATURE_AVX2); |
Eric Christopher | 1d73e22 | 2019-08-05 21:25:59 +0000 | [diff] [blame] | 1050 | if (HasLeaf7 && ((EBX >> 8) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1051 | setFeature(X86::FEATURE_BMI2); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1052 | if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1053 | setFeature(X86::FEATURE_AVX512F); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1054 | if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1055 | setFeature(X86::FEATURE_AVX512DQ); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1056 | if (HasLeaf7 && ((EBX >> 19) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1057 | setFeature(X86::FEATURE_ADX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1058 | if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1059 | setFeature(X86::FEATURE_AVX512IFMA); |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 1060 | if (HasLeaf7 && ((EBX >> 23) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1061 | setFeature(X86::FEATURE_CLFLUSHOPT); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1062 | if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1063 | setFeature(X86::FEATURE_AVX512PF); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1064 | if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1065 | setFeature(X86::FEATURE_AVX512ER); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1066 | if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1067 | setFeature(X86::FEATURE_AVX512CD); |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 1068 | if (HasLeaf7 && ((EBX >> 29) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1069 | setFeature(X86::FEATURE_SHA); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1070 | if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1071 | setFeature(X86::FEATURE_AVX512BW); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1072 | if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1073 | setFeature(X86::FEATURE_AVX512VL); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1076 | setFeature(X86::FEATURE_AVX512VBMI); |
| 1077 | if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save) |
| 1078 | setFeature(X86::FEATURE_AVX512VBMI2); |
| 1079 | if (HasLeaf7 && ((ECX >> 8) & 1)) |
| 1080 | setFeature(X86::FEATURE_GFNI); |
| 1081 | if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX) |
| 1082 | setFeature(X86::FEATURE_VPCLMULQDQ); |
| 1083 | if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save) |
| 1084 | setFeature(X86::FEATURE_AVX512VNNI); |
| 1085 | if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save) |
| 1086 | setFeature(X86::FEATURE_AVX512BITALG); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1087 | if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1088 | setFeature(X86::FEATURE_AVX512VPOPCNTDQ); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1091 | setFeature(X86::FEATURE_AVX5124VNNIW); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1092 | if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1093 | setFeature(X86::FEATURE_AVX5124FMAPS); |
Pengfei Wang | e28cbbd | 2019-08-12 01:29:46 +0000 | [diff] [blame] | 1094 | if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save) |
| 1095 | setFeature(X86::FEATURE_AVX512VP2INTERSECT); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1096 | |
Craig Topper | 5465875 | 2019-09-04 16:01:43 +0000 | [diff] [blame] | 1097 | bool HasLeaf7Subleaf1 = |
| 1098 | MaxLeaf >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX); |
| 1099 | if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save) |
| 1100 | setFeature(X86::FEATURE_AVX512BF16); |
| 1101 | |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 1102 | unsigned MaxExtLevel; |
| 1103 | getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX); |
| 1104 | |
| 1105 | bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 && |
| 1106 | !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1107 | if (HasExtLeaf1 && ((ECX >> 6) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1108 | setFeature(X86::FEATURE_SSE4_A); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1109 | if (HasExtLeaf1 && ((ECX >> 11) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1110 | setFeature(X86::FEATURE_XOP); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1111 | if (HasExtLeaf1 && ((ECX >> 16) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1112 | setFeature(X86::FEATURE_FMA4); |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 1113 | |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1114 | if (HasExtLeaf1 && ((EDX >> 29) & 1)) |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1115 | setFeature(X86::FEATURE_EM64T); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1116 | |
| 1117 | *FeaturesOut = Features; |
| 1118 | *Features2Out = Features2; |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1119 | *Features3Out = Features3; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | StringRef sys::getHostCPUName() { |
| 1123 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
| 1124 | unsigned MaxLeaf, Vendor; |
| 1125 | |
| 1126 | #if defined(__GNUC__) || defined(__clang__) |
| 1127 | //FIXME: include cpuid.h from clang or copy __get_cpuid_max here |
| 1128 | // and simplify it to not invoke __cpuid (like cpu_model.c in |
| 1129 | // compiler-rt/lib/builtins/cpu_model.c? |
| 1130 | // Opting for the second option. |
| 1131 | if(!isCpuIdSupported()) |
| 1132 | return "generic"; |
| 1133 | #endif |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 1134 | if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1) |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1135 | return "generic"; |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 1136 | getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1137 | |
| 1138 | unsigned Brand_id = EBX & 0xff; |
| 1139 | unsigned Family = 0, Model = 0; |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1140 | unsigned Features = 0, Features2 = 0, Features3 = 0; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1141 | detectX86FamilyModel(EAX, &Family, &Model); |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1142 | getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2, &Features3); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1143 | |
Craig Topper | 741e7e6 | 2017-11-03 18:02:44 +0000 | [diff] [blame] | 1144 | unsigned Type = 0; |
| 1145 | unsigned Subtype = 0; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1146 | |
| 1147 | if (Vendor == SIG_INTEL) { |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 1148 | getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, |
Craig Topper | 0aca35d | 2018-10-20 03:51:43 +0000 | [diff] [blame] | 1149 | Features2, Features3, &Type, &Subtype); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1150 | } else if (Vendor == SIG_AMD) { |
| 1151 | getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1152 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Check subtypes first since those are more specific. |
| 1155 | #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \ |
| 1156 | if (Subtype == X86::ENUM) \ |
| 1157 | return ARCHNAME; |
| 1158 | #include "llvm/Support/X86TargetParser.def" |
| 1159 | |
| 1160 | // Now check types. |
Craig Topper | 55ad329 | 2018-03-06 22:45:31 +0000 | [diff] [blame] | 1161 | #define X86_CPU_TYPE(ARCHNAME, ENUM) \ |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 1162 | if (Type == X86::ENUM) \ |
| 1163 | return ARCHNAME; |
| 1164 | #include "llvm/Support/X86TargetParser.def" |
| 1165 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1166 | return "generic"; |
| 1167 | } |
| 1168 | |
| 1169 | #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) |
| 1170 | StringRef sys::getHostCPUName() { |
| 1171 | host_basic_info_data_t hostInfo; |
| 1172 | mach_msg_type_number_t infoCount; |
| 1173 | |
| 1174 | infoCount = HOST_BASIC_INFO_COUNT; |
Kristina Brooks | 51ae934 | 2018-09-04 10:54:09 +0000 | [diff] [blame] | 1175 | mach_port_t hostPort = mach_host_self(); |
| 1176 | host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo, |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1177 | &infoCount); |
Kristina Brooks | 51ae934 | 2018-09-04 10:54:09 +0000 | [diff] [blame] | 1178 | mach_port_deallocate(mach_task_self(), hostPort); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1179 | |
| 1180 | if (hostInfo.cpu_type != CPU_TYPE_POWERPC) |
| 1181 | return "generic"; |
| 1182 | |
| 1183 | switch (hostInfo.cpu_subtype) { |
| 1184 | case CPU_SUBTYPE_POWERPC_601: |
| 1185 | return "601"; |
| 1186 | case CPU_SUBTYPE_POWERPC_602: |
| 1187 | return "602"; |
| 1188 | case CPU_SUBTYPE_POWERPC_603: |
| 1189 | return "603"; |
| 1190 | case CPU_SUBTYPE_POWERPC_603e: |
| 1191 | return "603e"; |
| 1192 | case CPU_SUBTYPE_POWERPC_603ev: |
| 1193 | return "603ev"; |
| 1194 | case CPU_SUBTYPE_POWERPC_604: |
| 1195 | return "604"; |
| 1196 | case CPU_SUBTYPE_POWERPC_604e: |
| 1197 | return "604e"; |
| 1198 | case CPU_SUBTYPE_POWERPC_620: |
| 1199 | return "620"; |
| 1200 | case CPU_SUBTYPE_POWERPC_750: |
| 1201 | return "750"; |
| 1202 | case CPU_SUBTYPE_POWERPC_7400: |
| 1203 | return "7400"; |
| 1204 | case CPU_SUBTYPE_POWERPC_7450: |
| 1205 | return "7450"; |
| 1206 | case CPU_SUBTYPE_POWERPC_970: |
| 1207 | return "970"; |
| 1208 | default:; |
| 1209 | } |
| 1210 | |
| 1211 | return "generic"; |
| 1212 | } |
| 1213 | #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__)) |
| 1214 | StringRef sys::getHostCPUName() { |
| 1215 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 1216 | StringRef Content = P ? P->getBuffer() : ""; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1217 | return detail::getHostCPUNameForPowerPC(Content); |
| 1218 | } |
| 1219 | #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) |
| 1220 | StringRef sys::getHostCPUName() { |
| 1221 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 1222 | StringRef Content = P ? P->getBuffer() : ""; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1223 | return detail::getHostCPUNameForARM(Content); |
| 1224 | } |
| 1225 | #elif defined(__linux__) && defined(__s390x__) |
| 1226 | StringRef sys::getHostCPUName() { |
| 1227 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 1228 | StringRef Content = P ? P->getBuffer() : ""; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1229 | return detail::getHostCPUNameForS390x(Content); |
| 1230 | } |
Chris Bieneman | 34688fa | 2019-10-30 12:50:04 -0700 | [diff] [blame] | 1231 | #elif defined(__APPLE__) && defined(__aarch64__) |
| 1232 | StringRef sys::getHostCPUName() { |
| 1233 | return "cyclone"; |
| 1234 | } |
| 1235 | #elif defined(__APPLE__) && defined(__arm__) |
| 1236 | StringRef sys::getHostCPUName() { |
| 1237 | host_basic_info_data_t hostInfo; |
| 1238 | mach_msg_type_number_t infoCount; |
| 1239 | |
| 1240 | infoCount = HOST_BASIC_INFO_COUNT; |
| 1241 | mach_port_t hostPort = mach_host_self(); |
| 1242 | host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo, |
| 1243 | &infoCount); |
| 1244 | mach_port_deallocate(mach_task_self(), hostPort); |
| 1245 | |
| 1246 | if (hostInfo.cpu_type != CPU_TYPE_ARM) { |
| 1247 | assert(false && "CPUType not equal to ARM should not be possible on ARM"); |
| 1248 | return "generic"; |
| 1249 | } |
| 1250 | switch (hostInfo.cpu_subtype) { |
| 1251 | case CPU_SUBTYPE_ARM_V7S: |
| 1252 | return "swift"; |
| 1253 | default:; |
| 1254 | } |
| 1255 | |
| 1256 | return "generic"; |
| 1257 | } |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1258 | #else |
| 1259 | StringRef sys::getHostCPUName() { return "generic"; } |
| 1260 | #endif |
| 1261 | |
| 1262 | #if defined(__linux__) && defined(__x86_64__) |
| 1263 | // On Linux, the number of physical cores can be computed from /proc/cpuinfo, |
| 1264 | // using the number of unique physical/core id pairs. The following |
| 1265 | // implementation reads the /proc/cpuinfo format on an x86_64 system. |
| 1266 | static int computeHostNumPhysicalCores() { |
| 1267 | // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be |
| 1268 | // mmapped because it appears to have 0 size. |
| 1269 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 1270 | llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo"); |
| 1271 | if (std::error_code EC = Text.getError()) { |
| 1272 | llvm::errs() << "Can't read " |
| 1273 | << "/proc/cpuinfo: " << EC.message() << "\n"; |
| 1274 | return -1; |
| 1275 | } |
| 1276 | SmallVector<StringRef, 8> strs; |
| 1277 | (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1, |
| 1278 | /*KeepEmpty=*/false); |
| 1279 | int CurPhysicalId = -1; |
| 1280 | int CurCoreId = -1; |
| 1281 | SmallSet<std::pair<int, int>, 32> UniqueItems; |
| 1282 | for (auto &Line : strs) { |
| 1283 | Line = Line.trim(); |
| 1284 | if (!Line.startswith("physical id") && !Line.startswith("core id")) |
| 1285 | continue; |
| 1286 | std::pair<StringRef, StringRef> Data = Line.split(':'); |
| 1287 | auto Name = Data.first.trim(); |
| 1288 | auto Val = Data.second.trim(); |
| 1289 | if (Name == "physical id") { |
| 1290 | assert(CurPhysicalId == -1 && |
| 1291 | "Expected a core id before seeing another physical id"); |
| 1292 | Val.getAsInteger(10, CurPhysicalId); |
| 1293 | } |
| 1294 | if (Name == "core id") { |
| 1295 | assert(CurCoreId == -1 && |
| 1296 | "Expected a physical id before seeing another core id"); |
| 1297 | Val.getAsInteger(10, CurCoreId); |
| 1298 | } |
| 1299 | if (CurPhysicalId != -1 && CurCoreId != -1) { |
| 1300 | UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId)); |
| 1301 | CurPhysicalId = -1; |
| 1302 | CurCoreId = -1; |
| 1303 | } |
| 1304 | } |
| 1305 | return UniqueItems.size(); |
| 1306 | } |
| 1307 | #elif defined(__APPLE__) && defined(__x86_64__) |
| 1308 | #include <sys/param.h> |
| 1309 | #include <sys/sysctl.h> |
| 1310 | |
| 1311 | // Gets the number of *physical cores* on the machine. |
| 1312 | static int computeHostNumPhysicalCores() { |
| 1313 | uint32_t count; |
| 1314 | size_t len = sizeof(count); |
| 1315 | sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0); |
| 1316 | if (count < 1) { |
| 1317 | int nm[2]; |
| 1318 | nm[0] = CTL_HW; |
| 1319 | nm[1] = HW_AVAILCPU; |
| 1320 | sysctl(nm, 2, &count, &len, NULL, 0); |
| 1321 | if (count < 1) |
| 1322 | return -1; |
| 1323 | } |
| 1324 | return count; |
| 1325 | } |
| 1326 | #else |
| 1327 | // On other systems, return -1 to indicate unknown. |
| 1328 | static int computeHostNumPhysicalCores() { return -1; } |
| 1329 | #endif |
| 1330 | |
| 1331 | int sys::getHostNumPhysicalCores() { |
| 1332 | static int NumCores = computeHostNumPhysicalCores(); |
| 1333 | return NumCores; |
| 1334 | } |
| 1335 | |
| 1336 | #if defined(__i386__) || defined(_M_IX86) || \ |
| 1337 | defined(__x86_64__) || defined(_M_X64) |
| 1338 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { |
| 1339 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
| 1340 | unsigned MaxLevel; |
| 1341 | union { |
| 1342 | unsigned u[3]; |
| 1343 | char c[12]; |
| 1344 | } text; |
| 1345 | |
| 1346 | if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) || |
| 1347 | MaxLevel < 1) |
| 1348 | return false; |
| 1349 | |
| 1350 | getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX); |
| 1351 | |
Craig Topper | 8d46403 | 2019-03-20 23:35:49 +0000 | [diff] [blame] | 1352 | Features["cx8"] = (EDX >> 8) & 1; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1353 | Features["cmov"] = (EDX >> 15) & 1; |
| 1354 | Features["mmx"] = (EDX >> 23) & 1; |
Craig Topper | 6829ca9 | 2019-02-13 18:21:36 +0000 | [diff] [blame] | 1355 | Features["fxsr"] = (EDX >> 24) & 1; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1356 | Features["sse"] = (EDX >> 25) & 1; |
| 1357 | Features["sse2"] = (EDX >> 26) & 1; |
| 1358 | |
| 1359 | Features["sse3"] = (ECX >> 0) & 1; |
| 1360 | Features["pclmul"] = (ECX >> 1) & 1; |
| 1361 | Features["ssse3"] = (ECX >> 9) & 1; |
| 1362 | Features["cx16"] = (ECX >> 13) & 1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1363 | Features["sse4.1"] = (ECX >> 19) & 1; |
| 1364 | Features["sse4.2"] = (ECX >> 20) & 1; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1365 | Features["movbe"] = (ECX >> 22) & 1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1366 | Features["popcnt"] = (ECX >> 23) & 1; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1367 | Features["aes"] = (ECX >> 25) & 1; |
| 1368 | Features["rdrnd"] = (ECX >> 30) & 1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1369 | |
| 1370 | // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV |
| 1371 | // indicates that the AVX registers will be saved and restored on context |
| 1372 | // switch, then we have full AVX support. |
| 1373 | bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) && |
| 1374 | !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6); |
Florian Hahn | 82921bf | 2019-11-21 09:03:16 +0000 | [diff] [blame^] | 1375 | #if defined(__APPLE__) |
| 1376 | // Darwin lazily saves the AVX512 context on first use: trust that the OS will |
| 1377 | // save the AVX512 context if we use AVX512 instructions, even the bit is not |
| 1378 | // set right now. |
| 1379 | bool HasAVX512Save = true; |
| 1380 | #else |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1381 | // AVX512 requires additional context to be saved by the OS. |
| 1382 | bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0); |
Florian Hahn | 82921bf | 2019-11-21 09:03:16 +0000 | [diff] [blame^] | 1383 | #endif |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1384 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1385 | Features["avx"] = HasAVXSave; |
| 1386 | Features["fma"] = ((ECX >> 12) & 1) && HasAVXSave; |
| 1387 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
| 1388 | Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave; |
| 1389 | Features["f16c"] = ((ECX >> 29) & 1) && HasAVXSave; |
| 1390 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1391 | unsigned MaxExtLevel; |
| 1392 | getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX); |
| 1393 | |
| 1394 | bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 && |
| 1395 | !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
Craig Topper | 8d02be3 | 2018-02-17 16:52:49 +0000 | [diff] [blame] | 1396 | Features["sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1); |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1397 | Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1); |
| 1398 | Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1); |
| 1399 | Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1); |
| 1400 | Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave; |
| 1401 | Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1); |
| 1402 | Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave; |
| 1403 | Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1404 | Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1); |
| 1405 | |
Craig Topper | 6cdab20 | 2018-09-24 18:55:41 +0000 | [diff] [blame] | 1406 | Features["64bit"] = HasExtLeaf1 && ((EDX >> 29) & 1); |
| 1407 | |
Gabor Buella | 2ef36f3 | 2018-04-11 20:01:57 +0000 | [diff] [blame] | 1408 | // Miscellaneous memory related features, detected by |
| 1409 | // using the 0x80000008 leaf of the CPUID instruction |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1410 | bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 && |
Craig Topper | dcd6979 | 2017-11-19 23:49:19 +0000 | [diff] [blame] | 1411 | !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX); |
Gabor Buella | 2ef36f3 | 2018-04-11 20:01:57 +0000 | [diff] [blame] | 1412 | Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1); |
| 1413 | Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1414 | |
| 1415 | bool HasLeaf7 = |
| 1416 | MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX); |
| 1417 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1418 | Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1); |
| 1419 | Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1); |
| 1420 | Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1421 | // AVX2 is only supported if we have the OS save support from AVX. |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1422 | Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave; |
| 1423 | Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1); |
Gabor Buella | d2f1ab1 | 2018-05-25 06:32:05 +0000 | [diff] [blame] | 1424 | Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1); |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1425 | Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1426 | // AVX512 is only supported if the OS supports the context save for it. |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1427 | Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save; |
| 1428 | Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save; |
| 1429 | Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1); |
| 1430 | Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1431 | Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1432 | Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1); |
| 1433 | Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1); |
| 1434 | Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save; |
| 1435 | Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save; |
| 1436 | Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save; |
| 1437 | Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1); |
| 1438 | Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save; |
| 1439 | Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1440 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1441 | Features["prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1); |
| 1442 | Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save; |
Craig Topper | 9b03f67 | 2017-11-21 18:50:41 +0000 | [diff] [blame] | 1443 | Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1); |
Gabor Buella | 31fa802 | 2018-04-20 18:42:47 +0000 | [diff] [blame] | 1444 | Features["waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1); |
Coby Tayree | 71e37cc | 2017-11-21 09:48:44 +0000 | [diff] [blame] | 1445 | Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save; |
Oren Ben Simhon | fa582b0 | 2017-11-26 13:02:45 +0000 | [diff] [blame] | 1446 | Features["shstk"] = HasLeaf7 && ((ECX >> 7) & 1); |
Coby Tayree | d8b17be | 2017-11-26 09:36:41 +0000 | [diff] [blame] | 1447 | Features["gfni"] = HasLeaf7 && ((ECX >> 8) & 1); |
Craig Topper | 9b03f67 | 2017-11-21 18:50:41 +0000 | [diff] [blame] | 1448 | Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave; |
| 1449 | Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave; |
| 1450 | Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save; |
| 1451 | Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save; |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 1452 | Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save; |
Craig Topper | 84b26b9 | 2018-01-18 23:52:31 +0000 | [diff] [blame] | 1453 | Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1); |
Gabor Buella | 604be44 | 2018-04-13 07:35:08 +0000 | [diff] [blame] | 1454 | Features["cldemote"] = HasLeaf7 && ((ECX >> 25) & 1); |
Gabor Buella | c8ded04 | 2018-05-01 10:01:16 +0000 | [diff] [blame] | 1455 | Features["movdiri"] = HasLeaf7 && ((ECX >> 27) & 1); |
| 1456 | Features["movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1); |
Pengfei Wang | 1f67d94 | 2019-05-30 03:59:16 +0000 | [diff] [blame] | 1457 | Features["enqcmd"] = HasLeaf7 && ((ECX >> 29) & 1); |
Craig Topper | 84b26b9 | 2018-01-18 23:52:31 +0000 | [diff] [blame] | 1458 | |
Gabor Buella | 2b5e960 | 2018-05-08 06:47:36 +0000 | [diff] [blame] | 1459 | // There are two CPUID leafs which information associated with the pconfig |
| 1460 | // instruction: |
| 1461 | // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th |
| 1462 | // bit of EDX), while the EAX=0x1b leaf returns information on the |
| 1463 | // availability of specific pconfig leafs. |
| 1464 | // The target feature here only refers to the the first of these two. |
| 1465 | // Users might need to check for the availability of specific pconfig |
| 1466 | // leaves using cpuid, since that information is ignored while |
| 1467 | // detecting features using the "-march=native" flag. |
| 1468 | // For more info, see X86 ISA docs. |
| 1469 | Features["pconfig"] = HasLeaf7 && ((EDX >> 18) & 1); |
Luo, Yuanke | beec41c | 2019-05-06 08:22:37 +0000 | [diff] [blame] | 1470 | bool HasLeaf7Subleaf1 = |
| 1471 | MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX); |
| 1472 | Features["avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save; |
Gabor Buella | 2b5e960 | 2018-05-08 06:47:36 +0000 | [diff] [blame] | 1473 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1474 | bool HasLeafD = MaxLevel >= 0xd && |
| 1475 | !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX); |
| 1476 | |
| 1477 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1478 | Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave; |
| 1479 | Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave; |
| 1480 | Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1481 | |
Gabor Buella | a832b22 | 2018-05-10 07:26:05 +0000 | [diff] [blame] | 1482 | bool HasLeaf14 = MaxLevel >= 0x14 && |
| 1483 | !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX); |
| 1484 | |
| 1485 | Features["ptwrite"] = HasLeaf14 && ((EBX >> 4) & 1); |
| 1486 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1487 | return true; |
| 1488 | } |
| 1489 | #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) |
| 1490 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { |
| 1491 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
| 1492 | if (!P) |
| 1493 | return false; |
| 1494 | |
| 1495 | SmallVector<StringRef, 32> Lines; |
| 1496 | P->getBuffer().split(Lines, "\n"); |
| 1497 | |
| 1498 | SmallVector<StringRef, 32> CPUFeatures; |
| 1499 | |
| 1500 | // Look for the CPU features. |
| 1501 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 1502 | if (Lines[I].startswith("Features")) { |
| 1503 | Lines[I].split(CPUFeatures, ' '); |
| 1504 | break; |
| 1505 | } |
| 1506 | |
| 1507 | #if defined(__aarch64__) |
| 1508 | // Keep track of which crypto features we have seen |
| 1509 | enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 }; |
| 1510 | uint32_t crypto = 0; |
| 1511 | #endif |
| 1512 | |
| 1513 | for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) { |
| 1514 | StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I]) |
| 1515 | #if defined(__aarch64__) |
| 1516 | .Case("asimd", "neon") |
| 1517 | .Case("fp", "fp-armv8") |
| 1518 | .Case("crc32", "crc") |
| 1519 | #else |
| 1520 | .Case("half", "fp16") |
| 1521 | .Case("neon", "neon") |
| 1522 | .Case("vfpv3", "vfp3") |
| 1523 | .Case("vfpv3d16", "d16") |
| 1524 | .Case("vfpv4", "vfp4") |
| 1525 | .Case("idiva", "hwdiv-arm") |
| 1526 | .Case("idivt", "hwdiv") |
| 1527 | #endif |
| 1528 | .Default(""); |
| 1529 | |
| 1530 | #if defined(__aarch64__) |
| 1531 | // We need to check crypto separately since we need all of the crypto |
| 1532 | // extensions to enable the subtarget feature |
| 1533 | if (CPUFeatures[I] == "aes") |
| 1534 | crypto |= CAP_AES; |
| 1535 | else if (CPUFeatures[I] == "pmull") |
| 1536 | crypto |= CAP_PMULL; |
| 1537 | else if (CPUFeatures[I] == "sha1") |
| 1538 | crypto |= CAP_SHA1; |
| 1539 | else if (CPUFeatures[I] == "sha2") |
| 1540 | crypto |= CAP_SHA2; |
| 1541 | #endif |
| 1542 | |
| 1543 | if (LLVMFeatureStr != "") |
| 1544 | Features[LLVMFeatureStr] = true; |
| 1545 | } |
| 1546 | |
| 1547 | #if defined(__aarch64__) |
| 1548 | // If we have all crypto bits we can add the feature |
| 1549 | if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2)) |
| 1550 | Features["crypto"] = true; |
| 1551 | #endif |
| 1552 | |
| 1553 | return true; |
| 1554 | } |
Martin Storsjo | 353ac42 | 2019-10-02 11:04:55 +0000 | [diff] [blame] | 1555 | #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64)) |
| 1556 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { |
| 1557 | if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)) |
| 1558 | Features["neon"] = true; |
| 1559 | if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE)) |
| 1560 | Features["crc"] = true; |
| 1561 | if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) |
| 1562 | Features["crypto"] = true; |
| 1563 | |
| 1564 | return true; |
| 1565 | } |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1566 | #else |
| 1567 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; } |
| 1568 | #endif |
| 1569 | |
| 1570 | std::string sys::getProcessTriple() { |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 1571 | std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE); |
| 1572 | Triple PT(Triple::normalize(TargetTripleString)); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1573 | |
| 1574 | if (sizeof(void *) == 8 && PT.isArch32Bit()) |
| 1575 | PT = PT.get64BitArchVariant(); |
| 1576 | if (sizeof(void *) == 4 && PT.isArch64Bit()) |
| 1577 | PT = PT.get32BitArchVariant(); |
| 1578 | |
| 1579 | return PT.str(); |
| 1580 | } |