I wrote:
> Also, the reason we manage to set USE_AVX2_WITH_RUNTIME_CHECK
> in a multi-arch build is that what configure actually tests
> is (a) is $host_cpu x86_64 and (b) does the compiler accept
> __attribute__((target("avx2"))). On a multi-arch build,
> the ARM side spits out a warning
> '+avx2' is not a recognized feature for this target (ignoring feature)
> but it doesn't actually fail, so configure thinks that's a success.
After further contemplation, I think the best fix for this problem
is to undo configure's mistake globally, not just in checksum.c.
checksum.c is the only file using USE_AVX2_WITH_RUNTIME_CHECK today,
but that might not be true forever. Also, I think it's pure chance
that the test for that symbol succeeds in a universal build while
other similar tests don't. So what the attached v2 does is to
#undef USE_AVX2_WITH_RUNTIME_CHECK as well as the USE_AVX512
symbols in c.h, if we're not in fact building for x86_64 hardware.
We could extend the list of things to #undef there later, if it
proves necessary.
The shape of this patch is partially dictated by not wanting to
create problems for Munro's pending patch [1], but I think it's
a reasonable solution even without that consideration.
regards, tom lane
[1]
https://www.postgresql.org/message-id/flat/CA%2BhUKGL8Hs-phHPugrWM%3D5dAkcT897rXyazYzLw-Szxnzgx-rA%40mail.gmail.com
diff --git a/src/include/c.h b/src/include/c.h
index 97ed8c63f5e..23db81fa7ca 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1340,6 +1340,17 @@ typedef struct PGAlignedXLogBlock PGAlignedXLogBlock;
#if (defined(__x86_64__) || defined(_M_AMD64))
#define USE_SSE2
+#else /* ! x86_64 */
+
+/*
+ * In "universal" macOS builds, it's possible for AVX-related symbols to
+ * get defined if the build host is x86_64, but we mustn't try to build
+ * that code when cross-compiling to aarch64.
+ */
+#undef USE_AVX2_WITH_RUNTIME_CHECK
+#undef USE_AVX512_CRC32C_WITH_RUNTIME_CHECK
+#undef USE_AVX512_POPCNT_WITH_RUNTIME_CHECK
+
/*
* We use the Neon instructions if the compiler provides access to them (as
* indicated by __ARM_NEON) and we are on aarch64. While Neon support is
@@ -1348,9 +1359,10 @@ typedef struct PGAlignedXLogBlock PGAlignedXLogBlock;
* could not realistically use it there without a run-time check, which seems
* not worth the trouble for now.
*/
-#elif defined(__aarch64__) && defined(__ARM_NEON)
+#if defined(__aarch64__) && defined(__ARM_NEON)
#define USE_NEON
#endif
+#endif /* ! x86_64 */
/* ----------------------------------------------------------------
* Section 9: system-specific hacks