Host.cpp 58 KB

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