Host.cpp 61 KB

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