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