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 1 commit
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
13 changes: 12 additions & 1 deletion Include/internal/pycore_uops.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ typedef struct {
} _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))

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


// TODO: continue editing this from tupleobject.c to fit _PyUOpExecutorObject
static _PyUOpExecutorObject *
tuple_alloc(Py_ssize_t size)
{
_PyUOpExecutorObject *op = maybe_freelist_pop(size);
if (op == NULL) {
/* Check for overflow */
if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - (sizeof(PyTupleObject) -
sizeof(PyObject *))) / sizeof(PyObject *)) {
return (_PyUOpExecutorObject *)PyErr_NoMemory();
}
op = PyObject_GC_NewVar(_PyUOpExecutorObject, &PyTuple_Type, size);
if (op == NULL)
return NULL;
}
return op;
}

PyObject *
PyUOpExecutor_New(_PyUOpInstruction trace[], Py_ssize_t size)
{
_PyUOpExecutorObject *op;
op = tuple_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
12 changes: 5 additions & 7 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 @@ -703,6 +697,10 @@ uop_optimize(
return -1;
}
executor->base.execute = _PyUopExecute;
/*
This should replace PyObject_New and memcpy
_PyUOpExecutorObject *executor = PyUOpExecutor_New(trace, trace_length);
*/
memcpy(executor->trace, trace, trace_length * sizeof(_PyUOpInstruction));
if (trace_length < _Py_UOP_MAX_TRACE_LENGTH) {
executor->trace[trace_length].opcode = 0; // Sentinel
Expand Down