Skip to content

Commit bbc5cb6

Browse files
committed
Remove redundancy in _PyJit_TryInitializeTracing
1 parent cf59bf7 commit bbc5cb6

File tree

6 files changed

+21
-49
lines changed

6 files changed

+21
-49
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ typedef struct _PyJitTracerInitialState {
8787
PyFunctionObject *func; // Strong
8888
struct _PyExecutorObject *executor; // Strong
8989
_Py_CODEUNIT *start_instr;
90-
_Py_CODEUNIT *close_loop_instr;
9190
_Py_CODEUNIT *jump_backward_instr;
9291
} _PyJitTracerInitialState;
9392

@@ -470,8 +469,8 @@ PyAPI_FUNC(int) _PyJit_translate_single_bytecode_to_trace(PyThreadState *tstate,
470469

471470
PyAPI_FUNC(int)
472471
_PyJit_TryInitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame,
473-
_Py_CODEUNIT *curr_instr, _Py_CODEUNIT *start_instr,
474-
_Py_CODEUNIT *close_loop_instr, _PyStackRef *stack_pointer, int chain_depth, _PyExitData *exit,
472+
_Py_CODEUNIT *curr_instr, _PyStackRef *stack_pointer,
473+
int chain_depth, _PyExitData *exit,
475474
int oparg, _PyExecutorObject *current_executor);
476475

477476
PyAPI_FUNC(void) _PyJit_FinalizeTracing(PyThreadState *tstate, int err);

Modules/_testinternalcapi/test_cases.c.h

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

Python/bytecodes.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,13 +3373,8 @@ dummy_func(
33733373
(this_instr->op.code == JUMP_BACKWARD_JIT || is_resume)) &&
33743374
next_instr->op.code != ENTER_EXECUTOR) {
33753375
/* Back up over EXTENDED_ARGs so executor is inserted at the correct place */
3376-
_Py_CODEUNIT *insert_exec_at = this_instr;
3377-
while (oparg > 255) {
3378-
oparg >>= 8;
3379-
insert_exec_at--;
3380-
}
3381-
int succ = _PyJit_TryInitializeTracing(tstate, frame, this_instr, insert_exec_at,
3382-
is_resume ? insert_exec_at : next_instr, stack_pointer, 0, NULL, oparg, NULL);
3376+
int succ = _PyJit_TryInitializeTracing(
3377+
tstate, frame, this_instr, stack_pointer, 0, NULL, oparg, NULL);
33833378
if (succ) {
33843379
ENTER_TRACING();
33853380
}
@@ -6066,7 +6061,7 @@ dummy_func(
60666061
// Note: it's safe to use target->op.arg here instead of the oparg given by EXTENDED_ARG.
60676062
// The invariant in the optimizer is the deopt target always points back to the first EXTENDED_ARG.
60686063
// So setting it to anything else is wrong.
6069-
int succ = _PyJit_TryInitializeTracing(tstate, frame, target, target, target, stack_pointer, chain_depth, exit, target->op.arg, previous_executor);
6064+
int succ = _PyJit_TryInitializeTracing(tstate, frame, target, stack_pointer, chain_depth, exit, target->op.arg, previous_executor);
60706065
exit->temperature = restart_backoff_counter(exit->temperature);
60716066
if (succ) {
60726067
GOTO_TIER_ONE_CONTINUE_TRACING(target);

Python/executor_cases.c.h

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

Python/generated_cases.c.h

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

Python/optimizer.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,7 @@ _PyJit_translate_single_bytecode_to_trace(
826826
_Py_FALLTHROUGH;
827827
case JUMP_BACKWARD_NO_INTERRUPT:
828828
{
829-
if ((next_instr != tracer->initial_state.close_loop_instr) &&
830-
(next_instr != tracer->initial_state.start_instr) &&
829+
if ((next_instr != tracer->initial_state.start_instr) &&
831830
uop_buffer_length(&tracer->code_buffer) > CODE_SIZE_NO_PROGRESS &&
832831
// For side exits, we don't want to terminate them early.
833832
tracer->initial_state.exit == NULL &&
@@ -840,8 +839,8 @@ _PyJit_translate_single_bytecode_to_trace(
840839
OPT_STAT_INC(inner_loop);
841840
ADD_TO_TRACE(_EXIT_TRACE, 0, 0, target);
842841
uop_buffer_last(trace)->operand1 = true; // is_control_flow
843-
DPRINTF(2, "JUMP_BACKWARD not to top ends trace %p %p %p\n", next_instr,
844-
tracer->initial_state.close_loop_instr, tracer->initial_state.start_instr);
842+
DPRINTF(2, "JUMP_BACKWARD not to top ends trace %p %p\n", next_instr,
843+
tracer->initial_state.start_instr);
845844
goto done;
846845
}
847846
break;
@@ -977,8 +976,7 @@ _PyJit_translate_single_bytecode_to_trace(
977976
}
978977
}
979978
// Loop back to the start
980-
int is_first_instr = tracer->initial_state.close_loop_instr == next_instr ||
981-
tracer->initial_state.start_instr == next_instr;
979+
int is_first_instr = tracer->initial_state.start_instr == next_instr;
982980
if (is_first_instr && uop_buffer_length(trace) > CODE_SIZE_NO_PROGRESS) {
983981
if (needs_guard_ip) {
984982
ADD_TO_TRACE(_SET_IP, 0, (uintptr_t)next_instr, 0);
@@ -1002,7 +1000,7 @@ _PyJit_translate_single_bytecode_to_trace(
10021000
Py_NO_INLINE int
10031001
_PyJit_TryInitializeTracing(
10041002
PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *curr_instr,
1005-
_Py_CODEUNIT *start_instr, _Py_CODEUNIT *close_loop_instr, _PyStackRef *stack_pointer, int chain_depth,
1003+
_PyStackRef *stack_pointer, int chain_depth,
10061004
_PyExitData *exit, int oparg, _PyExecutorObject *current_executor)
10071005
{
10081006
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
@@ -1022,6 +1020,7 @@ _PyJit_TryInitializeTracing(
10221020
if (oparg > 0xFFFF) {
10231021
return 0;
10241022
}
1023+
_Py_CODEUNIT *start_instr = curr_instr - (oparg > 0xFF);
10251024
PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
10261025
if (func == NULL || !PyFunction_Check(func)) {
10271026
return 0;
@@ -1038,7 +1037,7 @@ _PyJit_TryInitializeTracing(
10381037
PyUnicode_AsUTF8(code->co_qualname),
10391038
PyUnicode_AsUTF8(code->co_filename),
10401039
code->co_firstlineno,
1041-
2 * INSTR_IP(close_loop_instr, code),
1040+
2 * INSTR_IP(curr_instr, code),
10421041
chain_depth);
10431042
#endif
10441043
/* Set up tracing buffer*/
@@ -1048,7 +1047,6 @@ _PyJit_TryInitializeTracing(
10481047
ADD_TO_TRACE(_MAKE_WARM, 0, 0, 0);
10491048

10501049
tracer->initial_state.start_instr = start_instr;
1051-
tracer->initial_state.close_loop_instr = close_loop_instr;
10521050
tracer->initial_state.code = (PyCodeObject *)Py_NewRef(code);
10531051
tracer->initial_state.func = (PyFunctionObject *)Py_NewRef(func);
10541052
tracer->initial_state.executor = (_PyExecutorObject *)Py_XNewRef(current_executor);

0 commit comments

Comments
 (0)