Skip to content

[WIP] gh-106608: Have _PyUOpExecutorObject use a variable-length array #107411

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

Closed
wants to merge 6 commits into from
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
16 changes: 15 additions & 1 deletion Include/internal/pycore_uops.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,36 @@ extern "C" {

#define _Py_UOP_MAX_TRACE_LENGTH 32

PyAPI_DATA(PyTypeObject) UOpExecutor_Type;

typedef struct {
uint32_t opcode;
uint32_t oparg;
uint64_t operand; // A cache entry
} _PyUOpInstruction;

typedef struct {
PyObject_VAR_HEAD
_PyExecutorObject base;
_PyUOpInstruction trace[_Py_UOP_MAX_TRACE_LENGTH]; // TODO: variable length
_PyUOpInstruction trace[1];
} _PyUOpExecutorObject;

_PyInterpreterFrame *_PyUopExecute(
_PyExecutorObject *executor,
_PyInterpreterFrame *frame,
PyObject **stack_pointer);

/* Cast argument to _PyUOpExecutorObject* type. */
#define _PyUOpExecutor_CAST(op) \
_Py_CAST(_PyUOpExecutorObject*, (op))

static inline Py_ssize_t PyUOpExecutor_GET_SIZE(PyObject *op) {
_PyUOpExecutorObject *executor = _PyUOpExecutor_CAST(op);
return Py_SIZE(executor);
}
#define PyUOpExecutor_GET_SIZE(op) PyUOpExecutor_GET_SIZE(_PyObject_CAST(op))
PyAPI_FUNC(PyObject *) PyUOpExecutor_New(_PyUOpInstruction trace[], Py_ssize_t size);

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 24 additions & 0 deletions Python/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@
#undef ENABLE_SPECIALIZATION
#define ENABLE_SPECIALIZATION 0

static _PyUOpExecutorObject *
PyUOpExecutor_alloc(Py_ssize_t size)
{
_PyUOpExecutorObject *op = PyObject_GC_NewVar(_PyUOpExecutorObject, &UOpExecutor_Type, size);
if (op == NULL) {
return NULL;
}
return op;
}

PyObject *
PyUOpExecutor_New(_PyUOpInstruction trace[], Py_ssize_t size)
{
_PyUOpExecutorObject *op;
op = PyUOpExecutor_alloc(size);
if (op == NULL) {
return NULL;
}
for (Py_ssize_t i = 0; i < size; i++) {
op->trace[i] = trace[i];
}
_PyObject_GC_TRACK(op);
return (PyObject *) op;
}

_PyInterpreterFrame *
_PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **stack_pointer)
Expand Down
19 changes: 3 additions & 16 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,7 @@ uop_name(int index) {
static Py_ssize_t
uop_len(_PyUOpExecutorObject *self)
{
int count = 0;
for (; count < _Py_UOP_MAX_TRACE_LENGTH; count++) {
if (self->trace[count].opcode == 0) {
break;
}
}
return count;
return PyUOpExecutor_GET_SIZE(self);
}

static PyObject *
Expand Down Expand Up @@ -365,7 +359,7 @@ PySequenceMethods uop_as_sequence = {
.sq_item = (ssizeargfunc)uop_item,
};

static PyTypeObject UOpExecutor_Type = {
PyTypeObject UOpExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "uop_executor",
.tp_basicsize = sizeof(_PyUOpExecutorObject),
Expand Down Expand Up @@ -699,15 +693,8 @@ uop_optimize(
return trace_length;
}
OBJECT_STAT_INC(optimization_traces_created);
_PyUOpExecutorObject *executor = PyObject_New(_PyUOpExecutorObject, &UOpExecutor_Type);
if (executor == NULL) {
return -1;
}
_PyUOpExecutorObject *executor = (_PyUOpExecutorObject *) PyUOpExecutor_New(trace, trace_length);
executor->base.execute = _PyUopExecute;
memcpy(executor->trace, trace, trace_length * sizeof(_PyUOpInstruction));
if (trace_length < _Py_UOP_MAX_TRACE_LENGTH) {
executor->trace[trace_length].opcode = 0; // Sentinel
}
*exec_ptr = (_PyExecutorObject *)executor;
return 1;
}
Expand Down