Skip to content

GH-104584: Allow optimizers to opt out of optimizing. #105244

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

Merged
Merged
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
3 changes: 2 additions & 1 deletion Include/cpython/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ typedef struct _PyExecutorObject {

typedef struct _PyOptimizerObject _PyOptimizerObject;

typedef _PyExecutorObject *(*optimize_func)(_PyOptimizerObject* self, PyCodeObject *code, _Py_CODEUNIT *instr);
/* Should return > 0 if a new executor is created. O if no executor is produced and < 0 if an error occurred. */
typedef int (*optimize_func)(_PyOptimizerObject* self, PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutorObject **);

typedef struct _PyOptimizerObject {
PyObject_HEAD
Expand Down
29 changes: 18 additions & 11 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ PyUnstable_Replace_Executor(PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutor
return 0;
}

static _PyExecutorObject *
static int
error_optimize(
_PyOptimizerObject* self,
PyCodeObject *code,
_Py_CODEUNIT *instr)
_Py_CODEUNIT *instr,
_PyExecutorObject **exec)
{
PyErr_Format(PyExc_SystemError, "Should never call error_optimize");
return NULL;
return -1;
}

static PyTypeObject DefaultOptimizer_Type = {
Expand Down Expand Up @@ -154,15 +155,19 @@ _PyOptimizer_BackEdge(_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNI
return frame;
}
_PyOptimizerObject *opt = interp->optimizer;
_PyExecutorObject *executor = opt->optimize(opt, frame->f_code, dest);
if (executor == NULL) {
return NULL;
_PyExecutorObject *executor;
int err = opt->optimize(opt, frame->f_code, dest, &executor);
if (err <= 0) {
if (err < 0) {
return NULL;
}
_PyFrame_SetStackPointer(frame, stack_pointer);
return frame;
Comment on lines +164 to +165
Copy link
Member

Choose a reason for hiding this comment

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

We could also complicate the signature of _PyOptimizer_BackEdge to signal this case back to the caller so we could avoid the stack pointer write, and the caller could just continue regular interpretation instead of needing to goto resume_frame and update interpreter state from the frame again. But probably not worth it I guess, if optimizers declining to optimize should be rare?

Copy link
Member Author

Choose a reason for hiding this comment

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

Most calls to _PyOptimizer_BackEdge will return the same frame. We could add a check for it in JUMP_BACKWARD, but I think we would still need to save the stack pointer.
Even if we could avoid it, I suspect a single unconditional write is faster than the additional logic to avoid it.

}
insert_executor(frame->f_code, src, index, executor);
return executor->execute(executor, frame, stack_pointer);
}


/** Test support **/


Expand Down Expand Up @@ -202,21 +207,23 @@ counter_execute(_PyExecutorObject *self, _PyInterpreterFrame *frame, PyObject **
return frame;
}

static _PyExecutorObject *
static int
counter_optimize(
_PyOptimizerObject* self,
PyCodeObject *code,
_Py_CODEUNIT *instr)
_Py_CODEUNIT *instr,
_PyExecutorObject **exec_ptr)
{
_PyCounterExecutorObject *executor = (_PyCounterExecutorObject *)_PyObject_New(&CounterExecutor_Type);
if (executor == NULL) {
return NULL;
return -1;
}
executor->executor.execute = counter_execute;
Py_INCREF(self);
executor->optimizer = (_PyCounterOptimizerObject *)self;
executor->next_instr = instr;
return (_PyExecutorObject *)executor;
*exec_ptr = (_PyExecutorObject *)executor;
return 1;
}

static PyObject *
Expand Down