[X86] Calculate the needed size of the feature arrays in _cpu_indicator_init and...
authorCraig Topper <craig.topper@intel.com>
Mon, 22 Jun 2020 00:30:10 +0000 (17:30 -0700)
committerCraig Topper <craig.topper@intel.com>
Mon, 22 Jun 2020 18:46:20 +0000 (11:46 -0700)
Move 0 initialization up to the caller so we don't need to know
the size.

compiler-rt/lib/builtins/cpu_model.c
llvm/include/llvm/Support/X86TargetParser.h
llvm/lib/Support/Host.cpp

index 59c6974..3207f14 100644 (file)
@@ -125,7 +125,8 @@ enum ProcessorFeatures {
   FEATURE_AVX512VNNI,
   FEATURE_AVX512BITALG,
   FEATURE_AVX512BF16,
-  FEATURE_AVX512VP2INTERSECT
+  FEATURE_AVX512VP2INTERSECT,
+  CPU_FEATURE_MAX
 };
 
 // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
@@ -493,8 +494,6 @@ static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
 
 static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
                                  unsigned *Features) {
-  Features[0] = 0;
-  Features[1] = 0;
   unsigned EAX, EBX;
 
 #define setFeature(F)                                                          \
@@ -653,7 +652,8 @@ int CONSTRUCTOR_ATTRIBUTE __cpu_indicator_init(void) {
   unsigned MaxLeaf = 5;
   unsigned Vendor;
   unsigned Model, Family;
-  unsigned Features[2];
+  const unsigned SIZE_OF_FEATURES = (CPU_FEATURE_MAX + 31) / 32;
+  unsigned Features[SIZE_OF_FEATURES] = {0};
 
   // This function needs to run just once.
   if (__cpu_model.__cpu_vendor)
@@ -670,6 +670,8 @@ int CONSTRUCTOR_ATTRIBUTE __cpu_indicator_init(void) {
 
   // Find available features.
   getAvailableFeatures(ECX, EDX, MaxLeaf, &Features[0]);
+
+  assert(SIZE_OF_FEATURES == 2);
   __cpu_model.__cpu_features[0] = Features[0];
   __cpu_features2 = Features[1];
 
index 7cdf1c5..e357d11 100644 (file)
@@ -56,7 +56,7 @@ enum ProcessorFeatures {
 #define X86_FEATURE(VAL, ENUM) \
   ENUM = VAL,
 #include "llvm/Support/X86TargetParser.def"
-
+  CPU_FEATURE_MAX
 };
 
 enum CPUKind {
index c6b12e6..e4c6b87 100644 (file)
@@ -582,7 +582,7 @@ static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
 
 static void
 getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
-                                const unsigned (&Features)[3],
+                                const unsigned *Features,
                                 unsigned *Type, unsigned *Subtype) {
   auto testFeature = [&](unsigned F) {
     return (Features[F / 32] & (1U << (F % 32))) != 0;
@@ -910,7 +910,7 @@ getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
 }
 
 static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
-                                          const unsigned (&Features)[3],
+                                          const unsigned *Features,
                                           unsigned *Type, unsigned *Subtype) {
   auto testFeature = [&](unsigned F) {
     return (Features[F / 32] & (1U << (F % 32))) != 0;
@@ -1012,10 +1012,7 @@ static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
 }
 
 static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
-                                 unsigned (&Features)[3]) {
-  Features[0] = 0;
-  Features[1] = 0;
-  Features[2] = 0;
+                                 unsigned *Features) {
   unsigned EAX, EBX;
 
   auto setFeature = [&](unsigned F) {
@@ -1157,7 +1154,7 @@ StringRef sys::getHostCPUName() {
   getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
 
   unsigned Family = 0, Model = 0;
-  unsigned Features[3] = {0, 0, 0};
+  unsigned Features[(X86::CPU_FEATURE_MAX + 31) / 32] = {0};
   detectX86FamilyModel(EAX, &Family, &Model);
   getAvailableFeatures(ECX, EDX, MaxLeaf, Features);