Centralised architecture detection

Поиск
Список
Период
Сортировка
Искать

Centralised architecture detection

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
Вложения:
Hi,

Catching up with the timing_clock_source thread (great work!), I saw
that Andres mentioned that it'd be nice to have PG_ARCH_X86 etc macros
to standardise our detection of CPUs[1], and I remembered that I'd
already looked into that a couple of years ago and posted a patch...
and promptly forgot all about it.  Then I remembered the surprising
discovery that motivated it[2], picked up in passing while working on
early versions of my  patch, that we still haven't done
anything about:

We are not detecting x86 in several places on Visual Studio, and the
one where we fail to include src/port/atomics/arch-x86.h can't be too
great for performance.

     * pg_{read,write}_barrier() → pg_memory_barrier()!
     * pg_spin_delay() → nothing!
     * PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY → undefined

No Windows here but it's easy enough to confirm with CI: add #error to
arch-x86.h and you'll get a red MinGW task and a green Visual Studio
task.  Presumably the Windows support was either written blind or
developed on MinGW.

Here's an update of my old patch.  It just defines macros like this,
in c.h, though since then we gained port/pg_cpu.h, so perhaps it
belongs in there.  Then we can deal with the underlying
compiler-defined macros in one central place.  The names I came up
with were:

      PG_ARCH_{ARM,LOONGARCH,MIPS,PPC,RISCV,S390,SPARC,X86}
      PG_ARCH_{ARM,LOONGARCH,MIPS,PPC,RISCV,S390,SPARC,X86}_{32,64}

Lukas and John have both been doing similar sorts of things and may
have better ideas or patches, but I figured I should at least re-post
what I have.

I suppose we could also do something similar for PG_COMPILER_XXX and
PG_OS_XXX.  I've made mistakes with those before too...

[1] https://www.postgresql.org/message-id/flat/r5snevsnkyoifjqldu6gcssbnrnezpplq4ofcmekjfvzzu32dc%25405rn26itd4ubr
[2] https://www.postgresql.org/message-id/flat/CA%2BhUKGKAf_i6w7hB_3pqZXQeqn%2BixvY%2BCMps_n%3DmJ5HAatMjMw%40mail.gmail.com#4ec27ce4c67390c29528f5ef064c8b68

Re: Centralised architecture detection

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
Вложения:
Here's a better version (I'd omitted _M_AMD64 in the previous one
after testing something...).  CC'ing Bryan who might be interested in
the effects of this stuff on Windows builds.

Re: Centralised architecture detection

От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Thu, Apr 9, 2026 at 8:02 AM Thomas Munro  wrote:
> Here's an update of my old patch.  It just defines macros like this,
> in c.h, though since then we gained port/pg_cpu.h, so perhaps it
> belongs in there.

port/pg_cpu.h would seem like the natural place for something like
this, and I deliberately made that header's name not specific to one
architecture. But see below.

> Lukas and John have both been doing similar sorts of things and may
> have better ideas or patches, but I figured I should at least re-post
> what I have.

From that thread, I think the final committed version ended up with
fewer places that cared about architecture macros, and that's why it
was left out. I for one was not motivated to continue that work, but I
don't see a reason not to, either. And as you said, this might help
avoid errors of omission going forward.

--- a/src/port/pg_crc32c_sse42.c
+++ b/src/port/pg_crc32c_sse42.c
@@ -39,7 +39,7 @@ pg_comp_crc32c_sse42(pg_crc32c crc, const void
*data, size_t len)
  * and performance testing didn't show any performance gain from aligning
  * the begin address.
  */
-#ifdef __x86_64__
+#ifdef PG_ARCH_X86_64
  while (p + 8 <= pend)

That probably should have been "SIZEOF_VOID_P >= 8" to begin with.

Also, src/port/pg_cpu_x86.c currently has this hack:

#if defined(USE_SSE2) || defined(__i386__)

That's an awkward way of saying "x86 of any word size, but forget
about 32-bit MSVC because it won't get tested in the buildfarm", and
should probably use PG_ARCH_X86 from this patch. However, as currently
written, that would only work if the new macros were in c.h, to keep
the property that system headers come before (most) PG headers.

--
John Naylor
Amazon Web Services


Re: Centralised architecture detection

От:
Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Дата:
Thomas Munro  writes:

> Instead of repeating compilers' architecture macros throughout the tree
> and sometimes getting it wrong, let's detect them in one central place,
> and define our own macros of the form:
>
>   PG_ARCH_{ARM,LOONGARCH,MIPS,PPC,RISCV,S390,SPARC,X86}
>   PG_ARCH_{ARM,LOONGARCH,MIPS,PPC,RISCV,S390,SPARC,X86}_{32,64}
[...]
> diff --git a/contrib/pgcrypto/crypt-blowfish.c b/contrib/pgcrypto/crypt-blowfish.c
> index 5a1b1e10091..9c4e02e428b 100644
> --- a/contrib/pgcrypto/crypt-blowfish.c
> +++ b/contrib/pgcrypto/crypt-blowfish.c
> @@ -38,10 +38,10 @@
>  #include "px-crypt.h"
>  #include "px.h"
>  
> -#ifdef __i386__
> +#if defined(PG_ARCH_X86_32)
>  #define BF_ASM				0	/* 1 */
>  #define BF_SCALE			1
> -#elif defined(__x86_64__)
> +#elif defined(PG_ARCH_X86_64)
>  #define BF_ASM				0
>  #define BF_SCALE			1
>  #else

These could be combined into a single #ifdef PG_ARCH_X86.  Also, BF_ASM
has never been defined to anything but 0 since this file was added in
2001, and the _BF_body_r function it would call in that case has never
existed, so we could get rid of it (but that would be for a separate
patch).

- ilmari


Re: Centralised architecture detection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
I wrote:
> Nathan Bossart reminded me of this thread after I'd independently
> rediscovered the same thing [1].  I agree with standardizing on
> just one spelling of these CPU-type macros.  But I wonder why we
> should invent our own instead of standardizing on gcc's spellings
> (that is, __x86_64__ etc).  The amount of code churn required for
> this patch would drop drastically if we did it that way.  And I
> suspect it would be less likely that we'd need to fixup future patch
> submissions than if we have a homegrown standard.

Concretely, I'm imagining that we'd do more or less the attached in
c.h, and then the rest of the work would just be to remove the
not-very-large number of references to the alternative CPU symbols.

Note that I threw in an "#else #error" branch to ensure that we
successfully identify every architecture.  Even if you don't like
this naming scheme, we should do that with the original patch too.

			regards, tom lane

diff --git a/src/include/c.h b/src/include/c.h
index 97ed8c63f5e..85cf2604898 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -106,6 +106,58 @@ extern "C++"
  * ----------------------------------------------------------------
  */
 
+/*
+ * Not all compilers follow gcc's names of macros for particular target
+ * architectures.  Let's standardize on gcc's names (with trailing __),
+ * and cause those to become defined here if they are not already.
+ *
+ * Note: while this list is alphabetical, it's necessary to check _M_ARM64
+ * before _M_AMD64, because Microsoft's ARM64EC environment defines both.
+ */
+#if defined(__arm__) || defined(__arm)
+#ifndef __arm__
+#define __arm__ 1
+#endif
+#elif defined(__aarch64__) || defined(_M_ARM64)
+#ifndef __aarch64__
+#define __aarch64__ 1
+#endif
+#elif defined(__mips__)
+/* no work */
+#elif defined(__mips64__)
+/* no work */
+#elif defined(__powerpc__) || defined(__ppc__)
+#ifndef __powerpc__
+#define __powerpc__ 1
+#endif
+#elif defined(__powerpc64__) || defined(__ppc64__)
+#ifndef __powerpc64__
+#define __powerpc64__ 1
+#endif
+#elif defined(__riscv__)
+/* no work */
+#elif defined(__riscv64__)
+/* no work */
+#elif defined(__s390__)
+/* no work */
+#elif defined(__s390x__)
+/* no work */
+#elif defined(__sparc__) || defined(__sparc)
+#ifndef __sparc__
+#define __sparc__ 1
+#endif
+#elif defined(__i386__) || defined (__386) || defined(_M_IX86)
+#ifndef __i386__
+#define __i386__ 1
+#endif
+#elif defined(__x86_64__) || defined(__x86_64) || defined (__amd64) || defined(_M_AMD64)
+#ifndef __x86_64__
+#define __x86_64__ 1
+#endif
+#else
+#error "cannot identify target architecture"
+#endif
+
 /*
  * Disable "inline" if PG_FORCE_DISABLE_INLINE is defined.
  * This is used to work around compiler bugs and might also be useful for
@@ -1337,7 +1389,7 @@ typedef struct PGAlignedXLogBlock PGAlignedXLogBlock;
  * SSE2 instructions are part of the spec for the 64-bit x86 ISA. We assume
  * that compilers targeting this architecture understand SSE2 intrinsics.
  */
-#if (defined(__x86_64__) || defined(_M_AMD64))
+#if defined(__x86_64__)
 #define USE_SSE2
 
 /*

Re: Centralised architecture detection

От:
Nico Williams <nico@cryptonector.com>
Дата:
On Wed, Jun 03, 2026 at 05:08:55PM -0400, Tom Lane wrote:
> I wrote:
> > Nathan Bossart reminded me of this thread after I'd independently
> > rediscovered the same thing [1].  I agree with standardizing on
> > just one spelling of these CPU-type macros.  But I wonder why we
> > should invent our own instead of standardizing on gcc's spellings
> > (that is, __x86_64__ etc).  The amount of code churn required for
> > this patch would drop drastically if we did it that way.  And I
> > suspect it would be less likely that we'd need to fixup future patch
> > submissions than if we have a homegrown standard.
> 
> Concretely, I'm imagining that we'd do more or less the attached in
> c.h, and then the rest of the work would just be to remove the
> not-very-large number of references to the alternative CPU symbols.

Can a pre-processor make it an error for users to define __ macros?


Re: Centralised architecture detection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Nico Williams  writes:
> On Wed, Jun 03, 2026 at 05:08:55PM -0400, Tom Lane wrote:
>> Concretely, I'm imagining that we'd do more or less the attached in
>> c.h, and then the rest of the work would just be to remove the
>> not-very-large number of references to the alternative CPU symbols.

> Can a pre-processor make it an error for users to define __ macros?

I don't believe so.  We have done similar things elsewhere, eg
before 25f36066d we had this in solaris.h:

/*
 * Sort this out for all operating systems some time.  The __xxx
 * symbols are defined on both GCC and Solaris CC, although GCC
 * doesn't document them.  The __xxx__ symbols are only on GCC.
 */
#if defined(__i386) && !defined(__i386__)
#define __i386__
#endif

#if defined(__amd64) && !defined(__amd64__)
#define __amd64__
#endif

#if defined(__x86_64) && !defined(__x86_64__)
#define __x86_64__
#endif

#if defined(__sparc) && !defined(__sparc__)
#define __sparc__
#endif

Of course that only proves that Sun Studio didn't complain.

			regards, tom lane


Re: Centralised architecture detection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
I wrote:
> Concretely, I'm imagining that we'd do more or less the attached in
> c.h, and then the rest of the work would just be to remove the
> not-very-large number of references to the alternative CPU symbols.

Here's a fleshed-out (and now actually lightly-tested) version
of that.

			regards, tom lane

From 7dd6c9a9c898d9592999f6224beed93dbf853f70 Mon Sep 17 00:00:00 2001
From: Tom Lane 
Date: Tue, 16 Jun 2026 13:53:28 -0400
Subject: [PATCH v1] Clean up inconsistencies in CPU-detection macros.

In various places we depend on compiler-defined macros like __x86_64__
to guard CPU-type-specific code.  However, those macros aren't very
well standardized; in particular, it emerges that MSVC doesn't define
any of the ones gcc does, but has its own.  We were not coping with
that consistently, with the result that we're missing some useful
CPU-dependent optimizations in MSVC builds.  There are also some
places that are checking randomly-different spellings that may
have been the only ones recognized by some old compilers, but we
weren't doing that consistently either.

Let's standardize on using gcc's long-form spellings (with trailing
underscores), after putting a stanza into c.h that ensures that these
spellings are defined even when the compiler provides some other one.

I put an "#else #error" branch into the c.h addition so that we'll
get an error if the compiler provides none of the symbols we're
expecting.  That might be best removed in the end, since it might
annoy people trying to port to some new CPU type.  But for right
now it seems like a good idea, in case we've missed some common
variant spelling.

In addition to enabling some optimizations we previously missed on
MSVC, this cleans up a thinko.  Several places used "_M_X64" in the
apparent belief that that's MSVC's equivalent to __x86_64__, but
it's not: it will also get defined on some but not all ARM64 builds.

Also, guard the x86_feature_available() stuff in pg_cpu.[hc] with
	#if defined(__x86_64__) || defined(__i386__)
which seems like a more natural way of specifying what it applies to.

This builds on some previous work by Thomas Munro, but it requires
much less code churn because it re-uses gcc's names for the CPU-type
macros instead of inventing our own.

Author: Tom Lane 
Discussion: https://postgr.es/m/CA+hUKGL8Hs-phHPugrWM=5dAkcT897rXyazYzLw-Szxnzgx-rA@mail.gmail.com
Discussion: https://postgr.es/m/3035145.1780503430@sss.pgh.pa.us
---
 src/common/d2s.c                     |  2 +-
 src/include/c.h                      | 58 +++++++++++++++++++++++++++-
 src/include/port/atomics.h           |  6 +--
 src/include/port/atomics/arch-x86.h  |  4 +-
 src/include/port/pg_bitutils.h       |  4 +-
 src/include/port/pg_cpu.h            |  4 +-
 src/include/portability/instr_time.h |  2 +-
 src/include/storage/s_lock.h         | 10 ++---
 src/port/pg_cpu_x86.c                |  6 +--
 9 files changed, 76 insertions(+), 20 deletions(-)

diff --git a/src/common/d2s.c b/src/common/d2s.c
index 34e7cd33bf9..0a9cb83fc49 100644
--- a/src/common/d2s.c
+++ b/src/common/d2s.c
@@ -53,7 +53,7 @@
  * alignment concerns that apply elsewhere.
  */
 #if !defined(HAVE_INT128) && defined(_MSC_VER) \
-	&& !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64)
+	&& !defined(RYU_ONLY_64_BIT_OPS) && defined(__x86_64__)
 #define HAS_64_BIT_INTRINSICS
 #endif
 
diff --git a/src/include/c.h b/src/include/c.h
index f32989a6331..ef4dc5d1e63 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -106,6 +106,62 @@ extern "C++"
  * ----------------------------------------------------------------
  */
 
+/*
+ * Not all compilers follow gcc's names of macros for particular target
+ * architectures.  Let's standardize on gcc's names (with trailing __),
+ * and cause those to become defined here if they are not already.
+ *
+ * Note: while this list is alphabetical, it's necessary to check _M_ARM64
+ * before _M_AMD64, because Microsoft's ARM64EC environment defines both.
+ */
+#if defined(__arm__) || defined(__arm)
+#ifndef __arm__
+#define __arm__ 1
+#endif
+#elif defined(__aarch64__) || defined(_M_ARM64)
+#ifndef __aarch64__
+#define __aarch64__ 1
+#endif
+#elif defined(__loongarch64__) || defined(__loongarch64)
+#ifndef __loongarch64__
+#define __loongarch64__ 1
+#endif
+#elif defined(__mips__)
+/* no work */
+#elif defined(__mips64__)
+/* no work */
+#elif defined(__powerpc__) || defined(__ppc__)
+#ifndef __powerpc__
+#define __powerpc__ 1
+#endif
+#elif defined(__powerpc64__) || defined(__ppc64__)
+#ifndef __powerpc64__
+#define __powerpc64__ 1
+#endif
+#elif defined(__riscv__)
+/* no work */
+#elif defined(__riscv64__)
+/* no work */
+#elif defined(__s390__)
+/* no work */
+#elif defined(__s390x__)
+/* no work */
+#elif defined(__sparc__) || defined(__sparc)
+#ifndef __sparc__
+#define __sparc__ 1
+#endif
+#elif defined(__i386__) || defined (__i386) || defined(_M_IX86)
+#ifndef __i386__
+#define __i386__ 1
+#endif
+#elif defined(__x86_64__) || defined(__x86_64) || defined (__amd64) || defined(_M_AMD64)
+#ifndef __x86_64__
+#define __x86_64__ 1
+#endif
+#else
+#error "cannot identify target architecture"
+#endif
+
 /*
  * Disable "inline" if PG_FORCE_DISABLE_INLINE is defined.
  * This is used to work around compiler bugs and might also be useful for
@@ -1337,7 +1393,7 @@ typedef struct PGAlignedXLogBlock PGAlignedXLogBlock;
  * SSE2 instructions are part of the spec for the 64-bit x86 ISA. We assume
  * that compilers targeting this architecture understand SSE2 intrinsics.
  */
-#if (defined(__x86_64__) || defined(_M_AMD64))
+#if defined(__x86_64__)
 #define USE_SSE2
 
 #else							/* ! x86_64 */
diff --git a/src/include/port/atomics.h b/src/include/port/atomics.h
index d8b1d20fe60..4a26fe0039f 100644
--- a/src/include/port/atomics.h
+++ b/src/include/port/atomics.h
@@ -63,11 +63,11 @@
  * compiler barrier.
  *
  */
-#if defined(__arm__) || defined(__arm) || defined(__aarch64__)
+#if defined(__arm__) || defined(__aarch64__)
 #include "port/atomics/arch-arm.h"
-#elif defined(__i386__) || defined(__i386) || defined(__x86_64__)
+#elif defined(__i386__) || defined(__x86_64__)
 #include "port/atomics/arch-x86.h"
-#elif defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__) || defined(__powerpc64__)
+#elif defined(__powerpc__) || defined(__powerpc64__)
 #include "port/atomics/arch-ppc.h"
 #endif
 
diff --git a/src/include/port/atomics/arch-x86.h b/src/include/port/atomics/arch-x86.h
index 8cfe402c339..7637bce3e0b 100644
--- a/src/include/port/atomics/arch-x86.h
+++ b/src/include/port/atomics/arch-x86.h
@@ -32,7 +32,7 @@
  */
 
 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
-#if defined(__i386__) || defined(__i386)
+#if defined(__i386__)
 #define pg_memory_barrier_impl()		\
 	__asm__ __volatile__ ("lock; addl $0,0(%%esp)" : : : "memory", "cc")
 #elif defined(__x86_64__)
@@ -238,6 +238,6 @@ pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
 /*
  * 8 byte reads / writes have single-copy atomicity on all x86-64 cpus.
  */
-#if defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) /* gcc, msvc */
+#if defined(__x86_64__)
 #define PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY
 #endif /* 8 byte single-copy atomicity */
diff --git a/src/include/port/pg_bitutils.h b/src/include/port/pg_bitutils.h
index 7a00d197013..2864ba431db 100644
--- a/src/include/port/pg_bitutils.h
+++ b/src/include/port/pg_bitutils.h
@@ -82,7 +82,7 @@ pg_leftmost_one_pos64(uint64 word)
 #error "cannot find integer type of the same size as uint64_t"
 #endif
 
-#elif defined(_MSC_VER) && (defined(_M_AMD64) || defined(_M_ARM64))
+#elif defined(_MSC_VER) && (defined(__x86_64__) || defined(__aarch64__))
 	unsigned long result;
 	bool		non_zero;
 
@@ -155,7 +155,7 @@ pg_rightmost_one_pos64(uint64 word)
 #error "cannot find integer type of the same size as uint64_t"
 #endif
 
-#elif defined(_MSC_VER) && (defined(_M_AMD64) || defined(_M_ARM64))
+#elif defined(_MSC_VER) && (defined(__x86_64__) || defined(__aarch64__))
 	unsigned long result;
 	bool		non_zero;
 
diff --git a/src/include/port/pg_cpu.h b/src/include/port/pg_cpu.h
index 566ed7a16e3..22aaa1aa5bc 100644
--- a/src/include/port/pg_cpu.h
+++ b/src/include/port/pg_cpu.h
@@ -13,7 +13,7 @@
 #ifndef PG_CPU_H
 #define PG_CPU_H
 
-#if defined(USE_SSE2) || defined(__i386__)
+#if defined(__x86_64__) || defined(__i386__)
 
 typedef enum X86FeatureId
 {
@@ -58,6 +58,6 @@ x86_feature_available(X86FeatureId feature)
 
 extern uint32 x86_tsc_frequency_khz(char *source, size_t source_size);
 
-#endif							/* defined(USE_SSE2) || defined(__i386__) */
+#endif							/* defined(__x86_64__) || defined(__i386__) */
 
 #endif							/* PG_CPU_H */
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index 655f8737b6f..826cc202847 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -95,7 +95,7 @@ typedef struct instr_time
  * PG_INSTR_TSC_CLOCK controls whether the TSC clock source is compiled in, and
  * potentially used based on timing_tsc_enabled.
  */
-#if defined(__x86_64__) || defined(_M_X64)
+#if defined(__x86_64__)
 #define PG_INSTR_TICKS_TO_NS 1
 #define PG_INSTR_TSC_CLOCK 1
 #elif defined(WIN32)
diff --git a/src/include/storage/s_lock.h b/src/include/storage/s_lock.h
index c9e52511990..96fcaaac01e 100644
--- a/src/include/storage/s_lock.h
+++ b/src/include/storage/s_lock.h
@@ -247,7 +247,7 @@ spin_delay(void)
  * We use the int-width variant of the builtin because it works on more chips
  * than other widths.
  */
-#if defined(__arm__) || defined(__arm) || defined(__aarch64__)
+#if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_GCC__SYNC_INT32_TAS
 #define HAS_TEST_AND_SET
 
@@ -287,7 +287,7 @@ spin_delay(void)
 
 #endif	 /* __aarch64__ */
 #endif	 /* HAVE_GCC__SYNC_INT32_TAS */
-#endif	 /* __arm__ || __arm || __aarch64__ */
+#endif	 /* __arm__ || __aarch64__ */
 
 
 /* S/390 and S/390x Linux (32- and 64-bit zSeries) */
@@ -391,7 +391,7 @@ do \
 
 
 /* PowerPC */
-#if defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__) || defined(__powerpc64__)
+#if defined(__powerpc__) || defined(__powerpc64__)
 #define HAS_TEST_AND_SET
 
 typedef unsigned int slock_t;
@@ -602,7 +602,7 @@ typedef LONG slock_t;
 
 #define SPIN_DELAY() spin_delay()
 
-#ifdef _M_ARM64
+#ifdef __aarch64__
 static __forceinline void
 spin_delay(void)
 {
@@ -633,7 +633,7 @@ spin_delay(void)
 
 #include 
 
-#ifdef _M_ARM64
+#ifdef __aarch64__
 
 /* _ReadWriteBarrier() is insufficient on non-TSO architectures. */
 #pragma intrinsic(_InterlockedExchange)
diff --git a/src/port/pg_cpu_x86.c b/src/port/pg_cpu_x86.c
index 0405ba19f6f..b050677f717 100644
--- a/src/port/pg_cpu_x86.c
+++ b/src/port/pg_cpu_x86.c
@@ -19,7 +19,7 @@
 #include "postgres_fe.h"
 #endif
 
-#if defined(USE_SSE2) || defined(__i386__)
+#if defined(__x86_64__) || defined(__i386__)
 
 #ifdef _MSC_VER
 #include 
@@ -287,10 +287,10 @@ x86_hypervisor_tsc_frequency_khz(void)
 	return 0;
 }
 
-#else							/* defined(USE_SSE2) || defined(__i386__) */
+#else							/* defined(__x86_64__) || defined(__i386__) */
 
 /* prevent linker complaints about empty module */
 extern int	pg_cpu_x86_dummy_variable;
 int			pg_cpu_x86_dummy_variable = 0;
 
-#endif							/* ! (USE_SSE2 || __i386__) */
+#endif							/* ! (__x86_64__ || __i386__) */
-- 
2.52.0

Re: Centralised architecture detection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
> I wrote:
>> Concretely, I'm imagining that we'd do more or less the attached in
>> c.h, and then the rest of the work would just be to remove the
>> not-very-large number of references to the alternative CPU symbols.

> Here's a fleshed-out (and now actually lightly-tested) version
> of that.

Pushed.  I shall now watch the buildfarm from a safe distance.

			regards, tom lane


Re: Centralised architecture detection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
I wrote:
> Pushed.  I shall now watch the buildfarm from a safe distance.

Sure enough, greenfly is not happy:

ccache /scratch/opt/llvm-22/bin/clang -Isrc/port/libpgport_srv.a.p -Isrc/include -I../pgsql/src/include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O2 -g -fno-strict-aliasing -fwrapv -fexcess-precision=standard -D_GNU_SOURCE -Wpointer-arith -Werror=vla -Werror=unguarded-availability-new -Wmissing-format-attribute -Wcast-function-type -Wformat-security -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes -Wimplicit-fallthrough -Wdeclaration-after-statement -Wmissing-variable-declarations -Wno-unused-command-line-argument -Wno-compound-token-split-by-macro -Wno-format-truncation -Wno-cast-function-type-strict -march=rv64gcv -fPIC -DBUILDING_DLL -MD -MQ src/port/libpgport_srv.a.p/bsearch_arg.c.o -MF src/port/libpgport_srv.a.p/bsearch_arg.c.o.d -o src/port/libpgport_srv.a.p/bsearch_arg.c.o -c ../pgsql/src/port/bsearch_arg.c
In file included from ../pgsql/src/port/bsearch_arg.c:36:
../pgsql/src/include/c.h:162:2: error: "cannot identify target architecture"
  162 | #error "cannot identify target architecture"
      |  ^
1 error generated.

So I was wrong to guess that every riscv64 platform predefines
__riscv64__.  Greg, could you check what predefined architecture
symbols that compiler does supply?  I'm tempted to blindly guess
that __riscv64 will work, but I'd rather not guess.

To save you having to look it up, something like this should
do the trick:

clang -dM -E - clang-predefined-macros

			regards, tom lane


Re: Centralised architecture detection

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
On Wed, Jul 1, 2026 at 9:31 AM Tom Lane  wrote:
> So I was wrong to guess that every riscv64 platform predefines
> __riscv64__.  Greg, could you check what predefined architecture
> symbols that compiler does supply?  I'm tempted to blindly guess
> that __riscv64 will work, but I'd rather not guess.

No riscv here, but poking around, it looks like it's supposed to be
__riscv, and then __riscv_xlen == 32 or 64, or perhaps pointer size
check?  Not like the others, but this seems to have come down from the
RISCV project.

https://lists.riscv.org/g/sig-toolchains/attachment/688/0/riscv-toolchain.pdf
https://github.com/riscv-non-isa/riscv-c-api-doc/blob/main/src/c-api.adoc

It looks like they didn't want __riscv32 and __riscv64?

https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/E8EO-Fd4t3s


Re: Centralised architecture detection

От:
"Greg Burd" <greg@burd.me>
Дата:

On Tue, Jun 30, 2026, at 9:11 PM, Tom Lane wrote:
> Thomas Munro  writes:
>> It looks like they didn't want __riscv32 and __riscv64?
>> https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/E8EO-Fd4t3s
>
> Sigh ... another project that is convinced that they're smarter than
> everybody else and conforming to common practice is an anti-pattern.
>
> Based on that thread, I'm thinking
>
> ...
> #elif defined(__riscv)
> #if SIZEOF_VOID_P == 8
> #define __riscv64__ 1
> #else
> #define __riscv__ 1
> #endif
> #elif defined(__s390__)
> ...

I don't see SIZEOF_VOID_P but I do find:

#define __SIZEOF_POINTER__ 8

-greg

> I'd rather rely on our own pointer-size determination than YA
> magic compiler-defined symbol.
>
> 			regards, tom lane


Re: Centralised architecture detection

От:
"Greg Burd" <greg@burd.me>
Дата:

On Wed, Jul 1, 2026, at 9:20 AM, Tom Lane wrote:
> "Greg Burd"  writes:
>> On Tue, Jun 30, 2026, at 9:11 PM, Tom Lane wrote:
>>> Based on that thread, I'm thinking
>>> #if SIZEOF_VOID_P == 8
>
>> I don't see SIZEOF_VOID_P but I do find:
>> #define __SIZEOF_POINTER__ 8
>
> SIZEOF_VOID_P is set up by autoconf/meson via pg_config.h.

facepalm, of course! :)

> So it will be available here.  I'd rather use our own
> symbol because, if other arches emerge with similar issues,
> we can be sure of having a common pattern to follow.
>
> I do see __SIZEOF_POINTER__ getting predefined locally
> (in recent gcc and clang on x86_64), but it's far from clear
> to me how standard, or well-documented, that macro is.
>
> 			regards, tom lane

Was there anything else you needed from me on this one?

best.

-greg


Re: Centralised architecture detection

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Greg Burd"  writes:
> Was there anything else you needed from me on this one?

Nope, looks like all the riscv BF animals are green again,
so we're good.  Thanks for helping!

			regards, tom lane


FAQ