From 99c5aa3e0d596c21f75c6dbe80a9456c58d58196 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 3 Apr 2026 20:08:23 +0700 Subject: [PATCH 1/2] check for __has_attribute(x) instead of only basing existence on ZEND_GCC_VERSION --- Zend/zend_cpuinfo.h | 6 +++--- Zend/zend_portability.h | 10 +++++----- ext/opcache/jit/zend_jit_ir.c | 2 +- ext/opcache/zend_shared_alloc.h | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Zend/zend_cpuinfo.h b/Zend/zend_cpuinfo.h index 7e53ba654dd41..b730538472fe4 100644 --- a/Zend/zend_cpuinfo.h +++ b/Zend/zend_cpuinfo.h @@ -273,7 +273,7 @@ static zend_always_inline int zend_cpu_supports_avx512_vbmi(void) { #endif /* __builtin_cpu_supports has pclmul from gcc9 */ -#if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000)) +#if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && (!defined(__GNUC__) || defined(__clang__) || (ZEND_GCC_VERSION >= 9000)) ZEND_NO_SANITIZE_ADDRESS static inline int zend_cpu_supports_pclmul(void) { #ifdef PHP_HAVE_BUILTIN_CPU_INIT @@ -287,8 +287,8 @@ static inline int zend_cpu_supports_pclmul(void) { } #endif -/* __builtin_cpu_supports has cldemote from gcc11 */ -#if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000) +/* __builtin_cpu_supports has cldemote from gcc11 and clang 19 */ +#if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && ((defined(__clang__) && (__clang_major__ >= 19)) || (!defined(__clang__) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000))) ZEND_NO_SANITIZE_ADDRESS static inline int zend_cpu_supports_cldemote(void) { #ifdef PHP_HAVE_BUILTIN_CPU_INIT diff --git a/Zend/zend_portability.h b/Zend/zend_portability.h index c8a6dfa871b5a..5cc4a5eacf75c 100644 --- a/Zend/zend_portability.h +++ b/Zend/zend_portability.h @@ -272,7 +272,7 @@ char *alloca(); # define ZEND_ATTRIBUTE_NODISCARD #endif -#if ZEND_GCC_VERSION >= 3000 +#if ZEND_GCC_VERSION >= 3000 || __has_attribute(const) # define ZEND_ATTRIBUTE_CONST __attribute__((const)) #else # define ZEND_ATTRIBUTE_CONST @@ -314,7 +314,7 @@ char *alloca(); # define ZEND_ATTRIBUTE_NONNULL_ARGS(...) #endif -#if defined(__GNUC__) && ZEND_GCC_VERSION >= 4003 +#if (defined(__GNUC__) && ZEND_GCC_VERSION >= 4003) || __has_attribute(cold) # define ZEND_COLD __attribute__((cold)) # ifdef __OPTIMIZE__ # define ZEND_OPT_SIZE __attribute__((optimize("Os"))) @@ -329,7 +329,7 @@ char *alloca(); # define ZEND_OPT_SPEED #endif -#if defined(__GNUC__) && ZEND_GCC_VERSION >= 5000 +#if (defined(__GNUC__) && ZEND_GCC_VERSION >= 5000) || (__has_attribute(unused) && __has_attribute(cold)) # define ZEND_ATTRIBUTE_UNUSED_LABEL __attribute__((unused)); # define ZEND_ATTRIBUTE_COLD_LABEL __attribute__((cold)); #else @@ -653,7 +653,7 @@ extern "C++" { #endif /* Do not use for conditional declaration of API functions! */ -#if defined(ZEND_INTRIN_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000)) +#if defined(ZEND_INTRIN_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || defined(__clang__) || (ZEND_GCC_VERSION >= 9000)) /* __builtin_cpu_supports has pclmul from gcc9 */ # define ZEND_INTRIN_PCLMUL_FUNC_PROTO 1 #elif defined(ZEND_INTRIN_PCLMUL_RESOLVER) @@ -679,7 +679,7 @@ extern "C++" { #endif /* Do not use for conditional declaration of API functions! */ -#if defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000)) +#if defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || defined(__clang__) || (ZEND_GCC_VERSION >= 9000)) /* __builtin_cpu_supports has pclmul from gcc9 */ # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PROTO 1 #elif defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER) diff --git a/ext/opcache/jit/zend_jit_ir.c b/ext/opcache/jit/zend_jit_ir.c index a0e0ed46128c7..7f9e877d003f8 100644 --- a/ext/opcache/jit/zend_jit_ir.c +++ b/ext/opcache/jit/zend_jit_ir.c @@ -3396,7 +3396,7 @@ static void zend_jit_setup(bool reattached) if (zend_cpu_supports_avx()) { allowed_opt_flags |= ZEND_JIT_CPU_AVX; } -# if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000) +# if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && ((defined(__clang__) && (__clang_major__ >= 19)) || (!defined(__clang__) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000))) if (zend_cpu_supports_cldemote()) { default_mflags |= IR_X86_CLDEMOTE; } diff --git a/ext/opcache/zend_shared_alloc.h b/ext/opcache/zend_shared_alloc.h index 108349b13f816..3ad804f24c068 100644 --- a/ext/opcache/zend_shared_alloc.h +++ b/ext/opcache/zend_shared_alloc.h @@ -166,7 +166,7 @@ typedef union _align_test { zend_long lng; } align_test; -#if ZEND_GCC_VERSION >= 2000 +#if ZEND_GCC_VERSION >= 2000 || defined(__clang__) # define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test)) #else # define PLATFORM_ALIGNMENT (sizeof(align_test)) From 778bdd5a0a40152366f0d8a85811afc23dc6ab33 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 3 Apr 2026 21:36:16 +0700 Subject: [PATCH 2/2] @arnaud-lb suggestion, define HAVE_ZEND_CPU_SUPPORTS_CLDEMOTE to deduplicate lengthy #if checks --- Zend/zend_cpuinfo.h | 1 + ext/opcache/jit/zend_jit_ir.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Zend/zend_cpuinfo.h b/Zend/zend_cpuinfo.h index b730538472fe4..c80061ec775a6 100644 --- a/Zend/zend_cpuinfo.h +++ b/Zend/zend_cpuinfo.h @@ -289,6 +289,7 @@ static inline int zend_cpu_supports_pclmul(void) { /* __builtin_cpu_supports has cldemote from gcc11 and clang 19 */ #if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && ((defined(__clang__) && (__clang_major__ >= 19)) || (!defined(__clang__) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000))) +#define HAVE_ZEND_CPU_SUPPORTS_CLDEMOTE 1 ZEND_NO_SANITIZE_ADDRESS static inline int zend_cpu_supports_cldemote(void) { #ifdef PHP_HAVE_BUILTIN_CPU_INIT diff --git a/ext/opcache/jit/zend_jit_ir.c b/ext/opcache/jit/zend_jit_ir.c index 7f9e877d003f8..74e3d46bae2a7 100644 --- a/ext/opcache/jit/zend_jit_ir.c +++ b/ext/opcache/jit/zend_jit_ir.c @@ -16,6 +16,7 @@ * +----------------------------------------------------------------------+ */ +#include "Zend/zend_cpuinfo.h" #include "Zend/zend_types.h" #include "Zend/zend_type_info.h" #include "jit/ir/ir.h" @@ -3396,7 +3397,7 @@ static void zend_jit_setup(bool reattached) if (zend_cpu_supports_avx()) { allowed_opt_flags |= ZEND_JIT_CPU_AVX; } -# if defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && ((defined(__clang__) && (__clang_major__ >= 19)) || (!defined(__clang__) && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000))) +# ifdef HAVE_ZEND_CPU_SUPPORTS_CLDEMOTE if (zend_cpu_supports_cldemote()) { default_mflags |= IR_X86_CLDEMOTE; }