-
Notifications
You must be signed in to change notification settings - Fork 7.8k
JIT: Snapshotted poly_func / poly_this may be spilled #18408
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
arnaud-lb
wants to merge
4
commits into
php:PHP-8.4
Choose a base branch
from
arnaud-lb:snapshot-spill
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,6 +277,8 @@ typedef struct _zend_jit_ctx { | |
ir_ref tls; | ||
#endif | ||
ir_ref fp; | ||
ir_ref poly_func_ref; /* restored from parent trace snapshot */ | ||
ir_ref poly_this_ref; /* restored from parent trace snapshot */ | ||
ir_ref trace_loop_ref; | ||
ir_ref return_inputs; | ||
const zend_op_array *op_array; | ||
|
@@ -624,12 +626,10 @@ static void jit_SNAPSHOT(zend_jit_ctx *jit, ir_ref addr) | |
uint32_t exit_point = 0, n = 0; | ||
|
||
if (addr < 0) { | ||
if (t->exit_count > 0 | ||
&& jit->ctx.ir_base[addr].val.u64 == (uintptr_t)zend_jit_trace_get_exit_addr(t->exit_count - 1)) { | ||
exit_point = t->exit_count - 1; | ||
if (t->exit_info[exit_point].flags & ZEND_JIT_EXIT_METHOD_CALL) { | ||
n = 2; | ||
} | ||
exit_point = zend_jit_exit_point_by_addr((void*)(uintptr_t) jit->ctx.ir_base[addr].val.u64); | ||
ZEND_ASSERT(exit_point != -1); | ||
if (t->exit_info[exit_point].flags & ZEND_JIT_EXIT_METHOD_CALL) { | ||
n = 2; | ||
} | ||
} | ||
|
||
|
@@ -660,8 +660,8 @@ static void jit_SNAPSHOT(zend_jit_ctx *jit, ir_ref addr) | |
ir_SNAPSHOT_SET_OP(snapshot, i + 1, ref); | ||
} | ||
if (n) { | ||
ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 1, t->exit_info[exit_point].poly_func_ref); | ||
ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 2, t->exit_info[exit_point].poly_this_ref); | ||
ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 1, t->exit_info[exit_point].poly_func.ref); | ||
ir_SNAPSHOT_SET_OP(snapshot, snapshot_size + 2, t->exit_info[exit_point].poly_this.ref); | ||
} | ||
} | ||
} | ||
|
@@ -710,6 +710,29 @@ uint32_t zend_jit_duplicate_exit_point(ir_ctx *ctx, zend_jit_trace_info *t, uint | |
return new_exit_point; | ||
} | ||
|
||
zend_jit_ref_snapshot zend_jit_resolve_ref_snapshot(ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snapshot, int op) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer not to return "long" structures. |
||
{ | ||
int8_t *reg_ops = ctx->regs[snapshot_ref]; | ||
ZEND_ASSERT(reg_ops[op] != ZREG_NONE); | ||
|
||
zend_jit_ref_snapshot rs = { | ||
.reg = reg_ops[op], | ||
}; | ||
|
||
if (IR_REG_SPILLED(rs.reg)) { | ||
rs.reg = ((ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FP : IR_REG_SP) | IR_REG_SPILL_LOAD; | ||
rs.offset = ir_get_spill_slot_offset(ctx, ir_insn_op(snapshot, op)); | ||
} | ||
|
||
return rs; | ||
} | ||
|
||
bool zend_jit_ref_snapshot_equals(zend_jit_ref_snapshot *a, zend_jit_ref_snapshot *b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Could be const pointers |
||
{ | ||
return a->reg == b->reg | ||
&& (!IR_REG_SPILLED(a->reg) || (a->offset == b->offset)); | ||
} | ||
|
||
void *zend_jit_snapshot_handler(ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snapshot, void *addr) | ||
{ | ||
zend_jit_trace_info *t = ((zend_jit_ctx*)ctx)->trace; | ||
|
@@ -722,18 +745,18 @@ void *zend_jit_snapshot_handler(ir_ctx *ctx, ir_ref snapshot_ref, ir_insn *snaps | |
exit_flags = t->exit_info[exit_point].flags; | ||
|
||
if (exit_flags & ZEND_JIT_EXIT_METHOD_CALL) { | ||
int8_t *reg_ops = ctx->regs[snapshot_ref]; | ||
zend_jit_ref_snapshot func = zend_jit_resolve_ref_snapshot(ctx, snapshot_ref, snapshot, n - 1); | ||
zend_jit_ref_snapshot this = zend_jit_resolve_ref_snapshot(ctx, snapshot_ref, snapshot, n); | ||
|
||
ZEND_ASSERT(reg_ops[n - 1] != -1 && reg_ops[n] != -1); | ||
if ((exit_flags & ZEND_JIT_EXIT_FIXED) | ||
&& (t->exit_info[exit_point].poly_func_reg != reg_ops[n - 1] | ||
|| t->exit_info[exit_point].poly_this_reg != reg_ops[n])) { | ||
&& (!zend_jit_ref_snapshot_equals(&t->exit_info[exit_point].poly_func, &func) | ||
|| !zend_jit_ref_snapshot_equals(&t->exit_info[exit_point].poly_this, &this))) { | ||
exit_point = zend_jit_duplicate_exit_point(ctx, t, exit_point, snapshot_ref); | ||
addr = (void*)zend_jit_trace_get_exit_addr(exit_point); | ||
exit_flags &= ~ZEND_JIT_EXIT_FIXED; | ||
} | ||
t->exit_info[exit_point].poly_func_reg = reg_ops[n - 1]; | ||
t->exit_info[exit_point].poly_this_reg = reg_ops[n]; | ||
t->exit_info[exit_point].poly_func = func; | ||
t->exit_info[exit_point].poly_this = this; | ||
n -= 2; | ||
} | ||
|
||
|
@@ -2751,6 +2774,8 @@ static void zend_jit_init_ctx(zend_jit_ctx *jit, uint32_t flags) | |
jit->tls = IR_UNUSED; | ||
#endif | ||
jit->fp = IR_UNUSED; | ||
jit->poly_func_ref = IR_UNUSED; | ||
jit->poly_this_ref = IR_UNUSED; | ||
jit->trace_loop_ref = IR_UNUSED; | ||
jit->return_inputs = IR_UNUSED; | ||
jit->bb_start_ref = NULL; | ||
|
@@ -4423,6 +4448,18 @@ static ir_ref zend_jit_deopt_rload(zend_jit_ctx *jit, ir_type type, int32_t reg) | |
return ir_RLOAD(type, reg); | ||
} | ||
|
||
/* Same as zend_jit_deopt_rload(), but 'reg' may be spilled on C stack */ | ||
static ir_ref zend_jit_deopt_rload_spilled(zend_jit_ctx *jit, ir_type type, int8_t reg, int32_t offset) | ||
{ | ||
ZEND_ASSERT(reg >= 0); | ||
|
||
if (IR_REG_SPILLED(reg)) { | ||
return ir_LOAD(type, ir_ADD_OFFSET(zend_jit_deopt_rload(jit, type, IR_REG_NUM(reg)), offset)); | ||
} else { | ||
return zend_jit_deopt_rload(jit, type, reg); | ||
} | ||
} | ||
|
||
static int zend_jit_store_const_long(zend_jit_ctx *jit, int var, zend_long val) | ||
{ | ||
zend_jit_addr dst = ZEND_ADDR_MEM_ZVAL(ZREG_FP, EX_NUM_TO_VAR(var)); | ||
|
@@ -8477,10 +8514,9 @@ static int zend_jit_stack_check(zend_jit_ctx *jit, const zend_op *opline, uint32 | |
return 1; | ||
} | ||
|
||
static int zend_jit_free_trampoline(zend_jit_ctx *jit, int8_t func_reg) | ||
static int zend_jit_free_trampoline(zend_jit_ctx *jit, ir_ref func) | ||
{ | ||
// JIT: if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) | ||
ir_ref func = ir_RLOAD_A(func_reg); | ||
ir_ref if_trampoline = ir_IF(ir_AND_U32( | ||
ir_LOAD_U32(ir_ADD_OFFSET(func, offsetof(zend_function, common.fn_flags))), | ||
ir_CONST_U32(ZEND_ACC_CALL_VIA_TRAMPOLINE))); | ||
|
@@ -8962,15 +8998,15 @@ static int zend_jit_init_method_call(zend_jit_ctx *jit, | |
zend_class_entry *trace_ce, | ||
zend_jit_trace_rec *trace, | ||
int checked_stack, | ||
int8_t func_reg, | ||
int8_t this_reg, | ||
ir_ref func_ref, | ||
ir_ref this_ref, | ||
bool polymorphic_side_trace) | ||
{ | ||
zend_func_info *info = ZEND_FUNC_INFO(op_array); | ||
zend_call_info *call_info = NULL; | ||
zend_function *func = NULL; | ||
zval *function_name; | ||
ir_ref if_static = IR_UNUSED, cold_path, this_ref = IR_NULL, func_ref = IR_NULL; | ||
ir_ref if_static = IR_UNUSED, cold_path; | ||
|
||
ZEND_ASSERT(opline->op2_type == IS_CONST); | ||
ZEND_ASSERT(op1_info & MAY_BE_OBJECT); | ||
|
@@ -8988,10 +9024,8 @@ static int zend_jit_init_method_call(zend_jit_ctx *jit, | |
} | ||
|
||
if (polymorphic_side_trace) { | ||
/* function is passed in r0 from parent_trace */ | ||
ZEND_ASSERT(func_reg >= 0 && this_reg >= 0); | ||
func_ref = zend_jit_deopt_rload(jit, IR_ADDR, func_reg); | ||
this_ref = zend_jit_deopt_rload(jit, IR_ADDR, this_reg); | ||
/* function is passed from parent snapshot */ | ||
ZEND_ASSERT(func_ref != IR_UNUSED && this_ref != IR_UNUSED); | ||
} else { | ||
ir_ref ref, ref2, if_found, fast_path, run_time_cache, this_ref2; | ||
|
||
|
@@ -9137,8 +9171,8 @@ static int zend_jit_init_method_call(zend_jit_ctx *jit, | |
return 0; | ||
} | ||
|
||
jit->trace->exit_info[exit_point].poly_func_ref = func_ref; | ||
jit->trace->exit_info[exit_point].poly_this_ref = this_ref; | ||
jit->trace->exit_info[exit_point].poly_func.ref = func_ref; | ||
jit->trace->exit_info[exit_point].poly_this.ref = this_ref; | ||
|
||
func = (zend_function*)trace->func; | ||
|
||
|
@@ -16991,9 +17025,13 @@ static int zend_jit_trace_start(zend_jit_ctx *jit, | |
} | ||
|
||
if (parent && parent->exit_info[exit_num].flags & ZEND_JIT_EXIT_METHOD_CALL) { | ||
ZEND_ASSERT(parent->exit_info[exit_num].poly_func_reg >= 0 && parent->exit_info[exit_num].poly_this_reg >= 0); | ||
ir_RLOAD_A(parent->exit_info[exit_num].poly_func_reg); | ||
ir_RLOAD_A(parent->exit_info[exit_num].poly_this_reg); | ||
ZEND_ASSERT(parent->exit_info[exit_num].poly_func.reg >= 0 && parent->exit_info[exit_num].poly_this.reg >= 0); | ||
if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_func.reg)) { | ||
ir_RLOAD_A(parent->exit_info[exit_num].poly_func.reg); | ||
} | ||
if (!IR_REG_SPILLED(parent->exit_info[exit_num].poly_this.reg)) { | ||
ir_RLOAD_A(parent->exit_info[exit_num].poly_this.reg); | ||
} | ||
} | ||
|
||
ir_STORE(jit_EG(jit_trace_num), ir_CONST_U32(trace_num)); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addr
is not always the one of the last exit point, e.g. when more exit points are created before an earlier exit point is actually used in a guard:In this case,
exit_point
was left initialized to0
and we updated the wrong exit infos.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This deserves a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also can't you do
zend_jit_exit_point_by_addr(ptr);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed :)