Skip to content
Merged
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
Prev Previous commit
Next Next commit
Add len() and indexing support to uop superblocks.
  • Loading branch information
markshannon committed Jul 4, 2023
commit 46e99e6f59fa36e077392499d82a52927a5db61a
9 changes: 6 additions & 3 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Python/opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 45 additions & 4 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ uop_name(int index) {
return _PyOpcode_uop_name[index];
}

#ifdef Py_DEBUG
static PyObject *
uop_str(_PyUOpExecutorObject *self)
{
Expand Down Expand Up @@ -352,7 +351,50 @@ uop_str(_PyUOpExecutorObject *self)
_PyUnicodeWriter_Dealloc(&writer);
return NULL;
}
#endif

static Py_ssize_t
uop_len(_PyUOpExecutorObject *self)
{
int count = 1;
for (; count < _Py_UOP_MAX_TRACE_LENGTH; count++) {
if (self->trace[count-1].opcode == EXIT_TRACE) {
break;
}
}
return count;
}

static PyObject *
uop_item(_PyUOpExecutorObject *self, Py_ssize_t index)
{
for (int i = 0; i < _Py_UOP_MAX_TRACE_LENGTH; i++) {
if (self->trace[i].opcode == EXIT_TRACE) {
break;
}
if (i != index) {
continue;
}
const char *name = uop_name(self->trace[i].opcode);
PyObject *oname = _PyUnicode_FromASCII(name, strlen(name));
if (oname == NULL) {
return NULL;
}
PyObject *operand = PyLong_FromUnsignedLongLong(self->trace[i].operand);
if (operand == NULL) {
Py_DECREF(oname);
return NULL;
}
PyObject *args[2] = { oname, operand };
return _PyTuple_FromArraySteal(args, 2);
}
PyErr_SetNone(PyExc_IndexError);
return NULL;
}

PySequenceMethods uop_as_sequence = {
.sq_length = (lenfunc)uop_len,
.sq_item = (ssizeargfunc)uop_item,
};

static PyTypeObject UOpExecutor_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
Expand All @@ -361,9 +403,8 @@ static PyTypeObject UOpExecutor_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.tp_dealloc = (destructor)uop_dealloc,
#ifdef Py_DEBUG
.tp_str = (reprfunc)uop_str,
#endif
.tp_as_sequence = &uop_as_sequence,
};

static int
Expand Down
7 changes: 4 additions & 3 deletions Tools/build/generate_opcode_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,15 @@ def main(opcode_py,
fobj.write(f"#define ENABLE_SPECIALIZATION {int(ENABLE_SPECIALIZATION)}")

iobj.write("\n")
iobj.write("#ifdef Py_DEBUG\n")
iobj.write(f"static const char *const _PyOpcode_OpName[{NUM_OPCODES}] = {{\n")
iobj.write(f"\nextern const char *const _PyOpcode_OpName[{NUM_OPCODES}];\n")
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
iobj.write(f"const char *const _PyOpcode_OpName[{NUM_OPCODES}] = {{\n")
for op, name in enumerate(opname_including_specialized):
if name[0] != "<":
op = name
iobj.write(f''' [{op}] = "{name}",\n''')
iobj.write("};\n")
iobj.write("#endif\n")
iobj.write("#endif // NEED_OPCODE_TABLES\n")

iobj.write("\n")
iobj.write("#define EXTRA_CASES \\\n")
Expand Down
6 changes: 2 additions & 4 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,7 @@ def write_metadata(self) -> None:
self.out.emit("#ifndef NEED_OPCODE_METADATA")
self.out.emit("extern const struct opcode_metadata _PyOpcode_opcode_metadata[512];")
self.out.emit("extern const struct opcode_macro_expansion _PyOpcode_macro_expansion[256];")
self.out.emit("#ifdef Py_DEBUG")
self.out.emit("extern const char * const _PyOpcode_uop_name[512];")
self.out.emit("#endif")
self.out.emit("#else")

self.out.emit("const struct opcode_metadata _PyOpcode_opcode_metadata[512] = {")
Expand Down Expand Up @@ -1269,10 +1267,10 @@ def write_metadata(self) -> None:
case _:
typing.assert_never(thing)

self.out.emit("#ifdef Py_DEBUG")
self.out.emit("#ifdef NEED_OPCODE_METADATA")
with self.out.block("const char * const _PyOpcode_uop_name[512] =", ";"):
self.write_uop_items(lambda name, counter: f"[{counter}] = \"{name}\",")
self.out.emit("#endif")
self.out.emit("#endif // NEED_OPCODE_METADATA")

self.out.emit("#endif")

Expand Down