Skip to content

cacheline demote to improve cache performance #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: PHP-8.1.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Zend/zend_cpuinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,15 @@ static inline int zend_cpu_supports_pclmul(void) {
}
#endif

/* __builtin_cpu_supports has cldemote from gcc11 */
#if PHP_HAVE_BUILTIN_CPU_SUPPORTS && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
ZEND_NO_SANITIZE_ADDRESS
static inline int zend_cpu_supports_cldemote(void) {
#if PHP_HAVE_BUILTIN_CPU_INIT
__builtin_cpu_init();
#endif
return __builtin_cpu_supports("cldemote");
}
#endif

#endif
36 changes: 36 additions & 0 deletions ext/opcache/jit/zend_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ static zend_jit_trace_info *zend_jit_get_current_trace_info(void);
static uint32_t zend_jit_trace_find_exit_point(const void* addr);
#endif

#if ZEND_JIT_TARGET_X86 && defined(__linux__)
# if PHP_HAVE_BUILTIN_CPU_SUPPORTS && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
# define ZEND_JIT_SUPPORT_CLDEMOTE 1
# else
# define ZEND_JIT_SUPPORT_CLDEMOTE 0
# endif
#endif

#if ZEND_JIT_SUPPORT_CLDEMOTE
#include <immintrin.h>
#pragma GCC push_options
#pragma GCC target("cldemote")
// check cldemote by CPUID when JIT startup
static int cpu_support_cldemote = 0;
static inline void shared_cacheline_demote(uintptr_t start, size_t size) {
uintptr_t cache_line_base = start & ~0x3F;
do {
_cldemote((void *)cache_line_base);
// next cacheline start size
cache_line_base += 64;
} while (cache_line_base < start + size);
}
#pragma GCC pop_options
#endif

static int zend_jit_assign_to_variable(dasm_State **Dst,
const zend_op *opline,
zend_jit_addr var_use_addr,
Expand Down Expand Up @@ -972,6 +997,13 @@ static void *dasm_link_and_encode(dasm_State **dasm_state,
/* flush the hardware I-cache */
JIT_CACHE_FLUSH(entry, entry + size);

/* hint to the hardware to push out the cache line that contains the linear address */
#if ZEND_JIT_SUPPORT_CLDEMOTE
if (cpu_support_cldemote && JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
shared_cacheline_demote((uintptr_t)entry, size);
}
#endif

if (trace_num) {
zend_jit_trace_add_code(entry, dasm_getpclabel(dasm_state, 1));
}
Expand Down Expand Up @@ -4931,6 +4963,10 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, bool reattached)
}
#endif

#if ZEND_JIT_SUPPORT_CLDEMOTE
cpu_support_cldemote = zend_cpu_supports_cldemote();
#endif

dasm_buf = buf;
dasm_size = size;

Expand Down