Skip to content

Commit

Permalink
Bump the magic number and create optimized _POP_TOP variants
Browse files Browse the repository at this point in the history
  • Loading branch information
brandtbucher committed Dec 7, 2024
1 parent a925ba9 commit 50038ae
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 34 deletions.
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ Known values:
Python 3.14a1 3607 (Add pseudo instructions JUMP_IF_TRUE/FALSE)
Python 3.14a1 3608 (Add support for slices)
Python 3.14a2 3609 (Add LOAD_SMALL_INT and LOAD_CONST_IMMORTAL instructions, remove RETURN_CONST)
Python 3.14a3 3610 (Use additional scratch space for BINARY_OP)
Python 3.15 will start with 3650
Expand All @@ -274,7 +275,7 @@ PC/launcher.c must also be updated.
*/

#define PYC_MAGIC_NUMBER 3609
#define PYC_MAGIC_NUMBER 3610
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
64 changes: 34 additions & 30 deletions Include/internal/pycore_uop_ids.h

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

16 changes: 16 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

24 changes: 24 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,30 @@ dummy_func(
DECREF_INPUTS();
}

pure op(_POP_TOP_IMMORTAL, (pop --)) {
PyObject *pop_o = PyStackRef_AsPyObjectBorrow(pop);
assert(_Py_IsImmortal(pop_o));
INPUTS_DEAD();
}

pure op(_POP_TOP_INT, (pop --)) {
PyObject *pop_o = PyStackRef_AsPyObjectBorrow(pop);
assert(PyLong_CheckExact(pop_o));
PyStackRef_CLOSE_SPECIALIZED(pop, (destructor)PyObject_Free);
}

pure op(_POP_TOP_FLOAT, (pop --)) {
PyObject *pop_o = PyStackRef_AsPyObjectBorrow(pop);
assert(PyFloat_CheckExact(pop_o));
PyStackRef_CLOSE_SPECIALIZED(pop, _PyFloat_ExactDealloc);
}

pure op(_POP_TOP_UNICODE, (pop --)) {
PyObject *pop_o = PyStackRef_AsPyObjectBorrow(pop);
assert(PyUnicode_CheckExact(pop_o));
PyStackRef_CLOSE_SPECIALIZED(pop, _PyUnicode_ExactDealloc);
}

pure inst(PUSH_NULL, (-- res)) {
res = PyStackRef_NULL;
}
Expand Down
43 changes: 43 additions & 0 deletions Python/executor_cases.c.h

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

21 changes: 21 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,12 @@ dummy_func(void) {
top_out = top_in;
}

op(_SWAP_2, (bottom_in, skip_in, top_in -- top_out, skip_out, bottom_out)) {
bottom_out = bottom_in;
skip_out = skip_in;
top_out = top_in;
}

op(_LOAD_ATTR_INSTANCE_VALUE, (offset/1, owner -- attr, null if (oparg & 1))) {
attr = sym_new_not_null(ctx);
null = sym_new_null(ctx);
Expand Down Expand Up @@ -875,6 +881,21 @@ dummy_func(void) {
self_or_null = sym_new_unknown(ctx);
}

op(_POP_TOP, (pop --)) {
if (sym_is_const(pop) && _Py_IsImmortal(pop)) {
REPLACE_OP(this_instr, _POP_TOP_IMMORTAL, 0, 0);
}
else if (sym_matches_type(pop, &PyLong_Type)) {
REPLACE_OP(this_instr, _POP_TOP_INT, 0, 0);
}
else if (sym_matches_type(pop, &PyFloat_Type)) {
REPLACE_OP(this_instr, _POP_TOP_FLOAT, 0, 0);
}
else if (sym_matches_type(pop, &PyUnicode_Type)) {
REPLACE_OP(this_instr, _POP_TOP_UNICODE, 0, 0);
}
}

op(_JUMP_TO_TOP, (--)) {
ctx->done = true;
}
Expand Down
56 changes: 53 additions & 3 deletions Python/optimizer_cases.c.h

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

0 comments on commit 50038ae

Please sign in to comment.