v11-0002-Check-for-HAVE__CPUIDEX-and-HAVE__GET_CPUID_COUN.patch
application/octet-stream
Filename: v11-0002-Check-for-HAVE__CPUIDEX-and-HAVE__GET_CPUID_COUN.patch
Type: application/octet-stream
Part: 3
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v11-0002
Subject: Check for HAVE__CPUIDEX and HAVE__GET_CPUID_COUNT separately
| File | + | − |
|---|---|---|
| configure | 13 | 9 |
| configure.ac | 17 | 13 |
| meson.build | 9 | 3 |
| src/port/pg_cpu_x86.c | 5 | 5 |
From 73a159af80bfdac8c9c9afc678859bd3c3fc4c9c Mon Sep 17 00:00:00 2001
From: Lukas Fittl <lukas@fittl.com>
Date: Sat, 31 Jan 2026 08:49:46 -0800
Subject: [PATCH v11 2/7] Check for HAVE__CPUIDEX and HAVE__GET_CPUID_COUNT
separately
Previously we would only check for the availability of __cpuidex if
the related __get_cpuid_count was not available on a platform. But there
are cases where we want to be able to call __cpuidex as the only viable
option, specifically, when accessing a high leaf like VM Hypervisor
information (0x40000000), which __get_cpuid_count does not allow.
This will be used in an future commit to access Hypervisor information
about the TSC frequency of x86 CPUs, where available.
Note that __cpuidex is defined in cpuid.h for GCC/clang, but in intrin.h
for MSVC. Because we now set HAVE__CPUIDEX for GCC/clang when available,
adjust existing code to check for _MSC_VER when including intrin.h.
Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion: https://www.postgresql.org/message-id/flat/20200612232810.f46nbqkdhbutzqdg%40alap3.anarazel.de
---
configure | 22 +++++++++++++---------
configure.ac | 30 +++++++++++++++++-------------
meson.build | 12 +++++++++---
src/port/pg_cpu_x86.c | 10 +++++-----
4 files changed, 44 insertions(+), 30 deletions(-)
diff --git a/configure b/configure
index 42621ecd051..8279bdd8095 100755
--- a/configure
+++ b/configure
@@ -17657,7 +17657,8 @@ $as_echo "#define HAVE__CPUID 1" >>confdefs.h
fi
fi
-# Check for __get_cpuid_count() and __cpuidex() in a similar fashion.
+# Check for __get_cpuid_count() and __cpuidex() separately, since we sometimes
+# need __cpuidex() even if __get_cpuid_count() is available.
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __get_cpuid_count" >&5
$as_echo_n "checking for __get_cpuid_count... " >&6; }
if ${pgac_cv__get_cpuid_count+:} false; then :
@@ -17690,21 +17691,25 @@ if test x"$pgac_cv__get_cpuid_count" = x"yes"; then
$as_echo "#define HAVE__GET_CPUID_COUNT 1" >>confdefs.h
-else
- # __cpuidex()
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for __cpuidex" >&5
+fi
+# __cpuidex()
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __cpuidex" >&5
$as_echo_n "checking for __cpuidex... " >&6; }
if ${pgac_cv__cpuidex+:} false; then :
$as_echo_n "(cached) " >&6
else
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
-#include <intrin.h>
+#ifdef _MSC_VER
+ #include <intrin.h>
+ #else
+ #include <cpuid.h>
+ #endif
int
main ()
{
-unsigned int exx[4] = {0, 0, 0, 0};
- __cpuidex(exx, 7, 0);
+int exx[4] = {0, 0, 0, 0};
+ __cpuidex(exx, 7, 0);
;
return 0;
@@ -17720,11 +17725,10 @@ rm -f core conftest.err conftest.$ac_objext \
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__cpuidex" >&5
$as_echo "$pgac_cv__cpuidex" >&6; }
- if test x"$pgac_cv__cpuidex" = x"yes"; then
+if test x"$pgac_cv__cpuidex" = x"yes"; then
$as_echo "#define HAVE__CPUIDEX 1" >>confdefs.h
- fi
fi
# Check for XSAVE intrinsics
diff --git a/configure.ac b/configure.ac
index 61ec895d23c..b5e6bbcb126 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2104,7 +2104,8 @@ else
fi
fi
-# Check for __get_cpuid_count() and __cpuidex() in a similar fashion.
+# Check for __get_cpuid_count() and __cpuidex() separately, since we sometimes
+# need __cpuidex() even if __get_cpuid_count() is available.
AC_CACHE_CHECK([for __get_cpuid_count], [pgac_cv__get_cpuid_count],
[AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <cpuid.h>],
[[unsigned int exx[4] = {0, 0, 0, 0};
@@ -2114,18 +2115,21 @@ AC_CACHE_CHECK([for __get_cpuid_count], [pgac_cv__get_cpuid_count],
[pgac_cv__get_cpuid_count="no"])])
if test x"$pgac_cv__get_cpuid_count" = x"yes"; then
AC_DEFINE(HAVE__GET_CPUID_COUNT, 1, [Define to 1 if you have __get_cpuid_count.])
-else
- # __cpuidex()
- AC_CACHE_CHECK([for __cpuidex], [pgac_cv__cpuidex],
- [AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <intrin.h>],
- [[unsigned int exx[4] = {0, 0, 0, 0};
- __cpuidex(exx, 7, 0);
- ]])],
- [pgac_cv__cpuidex="yes"],
- [pgac_cv__cpuidex="no"])])
- if test x"$pgac_cv__cpuidex" = x"yes"; then
- AC_DEFINE(HAVE__CPUIDEX, 1, [Define to 1 if you have __cpuidex.])
- fi
+fi
+# __cpuidex()
+AC_CACHE_CHECK([for __cpuidex], [pgac_cv__cpuidex],
+[AC_LINK_IFELSE([AC_LANG_PROGRAM([#ifdef _MSC_VER
+ #include <intrin.h>
+ #else
+ #include <cpuid.h>
+ #endif],
+ [[int exx[4] = {0, 0, 0, 0};
+ __cpuidex(exx, 7, 0);
+ ]])],
+ [pgac_cv__cpuidex="yes"],
+ [pgac_cv__cpuidex="no"])])
+if test x"$pgac_cv__cpuidex" = x"yes"; then
+ AC_DEFINE(HAVE__CPUIDEX, 1, [Define to 1 if you have __cpuidex.])
fi
# Check for XSAVE intrinsics
diff --git a/meson.build b/meson.build
index 2df54409ca6..d22ccf8f238 100644
--- a/meson.build
+++ b/meson.build
@@ -2121,7 +2121,8 @@ elif cc.links('''
endif
-# Check for __get_cpuid_count() and __cpuidex() in a similar fashion.
+# Check for __get_cpuid_count() and __cpuidex() separately, since we sometimes
+# need __cpuidex() even if __get_cpuid_count() is available.
if cc.links('''
#include <cpuid.h>
int main(int arg, char **argv)
@@ -2132,11 +2133,16 @@ if cc.links('''
''', name: '__get_cpuid_count',
args: test_c_args)
cdata.set('HAVE__GET_CPUID_COUNT', 1)
-elif cc.links('''
+endif
+if cc.links('''
+ #ifdef _MSC_VER
#include <intrin.h>
+ #else
+ #include <cpuid.h>
+ #endif
int main(int arg, char **argv)
{
- unsigned int exx[4] = {0, 0, 0, 0};
+ int exx[4] = {0, 0, 0, 0};
__cpuidex(exx, 7, 0);
}
''', name: '__cpuidex',
diff --git a/src/port/pg_cpu_x86.c b/src/port/pg_cpu_x86.c
index 0fe4753eecc..7b01c17750c 100644
--- a/src/port/pg_cpu_x86.c
+++ b/src/port/pg_cpu_x86.c
@@ -17,12 +17,12 @@
#if defined(USE_SSE2) || defined(__i386__)
-#if defined(HAVE__GET_CPUID) || defined(HAVE__GET_CPUID_COUNT)
-#include <cpuid.h>
-#endif
-
-#if defined(HAVE__CPUID) || defined(HAVE__CPUIDEX)
+#if defined(HAVE__CPUID) || defined(HAVE__GET_CPUID) || defined(HAVE__GET_CPUID_COUNT) || defined(HAVE__CPUIDEX)
+#if defined(_MSC_VER)
#include <intrin.h>
+#else
+#include <cpuid.h>
+#endif /* defined(_MSC_VER) */
#endif
#ifdef HAVE_XSAVE_INTRINSICS
--
2.47.1