Skip to content

JIT deepest function firstly #10897

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: master
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
1 change: 1 addition & 0 deletions ext/opcache/jit/zend_jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ typedef struct _zend_jit_globals {
zend_long max_recursive_calls; /* max number of recursive inlined call unrolls */
zend_long max_recursive_returns; /* max number of recursive inlined return unrolls */
zend_long max_polymorphic_calls; /* max number of inlined polymorphic calls */
zend_long max_inline_func_length; /* max length of inlined functions in one trace */

zend_sym_node *symbols; /* symbols for disassembler */

Expand Down
4 changes: 3 additions & 1 deletion ext/opcache/jit/zend_jit_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ zend_constant* ZEND_FASTCALL zend_jit_check_constant(const zval *key);
_(RECURSION_EXIT, "return from recursive function") \
_(BLACK_LIST, "trace blacklisted") \
_(INNER_LOOP, "inner loop") /* trace it */ \
_(JIT_INLINE_FUNC, "JIT inlined function and skip current trace") /* trace inlined function */ \
_(COMPILED_LOOP, "compiled loop") \
_(TRAMPOLINE, "trampoline call") \
_(BAD_FUNC, "bad function call") \
Expand All @@ -383,8 +384,9 @@ typedef enum _zend_jit_trace_stop {
#define ZEND_JIT_TRACE_STOP_DONE(ret) \
(ret < ZEND_JIT_TRACE_STOP_ERROR)

/* restart to trace an inner loop or inlined function */
#define ZEND_JIT_TRACE_STOP_REPEAT(ret) \
(ret == ZEND_JIT_TRACE_STOP_INNER_LOOP)
(ret == ZEND_JIT_TRACE_STOP_INNER_LOOP || ret == ZEND_JIT_TRACE_STOP_JIT_INLINE_FUNC)

#define ZEND_JIT_TRACE_STOP_MAY_RECOVER(ret) \
(ret <= ZEND_JIT_TRACE_STOP_COMPILER_ERROR)
Expand Down
4 changes: 2 additions & 2 deletions ext/opcache/jit/zend_jit_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -7696,7 +7696,7 @@ int ZEND_FASTCALL zend_jit_trace_hot_root(zend_execute_data *execute_data, const
zend_jit_trace_stop_description[stop]);
}
if (!ZEND_JIT_TRACE_STOP_MAY_RECOVER(stop)
|| zend_jit_trace_is_bad_root(orig_opline, stop, offset)) {
|| (stop != ZEND_JIT_TRACE_STOP_JIT_INLINE_FUNC && zend_jit_trace_is_bad_root(orig_opline, stop, offset))) {
if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BLACKLIST) {
fprintf(stderr, "---- TRACE %d blacklisted\n",
trace_num);
Expand Down Expand Up @@ -8039,7 +8039,7 @@ int ZEND_FASTCALL zend_jit_trace_hot_side(zend_execute_data *execute_data, uint3
zend_jit_trace_stop_description[stop]);
}
if (!ZEND_JIT_TRACE_STOP_MAY_RECOVER(stop)
|| zend_jit_trace_exit_is_bad(parent_num, exit_num)) {
|| (stop != ZEND_JIT_TRACE_STOP_JIT_INLINE_FUNC && zend_jit_trace_exit_is_bad(parent_num, exit_num))) {
zend_jit_blacklist_trace_exit(parent_num, exit_num);
if (JIT_G(debug) & ZEND_JIT_DEBUG_TRACE_BLACKLIST) {
fprintf(stderr, "---- EXIT %d/%d blacklisted\n",
Expand Down
7 changes: 7 additions & 0 deletions ext/opcache/jit/zend_jit_vm_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,13 @@ zend_jit_trace_stop ZEND_FASTCALL zend_jit_trace_execute(zend_execute_data *ex,

trace_flags = ZEND_OP_TRACE_INFO(opline, offset)->trace_flags;
if (trace_flags) {
/* if inlined functions are too long, stop current tracing and restart a new one */
if (trace_buffer[idx-1].op == ZEND_JIT_TRACE_ENTER && idx > JIT_G(max_inline_func_length)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't limit the inlined function length. This limits function inlining to the first trace instructions.

Copy link
Contributor Author

@wxue1 wxue1 Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't limit the inlined function length. This limits function inlining to the first trace instructions.

Actually, JAVA JIT support inlined function up to a particular size. Here we limit size by separating the long trace by max_inline_func_length.

If we limit the inlined function length, some functions may never be JITTed. So I chose a compromise method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code allows inlining only if it occurs in the first 16 instructions of the trace. It easelly prohibits inlining of the short functions.

---- TRACE 44 start (side trace 43/3) WP_Scripts::localize() /home/www/bench/wordpress-3.6/wp-includes/class.wp-scripts.php:148
0027 FE_FREE V8 ; op1(array)
0028 T10 = ROPE_INIT 3 string("var ")
0029 T10 = ROPE_ADD 1 T10 CV1($object_name) ; op2(string)
0030 T9 = ROPE_END 2 T10 string(" = ")
0031 INIT_FCALL 1 96 string("json_encode")
     >init json_encode
0032 SEND_VAR CV2($l10n) 1 ; op1(array)
0033 V10 = DO_ICALL
     >call json_encode
0034 T8 = FAST_CONCAT T9 V10 ; op1(string) op2(string)
0035 CV6($script) = FAST_CONCAT T8 string(";") ; op1(string)
0036 T8 = ISSET_ISEMPTY_CV (empty) CV3($after) ; op1(undef)
0037 ;JMPNZ T8 0042
0042 INIT_METHOD_CALL 2 THIS string("get_data")
     >init WP_Dependencies::get_data
0043 SEND_VAR_EX CV0($handle) 1 ; op1(string)
0044 SEND_VAL_EX string("data") 2
0045 V8 = DO_FCALL
     >enter WP_Dependencies::get_data
---- TRACE 44 abort (JIT inlined function and skip current trace)
---- TRACE 44 start (enter) WP_Dependencies::get_data() /home/www/bench/wordpress-3.6/wp-includes/class.wp-dependencies.php:163
0002 T3 = FETCH_OBJ_IS THIS string("registered") ; val(array)
0003 T2 = ISSET_ISEMPTY_DIM_OBJ (isset) T3 CV0($handle) ; op1(array) op2(string) val(object)
0004 ;JMPNZ T2 0006
0006 T3 = FETCH_OBJ_IS THIS string("registered") ; val(array)
0007 T2 = FETCH_DIM_IS T3 CV0($handle) ; op1(array) op2(string) val(object)
0008 T3 = FETCH_OBJ_IS T2 string("extra") ; op1(object of class _WP_Dependency) val(array)
0009 T2 = ISSET_ISEMPTY_DIM_OBJ (isset) T3 CV1($key) ; op1(array) op2(string) val(undef)
0010 ;JMPNZ T2 0012
0011 RETURN bool(false)
---- TRACE 44 stop (return)

JAVA doesn't use tracing JIT. The current PHP tracing rules were mainly developed on top of LuaJIT.

if (!(trace_flags & ZEND_JIT_TRACE_JITED)) {
stop = ZEND_JIT_TRACE_STOP_JIT_INLINE_FUNC;
break;
}
}
if (trace_flags & ZEND_JIT_TRACE_JITED) {
if (trace_flags & ZEND_JIT_TRACE_START_LOOP) {
if ((start & ZEND_JIT_TRACE_START_LOOP) != 0
Expand Down
2 changes: 2 additions & 0 deletions ext/opcache/zend_accelerator_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ ZEND_INI_BEGIN()
STD_PHP_INI_ENTRY("opcache.jit_max_root_traces" , "1024", PHP_INI_SYSTEM, OnUpdateLong, max_root_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_side_traces" , "128", PHP_INI_SYSTEM, OnUpdateLong, max_side_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_exit_counters" , "8192", PHP_INI_SYSTEM, OnUpdateLong, max_exit_counters, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_inline_func_length" , "16", PHP_INI_SYSTEM, OnUpdateLong, max_inline_func_length,zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_hot_loop" , "64", PHP_INI_SYSTEM, OnUpdateCounter, hot_loop, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_hot_func" , "127", PHP_INI_SYSTEM, OnUpdateCounter, hot_func, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_hot_return" , "8", PHP_INI_SYSTEM, OnUpdateCounter, hot_return, zend_jit_globals, jit_globals)
Expand Down Expand Up @@ -849,6 +850,7 @@ ZEND_FUNCTION(opcache_get_configuration)
add_assoc_long(&directives, "opcache.jit_max_root_traces", JIT_G(max_root_traces));
add_assoc_long(&directives, "opcache.jit_max_side_traces", JIT_G(max_side_traces));
add_assoc_long(&directives, "opcache.jit_prof_threshold", JIT_G(prof_threshold));
add_assoc_long(&directives, "opcache.jit_max_inline_func_length", JIT_G(max_inline_func_length));
#endif

add_assoc_zval(return_value, "directives", &directives);
Expand Down