Skip to content

Commit 299a068

Browse files
Handle recursive tracing and CALL_ALLOC_AND_ENTER_INIT
1 parent 2032b9c commit 299a068

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

Python/bytecodes.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5268,6 +5268,7 @@ dummy_func(
52685268

52695269
tier2 op(_EXIT_TRACE, (exit_p/4 --)) {
52705270
_PyExitData *exit = (_PyExitData *)exit_p;
5271+
assert(!exit->is_dynamic);
52715272
#if defined(Py_DEBUG) && !defined(_Py_JIT)
52725273
_Py_CODEUNIT *target = _PyFrame_GetBytecode(frame) + exit->target;
52735274
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
@@ -5281,7 +5282,6 @@ dummy_func(
52815282
}
52825283
#endif
52835284
tstate->jit_exit = exit;
5284-
assert(!exit->is_dynamic);
52855285
TIER2_TO_TIER2(exit->executor);
52865286
}
52875287

@@ -5441,6 +5441,9 @@ dummy_func(
54415441
TIER2_TO_TIER2(exit->executor);
54425442
}
54435443
else {
5444+
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
5445+
GOTO_TIER_ONE(target, 0);
5446+
}
54445447
if (!backoff_counter_triggers(temperature)) {
54455448
exit->temperature = advance_backoff_counter(temperature);
54465449
GOTO_TIER_ONE(target, 0);

Python/ceval_macros.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,15 @@
138138
#if _Py_TAIL_CALL_INTERP || USE_COMPUTED_GOTOS
139139
# define IS_JIT_TRACING() (tstate->interp->jit_is_tracing)
140140
# define ENTER_TRACING() \
141-
assert(!IS_JIT_TRACING()); \
142141
DISPATCH_TABLE_VAR = TRACING_DISPATCH_TABLE; \
143142
tstate->interp->jit_is_tracing = true;
144143
# define LEAVE_TRACING() \
145144
assert(IS_JIT_TRACING()); \
146145
DISPATCH_TABLE_VAR = DISPATCH_TABLE; \
147146
tstate->interp->jit_is_tracing = false;
147+
// This handles recursive tracing over C calls.
148+
# define RELOAD_TRACING() \
149+
DISPATCH_TABLE_VAR = tstate->interp->jit_is_tracing ? TRACING_DISPATCH_TABLE : DISPATCH_TABLE;
148150
# define BAIL_TRACING_NO_DISPATCH() \
149151
LEAVE_TRACING(); \
150152
_PyFrame_SetStackPointer(frame, stack_pointer); \
@@ -155,7 +157,7 @@
155157
JUMP_TO_LABEL(error); \
156158
}
157159
# define RECORD_TRACE_NO_DISPATCH() do { \
158-
if (add_to_code_trace(tstate, frame, old_code, old_func, this_instr, next_instr, opcode, oparg, _jump_taken)) { \
160+
if (tstate->interp->jit_is_tracing && add_to_code_trace(tstate, frame, old_code, old_func, this_instr, next_instr, opcode, oparg, _jump_taken)) { \
159161
BAIL_TRACING_NO_DISPATCH(); \
160162
} \
161163
} while (0);
@@ -222,6 +224,7 @@ do { \
222224
#define TRACING_DISPATCH() \
223225
{ \
224226
assert(frame->stackpointer == NULL); \
227+
RELOAD_TRACING(); \
225228
RECORD_TRACE_NO_DISPATCH(); \
226229
NEXTOPARG(); \
227230
PRE_DISPATCH_GOTO(); \

Python/executor_cases.c.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ _PyJIT_translate_single_bytecode_to_trace(
605605
return 1;
606606
}
607607

608+
if (opcode == JUMP_FORWARD) {
609+
return 1;
610+
}
611+
608612
if (opcode == ENTER_EXECUTOR) {
609613
ADD_TO_TRACE(_CHECK_VALIDITY, 0, 0, target);
610614
ADD_TO_TRACE(_SET_IP, 0, (uintptr_t)target_instr, target);
@@ -623,7 +627,10 @@ _PyJIT_translate_single_bytecode_to_trace(
623627

624628
// Strange control-flow, unsupported opcode, etc.
625629
if (jump_taken ||
626-
opcode == WITH_EXCEPT_START || opcode == RERAISE || opcode == CLEANUP_THROW || opcode == PUSH_EXC_INFO) {
630+
opcode == WITH_EXCEPT_START || opcode == RERAISE || opcode == CLEANUP_THROW || opcode == PUSH_EXC_INFO ||
631+
frame->owner >= FRAME_OWNED_BY_INTERPRETER ||
632+
// This can be supported, but requires a tracing shim frame.
633+
opcode == CALL_ALLOC_AND_ENTER_INIT) {
627634
unsupported:
628635
{
629636
// Rewind to previous instruction and replace with _EXIT_TRACE.
@@ -814,6 +821,7 @@ _PyJIT_translate_single_bytecode_to_trace(
814821
if (!is_terminator(&tstate->interp->jit_tracer_code_buffer[trace_length-1])) {
815822
// Undo the last few instructions.
816823
trace_length = tstate->interp->jit_tracer_code_curr_size;
824+
max_length = tstate->interp->jit_tracer_code_max_size;
817825
// We previously reversed one.
818826
max_length += 1;
819827
ADD_TO_TRACE(_EXIT_TRACE, 0, 0, target);

0 commit comments

Comments
 (0)