Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/cpu_model/x86.c
35292 views
//===-- cpu_model/x86.c - Support for __cpu_model builtin --------*- C -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is based on LLVM's lib/Support/Host.cpp.9// It implements the operating system Host concept and builtin10// __cpu_model for the compiler_rt library for x86.11//12//===----------------------------------------------------------------------===//1314#include "cpu_model.h"1516#if !(defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \17defined(_M_X64))18#error This file is intended only for x86-based targets19#endif2021#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)2223#include <assert.h>2425#ifdef _MSC_VER26#include <intrin.h>27#endif2829enum VendorSignatures {30SIG_INTEL = 0x756e6547, // Genu31SIG_AMD = 0x68747541, // Auth32};3334enum ProcessorVendors {35VENDOR_INTEL = 1,36VENDOR_AMD,37VENDOR_OTHER,38VENDOR_MAX39};4041enum ProcessorTypes {42INTEL_BONNELL = 1,43INTEL_CORE2,44INTEL_COREI7,45AMDFAM10H,46AMDFAM15H,47INTEL_SILVERMONT,48INTEL_KNL,49AMD_BTVER1,50AMD_BTVER2,51AMDFAM17H,52INTEL_KNM,53INTEL_GOLDMONT,54INTEL_GOLDMONT_PLUS,55INTEL_TREMONT,56AMDFAM19H,57ZHAOXIN_FAM7H,58INTEL_SIERRAFOREST,59INTEL_GRANDRIDGE,60INTEL_CLEARWATERFOREST,61AMDFAM1AH,62CPU_TYPE_MAX63};6465enum ProcessorSubtypes {66INTEL_COREI7_NEHALEM = 1,67INTEL_COREI7_WESTMERE,68INTEL_COREI7_SANDYBRIDGE,69AMDFAM10H_BARCELONA,70AMDFAM10H_SHANGHAI,71AMDFAM10H_ISTANBUL,72AMDFAM15H_BDVER1,73AMDFAM15H_BDVER2,74AMDFAM15H_BDVER3,75AMDFAM15H_BDVER4,76AMDFAM17H_ZNVER1,77INTEL_COREI7_IVYBRIDGE,78INTEL_COREI7_HASWELL,79INTEL_COREI7_BROADWELL,80INTEL_COREI7_SKYLAKE,81INTEL_COREI7_SKYLAKE_AVX512,82INTEL_COREI7_CANNONLAKE,83INTEL_COREI7_ICELAKE_CLIENT,84INTEL_COREI7_ICELAKE_SERVER,85AMDFAM17H_ZNVER2,86INTEL_COREI7_CASCADELAKE,87INTEL_COREI7_TIGERLAKE,88INTEL_COREI7_COOPERLAKE,89INTEL_COREI7_SAPPHIRERAPIDS,90INTEL_COREI7_ALDERLAKE,91AMDFAM19H_ZNVER3,92INTEL_COREI7_ROCKETLAKE,93ZHAOXIN_FAM7H_LUJIAZUI,94AMDFAM19H_ZNVER4,95INTEL_COREI7_GRANITERAPIDS,96INTEL_COREI7_GRANITERAPIDS_D,97INTEL_COREI7_ARROWLAKE,98INTEL_COREI7_ARROWLAKE_S,99INTEL_COREI7_PANTHERLAKE,100AMDFAM1AH_ZNVER5,101CPU_SUBTYPE_MAX102};103104enum ProcessorFeatures {105FEATURE_CMOV = 0,106FEATURE_MMX,107FEATURE_POPCNT,108FEATURE_SSE,109FEATURE_SSE2,110FEATURE_SSE3,111FEATURE_SSSE3,112FEATURE_SSE4_1,113FEATURE_SSE4_2,114FEATURE_AVX,115FEATURE_AVX2,116FEATURE_SSE4_A,117FEATURE_FMA4,118FEATURE_XOP,119FEATURE_FMA,120FEATURE_AVX512F,121FEATURE_BMI,122FEATURE_BMI2,123FEATURE_AES,124FEATURE_PCLMUL,125FEATURE_AVX512VL,126FEATURE_AVX512BW,127FEATURE_AVX512DQ,128FEATURE_AVX512CD,129FEATURE_AVX512ER,130FEATURE_AVX512PF,131FEATURE_AVX512VBMI,132FEATURE_AVX512IFMA,133FEATURE_AVX5124VNNIW,134FEATURE_AVX5124FMAPS,135FEATURE_AVX512VPOPCNTDQ,136FEATURE_AVX512VBMI2,137FEATURE_GFNI,138FEATURE_VPCLMULQDQ,139FEATURE_AVX512VNNI,140FEATURE_AVX512BITALG,141FEATURE_AVX512BF16,142FEATURE_AVX512VP2INTERSECT,143// FIXME: Below Features has some missings comparing to gcc, it's because gcc144// has some not one-to-one mapped in llvm.145// FEATURE_3DNOW,146// FEATURE_3DNOWP,147FEATURE_ADX = 40,148// FEATURE_ABM,149FEATURE_CLDEMOTE = 42,150FEATURE_CLFLUSHOPT,151FEATURE_CLWB,152FEATURE_CLZERO,153FEATURE_CMPXCHG16B,154// FIXME: Not adding FEATURE_CMPXCHG8B is a workaround to make 'generic' as155// a cpu string with no X86_FEATURE_COMPAT features, which is required in156// current implementantion of cpu_specific/cpu_dispatch FMV feature.157// FEATURE_CMPXCHG8B,158FEATURE_ENQCMD = 48,159FEATURE_F16C,160FEATURE_FSGSBASE,161// FEATURE_FXSAVE,162// FEATURE_HLE,163// FEATURE_IBT,164FEATURE_LAHF_LM = 54,165FEATURE_LM,166FEATURE_LWP,167FEATURE_LZCNT,168FEATURE_MOVBE,169FEATURE_MOVDIR64B,170FEATURE_MOVDIRI,171FEATURE_MWAITX,172// FEATURE_OSXSAVE,173FEATURE_PCONFIG = 63,174FEATURE_PKU,175FEATURE_PREFETCHWT1,176FEATURE_PRFCHW,177FEATURE_PTWRITE,178FEATURE_RDPID,179FEATURE_RDRND,180FEATURE_RDSEED,181FEATURE_RTM,182FEATURE_SERIALIZE,183FEATURE_SGX,184FEATURE_SHA,185FEATURE_SHSTK,186FEATURE_TBM,187FEATURE_TSXLDTRK,188FEATURE_VAES,189FEATURE_WAITPKG,190FEATURE_WBNOINVD,191FEATURE_XSAVE,192FEATURE_XSAVEC,193FEATURE_XSAVEOPT,194FEATURE_XSAVES,195FEATURE_AMX_TILE,196FEATURE_AMX_INT8,197FEATURE_AMX_BF16,198FEATURE_UINTR,199FEATURE_HRESET,200FEATURE_KL,201// FEATURE_AESKLE,202FEATURE_WIDEKL = 92,203FEATURE_AVXVNNI,204FEATURE_AVX512FP16,205FEATURE_X86_64_BASELINE,206FEATURE_X86_64_V2,207FEATURE_X86_64_V3,208FEATURE_X86_64_V4,209FEATURE_AVXIFMA,210FEATURE_AVXVNNIINT8,211FEATURE_AVXNECONVERT,212FEATURE_CMPCCXADD,213FEATURE_AMX_FP16,214FEATURE_PREFETCHI,215FEATURE_RAOINT,216FEATURE_AMX_COMPLEX,217FEATURE_AVXVNNIINT16,218FEATURE_SM3,219FEATURE_SHA512,220FEATURE_SM4,221FEATURE_APXF,222FEATURE_USERMSR,223FEATURE_AVX10_1_256,224FEATURE_AVX10_1_512,225CPU_FEATURE_MAX226};227228// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).229// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID230// support. Consequently, for i386, the presence of CPUID is checked first231// via the corresponding eflags bit.232static bool isCpuIdSupported(void) {233#if defined(__GNUC__) || defined(__clang__)234#if defined(__i386__)235int __cpuid_supported;236__asm__(" pushfl\n"237" popl %%eax\n"238" movl %%eax,%%ecx\n"239" xorl $0x00200000,%%eax\n"240" pushl %%eax\n"241" popfl\n"242" pushfl\n"243" popl %%eax\n"244" movl $0,%0\n"245" cmpl %%eax,%%ecx\n"246" je 1f\n"247" movl $1,%0\n"248"1:"249: "=r"(__cpuid_supported)250:251: "eax", "ecx");252if (!__cpuid_supported)253return false;254#endif255return true;256#endif257return true;258}259260// This code is copied from lib/Support/Host.cpp.261// Changes to either file should be mirrored in the other.262263/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in264/// the specified arguments. If we can't run cpuid on the host, return true.265static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,266unsigned *rECX, unsigned *rEDX) {267#if defined(__GNUC__) || defined(__clang__)268#if defined(__x86_64__)269// gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.270// FIXME: should we save this for Clang?271__asm__("movq\t%%rbx, %%rsi\n\t"272"cpuid\n\t"273"xchgq\t%%rbx, %%rsi\n\t"274: "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)275: "a"(value));276return false;277#elif defined(__i386__)278__asm__("movl\t%%ebx, %%esi\n\t"279"cpuid\n\t"280"xchgl\t%%ebx, %%esi\n\t"281: "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)282: "a"(value));283return false;284#else285return true;286#endif287#elif defined(_MSC_VER)288// The MSVC intrinsic is portable across x86 and x64.289int registers[4];290__cpuid(registers, value);291*rEAX = registers[0];292*rEBX = registers[1];293*rECX = registers[2];294*rEDX = registers[3];295return false;296#else297return true;298#endif299}300301/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return302/// the 4 values in the specified arguments. If we can't run cpuid on the host,303/// return true.304static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,305unsigned *rEAX, unsigned *rEBX, unsigned *rECX,306unsigned *rEDX) {307#if defined(__GNUC__) || defined(__clang__)308#if defined(__x86_64__)309// gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.310// FIXME: should we save this for Clang?311__asm__("movq\t%%rbx, %%rsi\n\t"312"cpuid\n\t"313"xchgq\t%%rbx, %%rsi\n\t"314: "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)315: "a"(value), "c"(subleaf));316return false;317#elif defined(__i386__)318__asm__("movl\t%%ebx, %%esi\n\t"319"cpuid\n\t"320"xchgl\t%%ebx, %%esi\n\t"321: "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)322: "a"(value), "c"(subleaf));323return false;324#else325return true;326#endif327#elif defined(_MSC_VER)328int registers[4];329__cpuidex(registers, value, subleaf);330*rEAX = registers[0];331*rEBX = registers[1];332*rECX = registers[2];333*rEDX = registers[3];334return false;335#else336return true;337#endif338}339340// Read control register 0 (XCR0). Used to detect features such as AVX.341static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {342#if defined(__GNUC__) || defined(__clang__)343// Check xgetbv; this uses a .byte sequence instead of the instruction344// directly because older assemblers do not include support for xgetbv and345// there is no easy way to conditionally compile based on the assembler used.346__asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));347return false;348#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)349unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);350*rEAX = Result;351*rEDX = Result >> 32;352return false;353#else354return true;355#endif356}357358static void detectX86FamilyModel(unsigned EAX, unsigned *Family,359unsigned *Model) {360*Family = (EAX >> 8) & 0xf; // Bits 8 - 11361*Model = (EAX >> 4) & 0xf; // Bits 4 - 7362if (*Family == 6 || *Family == 0xf) {363if (*Family == 0xf)364// Examine extended family ID if family ID is F.365*Family += (EAX >> 20) & 0xff; // Bits 20 - 27366// Examine extended model ID if family ID is 6 or F.367*Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19368}369}370371#define testFeature(F) (Features[F / 32] & (1 << (F % 32))) != 0372373static const char *getIntelProcessorTypeAndSubtype(unsigned Family,374unsigned Model,375const unsigned *Features,376unsigned *Type,377unsigned *Subtype) {378// We select CPU strings to match the code in Host.cpp, but we don't use them379// in compiler-rt.380const char *CPU = 0;381382switch (Family) {383case 6:384switch (Model) {385case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile386// processor, Intel Core 2 Quad processor, Intel Core 2 Quad387// mobile processor, Intel Core 2 Extreme processor, Intel388// Pentium Dual-Core processor, Intel Xeon processor, model389// 0Fh. All processors are manufactured using the 65 nm process.390case 0x16: // Intel Celeron processor model 16h. All processors are391// manufactured using the 65 nm process392CPU = "core2";393*Type = INTEL_CORE2;394break;395case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model396// 17h. All processors are manufactured using the 45 nm process.397//398// 45nm: Penryn , Wolfdale, Yorkfield (XE)399case 0x1d: // Intel Xeon processor MP. All processors are manufactured using400// the 45 nm process.401CPU = "penryn";402*Type = INTEL_CORE2;403break;404case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All405// processors are manufactured using the 45 nm process.406case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.407// As found in a Summer 2010 model iMac.408case 0x1f:409case 0x2e: // Nehalem EX410CPU = "nehalem";411*Type = INTEL_COREI7;412*Subtype = INTEL_COREI7_NEHALEM;413break;414case 0x25: // Intel Core i7, laptop version.415case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All416// processors are manufactured using the 32 nm process.417case 0x2f: // Westmere EX418CPU = "westmere";419*Type = INTEL_COREI7;420*Subtype = INTEL_COREI7_WESTMERE;421break;422case 0x2a: // Intel Core i7 processor. All processors are manufactured423// using the 32 nm process.424case 0x2d:425CPU = "sandybridge";426*Type = INTEL_COREI7;427*Subtype = INTEL_COREI7_SANDYBRIDGE;428break;429case 0x3a:430case 0x3e: // Ivy Bridge EP431CPU = "ivybridge";432*Type = INTEL_COREI7;433*Subtype = INTEL_COREI7_IVYBRIDGE;434break;435436// Haswell:437case 0x3c:438case 0x3f:439case 0x45:440case 0x46:441CPU = "haswell";442*Type = INTEL_COREI7;443*Subtype = INTEL_COREI7_HASWELL;444break;445446// Broadwell:447case 0x3d:448case 0x47:449case 0x4f:450case 0x56:451CPU = "broadwell";452*Type = INTEL_COREI7;453*Subtype = INTEL_COREI7_BROADWELL;454break;455456// Skylake:457case 0x4e: // Skylake mobile458case 0x5e: // Skylake desktop459case 0x8e: // Kaby Lake mobile460case 0x9e: // Kaby Lake desktop461case 0xa5: // Comet Lake-H/S462case 0xa6: // Comet Lake-U463CPU = "skylake";464*Type = INTEL_COREI7;465*Subtype = INTEL_COREI7_SKYLAKE;466break;467468// Rocketlake:469case 0xa7:470CPU = "rocketlake";471*Type = INTEL_COREI7;472*Subtype = INTEL_COREI7_ROCKETLAKE;473break;474475// Skylake Xeon:476case 0x55:477*Type = INTEL_COREI7;478if (testFeature(FEATURE_AVX512BF16)) {479CPU = "cooperlake";480*Subtype = INTEL_COREI7_COOPERLAKE;481} else if (testFeature(FEATURE_AVX512VNNI)) {482CPU = "cascadelake";483*Subtype = INTEL_COREI7_CASCADELAKE;484} else {485CPU = "skylake-avx512";486*Subtype = INTEL_COREI7_SKYLAKE_AVX512;487}488break;489490// Cannonlake:491case 0x66:492CPU = "cannonlake";493*Type = INTEL_COREI7;494*Subtype = INTEL_COREI7_CANNONLAKE;495break;496497// Icelake:498case 0x7d:499case 0x7e:500CPU = "icelake-client";501*Type = INTEL_COREI7;502*Subtype = INTEL_COREI7_ICELAKE_CLIENT;503break;504505// Tigerlake:506case 0x8c:507case 0x8d:508CPU = "tigerlake";509*Type = INTEL_COREI7;510*Subtype = INTEL_COREI7_TIGERLAKE;511break;512513// Alderlake:514case 0x97:515case 0x9a:516// Raptorlake:517case 0xb7:518case 0xba:519case 0xbf:520// Meteorlake:521case 0xaa:522case 0xac:523// Gracemont:524case 0xbe:525CPU = "alderlake";526*Type = INTEL_COREI7;527*Subtype = INTEL_COREI7_ALDERLAKE;528break;529530// Arrowlake:531case 0xc5:532CPU = "arrowlake";533*Type = INTEL_COREI7;534*Subtype = INTEL_COREI7_ARROWLAKE;535break;536537// Arrowlake S:538case 0xc6:539// Lunarlake:540case 0xbd:541CPU = "arrowlake-s";542*Type = INTEL_COREI7;543*Subtype = INTEL_COREI7_ARROWLAKE_S;544break;545546// Pantherlake:547case 0xcc:548CPU = "pantherlake";549*Type = INTEL_COREI7;550*Subtype = INTEL_COREI7_PANTHERLAKE;551break;552553// Icelake Xeon:554case 0x6a:555case 0x6c:556CPU = "icelake-server";557*Type = INTEL_COREI7;558*Subtype = INTEL_COREI7_ICELAKE_SERVER;559break;560561// Emerald Rapids:562case 0xcf:563// Sapphire Rapids:564case 0x8f:565CPU = "sapphirerapids";566*Type = INTEL_COREI7;567*Subtype = INTEL_COREI7_SAPPHIRERAPIDS;568break;569570// Granite Rapids:571case 0xad:572CPU = "graniterapids";573*Type = INTEL_COREI7;574*Subtype = INTEL_COREI7_GRANITERAPIDS;575break;576577// Granite Rapids D:578case 0xae:579CPU = "graniterapids-d";580*Type = INTEL_COREI7;581*Subtype = INTEL_COREI7_GRANITERAPIDS_D;582break;583584case 0x1c: // Most 45 nm Intel Atom processors585case 0x26: // 45 nm Atom Lincroft586case 0x27: // 32 nm Atom Medfield587case 0x35: // 32 nm Atom Midview588case 0x36: // 32 nm Atom Midview589CPU = "bonnell";590*Type = INTEL_BONNELL;591break;592593// Atom Silvermont codes from the Intel software optimization guide.594case 0x37:595case 0x4a:596case 0x4d:597case 0x5a:598case 0x5d:599case 0x4c: // really airmont600CPU = "silvermont";601*Type = INTEL_SILVERMONT;602break;603// Goldmont:604case 0x5c: // Apollo Lake605case 0x5f: // Denverton606CPU = "goldmont";607*Type = INTEL_GOLDMONT;608break; // "goldmont"609case 0x7a:610CPU = "goldmont-plus";611*Type = INTEL_GOLDMONT_PLUS;612break;613case 0x86:614case 0x8a: // Lakefield615case 0x96: // Elkhart Lake616case 0x9c: // Jasper Lake617CPU = "tremont";618*Type = INTEL_TREMONT;619break;620621// Sierraforest:622case 0xaf:623CPU = "sierraforest";624*Type = INTEL_SIERRAFOREST;625break;626627// Grandridge:628case 0xb6:629CPU = "grandridge";630*Type = INTEL_GRANDRIDGE;631break;632633// Clearwaterforest:634case 0xdd:635CPU = "clearwaterforest";636*Type = INTEL_COREI7;637*Subtype = INTEL_CLEARWATERFOREST;638break;639640case 0x57:641CPU = "knl";642*Type = INTEL_KNL;643break;644645case 0x85:646CPU = "knm";647*Type = INTEL_KNM;648break;649650default: // Unknown family 6 CPU.651break;652}653break;654default:655break; // Unknown.656}657658return CPU;659}660661static const char *getAMDProcessorTypeAndSubtype(unsigned Family,662unsigned Model,663const unsigned *Features,664unsigned *Type,665unsigned *Subtype) {666const char *CPU = 0;667668switch (Family) {669case 4:670CPU = "i486";671break;672case 5:673CPU = "pentium";674switch (Model) {675case 6:676case 7:677CPU = "k6";678break;679case 8:680CPU = "k6-2";681break;682case 9:683case 13:684CPU = "k6-3";685break;686case 10:687CPU = "geode";688break;689}690break;691case 6:692if (testFeature(FEATURE_SSE)) {693CPU = "athlon-xp";694break;695}696CPU = "athlon";697break;698case 15:699if (testFeature(FEATURE_SSE3)) {700CPU = "k8-sse3";701break;702}703CPU = "k8";704break;705case 16:706CPU = "amdfam10";707*Type = AMDFAM10H; // "amdfam10"708switch (Model) {709case 2:710*Subtype = AMDFAM10H_BARCELONA;711break;712case 4:713*Subtype = AMDFAM10H_SHANGHAI;714break;715case 8:716*Subtype = AMDFAM10H_ISTANBUL;717break;718}719break;720case 20:721CPU = "btver1";722*Type = AMD_BTVER1;723break;724case 21:725CPU = "bdver1";726*Type = AMDFAM15H;727if (Model >= 0x60 && Model <= 0x7f) {728CPU = "bdver4";729*Subtype = AMDFAM15H_BDVER4;730break; // 60h-7Fh: Excavator731}732if (Model >= 0x30 && Model <= 0x3f) {733CPU = "bdver3";734*Subtype = AMDFAM15H_BDVER3;735break; // 30h-3Fh: Steamroller736}737if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {738CPU = "bdver2";739*Subtype = AMDFAM15H_BDVER2;740break; // 02h, 10h-1Fh: Piledriver741}742if (Model <= 0x0f) {743*Subtype = AMDFAM15H_BDVER1;744break; // 00h-0Fh: Bulldozer745}746break;747case 22:748CPU = "btver2";749*Type = AMD_BTVER2;750break;751case 23:752CPU = "znver1";753*Type = AMDFAM17H;754if ((Model >= 0x30 && Model <= 0x3f) || (Model == 0x47) ||755(Model >= 0x60 && Model <= 0x67) || (Model >= 0x68 && Model <= 0x6f) ||756(Model >= 0x70 && Model <= 0x7f) || (Model >= 0x84 && Model <= 0x87) ||757(Model >= 0x90 && Model <= 0x97) || (Model >= 0x98 && Model <= 0x9f) ||758(Model >= 0xa0 && Model <= 0xaf)) {759// Family 17h Models 30h-3Fh (Starship) Zen 2760// Family 17h Models 47h (Cardinal) Zen 2761// Family 17h Models 60h-67h (Renoir) Zen 2762// Family 17h Models 68h-6Fh (Lucienne) Zen 2763// Family 17h Models 70h-7Fh (Matisse) Zen 2764// Family 17h Models 84h-87h (ProjectX) Zen 2765// Family 17h Models 90h-97h (VanGogh) Zen 2766// Family 17h Models 98h-9Fh (Mero) Zen 2767// Family 17h Models A0h-AFh (Mendocino) Zen 2768CPU = "znver2";769*Subtype = AMDFAM17H_ZNVER2;770break;771}772if ((Model >= 0x10 && Model <= 0x1f) || (Model >= 0x20 && Model <= 0x2f)) {773// Family 17h Models 10h-1Fh (Raven1) Zen774// Family 17h Models 10h-1Fh (Picasso) Zen+775// Family 17h Models 20h-2Fh (Raven2 x86) Zen776*Subtype = AMDFAM17H_ZNVER1;777break;778}779break;780case 25:781CPU = "znver3";782*Type = AMDFAM19H;783if (Model <= 0x0f || (Model >= 0x20 && Model <= 0x2f) ||784(Model >= 0x30 && Model <= 0x3f) || (Model >= 0x40 && Model <= 0x4f) ||785(Model >= 0x50 && Model <= 0x5f)) {786// Family 19h Models 00h-0Fh (Genesis, Chagall) Zen 3787// Family 19h Models 20h-2Fh (Vermeer) Zen 3788// Family 19h Models 30h-3Fh (Badami) Zen 3789// Family 19h Models 40h-4Fh (Rembrandt) Zen 3+790// Family 19h Models 50h-5Fh (Cezanne) Zen 3791*Subtype = AMDFAM19H_ZNVER3;792break;793}794if ((Model >= 0x10 && Model <= 0x1f) || (Model >= 0x60 && Model <= 0x6f) ||795(Model >= 0x70 && Model <= 0x77) || (Model >= 0x78 && Model <= 0x7f) ||796(Model >= 0xa0 && Model <= 0xaf)) {797// Family 19h Models 10h-1Fh (Stones; Storm Peak) Zen 4798// Family 19h Models 60h-6Fh (Raphael) Zen 4799// Family 19h Models 70h-77h (Phoenix, Hawkpoint1) Zen 4800// Family 19h Models 78h-7Fh (Phoenix 2, Hawkpoint2) Zen 4801// Family 19h Models A0h-AFh (Stones-Dense) Zen 4802CPU = "znver4";803*Subtype = AMDFAM19H_ZNVER4;804break; // "znver4"805}806break; // family 19h807case 26:808CPU = "znver5";809*Type = AMDFAM1AH;810if (Model <= 0x77) {811// Models 00h-0Fh (Breithorn).812// Models 10h-1Fh (Breithorn-Dense).813// Models 20h-2Fh (Strix 1).814// Models 30h-37h (Strix 2).815// Models 38h-3Fh (Strix 3).816// Models 40h-4Fh (Granite Ridge).817// Models 50h-5Fh (Weisshorn).818// Models 60h-6Fh (Krackan1).819// Models 70h-77h (Sarlak).820CPU = "znver5";821*Subtype = AMDFAM1AH_ZNVER5;822break; // "znver5"823}824break;825default:826break; // Unknown AMD CPU.827}828829return CPU;830}831832#undef testFeature833834static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,835unsigned *Features) {836unsigned EAX = 0, EBX = 0;837838#define hasFeature(F) ((Features[F / 32] >> (F % 32)) & 1)839#define setFeature(F) Features[F / 32] |= 1U << (F % 32)840841if ((EDX >> 15) & 1)842setFeature(FEATURE_CMOV);843if ((EDX >> 23) & 1)844setFeature(FEATURE_MMX);845if ((EDX >> 25) & 1)846setFeature(FEATURE_SSE);847if ((EDX >> 26) & 1)848setFeature(FEATURE_SSE2);849850if ((ECX >> 0) & 1)851setFeature(FEATURE_SSE3);852if ((ECX >> 1) & 1)853setFeature(FEATURE_PCLMUL);854if ((ECX >> 9) & 1)855setFeature(FEATURE_SSSE3);856if ((ECX >> 12) & 1)857setFeature(FEATURE_FMA);858if ((ECX >> 13) & 1)859setFeature(FEATURE_CMPXCHG16B);860if ((ECX >> 19) & 1)861setFeature(FEATURE_SSE4_1);862if ((ECX >> 20) & 1)863setFeature(FEATURE_SSE4_2);864if ((ECX >> 22) & 1)865setFeature(FEATURE_MOVBE);866if ((ECX >> 23) & 1)867setFeature(FEATURE_POPCNT);868if ((ECX >> 25) & 1)869setFeature(FEATURE_AES);870if ((ECX >> 29) & 1)871setFeature(FEATURE_F16C);872if ((ECX >> 30) & 1)873setFeature(FEATURE_RDRND);874875// If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV876// indicates that the AVX registers will be saved and restored on context877// switch, then we have full AVX support.878const unsigned AVXBits = (1 << 27) | (1 << 28);879bool HasAVXSave = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&880((EAX & 0x6) == 0x6);881#if defined(__APPLE__)882// Darwin lazily saves the AVX512 context on first use: trust that the OS will883// save the AVX512 context if we use AVX512 instructions, even the bit is not884// set right now.885bool HasAVX512Save = true;886#else887// AVX512 requires additional context to be saved by the OS.888bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);889#endif890// AMX requires additional context to be saved by the OS.891const unsigned AMXBits = (1 << 17) | (1 << 18);892bool HasXSave = ((ECX >> 27) & 1) && !getX86XCR0(&EAX, &EDX);893bool HasAMXSave = HasXSave && ((EAX & AMXBits) == AMXBits);894895if (HasAVXSave)896setFeature(FEATURE_AVX);897898if (((ECX >> 26) & 1) && HasAVXSave)899setFeature(FEATURE_XSAVE);900901bool HasLeaf7 =902MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);903904if (HasLeaf7 && ((EBX >> 0) & 1))905setFeature(FEATURE_FSGSBASE);906if (HasLeaf7 && ((EBX >> 2) & 1))907setFeature(FEATURE_SGX);908if (HasLeaf7 && ((EBX >> 3) & 1))909setFeature(FEATURE_BMI);910if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave)911setFeature(FEATURE_AVX2);912if (HasLeaf7 && ((EBX >> 8) & 1))913setFeature(FEATURE_BMI2);914if (HasLeaf7 && ((EBX >> 11) & 1))915setFeature(FEATURE_RTM);916if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)917setFeature(FEATURE_AVX512F);918if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)919setFeature(FEATURE_AVX512DQ);920if (HasLeaf7 && ((EBX >> 18) & 1))921setFeature(FEATURE_RDSEED);922if (HasLeaf7 && ((EBX >> 19) & 1))923setFeature(FEATURE_ADX);924if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)925setFeature(FEATURE_AVX512IFMA);926if (HasLeaf7 && ((EBX >> 24) & 1))927setFeature(FEATURE_CLWB);928if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)929setFeature(FEATURE_AVX512PF);930if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)931setFeature(FEATURE_AVX512ER);932if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)933setFeature(FEATURE_AVX512CD);934if (HasLeaf7 && ((EBX >> 29) & 1))935setFeature(FEATURE_SHA);936if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)937setFeature(FEATURE_AVX512BW);938if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)939setFeature(FEATURE_AVX512VL);940941if (HasLeaf7 && ((ECX >> 0) & 1))942setFeature(FEATURE_PREFETCHWT1);943if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)944setFeature(FEATURE_AVX512VBMI);945if (HasLeaf7 && ((ECX >> 4) & 1))946setFeature(FEATURE_PKU);947if (HasLeaf7 && ((ECX >> 5) & 1))948setFeature(FEATURE_WAITPKG);949if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)950setFeature(FEATURE_AVX512VBMI2);951if (HasLeaf7 && ((ECX >> 7) & 1))952setFeature(FEATURE_SHSTK);953if (HasLeaf7 && ((ECX >> 8) & 1))954setFeature(FEATURE_GFNI);955if (HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave)956setFeature(FEATURE_VAES);957if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave)958setFeature(FEATURE_VPCLMULQDQ);959if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)960setFeature(FEATURE_AVX512VNNI);961if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)962setFeature(FEATURE_AVX512BITALG);963if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)964setFeature(FEATURE_AVX512VPOPCNTDQ);965if (HasLeaf7 && ((ECX >> 22) & 1))966setFeature(FEATURE_RDPID);967if (HasLeaf7 && ((ECX >> 23) & 1))968setFeature(FEATURE_KL);969if (HasLeaf7 && ((ECX >> 25) & 1))970setFeature(FEATURE_CLDEMOTE);971if (HasLeaf7 && ((ECX >> 27) & 1))972setFeature(FEATURE_MOVDIRI);973if (HasLeaf7 && ((ECX >> 28) & 1))974setFeature(FEATURE_MOVDIR64B);975if (HasLeaf7 && ((ECX >> 29) & 1))976setFeature(FEATURE_ENQCMD);977978if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)979setFeature(FEATURE_AVX5124VNNIW);980if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)981setFeature(FEATURE_AVX5124FMAPS);982if (HasLeaf7 && ((EDX >> 5) & 1))983setFeature(FEATURE_UINTR);984if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)985setFeature(FEATURE_AVX512VP2INTERSECT);986if (HasLeaf7 && ((EDX >> 14) & 1))987setFeature(FEATURE_SERIALIZE);988if (HasLeaf7 && ((EDX >> 16) & 1))989setFeature(FEATURE_TSXLDTRK);990if (HasLeaf7 && ((EDX >> 18) & 1))991setFeature(FEATURE_PCONFIG);992if (HasLeaf7 && ((EDX >> 22) & 1) && HasAMXSave)993setFeature(FEATURE_AMX_BF16);994if (HasLeaf7 && ((EDX >> 23) & 1) && HasAVX512Save)995setFeature(FEATURE_AVX512FP16);996if (HasLeaf7 && ((EDX >> 24) & 1) && HasAMXSave)997setFeature(FEATURE_AMX_TILE);998if (HasLeaf7 && ((EDX >> 25) & 1) && HasAMXSave)999setFeature(FEATURE_AMX_INT8);10001001// EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't1002// return all 0s for invalid subleaves so check the limit.1003bool HasLeaf7Subleaf1 =1004HasLeaf7 && EAX >= 1 &&1005!getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);1006if (HasLeaf7Subleaf1 && ((EAX >> 0) & 1))1007setFeature(FEATURE_SHA512);1008if (HasLeaf7Subleaf1 && ((EAX >> 1) & 1))1009setFeature(FEATURE_SM3);1010if (HasLeaf7Subleaf1 && ((EAX >> 2) & 1))1011setFeature(FEATURE_SM4);1012if (HasLeaf7Subleaf1 && ((EAX >> 3) & 1))1013setFeature(FEATURE_RAOINT);1014if (HasLeaf7Subleaf1 && ((EAX >> 4) & 1) && HasAVXSave)1015setFeature(FEATURE_AVXVNNI);1016if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)1017setFeature(FEATURE_AVX512BF16);1018if (HasLeaf7Subleaf1 && ((EAX >> 7) & 1))1019setFeature(FEATURE_CMPCCXADD);1020if (HasLeaf7Subleaf1 && ((EAX >> 21) & 1) && HasAMXSave)1021setFeature(FEATURE_AMX_FP16);1022if (HasLeaf7Subleaf1 && ((EAX >> 22) & 1))1023setFeature(FEATURE_HRESET);1024if (HasLeaf7Subleaf1 && ((EAX >> 23) & 1) && HasAVXSave)1025setFeature(FEATURE_AVXIFMA);10261027if (HasLeaf7Subleaf1 && ((EDX >> 4) & 1) && HasAVXSave)1028setFeature(FEATURE_AVXVNNIINT8);1029if (HasLeaf7Subleaf1 && ((EDX >> 5) & 1) && HasAVXSave)1030setFeature(FEATURE_AVXNECONVERT);1031if (HasLeaf7Subleaf1 && ((EDX >> 8) & 1) && HasAMXSave)1032setFeature(FEATURE_AMX_COMPLEX);1033if (HasLeaf7Subleaf1 && ((EDX >> 10) & 1) && HasAVXSave)1034setFeature(FEATURE_AVXVNNIINT16);1035if (HasLeaf7Subleaf1 && ((EDX >> 14) & 1))1036setFeature(FEATURE_PREFETCHI);1037if (HasLeaf7Subleaf1 && ((EDX >> 15) & 1))1038setFeature(FEATURE_USERMSR);1039if (HasLeaf7Subleaf1 && ((EDX >> 19) & 1))1040setFeature(FEATURE_AVX10_1_256);1041if (HasLeaf7Subleaf1 && ((EDX >> 21) & 1))1042setFeature(FEATURE_APXF);10431044unsigned MaxLevel;1045getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX);1046bool HasLeafD = MaxLevel >= 0xd &&1047!getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);1048if (HasLeafD && ((EAX >> 0) & 1) && HasAVXSave)1049setFeature(FEATURE_XSAVEOPT);1050if (HasLeafD && ((EAX >> 1) & 1) && HasAVXSave)1051setFeature(FEATURE_XSAVEC);1052if (HasLeafD && ((EAX >> 3) & 1) && HasAVXSave)1053setFeature(FEATURE_XSAVES);10541055bool HasLeaf24 =1056MaxLevel >= 0x24 && !getX86CpuIDAndInfo(0x24, &EAX, &EBX, &ECX, &EDX);1057if (HasLeaf7Subleaf1 && ((EDX >> 19) & 1) && HasLeaf24 && ((EBX >> 18) & 1))1058setFeature(FEATURE_AVX10_1_512);10591060unsigned MaxExtLevel;1061getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);10621063bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&1064!getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);1065if (HasExtLeaf1) {1066if (ECX & 1)1067setFeature(FEATURE_LAHF_LM);1068if ((ECX >> 5) & 1)1069setFeature(FEATURE_LZCNT);1070if (((ECX >> 6) & 1))1071setFeature(FEATURE_SSE4_A);1072if (((ECX >> 8) & 1))1073setFeature(FEATURE_PRFCHW);1074if (((ECX >> 11) & 1))1075setFeature(FEATURE_XOP);1076if (((ECX >> 15) & 1))1077setFeature(FEATURE_LWP);1078if (((ECX >> 16) & 1))1079setFeature(FEATURE_FMA4);1080if (((ECX >> 21) & 1))1081setFeature(FEATURE_TBM);1082if (((ECX >> 29) & 1))1083setFeature(FEATURE_MWAITX);10841085if (((EDX >> 29) & 1))1086setFeature(FEATURE_LM);1087}10881089bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&1090!getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);1091if (HasExtLeaf8 && ((EBX >> 0) & 1))1092setFeature(FEATURE_CLZERO);1093if (HasExtLeaf8 && ((EBX >> 9) & 1))1094setFeature(FEATURE_WBNOINVD);10951096bool HasLeaf14 = MaxLevel >= 0x14 &&1097!getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX);1098if (HasLeaf14 && ((EBX >> 4) & 1))1099setFeature(FEATURE_PTWRITE);11001101bool HasLeaf19 =1102MaxLevel >= 0x19 && !getX86CpuIDAndInfo(0x19, &EAX, &EBX, &ECX, &EDX);1103if (HasLeaf7 && HasLeaf19 && ((EBX >> 2) & 1))1104setFeature(FEATURE_WIDEKL);11051106if (hasFeature(FEATURE_LM) && hasFeature(FEATURE_SSE2)) {1107setFeature(FEATURE_X86_64_BASELINE);1108if (hasFeature(FEATURE_CMPXCHG16B) && hasFeature(FEATURE_POPCNT) &&1109hasFeature(FEATURE_LAHF_LM) && hasFeature(FEATURE_SSE4_2)) {1110setFeature(FEATURE_X86_64_V2);1111if (hasFeature(FEATURE_AVX2) && hasFeature(FEATURE_BMI) &&1112hasFeature(FEATURE_BMI2) && hasFeature(FEATURE_F16C) &&1113hasFeature(FEATURE_FMA) && hasFeature(FEATURE_LZCNT) &&1114hasFeature(FEATURE_MOVBE)) {1115setFeature(FEATURE_X86_64_V3);1116if (hasFeature(FEATURE_AVX512BW) && hasFeature(FEATURE_AVX512CD) &&1117hasFeature(FEATURE_AVX512DQ) && hasFeature(FEATURE_AVX512VL))1118setFeature(FEATURE_X86_64_V4);1119}1120}1121}11221123#undef hasFeature1124#undef setFeature1125}11261127#ifndef _WIN321128__attribute__((visibility("hidden")))1129#endif1130int __cpu_indicator_init(void) CONSTRUCTOR_ATTRIBUTE;11311132#ifndef _WIN321133__attribute__((visibility("hidden")))1134#endif1135struct __processor_model {1136unsigned int __cpu_vendor;1137unsigned int __cpu_type;1138unsigned int __cpu_subtype;1139unsigned int __cpu_features[1];1140} __cpu_model = {0, 0, 0, {0}};11411142#ifndef _WIN321143__attribute__((visibility("hidden")))1144#endif1145unsigned __cpu_features2[(CPU_FEATURE_MAX - 1) / 32];11461147// A constructor function that is sets __cpu_model and __cpu_features2 with1148// the right values. This needs to run only once. This constructor is1149// given the highest priority and it should run before constructors without1150// the priority set. However, it still runs after ifunc initializers and1151// needs to be called explicitly there.11521153int CONSTRUCTOR_ATTRIBUTE __cpu_indicator_init(void) {1154unsigned EAX, EBX, ECX, EDX;1155unsigned MaxLeaf = 5;1156unsigned Vendor;1157unsigned Model, Family;1158unsigned Features[(CPU_FEATURE_MAX + 31) / 32] = {0};1159static_assert(sizeof(Features) / sizeof(Features[0]) == 4, "");1160static_assert(sizeof(__cpu_features2) / sizeof(__cpu_features2[0]) == 3, "");11611162// This function needs to run just once.1163if (__cpu_model.__cpu_vendor)1164return 0;11651166if (!isCpuIdSupported() ||1167getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1) {1168__cpu_model.__cpu_vendor = VENDOR_OTHER;1169return -1;1170}11711172getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);1173detectX86FamilyModel(EAX, &Family, &Model);11741175// Find available features.1176getAvailableFeatures(ECX, EDX, MaxLeaf, &Features[0]);11771178__cpu_model.__cpu_features[0] = Features[0];1179__cpu_features2[0] = Features[1];1180__cpu_features2[1] = Features[2];1181__cpu_features2[2] = Features[3];11821183if (Vendor == SIG_INTEL) {1184// Get CPU type.1185getIntelProcessorTypeAndSubtype(Family, Model, &Features[0],1186&(__cpu_model.__cpu_type),1187&(__cpu_model.__cpu_subtype));1188__cpu_model.__cpu_vendor = VENDOR_INTEL;1189} else if (Vendor == SIG_AMD) {1190// Get CPU type.1191getAMDProcessorTypeAndSubtype(Family, Model, &Features[0],1192&(__cpu_model.__cpu_type),1193&(__cpu_model.__cpu_subtype));1194__cpu_model.__cpu_vendor = VENDOR_AMD;1195} else1196__cpu_model.__cpu_vendor = VENDOR_OTHER;11971198assert(__cpu_model.__cpu_vendor < VENDOR_MAX);1199assert(__cpu_model.__cpu_type < CPU_TYPE_MAX);1200assert(__cpu_model.__cpu_subtype < CPU_SUBTYPE_MAX);12011202return 0;1203}1204#endif // defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)120512061207