1 //===-- cpu_model.c - Support for __cpu_model builtin  ------------*- 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 is based on LLVM's lib/Support/Host.cpp.
10 //  It implements the operating system Host concept and builtin
11 //  __cpu_model for the compiler_rt library, for x86 only.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) ||           \
16      defined(_M_X64)) &&                                                       \
17     (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER))
18 
19 #include <assert.h>
20 
21 #define bool int
22 #define true 1
23 #define false 0
24 
25 #ifdef _MSC_VER
26 #include <intrin.h>
27 #endif
28 
29 #ifndef __has_attribute
30 #define __has_attribute(attr) 0
31 #endif
32 
33 enum VendorSignatures {
34   SIG_INTEL = 0x756e6547, // Genu
35   SIG_AMD = 0x68747541,   // Auth
36 };
37 
38 enum ProcessorVendors {
39   VENDOR_INTEL = 1,
40   VENDOR_AMD,
41   VENDOR_OTHER,
42   VENDOR_MAX
43 };
44 
45 enum ProcessorTypes {
46   INTEL_BONNELL = 1,
47   INTEL_CORE2,
48   INTEL_COREI7,
49   AMDFAM10H,
50   AMDFAM15H,
51   INTEL_SILVERMONT,
52   INTEL_KNL,
53   AMD_BTVER1,
54   AMD_BTVER2,
55   AMDFAM17H,
56   INTEL_KNM,
57   INTEL_GOLDMONT,
58   INTEL_GOLDMONT_PLUS,
59   INTEL_TREMONT,
60   CPU_TYPE_MAX
61 };
62 
63 enum ProcessorSubtypes {
64   INTEL_COREI7_NEHALEM = 1,
65   INTEL_COREI7_WESTMERE,
66   INTEL_COREI7_SANDYBRIDGE,
67   AMDFAM10H_BARCELONA,
68   AMDFAM10H_SHANGHAI,
69   AMDFAM10H_ISTANBUL,
70   AMDFAM15H_BDVER1,
71   AMDFAM15H_BDVER2,
72   AMDFAM15H_BDVER3,
73   AMDFAM15H_BDVER4,
74   AMDFAM17H_ZNVER1,
75   INTEL_COREI7_IVYBRIDGE,
76   INTEL_COREI7_HASWELL,
77   INTEL_COREI7_BROADWELL,
78   INTEL_COREI7_SKYLAKE,
79   INTEL_COREI7_SKYLAKE_AVX512,
80   INTEL_COREI7_CANNONLAKE,
81   INTEL_COREI7_ICELAKE_CLIENT,
82   INTEL_COREI7_ICELAKE_SERVER,
83   AMDFAM17H_ZNVER2,
84   INTEL_COREI7_CASCADELAKE,
85   INTEL_COREI7_TIGERLAKE,
86   INTEL_COREI7_COOPERLAKE,
87   CPU_SUBTYPE_MAX
88 };
89 
90 enum ProcessorFeatures {
91   FEATURE_CMOV = 0,
92   FEATURE_MMX,
93   FEATURE_POPCNT,
94   FEATURE_SSE,
95   FEATURE_SSE2,
96   FEATURE_SSE3,
97   FEATURE_SSSE3,
98   FEATURE_SSE4_1,
99   FEATURE_SSE4_2,
100   FEATURE_AVX,
101   FEATURE_AVX2,
102   FEATURE_SSE4_A,
103   FEATURE_FMA4,
104   FEATURE_XOP,
105   FEATURE_FMA,
106   FEATURE_AVX512F,
107   FEATURE_BMI,
108   FEATURE_BMI2,
109   FEATURE_AES,
110   FEATURE_PCLMUL,
111   FEATURE_AVX512VL,
112   FEATURE_AVX512BW,
113   FEATURE_AVX512DQ,
114   FEATURE_AVX512CD,
115   FEATURE_AVX512ER,
116   FEATURE_AVX512PF,
117   FEATURE_AVX512VBMI,
118   FEATURE_AVX512IFMA,
119   FEATURE_AVX5124VNNIW,
120   FEATURE_AVX5124FMAPS,
121   FEATURE_AVX512VPOPCNTDQ,
122   FEATURE_AVX512VBMI2,
123   FEATURE_GFNI,
124   FEATURE_VPCLMULQDQ,
125   FEATURE_AVX512VNNI,
126   FEATURE_AVX512BITALG,
127   FEATURE_AVX512BF16,
128   FEATURE_AVX512VP2INTERSECT,
129   CPU_FEATURE_MAX
130 };
131 
132 // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
133 // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
134 // support. Consequently, for i386, the presence of CPUID is checked first
135 // via the corresponding eflags bit.
136 static bool isCpuIdSupported() {
137 #if defined(__GNUC__) || defined(__clang__)
138 #if defined(__i386__)
139   int __cpuid_supported;
140   __asm__("  pushfl\n"
141           "  popl   %%eax\n"
142           "  movl   %%eax,%%ecx\n"
143           "  xorl   $0x00200000,%%eax\n"
144           "  pushl  %%eax\n"
145           "  popfl\n"
146           "  pushfl\n"
147           "  popl   %%eax\n"
148           "  movl   $0,%0\n"
149           "  cmpl   %%eax,%%ecx\n"
150           "  je     1f\n"
151           "  movl   $1,%0\n"
152           "1:"
153           : "=r"(__cpuid_supported)
154           :
155           : "eax", "ecx");
156   if (!__cpuid_supported)
157     return false;
158 #endif
159   return true;
160 #endif
161   return true;
162 }
163 
164 // This code is copied from lib/Support/Host.cpp.
165 // Changes to either file should be mirrored in the other.
166 
167 /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
168 /// the specified arguments.  If we can't run cpuid on the host, return true.
169 static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
170                                unsigned *rECX, unsigned *rEDX) {
171 #if defined(__GNUC__) || defined(__clang__)
172 #if defined(__x86_64__)
173   // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
174   // FIXME: should we save this for Clang?
175   __asm__("movq\t%%rbx, %%rsi\n\t"
176           "cpuid\n\t"
177           "xchgq\t%%rbx, %%rsi\n\t"
178           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
179           : "a"(value));
180   return false;
181 #elif defined(__i386__)
182   __asm__("movl\t%%ebx, %%esi\n\t"
183           "cpuid\n\t"
184           "xchgl\t%%ebx, %%esi\n\t"
185           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
186           : "a"(value));
187   return false;
188 #else
189   return true;
190 #endif
191 #elif defined(_MSC_VER)
192   // The MSVC intrinsic is portable across x86 and x64.
193   int registers[4];
194   __cpuid(registers, value);
195   *rEAX = registers[0];
196   *rEBX = registers[1];
197   *rECX = registers[2];
198   *rEDX = registers[3];
199   return false;
200 #else
201   return true;
202 #endif
203 }
204 
205 /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
206 /// the 4 values in the specified arguments.  If we can't run cpuid on the host,
207 /// return true.
208 static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
209                                  unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
210                                  unsigned *rEDX) {
211 #if defined(__GNUC__) || defined(__clang__)
212 #if defined(__x86_64__)
213   // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
214   // FIXME: should we save this for Clang?
215   __asm__("movq\t%%rbx, %%rsi\n\t"
216           "cpuid\n\t"
217           "xchgq\t%%rbx, %%rsi\n\t"
218           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
219           : "a"(value), "c"(subleaf));
220   return false;
221 #elif defined(__i386__)
222   __asm__("movl\t%%ebx, %%esi\n\t"
223           "cpuid\n\t"
224           "xchgl\t%%ebx, %%esi\n\t"
225           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
226           : "a"(value), "c"(subleaf));
227   return false;
228 #else
229   return true;
230 #endif
231 #elif defined(_MSC_VER)
232   int registers[4];
233   __cpuidex(registers, value, subleaf);
234   *rEAX = registers[0];
235   *rEBX = registers[1];
236   *rECX = registers[2];
237   *rEDX = registers[3];
238   return false;
239 #else
240   return true;
241 #endif
242 }
243 
244 // Read control register 0 (XCR0). Used to detect features such as AVX.
245 static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
246 #if defined(__GNUC__) || defined(__clang__)
247   // Check xgetbv; this uses a .byte sequence instead of the instruction
248   // directly because older assemblers do not include support for xgetbv and
249   // there is no easy way to conditionally compile based on the assembler used.
250   __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
251   return false;
252 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
253   unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
254   *rEAX = Result;
255   *rEDX = Result >> 32;
256   return false;
257 #else
258   return true;
259 #endif
260 }
261 
262 static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
263                                  unsigned *Model) {
264   *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
265   *Model = (EAX >> 4) & 0xf;  // Bits 4 - 7
266   if (*Family == 6 || *Family == 0xf) {
267     if (*Family == 0xf)
268       // Examine extended family ID if family ID is F.
269       *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
270     // Examine extended model ID if family ID is 6 or F.
271     *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
272   }
273 }
274 
275 static void getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
276                                             const unsigned *Features,
277                                             unsigned *Type, unsigned *Subtype) {
278 #define testFeature(F)                                                         \
279   (Features[F / 32] & (F % 32)) != 0
280 
281   switch (Family) {
282   case 6:
283     switch (Model) {
284     case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
285                // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
286                // mobile processor, Intel Core 2 Extreme processor, Intel
287                // Pentium Dual-Core processor, Intel Xeon processor, model
288                // 0Fh. All processors are manufactured using the 65 nm process.
289     case 0x16: // Intel Celeron processor model 16h. All processors are
290                // manufactured using the 65 nm process
291     case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
292                // 17h. All processors are manufactured using the 45 nm process.
293                //
294                // 45nm: Penryn , Wolfdale, Yorkfield (XE)
295     case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
296                // the 45 nm process.
297       *Type = INTEL_CORE2; // "penryn"
298       break;
299     case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
300                // processors are manufactured using the 45 nm process.
301     case 0x1e: // Intel(R) Core(TM) i7 CPU         870  @ 2.93GHz.
302                // As found in a Summer 2010 model iMac.
303     case 0x1f:
304     case 0x2e:              // Nehalem EX
305       *Type = INTEL_COREI7; // "nehalem"
306       *Subtype = INTEL_COREI7_NEHALEM;
307       break;
308     case 0x25: // Intel Core i7, laptop version.
309     case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
310                // processors are manufactured using the 32 nm process.
311     case 0x2f: // Westmere EX
312       *Type = INTEL_COREI7; // "westmere"
313       *Subtype = INTEL_COREI7_WESTMERE;
314       break;
315     case 0x2a: // Intel Core i7 processor. All processors are manufactured
316                // using the 32 nm process.
317     case 0x2d:
318       *Type = INTEL_COREI7; //"sandybridge"
319       *Subtype = INTEL_COREI7_SANDYBRIDGE;
320       break;
321     case 0x3a:
322     case 0x3e:              // Ivy Bridge EP
323       *Type = INTEL_COREI7; // "ivybridge"
324       *Subtype = INTEL_COREI7_IVYBRIDGE;
325       break;
326 
327     // Haswell:
328     case 0x3c:
329     case 0x3f:
330     case 0x45:
331     case 0x46:
332       *Type = INTEL_COREI7; // "haswell"
333       *Subtype = INTEL_COREI7_HASWELL;
334       break;
335 
336     // Broadwell:
337     case 0x3d:
338     case 0x47:
339     case 0x4f:
340     case 0x56:
341       *Type = INTEL_COREI7; // "broadwell"
342       *Subtype = INTEL_COREI7_BROADWELL;
343       break;
344 
345     // Skylake:
346     case 0x4e:              // Skylake mobile
347     case 0x5e:              // Skylake desktop
348     case 0x8e:              // Kaby Lake mobile
349     case 0x9e:              // Kaby Lake desktop
350     case 0xa5:              // Comet Lake-H/S
351     case 0xa6:              // Comet Lake-U
352       *Type = INTEL_COREI7; // "skylake"
353       *Subtype = INTEL_COREI7_SKYLAKE;
354       break;
355 
356     // Skylake Xeon:
357     case 0x55:
358       *Type = INTEL_COREI7;
359       if (testFeature(FEATURE_AVX512BF16))
360         *Subtype = INTEL_COREI7_COOPERLAKE; // "cooperlake"
361       else if (testFeature(FEATURE_AVX512VNNI))
362         *Subtype = INTEL_COREI7_CASCADELAKE; // "cascadelake"
363       else
364         *Subtype = INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
365       break;
366 
367     // Cannonlake:
368     case 0x66:
369       *Type = INTEL_COREI7;
370       *Subtype = INTEL_COREI7_CANNONLAKE; // "cannonlake"
371       break;
372 
373     // Icelake:
374     case 0x7d:
375     case 0x7e:
376       *Type = INTEL_COREI7;
377       *Subtype = INTEL_COREI7_ICELAKE_CLIENT; // "icelake-client"
378       break;
379 
380     // Icelake Xeon:
381     case 0x6a:
382     case 0x6c:
383       *Type = INTEL_COREI7;
384       *Subtype = INTEL_COREI7_ICELAKE_SERVER; // "icelake-server"
385       break;
386 
387     case 0x1c: // Most 45 nm Intel Atom processors
388     case 0x26: // 45 nm Atom Lincroft
389     case 0x27: // 32 nm Atom Medfield
390     case 0x35: // 32 nm Atom Midview
391     case 0x36: // 32 nm Atom Midview
392       *Type = INTEL_BONNELL;
393       break; // "bonnell"
394 
395     // Atom Silvermont codes from the Intel software optimization guide.
396     case 0x37:
397     case 0x4a:
398     case 0x4d:
399     case 0x5a:
400     case 0x5d:
401     case 0x4c: // really airmont
402       *Type = INTEL_SILVERMONT;
403       break; // "silvermont"
404     // Goldmont:
405     case 0x5c: // Apollo Lake
406     case 0x5f: // Denverton
407       *Type = INTEL_GOLDMONT;
408       break; // "goldmont"
409     case 0x7a:
410       *Type = INTEL_GOLDMONT_PLUS;
411       break;
412     case 0x86:
413       *Type = INTEL_TREMONT;
414       break;
415 
416     case 0x57:
417       *Type = INTEL_KNL; // knl
418       break;
419 
420     case 0x85:
421       *Type = INTEL_KNM; // knm
422       break;
423 
424     default: // Unknown family 6 CPU.
425       break;
426     }
427     break;
428   default:
429     break; // Unknown.
430   }
431 }
432 
433 static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
434                                           const unsigned *Features,
435                                           unsigned *Type, unsigned *Subtype) {
436   // FIXME: this poorly matches the generated SubtargetFeatureKV table.  There
437   // appears to be no way to generate the wide variety of AMD-specific targets
438   // from the information returned from CPUID.
439   switch (Family) {
440   case 16:
441     *Type = AMDFAM10H; // "amdfam10"
442     switch (Model) {
443     case 2:
444       *Subtype = AMDFAM10H_BARCELONA;
445       break;
446     case 4:
447       *Subtype = AMDFAM10H_SHANGHAI;
448       break;
449     case 8:
450       *Subtype = AMDFAM10H_ISTANBUL;
451       break;
452     }
453     break;
454   case 20:
455     *Type = AMD_BTVER1;
456     break; // "btver1";
457   case 21:
458     *Type = AMDFAM15H;
459     if (Model >= 0x60 && Model <= 0x7f) {
460       *Subtype = AMDFAM15H_BDVER4;
461       break; // "bdver4"; 60h-7Fh: Excavator
462     }
463     if (Model >= 0x30 && Model <= 0x3f) {
464       *Subtype = AMDFAM15H_BDVER3;
465       break; // "bdver3"; 30h-3Fh: Steamroller
466     }
467     if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
468       *Subtype = AMDFAM15H_BDVER2;
469       break; // "bdver2"; 02h, 10h-1Fh: Piledriver
470     }
471     if (Model <= 0x0f) {
472       *Subtype = AMDFAM15H_BDVER1;
473       break; // "bdver1"; 00h-0Fh: Bulldozer
474     }
475     break;
476   case 22:
477     *Type = AMD_BTVER2;
478     break; // "btver2"
479   case 23:
480     *Type = AMDFAM17H;
481     if ((Model >= 0x30 && Model <= 0x3f) || Model == 0x71) {
482       *Subtype = AMDFAM17H_ZNVER2;
483       break; // "znver2"; 30h-3fh, 71h: Zen2
484     }
485     if (Model <= 0x0f) {
486       *Subtype = AMDFAM17H_ZNVER1;
487       break; // "znver1"; 00h-0Fh: Zen1
488     }
489     break;
490   default:
491     break; // "generic"
492   }
493 }
494 
495 static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
496                                  unsigned *Features) {
497   unsigned EAX, EBX;
498 
499 #define setFeature(F)                                                          \
500   Features[F / 32] |= 1U << (F % 32)
501 
502   if ((EDX >> 15) & 1)
503     setFeature(FEATURE_CMOV);
504   if ((EDX >> 23) & 1)
505     setFeature(FEATURE_MMX);
506   if ((EDX >> 25) & 1)
507     setFeature(FEATURE_SSE);
508   if ((EDX >> 26) & 1)
509     setFeature(FEATURE_SSE2);
510 
511   if ((ECX >> 0) & 1)
512     setFeature(FEATURE_SSE3);
513   if ((ECX >> 1) & 1)
514     setFeature(FEATURE_PCLMUL);
515   if ((ECX >> 9) & 1)
516     setFeature(FEATURE_SSSE3);
517   if ((ECX >> 12) & 1)
518     setFeature(FEATURE_FMA);
519   if ((ECX >> 19) & 1)
520     setFeature(FEATURE_SSE4_1);
521   if ((ECX >> 20) & 1)
522     setFeature(FEATURE_SSE4_2);
523   if ((ECX >> 23) & 1)
524     setFeature(FEATURE_POPCNT);
525   if ((ECX >> 25) & 1)
526     setFeature(FEATURE_AES);
527 
528   // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
529   // indicates that the AVX registers will be saved and restored on context
530   // switch, then we have full AVX support.
531   const unsigned AVXBits = (1 << 27) | (1 << 28);
532   bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
533                 ((EAX & 0x6) == 0x6);
534 #if defined(__APPLE__)
535   // Darwin lazily saves the AVX512 context on first use: trust that the OS will
536   // save the AVX512 context if we use AVX512 instructions, even the bit is not
537   // set right now.
538   bool HasAVX512Save = true;
539 #else
540   // AVX512 requires additional context to be saved by the OS.
541   bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
542 #endif
543 
544   if (HasAVX)
545     setFeature(FEATURE_AVX);
546 
547   bool HasLeaf7 =
548       MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
549 
550   if (HasLeaf7 && ((EBX >> 3) & 1))
551     setFeature(FEATURE_BMI);
552   if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
553     setFeature(FEATURE_AVX2);
554   if (HasLeaf7 && ((EBX >> 8) & 1))
555     setFeature(FEATURE_BMI2);
556   if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
557     setFeature(FEATURE_AVX512F);
558   if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
559     setFeature(FEATURE_AVX512DQ);
560   if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
561     setFeature(FEATURE_AVX512IFMA);
562   if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
563     setFeature(FEATURE_AVX512PF);
564   if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
565     setFeature(FEATURE_AVX512ER);
566   if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
567     setFeature(FEATURE_AVX512CD);
568   if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
569     setFeature(FEATURE_AVX512BW);
570   if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
571     setFeature(FEATURE_AVX512VL);
572 
573   if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
574     setFeature(FEATURE_AVX512VBMI);
575   if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
576     setFeature(FEATURE_AVX512VBMI2);
577   if (HasLeaf7 && ((ECX >> 8) & 1))
578     setFeature(FEATURE_GFNI);
579   if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
580     setFeature(FEATURE_VPCLMULQDQ);
581   if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
582     setFeature(FEATURE_AVX512VNNI);
583   if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
584     setFeature(FEATURE_AVX512BITALG);
585   if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
586     setFeature(FEATURE_AVX512VPOPCNTDQ);
587 
588   if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
589     setFeature(FEATURE_AVX5124VNNIW);
590   if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
591     setFeature(FEATURE_AVX5124FMAPS);
592   if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)
593     setFeature(FEATURE_AVX512VP2INTERSECT);
594 
595   bool HasLeaf7Subleaf1 =
596       MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
597   if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
598     setFeature(FEATURE_AVX512BF16);
599 
600   unsigned MaxExtLevel;
601   getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
602 
603   bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
604                      !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
605   if (HasExtLeaf1 && ((ECX >> 6) & 1))
606     setFeature(FEATURE_SSE4_A);
607   if (HasExtLeaf1 && ((ECX >> 11) & 1))
608     setFeature(FEATURE_XOP);
609   if (HasExtLeaf1 && ((ECX >> 16) & 1))
610     setFeature(FEATURE_FMA4);
611 #undef setFeature
612 }
613 
614 #if defined(HAVE_INIT_PRIORITY)
615 #define CONSTRUCTOR_ATTRIBUTE __attribute__((__constructor__ 101))
616 #elif __has_attribute(__constructor__)
617 #define CONSTRUCTOR_ATTRIBUTE __attribute__((__constructor__))
618 #else
619 // FIXME: For MSVC, we should make a function pointer global in .CRT$X?? so that
620 // this runs during initialization.
621 #define CONSTRUCTOR_ATTRIBUTE
622 #endif
623 
624 #ifndef _WIN32
625 __attribute__((visibility("hidden")))
626 #endif
627 int __cpu_indicator_init(void) CONSTRUCTOR_ATTRIBUTE;
628 
629 #ifndef _WIN32
630 __attribute__((visibility("hidden")))
631 #endif
632 struct __processor_model {
633   unsigned int __cpu_vendor;
634   unsigned int __cpu_type;
635   unsigned int __cpu_subtype;
636   unsigned int __cpu_features[1];
637 } __cpu_model = {0, 0, 0, {0}};
638 
639 #ifndef _WIN32
640 __attribute__((visibility("hidden")))
641 #endif
642 unsigned int __cpu_features2 = 0;
643 
644 // A constructor function that is sets __cpu_model and __cpu_features2 with
645 // the right values.  This needs to run only once.  This constructor is
646 // given the highest priority and it should run before constructors without
647 // the priority set.  However, it still runs after ifunc initializers and
648 // needs to be called explicitly there.
649 
650 int CONSTRUCTOR_ATTRIBUTE __cpu_indicator_init(void) {
651   unsigned EAX, EBX, ECX, EDX;
652   unsigned MaxLeaf = 5;
653   unsigned Vendor;
654   unsigned Model, Family;
655   unsigned Features[(CPU_FEATURE_MAX + 31) / 32] = {0};
656 
657   // This function needs to run just once.
658   if (__cpu_model.__cpu_vendor)
659     return 0;
660 
661   if (!isCpuIdSupported() ||
662       getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1) {
663     __cpu_model.__cpu_vendor = VENDOR_OTHER;
664     return -1;
665   }
666 
667   getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
668   detectX86FamilyModel(EAX, &Family, &Model);
669 
670   // Find available features.
671   getAvailableFeatures(ECX, EDX, MaxLeaf, &Features[0]);
672 
673   assert((sizeof(Features)/sizeof(Features[0])) == 2);
674   __cpu_model.__cpu_features[0] = Features[0];
675   __cpu_features2 = Features[1];
676 
677   if (Vendor == SIG_INTEL) {
678     // Get CPU type.
679     getIntelProcessorTypeAndSubtype(Family, Model, &Features[0],
680                                     &(__cpu_model.__cpu_type),
681                                     &(__cpu_model.__cpu_subtype));
682     __cpu_model.__cpu_vendor = VENDOR_INTEL;
683   } else if (Vendor == SIG_AMD) {
684     // Get CPU type.
685     getAMDProcessorTypeAndSubtype(Family, Model, &Features[0],
686                                   &(__cpu_model.__cpu_type),
687                                   &(__cpu_model.__cpu_subtype));
688     __cpu_model.__cpu_vendor = VENDOR_AMD;
689   } else
690     __cpu_model.__cpu_vendor = VENDOR_OTHER;
691 
692   assert(__cpu_model.__cpu_vendor < VENDOR_MAX);
693   assert(__cpu_model.__cpu_type < CPU_TYPE_MAX);
694   assert(__cpu_model.__cpu_subtype < CPU_SUBTYPE_MAX);
695 
696   return 0;
697 }
698 
699 #endif
700