[PATCH] Support Microsoft __cpuid/__cpuidex intrinsics.

Rui Ueyama ruiu at google.com
Wed May 1 22:31:21 PDT 2013


Hi rnk,

Support Microsoft __cpuid/__cpuidex intrinsics.

In this patch, these functions are not implemented as intrinsic
but as regular inlined function. Including intrin.h seems to
be recommended to use the functions, and many programs using the
functions do include the header file. So I think we can avoid
implementing them as intrinsic for the sake of simplicity.

What do you think?

http://llvm-reviews.chandlerc.com/D733

Files:
  lib/Headers/CMakeLists.txt
  lib/Headers/intrin.h
  lib/Headers/module.map
  test/CodeGen/cpuid.c

Index: lib/Headers/CMakeLists.txt
===================================================================
--- lib/Headers/CMakeLists.txt
+++ lib/Headers/CMakeLists.txt
@@ -11,6 +11,7 @@
   fma4intrin.h
   fmaintrin.h
   immintrin.h
+  intrin.h
   iso646.h
   limits.h
   lzcntintrin.h
Index: lib/Headers/intrin.h
===================================================================
--- /dev/null
+++ lib/Headers/intrin.h
@@ -0,0 +1,40 @@
+/*===---- intrin.h - intrinsic functions for Microsoft C -------------------===
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *===-----------------------------------------------------------------------===
+ */
+
+#if __x86_64__ || __i386__
+
+static __inline void __cpuid(int cpu_info[4], int info_type) {
+  __asm("cpuid"
+	: "=a"(cpu_info[0]), "=b"(cpu_info[1]),
+	  "=c"(cpu_info[2]), "=d"(cpu_info[3])
+	: "a"(info_type));
+}
+
+static __inline void __cpuidex(int cpu_info[4], int info_type, int ecx_value) {
+  __asm("cpuid"
+	: "=a"(cpu_info[0]), "=b"(cpu_info[1]),
+          "=c"(cpu_info[2]), "=d"(cpu_info[3])
+	: "a"(info_type), "c"(ecx_value));
+}
+
+#endif  // __x86_64__ || __i386__
Index: lib/Headers/module.map
===================================================================
--- lib/Headers/module.map
+++ lib/Headers/module.map
@@ -141,5 +141,10 @@
       requires pclmul
       header "__wmmintrin_pclmul.h"
     }
+
+    explicit module intrin {
+      requires x86
+      header "intrin.h"
+    }
   }
 }
Index: test/CodeGen/cpuid.c
===================================================================
--- /dev/null
+++ test/CodeGen/cpuid.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 %s -triple=x86_64-unknown-unknown -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple=i386-pc-linux-gnu -emit-llvm -o - | FileCheck %s
+
+#include <cpuid.h>
+#include <intrin.h>
+
+void test_get_cpuid(void) {
+  unsigned int level = 1;
+  unsigned int eax, ebx, ecx, edx;
+  __get_cpuid(level, &eax, &ebx, &ecx, &edx);
+  // CHECK: asm "cpuid", "={ax},={bx},={cx},={dx},0
+}
+
+void test_cpuid(void) {
+  int cpu_info[4];
+  int info_type = 1;
+  __cpuid(cpu_info, info_type);
+  // CHECK: asm "cpuid", "={ax},={bx},={cx},={dx},{ax}
+}
+
+void test_cpuidex(void) {
+  int cpu_info[4];
+  int info_type = 1;
+  int ecx_value = 1;
+  __cpuidex(cpu_info, info_type, ecx_value);
+  // CHECK: asm "cpuid", "={ax},={bx},={cx},={dx},{ax},{cx}
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D733.1.patch
Type: text/x-patch
Size: 3437 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130501/5c2e54ba/attachment.bin>


More information about the cfe-commits mailing list