Skip to content

Commit d357c59

Browse files
author
wxue1
committed
Cacheline demote to improve performance
Once code is emitted to JIT buffer, hint the hardware to demote the corresponding cache lines to more distant level so other CPUs can access them more quickly. This gets nearly 1% performance gain on our workload. Signed-off-by: Xue,Wang <[email protected]> Signed-off-by: Tao,Su <[email protected]> Signed-off-by: Hu,chen <[email protected]>
1 parent 360e6f8 commit d357c59

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Zend/zend_cpuinfo.h

+11
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,15 @@ static inline int zend_cpu_supports_pclmul(void) {
258258
}
259259
#endif
260260

261+
/* __builtin_cpu_supports has cldemote from gcc11 */
262+
#if PHP_HAVE_BUILTIN_CPU_SUPPORTS && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
263+
ZEND_NO_SANITIZE_ADDRESS
264+
static inline int zend_cpu_supports_cldemote(void) {
265+
#if PHP_HAVE_BUILTIN_CPU_INIT
266+
__builtin_cpu_init();
267+
#endif
268+
return __builtin_cpu_supports("cldemote");
269+
}
270+
#endif
271+
261272
#endif

ext/opcache/jit/zend_jit.c

+35
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ static zend_jit_trace_info *zend_jit_get_current_trace_info(void);
144144
static uint32_t zend_jit_trace_find_exit_point(const void* addr);
145145
#endif
146146

147+
#if ZEND_JIT_TARGET_X86 && defined(__linux__)
148+
# if PHP_HAVE_BUILTIN_CPU_SUPPORTS && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
149+
# define ZEND_JIT_SUPPORT_CLDEMOTE 1
150+
# else
151+
# define ZEND_JIT_SUPPORT_CLDEMOTE 0
152+
# endif
153+
#endif
154+
155+
#if ZEND_JIT_SUPPORT_CLDEMOTE
156+
#include <immintrin.h>
157+
#pragma GCC push_options
158+
#pragma GCC target("cldemote")
159+
// check cldemote by CPUID when JIT startup
160+
static int cpu_support_cldemote = 0;
161+
static inline void shared_cacheline_demote(uintptr_t start, size_t size) {
162+
uintptr_t cache_line_base = start & ~0x3F;
163+
do {
164+
_cldemote((void *)cache_line_base);
165+
// next cacheline start size
166+
cache_line_base += 64;
167+
} while (cache_line_base < start + size);
168+
}
169+
#pragma GCC pop_options
170+
#endif
171+
147172
static int zend_jit_assign_to_variable(dasm_State **Dst,
148173
const zend_op *opline,
149174
zend_jit_addr var_use_addr,
@@ -973,6 +998,12 @@ static void *dasm_link_and_encode(dasm_State **dasm_state,
973998

974999
/* flush the hardware I-cache */
9751000
JIT_CACHE_FLUSH(entry, entry + size);
1001+
/* hint to the hardware to push out the cache line that contains the linear address */
1002+
#if ZEND_JIT_SUPPORT_CLDEMOTE
1003+
if (cpu_support_cldemote && JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
1004+
shared_cacheline_demote((uintptr_t)entry, size);
1005+
}
1006+
#endif
9761007

9771008
if (trace_num) {
9781009
zend_jit_trace_add_code(entry, dasm_getpclabel(dasm_state, 1));
@@ -4901,6 +4932,10 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, bool reattached)
49014932
zend_jit_gdb_init();
49024933
#endif
49034934

4935+
#if ZEND_JIT_SUPPORT_CLDEMOTE
4936+
cpu_support_cldemote = zend_cpu_supports_cldemote();
4937+
#endif
4938+
49044939
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
49054940
zend_write_protect = pthread_jit_write_protect_supported_np();
49064941
#endif

0 commit comments

Comments
 (0)