GH-108866: Change optimizer API and contract by markshannon · Pull Request #109144 · python/cpython · GitHub
Skip to content
Closed
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
4 changes: 2 additions & 2 deletions Include/cpython/optimizer.h
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Include/internal/pycore_uops.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct {
_PyUOpInstruction trace[1];
} _PyUOpExecutorObject;

_PyInterpreterFrame *_PyUopExecute(
_Py_CODEUNIT *_PyUopExecute(
_PyExecutorObject *executor,
_PyInterpreterFrame *frame,
PyObject **stack_pointer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Change the API and contract of ``_PyExecutorObject`` to return the
next_instr pointer, instead of the frame, and to always execute at least one
instruction.
43 changes: 20 additions & 23 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2218,23 +2218,25 @@ dummy_func(
JUMPBY(1-oparg);
#if ENABLE_SPECIALIZATION
here[1].cache += (1 << OPTIMIZER_BITS_IN_COUNTER);
if (here[1].cache > tstate->interp->optimizer_backedge_threshold &&
// Double-check that the opcode isn't instrumented or something:
here->op.code == JUMP_BACKWARD &&
// _PyOptimizer_BackEdge is going to change frame->prev_instr,
// which breaks line event calculations:
next_instr->op.code != INSTRUMENTED_LINE
)
{
if (here[1].cache > tstate->interp->optimizer_backedge_threshold) {
/* We should not be here if the code has been instrumented,
* or already has an attached executor */
assert(here->op.code != ENTER_EXECUTOR);
assert(here->op.code != INSTRUMENTED_JUMP_BACKWARD);
OBJECT_STAT_INC(optimization_attempts);
frame = _PyOptimizer_BackEdge(frame, here, next_instr, stack_pointer);
if (frame == NULL) {
frame = tstate->current_frame;
_Py_CODEUNIT *src = here;
while(oparg > 255) {
oparg >>= 8;
src--;
assert(src->op.code == EXTENDED_ARG);
}
next_instr = _PyOptimizer_BackEdge(frame, src, next_instr, stack_pointer);
frame = tstate->current_frame;
if (next_instr == NULL) {
goto resume_with_error;
}
assert(frame == tstate->current_frame);
stack_pointer = _PyFrame_GetStackPointer(frame);
here[1].cache &= ((1 << OPTIMIZER_BITS_IN_COUNTER) -1);
goto resume_frame;
}
#endif /* ENABLE_SPECIALIZATION */
}
Expand All @@ -2251,19 +2253,15 @@ dummy_func(

inst(ENTER_EXECUTOR, (--)) {
CHECK_EVAL_BREAKER();

PyCodeObject *code = _PyFrame_GetCode(frame);
_PyExecutorObject *executor = (_PyExecutorObject *)code->co_executors->executors[oparg&255];
int original_oparg = executor->vm_data.oparg | (oparg & 0xfffff00);
JUMPBY(1-original_oparg);
frame->prev_instr = next_instr - 1;
Py_INCREF(executor);
frame = executor->execute(executor, frame, stack_pointer);
if (frame == NULL) {
frame = tstate->current_frame;
next_instr = executor->execute(executor, frame, stack_pointer);
frame = tstate->current_frame;
if (next_instr == NULL) {
goto resume_with_error;
}
goto resume_frame;
stack_pointer = _PyFrame_GetStackPointer(frame);
}

inst(POP_JUMP_IF_FALSE, (cond -- )) {
Expand Down Expand Up @@ -3821,10 +3819,9 @@ dummy_func(
}

op(EXIT_TRACE, (--)) {
frame->prev_instr--; // Back up to just before destination
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
return frame;
return frame->prev_instr;
}

op(INSERT, (unused[oparg], top -- top, unused[oparg])) {
Expand Down
5 changes: 2 additions & 3 deletions Python/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define ENABLE_SPECIALIZATION 0


_PyInterpreterFrame *
_Py_CODEUNIT *
_PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **stack_pointer)
{
#ifdef Py_DEBUG
Expand Down Expand Up @@ -122,8 +122,7 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
// On DEOPT_IF we just repeat the last instruction.
// This presumes nothing was popped from the stack (nor pushed).
DPRINTF(2, "DEOPT: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
frame->prev_instr--; // Back up to just before destination
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
return frame;
return frame->prev_instr;
}
3 changes: 1 addition & 2 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 20 additions & 21 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 21 additions & 10 deletions Python/optimizer.c