GH-104584: Plugin optimizer API (GH-105100)

This commit is contained in:
Mark Shannon 2023-06-02 11:46:18 +01:00 committed by GitHub
parent 601ae09f0c
commit 4bfa01b9d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 950 additions and 501 deletions

View file

@ -31,6 +31,7 @@
#include "dictobject.h"
#include "pycore_frame.h"
#include "opcode.h"
#include "optimizer.h"
#include "pydtrace.h"
#include "setobject.h"
#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
@ -2113,11 +2114,36 @@ dummy_func(
}
inst(JUMP_BACKWARD, (--)) {
assert(oparg < INSTR_OFFSET());
JUMPBY(-oparg);
_Py_CODEUNIT *here = next_instr - 1;
assert(oparg <= INSTR_OFFSET());
JUMPBY(1-oparg);
#if ENABLE_SPECIALIZATION
here[1].cache += (1 << OPTIMIZER_BITS_IN_COUNTER);
if (here[1].cache > tstate->interp->optimizer_backedge_threshold) {
OBJECT_STAT_INC(optimization_attempts);
frame = _PyOptimizer_BackEdge(frame, here, next_instr, stack_pointer);
if (frame == NULL) {
frame = cframe.current_frame;
goto error;
}
here[1].cache &= ((1 << OPTIMIZER_BITS_IN_COUNTER) -1);
goto resume_frame;
}
#endif /* ENABLE_SPECIALIZATION */
CHECK_EVAL_BREAKER();
}
inst(ENTER_EXECUTOR, (--)) {
_PyExecutorObject *executor = (_PyExecutorObject *)frame->f_code->co_executors->executors[oparg];
Py_INCREF(executor);
frame = executor->execute(executor, frame, stack_pointer);
if (frame == NULL) {
frame = cframe.current_frame;
goto error;
}
goto resume_frame;
}
inst(POP_JUMP_IF_FALSE, (cond -- )) {
if (Py_IsFalse(cond)) {
JUMPBY(oparg);
@ -3368,7 +3394,7 @@ dummy_func(
}
inst(INSTRUMENTED_JUMP_BACKWARD, ( -- )) {
INSTRUMENTED_JUMP(next_instr-1, next_instr-oparg, PY_MONITORING_EVENT_JUMP);
INSTRUMENTED_JUMP(next_instr-1, next_instr+1-oparg, PY_MONITORING_EVENT_JUMP);
CHECK_EVAL_BREAKER();
}

View file

@ -300,7 +300,6 @@ GETITEM(PyObject *v, Py_ssize_t i) {
#define INCREMENT_ADAPTIVE_COUNTER(COUNTER) \
do { \
assert(!ADAPTIVE_COUNTER_IS_MAX((COUNTER))); \
(COUNTER) += (1 << ADAPTIVE_BACKOFF_BITS); \
} while (0);

File diff suppressed because it is too large Load diff

View file

@ -256,8 +256,8 @@ compute_line(PyCodeObject *code, int offset, int8_t line_delta)
return PyCode_Addr2Line(code, offset * sizeof(_Py_CODEUNIT));
}
static int
instruction_length(PyCodeObject *code, int offset)
int
_PyInstruction_GetLength(PyCodeObject *code, int offset)
{
int opcode = _PyCode_CODE(code)[offset].op.code;
assert(opcode != 0);
@ -395,7 +395,7 @@ dump_instrumentation_data(PyCodeObject *code, int star, FILE*out)
dump_monitors("Active", data->active_monitors, out);
int code_len = (int)Py_SIZE(code);
bool starred = false;
for (int i = 0; i < code_len; i += instruction_length(code, i)) {
for (int i = 0; i < code_len; i += _PyInstruction_GetLength(code, i)) {
_Py_CODEUNIT *instr = &_PyCode_CODE(code)[i];
int opcode = instr->op.code;
if (i == star) {
@ -520,7 +520,7 @@ sanity_check_instrumentation(PyCodeObject *code)
CHECK(local_tools == 0xff);
}
}
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
assert(i <= code_len);
}
}
@ -1291,7 +1291,7 @@ initialize_lines(PyCodeObject *code)
int opcode = _Py_GetBaseOpcode(code, i);
int line = _PyCode_CheckLineNumber(i*(int)sizeof(_Py_CODEUNIT), &range);
line_data[i].line_delta = compute_line_delta(code, i, line);
int length = instruction_length(code, i);
int length = _PyInstruction_GetLength(code, i);
switch (opcode) {
case END_ASYNC_FOR:
case END_FOR:
@ -1332,7 +1332,7 @@ initialize_lines(PyCodeObject *code)
opcode = _Py_GetBaseOpcode(code, i);
}
oparg = (oparg << 8) | _PyCode_CODE(code)[i].op.arg;
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
int target = -1;
switch (opcode) {
case POP_JUMP_IF_FALSE:
@ -1504,7 +1504,6 @@ is_super_instruction(uint8_t opcode) {
int
_Py_Instrument(PyCodeObject *code, PyInterpreterState *interp)
{
if (is_version_up_to_date(code, interp)) {
assert(
interp->monitoring_version == 0 ||
@ -1541,7 +1540,7 @@ _Py_Instrument(PyCodeObject *code, PyInterpreterState *interp)
return 0;
}
/* Insert instrumentation */
for (int i = 0; i < code_len; i+= instruction_length(code, i)) {
for (int i = 0; i < code_len; i+= _PyInstruction_GetLength(code, i)) {
_Py_CODEUNIT *instr = &_PyCode_CODE(code)[i];
if (is_super_instruction(instr->op.code)) {
instr->op.code = _PyOpcode_Deopt[instr->op.code];
@ -1582,20 +1581,20 @@ _Py_Instrument(PyCodeObject *code, PyInterpreterState *interp)
remove_line_tools(code, i, removed_line_tools);
}
}
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
}
}
if (removed_per_instruction_tools) {
for (int i = code->_co_firsttraceable; i < code_len;) {
int opcode = _Py_GetBaseOpcode(code, i);
if (opcode == RESUME || opcode == END_FOR) {
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
continue;
}
if (removed_per_instruction_tools) {
remove_per_instruction_tools(code, i, removed_per_instruction_tools);
}
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
}
}
@ -1610,20 +1609,20 @@ _Py_Instrument(PyCodeObject *code, PyInterpreterState *interp)
add_line_tools(code, i, new_line_tools);
}
}
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
}
}
if (new_per_instruction_tools) {
for (int i = code->_co_firsttraceable; i < code_len;) {
int opcode = _Py_GetBaseOpcode(code, i);
if (opcode == RESUME || opcode == END_FOR) {
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
continue;
}
if (new_per_instruction_tools) {
add_per_instruction_tools(code, i, new_per_instruction_tools);
}
i += instruction_length(code, i);
i += _PyInstruction_GetLength(code, i);
}
}
#ifdef INSTRUMENT_DEBUG

View file

@ -265,6 +265,8 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
return 0;
case JUMP_BACKWARD:
return 0;
case ENTER_EXECUTOR:
return 0;
case POP_JUMP_IF_FALSE:
return 1;
case POP_JUMP_IF_TRUE:
@ -661,6 +663,8 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
return 0;
case JUMP_BACKWARD:
return 0;
case ENTER_EXECUTOR:
return 0;
case POP_JUMP_IF_FALSE:
return 0;
case POP_JUMP_IF_TRUE:
@ -933,6 +937,7 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {
[IMPORT_FROM] = { true, INSTR_FMT_IB },
[JUMP_FORWARD] = { true, INSTR_FMT_IB },
[JUMP_BACKWARD] = { true, INSTR_FMT_IB },
[ENTER_EXECUTOR] = { true, INSTR_FMT_IB },
[POP_JUMP_IF_FALSE] = { true, INSTR_FMT_IB },
[POP_JUMP_IF_TRUE] = { true, INSTR_FMT_IB },
[POP_JUMP_IF_NOT_NONE] = { true, INSTR_FMT_IB },

View file

@ -229,7 +229,7 @@ static void *opcode_targets[256] = {
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,
&&TARGET_ENTER_EXECUTOR,
&&_unknown_opcode,
&&_unknown_opcode,
&&_unknown_opcode,

254
Python/optimizer.c Normal file
View file

@ -0,0 +1,254 @@
#include "Python.h"
#include "opcode.h"
#include "pycore_interp.h"
#include "pycore_opcode.h"
#include "pycore_pystate.h"
#include "cpython/optimizer.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
/* Returns the index of the next space, or -1 if there is no
* more space. Doesn't set an exception. */
static int32_t
get_next_free_in_executor_array(PyCodeObject *code)
{
_PyExecutorArray *old = code->co_executors;
int size = 0;
int capacity = 0;
if (old != NULL) {
size = old->size;
capacity = old->capacity;
if (capacity >= 256) {
return -1;
}
}
assert(size <= capacity);
if (size == capacity) {
/* Array is full. Grow array */
int new_capacity = capacity ? capacity * 2 : 4;
_PyExecutorArray *new = PyMem_Realloc(
old,
offsetof(_PyExecutorArray, executors) +
new_capacity * sizeof(_PyExecutorObject *));
if (new == NULL) {
return -1;
}
new->capacity = new_capacity;
new->size = size;
code->co_executors = new;
}
assert(size < code->co_executors->capacity);
code->co_executors->size++;
return size;
}
static void
insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorObject *executor)
{
if (instr->op.code == ENTER_EXECUTOR) {
assert(index == instr->op.arg);
_PyExecutorObject *old = code->co_executors->executors[index];
executor->vm_data.opcode = old->vm_data.opcode;
executor->vm_data.oparg = old->vm_data.oparg;
old->vm_data.opcode = 0;
Py_INCREF(executor);
code->co_executors->executors[index] = executor;
Py_DECREF(old);
}
else {
Py_INCREF(executor);
executor->vm_data.opcode = instr->op.code;
executor->vm_data.oparg = instr->op.arg;
code->co_executors->executors[index] = executor;
assert(index < 256);
instr->op.code = ENTER_EXECUTOR;
instr->op.arg = index;
}
return;
}
static int
get_executor_index(PyCodeObject *code, _Py_CODEUNIT *instr)
{
if (instr->op.code == ENTER_EXECUTOR) {
return instr->op.arg;
}
else {
return get_next_free_in_executor_array(code);
}
}
int
PyUnstable_Replace_Executor(PyCodeObject *code, _Py_CODEUNIT *instr, _PyExecutorObject *new)
{
if (instr->op.code != ENTER_EXECUTOR) {
PyErr_Format(PyExc_ValueError, "No executor to replace");
return -1;
}
int index = get_executor_index(code, instr);
assert(index >= 0);
insert_executor(code, instr, index, new);
return 0;
}
static _PyExecutorObject *
error_optimize(
_PyOptimizerObject* self,
PyCodeObject *code,
_Py_CODEUNIT *instr)
{
PyErr_Format(PyExc_SystemError, "Should never call error_optimize");
return NULL;
}
static PyTypeObject DefaultOptimizer_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "noop_optimizer",
.tp_basicsize = sizeof(_PyOptimizerObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
};
_PyOptimizerObject _PyOptimizer_Default = {
PyObject_HEAD_INIT(&DefaultOptimizer_Type)
.optimize = error_optimize,
.resume_threshold = UINT16_MAX,
.backedge_threshold = UINT16_MAX,
};
_PyOptimizerObject *
PyUnstable_GetOptimizer(void)
{
PyInterpreterState *interp = PyInterpreterState_Get();
if (interp->optimizer == &_PyOptimizer_Default) {
return NULL;
}
Py_INCREF(interp->optimizer);
return interp->optimizer;
}
void
PyUnstable_SetOptimizer(_PyOptimizerObject *optimizer)
{
PyInterpreterState *interp = PyInterpreterState_Get();
if (optimizer == NULL) {
optimizer = &_PyOptimizer_Default;
}
_PyOptimizerObject *old = interp->optimizer;
Py_INCREF(optimizer);
interp->optimizer = optimizer;
interp->optimizer_backedge_threshold = optimizer->backedge_threshold;
interp->optimizer_resume_threshold = optimizer->resume_threshold;
Py_DECREF(old);
}
_PyInterpreterFrame *
_PyOptimizer_BackEdge(_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest, PyObject **stack_pointer)
{
PyInterpreterState *interp = PyInterpreterState_Get();
int index = get_executor_index(frame->f_code, src);
if (index < 0) {
_PyFrame_SetStackPointer(frame, stack_pointer);
return frame;
}
_PyOptimizerObject *opt = interp->optimizer;
_PyExecutorObject *executor = opt->optimize(opt, frame->f_code, dest);
if (executor == NULL) {
return NULL;
}
insert_executor(frame->f_code, src, index, executor);
return executor->execute(executor, frame, stack_pointer);
}
/** Test support **/
typedef struct {
_PyOptimizerObject base;
int64_t count;
} _PyCounterOptimizerObject;
typedef struct {
_PyExecutorObject executor;
_PyCounterOptimizerObject *optimizer;
_Py_CODEUNIT *next_instr;
} _PyCounterExecutorObject;
static void
counter_dealloc(_PyCounterExecutorObject *self) {
Py_DECREF(self->optimizer);
PyObject_Free(self);
}
static PyTypeObject CounterExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "counting_executor",
.tp_basicsize = sizeof(_PyCounterExecutorObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_dealloc = (destructor)counter_dealloc,
};
static _PyInterpreterFrame *
counter_execute(_PyExecutorObject *self, _PyInterpreterFrame *frame, PyObject **stack_pointer)
{
((_PyCounterExecutorObject *)self)->optimizer->count++;
_PyFrame_SetStackPointer(frame, stack_pointer);
frame->prev_instr = ((_PyCounterExecutorObject *)self)->next_instr - 1;
Py_DECREF(self);
return frame;
}
static _PyExecutorObject *
counter_optimize(
_PyOptimizerObject* self,
PyCodeObject *code,
_Py_CODEUNIT *instr)
{
_PyCounterExecutorObject *executor = (_PyCounterExecutorObject *)_PyObject_New(&CounterExecutor_Type);
if (executor == NULL) {
return NULL;
}
executor->executor.execute = counter_execute;
Py_INCREF(self);
executor->optimizer = (_PyCounterOptimizerObject *)self;
executor->next_instr = instr;
return (_PyExecutorObject *)executor;
}
static PyObject *
counter_get_counter(PyObject *self, PyObject *args)
{
return PyLong_FromLongLong(((_PyCounterOptimizerObject *)self)->count);
}
static PyMethodDef counter_methods[] = {
{ "get_count", counter_get_counter, METH_NOARGS, NULL },
{ NULL, NULL },
};
static PyTypeObject CounterOptimizer_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "Counter optimizer",
.tp_basicsize = sizeof(_PyCounterOptimizerObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_methods = counter_methods,
};
PyObject *
PyUnstable_Optimizer_NewCounter(void)
{
_PyCounterOptimizerObject *opt = (_PyCounterOptimizerObject *)_PyObject_New(&CounterOptimizer_Type);
if (opt == NULL) {
return NULL;
}
opt->base.optimize = counter_optimize;
opt->base.resume_threshold = UINT16_MAX;
opt->base.backedge_threshold = 0;
opt->count = 0;
return (PyObject *)opt;
}

View file

@ -695,6 +695,9 @@ init_interpreter(PyInterpreterState *interp,
}
interp->sys_profile_initialized = false;
interp->sys_trace_initialized = false;
interp->optimizer = &_PyOptimizer_Default;
interp->optimizer_backedge_threshold = _PyOptimizer_Default.backedge_threshold;
interp->optimizer_resume_threshold = _PyOptimizer_Default.backedge_threshold;
if (interp != &runtime->_main_interpreter) {
/* Fix the self-referential, statically initialized fields. */
interp->dtoa = (struct _dtoa_state)_dtoa_state_INIT(interp);
@ -829,6 +832,11 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
tstate->_status.cleared = 0;
}
Py_CLEAR(interp->optimizer);
interp->optimizer = &_PyOptimizer_Default;
interp->optimizer_backedge_threshold = _PyOptimizer_Default.backedge_threshold;
interp->optimizer_resume_threshold = _PyOptimizer_Default.backedge_threshold;
/* It is possible that any of the objects below have a finalizer
that runs Python code or otherwise relies on a thread state
or even the interpreter state. For now we trust that isn't