[PATCH] D22578: Removing the dependency on clang for now. Adding a simplified version of get_cpuid_max instead.

Alina Sbirlea via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 20 11:45:08 PDT 2016


asbirlea created this revision.
asbirlea added reviewers: llvm-commits, echristo.
Herald added a subscriber: mehdi_amini.

Removing the dependency on cpuid.h and adding the i386 check in an additional function instead.
Choice can be revisited to depend on cpuid and properly ifdef the include and usage.

https://reviews.llvm.org/D22578

Files:
  lib/Support/Host.cpp

Index: lib/Support/Host.cpp
===================================================================
--- lib/Support/Host.cpp
+++ lib/Support/Host.cpp
@@ -22,7 +22,6 @@
 #include "llvm/Support/raw_ostream.h"
 #include <string.h>
 #include <assert.h>
-#include <cpuid.h>
 
 // Include the platform-specific parts of this class.
 #ifdef LLVM_ON_UNIX
@@ -170,6 +169,38 @@
   FEATURE_EM64T
 };
 
+// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
+// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
+// support. Consequently, for i386, the presence of CPUID is checked first
+// via the corresponding eflags bit.
+static bool isCpuIdSupported() {
+#if defined(__GNUC__) || defined(__clang__)
+#if defined(__i386__)
+  int __cpuid_supported;
+  __asm__("  pushfl\n"
+          "  popl   %%eax\n"
+          "  movl   %%eax,%%ecx\n"
+          "  xorl   $0x00200000,%%eax\n"
+          "  pushl  %%eax\n"
+          "  popfl\n"
+          "  pushfl\n"
+          "  popl   %%eax\n"
+          "  movl   $0,%0\n"
+          "  cmpl   %%eax,%%ecx\n"
+          "  je     1f\n"
+          "  movl   $1,%0\n"
+          "1:"
+          : "=r"(__cpuid_supported)
+          :
+          : "eax", "ecx");
+  if (!__cpuid_supported)
+    return false;
+#endif
+  return true;
+#endif
+  return true;
+}
+
 /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
 /// the specified arguments.  If we can't run cpuid on the host, return true.
 static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
@@ -744,7 +775,7 @@
   //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
   // and simplify it to not invoke __cpuid (like cpu_model.c in
   // compiler-rt/lib/builtins/cpu_model.c?
-  if(!__get_cpuid_max(0, &Vendor))
+  if(!isCpuIdSupported())
     return "generic";
   if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX))
     return "generic";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22578.64720.patch
Type: text/x-patch
Size: 1949 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160720/83816a31/attachment.bin>


More information about the llvm-commits mailing list