Skip to content

[compiler-rt][X86] Unify getIntelProcessorTypeAndSubtype #97861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 112 additions & 9 deletions compiler-rt/lib/builtins/cpu_model/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ enum ProcessorFeatures {
// FEATURE_3DNOW,
// FEATURE_3DNOWP,
FEATURE_ADX = 40,
// FEATURE_ABM,
FEATURE_CLDEMOTE = 42,
FEATURE_64BIT,
FEATURE_CLDEMOTE,
FEATURE_CLFLUSHOPT,
FEATURE_CLWB,
FEATURE_CLZERO,
Expand Down Expand Up @@ -323,11 +323,22 @@ static const char *getIntelProcessorTypeAndSubtype(unsigned Family,
const unsigned *Features,
unsigned *Type,
unsigned *Subtype) {
// We select CPU strings to match the code in Host.cpp, but we don't use them
// in compiler-rt.
const char *CPU = 0;

switch (Family) {
case 3:
CPU = "i386";
break;
case 4:
CPU = "i486";
break;
case 5:
if (testFeature(FEATURE_MMX)) {
CPU = "pentium-mmx";
break;
}
CPU = "pentium";
break;
case 6:
switch (Model) {
case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
Expand Down Expand Up @@ -542,7 +553,7 @@ static const char *getIntelProcessorTypeAndSubtype(unsigned Family,
*Subtype = INTEL_COREI7_SAPPHIRERAPIDS;
break;

// Granite Rapids:
// Granite rapids:
case 0xad:
CPU = "graniterapids";
*Type = INTEL_COREI7;
Expand Down Expand Up @@ -580,7 +591,7 @@ static const char *getIntelProcessorTypeAndSubtype(unsigned Family,
case 0x5f: // Denverton
CPU = "goldmont";
*Type = INTEL_GOLDMONT;
break; // "goldmont"
break;
case 0x7a:
CPU = "goldmont-plus";
*Type = INTEL_GOLDMONT_PLUS;
Expand Down Expand Up @@ -608,10 +619,10 @@ static const char *getIntelProcessorTypeAndSubtype(unsigned Family,
// Clearwaterforest:
case 0xdd:
CPU = "clearwaterforest";
*Type = INTEL_COREI7;
*Subtype = INTEL_CLEARWATERFOREST;
*Type = INTEL_CLEARWATERFOREST;
break;

// Xeon Phi (Knights Landing + Knights Mill):
case 0x57:
CPU = "knl";
*Type = INTEL_KNL;
Expand All @@ -622,10 +633,102 @@ static const char *getIntelProcessorTypeAndSubtype(unsigned Family,
*Type = INTEL_KNM;
break;

default: // Unknown family 6 CPU.
default: // Unknown family 6 CPU, try to guess.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall I saw discussion somewhere says it's not proved returning a guessed CPU is better than unknown one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a link? Should we remove this then? This just unifies the implementations, so if we shouldn't guess, then we should remove it from the LLVM implementation.

More than happy to put up a patch for that if that's what is desired.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I can't find it. I'm even not quite sure of it. I think we can put both way in an RFC and see others' opinion. Do you know the behavior of libgcc? It would be a good reference.

// TODO detect tigerlake host from model
if (testFeature(FEATURE_AVX512VP2INTERSECT)) {
CPU = "tigerlake";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_TIGERLAKE;
} else if (testFeature(FEATURE_AVX512VBMI2)) {
CPU = "icelake-client";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_ICELAKE_CLIENT;
} else if (testFeature(FEATURE_AVX512VBMI)) {
CPU = "cannonlake";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_CANNONLAKE;
} else if (testFeature(FEATURE_AVX512BF16)) {
CPU = "cooperlake";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_COOPERLAKE;
} else if (testFeature(FEATURE_AVX512VNNI)) {
CPU = "cascadelake";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_CASCADELAKE;
} else if (testFeature(FEATURE_AVX512VL)) {
CPU = "skylake-avx512";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_SKYLAKE_AVX512;
} else if (testFeature(FEATURE_CLFLUSHOPT)) {
if (testFeature(FEATURE_SHA)) {
CPU = "goldmont";
*Type = INTEL_GOLDMONT;
} else {
CPU = "skylake";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_SKYLAKE;
}
} else if (testFeature(FEATURE_ADX)) {
CPU = "broadwell";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_BROADWELL;
} else if (testFeature(FEATURE_AVX2)) {
CPU = "haswell";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_HASWELL;
} else if (testFeature(FEATURE_AVX)) {
CPU = "sandybridge";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_SANDYBRIDGE;
} else if (testFeature(FEATURE_SSE4_2)) {
if (testFeature(FEATURE_MOVBE)) {
CPU = "silvermont";
*Type = INTEL_SILVERMONT;
} else {
CPU = "nehalem";
*Type = INTEL_COREI7;
*Subtype = INTEL_COREI7_NEHALEM;
}
} else if (testFeature(FEATURE_SSE4_1)) {
CPU = "penryn";
*Type = INTEL_CORE2;
} else if (testFeature(FEATURE_SSSE3)) {
if (testFeature(FEATURE_MOVBE)) {
CPU = "bonnell";
*Type = INTEL_BONNELL;
} else {
CPU = "core2";
*Type = INTEL_CORE2;
}
} else if (testFeature(FEATURE_64BIT)) {
CPU = "core2";
*Type = INTEL_CORE2;
} else if (testFeature(FEATURE_SSE3)) {
CPU = "yonah";
} else if (testFeature(FEATURE_SSE2)) {
CPU = "pentium-m";
} else if (testFeature(FEATURE_SSE)) {
CPU = "pentium3";
} else if (testFeature(FEATURE_MMX)) {
CPU = "pentium2";
} else {
CPU = "pentiumpro";
}
break;
}
break;
case 15: {
if (testFeature(FEATURE_64BIT)) {
CPU = "nocona";
break;
}
if (testFeature(FEATURE_SSE3)) {
CPU = "prescott";
break;
}
CPU = "pentium4";
break;
}
case 19:
switch (Model) {
// Diamond Rapids:
Expand Down
92 changes: 61 additions & 31 deletions llvm/lib/TargetParser/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
const unsigned *Features,
unsigned *Type,
unsigned *Subtype) {
StringRef CPU;
const char *CPU = 0;

switch (Family) {
case 3:
Expand Down Expand Up @@ -702,7 +702,7 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
// As found in a Summer 2010 model iMac.
case 0x1f:
case 0x2e: // Nehalem EX
case 0x2e: // Nehalem EX
CPU = "nehalem";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_NEHALEM;
Expand All @@ -723,7 +723,7 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
*Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
break;
case 0x3a:
case 0x3e: // Ivy Bridge EP
case 0x3e: // Ivy Bridge EP
CPU = "ivybridge";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_IVYBRIDGE;
Expand All @@ -750,12 +750,12 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
break;

// Skylake:
case 0x4e: // Skylake mobile
case 0x5e: // Skylake desktop
case 0x8e: // Kaby Lake mobile
case 0x9e: // Kaby Lake desktop
case 0xa5: // Comet Lake-H/S
case 0xa6: // Comet Lake-U
case 0x4e: // Skylake mobile
case 0x5e: // Skylake desktop
case 0x8e: // Kaby Lake mobile
case 0x9e: // Kaby Lake desktop
case 0xa5: // Comet Lake-H/S
case 0xa6: // Comet Lake-U
CPU = "skylake";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_SKYLAKE;
Expand Down Expand Up @@ -868,20 +868,6 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
*Subtype = X86::INTEL_COREI7_PANTHERLAKE;
break;

// Graniterapids:
case 0xad:
CPU = "graniterapids";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_GRANITERAPIDS;
break;

// Granite Rapids D:
case 0xae:
CPU = "graniterapids-d";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_GRANITERAPIDS_D;
break;

// Icelake Xeon:
case 0x6a:
case 0x6c:
Expand All @@ -904,6 +890,20 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
*Subtype = X86::INTEL_COREI7_SAPPHIRERAPIDS;
break;

// Granite rapids:
case 0xad:
CPU = "graniterapids";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_GRANITERAPIDS;
break;

// Granite Rapids D:
case 0xae:
CPU = "graniterapids-d";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_GRANITERAPIDS_D;
break;

case 0x1c: // Most 45 nm Intel Atom processors
case 0x26: // 45 nm Atom Lincroft
case 0x27: // 32 nm Atom Medfield
Expand Down Expand Up @@ -964,52 +964,82 @@ static StringRef getIntelProcessorTypeAndSubtype(unsigned Family,
CPU = "knl";
*Type = X86::INTEL_KNL;
break;

case 0x85:
CPU = "knm";
*Type = X86::INTEL_KNM;
break;

default: // Unknown family 6 CPU, try to guess.
// Don't both with Type/Subtype here, they aren't used by the caller.
// They're used above to keep the code in sync with compiler-rt.
// TODO detect tigerlake host from model
if (testFeature(X86::FEATURE_AVX512VP2INTERSECT)) {
CPU = "tigerlake";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_TIGERLAKE;
} else if (testFeature(X86::FEATURE_AVX512VBMI2)) {
CPU = "icelake-client";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
} else if (testFeature(X86::FEATURE_AVX512VBMI)) {
CPU = "cannonlake";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_CANNONLAKE;
} else if (testFeature(X86::FEATURE_AVX512BF16)) {
CPU = "cooperlake";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_COOPERLAKE;
} else if (testFeature(X86::FEATURE_AVX512VNNI)) {
CPU = "cascadelake";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_CASCADELAKE;
} else if (testFeature(X86::FEATURE_AVX512VL)) {
CPU = "skylake-avx512";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
} else if (testFeature(X86::FEATURE_CLFLUSHOPT)) {
if (testFeature(X86::FEATURE_SHA))
if (testFeature(X86::FEATURE_SHA)) {
CPU = "goldmont";
else
*Type = X86::INTEL_GOLDMONT;
} else {
CPU = "skylake";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_SKYLAKE;
}
} else if (testFeature(X86::FEATURE_ADX)) {
CPU = "broadwell";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_BROADWELL;
} else if (testFeature(X86::FEATURE_AVX2)) {
CPU = "haswell";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_HASWELL;
} else if (testFeature(X86::FEATURE_AVX)) {
CPU = "sandybridge";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
} else if (testFeature(X86::FEATURE_SSE4_2)) {
if (testFeature(X86::FEATURE_MOVBE))
if (testFeature(X86::FEATURE_MOVBE)) {
CPU = "silvermont";
else
*Type = X86::INTEL_SILVERMONT;
} else {
CPU = "nehalem";
*Type = X86::INTEL_COREI7;
*Subtype = X86::INTEL_COREI7_NEHALEM;
}
} else if (testFeature(X86::FEATURE_SSE4_1)) {
CPU = "penryn";
*Type = X86::INTEL_CORE2;
} else if (testFeature(X86::FEATURE_SSSE3)) {
if (testFeature(X86::FEATURE_MOVBE))
if (testFeature(X86::FEATURE_MOVBE)) {
CPU = "bonnell";
else
*Type = X86::INTEL_BONNELL;
} else {
CPU = "core2";
*Type = X86::INTEL_CORE2;
}
} else if (testFeature(X86::FEATURE_64BIT)) {
CPU = "core2";
*Type = X86::INTEL_CORE2;
} else if (testFeature(X86::FEATURE_SSE3)) {
CPU = "yonah";
} else if (testFeature(X86::FEATURE_SSE2)) {
Expand Down
Loading