Skip to content

Commit e8ecb9e

Browse files
authored
GH-104584: Allow optimizers to opt out of optimizing. (GH-105244)
1 parent 9efaff5 commit e8ecb9e

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

Include/cpython/optimizer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ typedef struct _PyExecutorObject {
2121

2222
typedef struct _PyOptimizerObject _PyOptimizerObject;
2323

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

2627
typedef struct _PyOptimizerObject {
2728
PyObject_HEAD

Python/optimizer.c

+18-11
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ PyUnstable_Replace_Executor(PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutor
9393
return 0;
9494
}
9595

96-
static _PyExecutorObject *
96+
static int
9797
error_optimize(
9898
_PyOptimizerObject* self,
9999
PyCodeObject *code,
100-
_Py_CODEUNIT *instr)
100+
_Py_CODEUNIT *instr,
101+
_PyExecutorObject **exec)
101102
{
102103
PyErr_Format(PyExc_SystemError, "Should never call error_optimize");
103-
return NULL;
104+
return -1;
104105
}
105106

106107
static PyTypeObject DefaultOptimizer_Type = {
@@ -154,15 +155,19 @@ _PyOptimizer_BackEdge(_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNI
154155
return frame;
155156
}
156157
_PyOptimizerObject *opt = interp->optimizer;
157-
_PyExecutorObject *executor = opt->optimize(opt, frame->f_code, dest);
158-
if (executor == NULL) {
159-
return NULL;
158+
_PyExecutorObject *executor;
159+
int err = opt->optimize(opt, frame->f_code, dest, &executor);
160+
if (err <= 0) {
161+
if (err < 0) {
162+
return NULL;
163+
}
164+
_PyFrame_SetStackPointer(frame, stack_pointer);
165+
return frame;
160166
}
161167
insert_executor(frame->f_code, src, index, executor);
162168
return executor->execute(executor, frame, stack_pointer);
163169
}
164170

165-
166171
/** Test support **/
167172

168173

@@ -202,21 +207,23 @@ counter_execute(_PyExecutorObject *self, _PyInterpreterFrame *frame, PyObject **
202207
return frame;
203208
}
204209

205-
static _PyExecutorObject *
210+
static int
206211
counter_optimize(
207212
_PyOptimizerObject* self,
208213
PyCodeObject *code,
209-
_Py_CODEUNIT *instr)
214+
_Py_CODEUNIT *instr,
215+
_PyExecutorObject **exec_ptr)
210216
{
211217
_PyCounterExecutorObject *executor = (_PyCounterExecutorObject *)_PyObject_New(&CounterExecutor_Type);
212218
if (executor == NULL) {
213-
return NULL;
219+
return -1;
214220
}
215221
executor->executor.execute = counter_execute;
216222
Py_INCREF(self);
217223
executor->optimizer = (_PyCounterOptimizerObject *)self;
218224
executor->next_instr = instr;
219-
return (_PyExecutorObject *)executor;
225+
*exec_ptr = (_PyExecutorObject *)executor;
226+
return 1;
220227
}
221228

222229
static PyObject *

0 commit comments

Comments
 (0)