Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 8 additions & 7 deletions Include/internal/pycore_uop_ids.h

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

2 changes: 2 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.

5 changes: 5 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4072,6 +4072,11 @@ dummy_func(
TIER_TWO_ONLY
value = ptr;
}
pure op (_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
TIER_TWO_ONLY;
DECREF_INPUTS();
value = ptr;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there could be an even more specialized opcode that doesn't DECREF its input? That would be handy for the case where you introduced this -- the input is known to be None, so we don't really need to bother with the DECREF of the immortal value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expectation is that the _POP_TOP part will be removed in a later pass.
LOAD_CONST ; _POP_TOP_LOAD_CONST_INLINE_BORROW -> _LOAD_CONST_INLINE_BORROW


pure op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) {
TIER_TWO_ONLY
Expand Down
12 changes: 12 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.

7 changes: 7 additions & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,13 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
} \
} while (0);

#define ERROR_IF(COND, LABEL) \
do { \
if (COND) { \
goto LABEL; \
} \
} while (0);

#define _LOAD_ATTR_NOT_NULL \
do { \
OUT_OF_SPACE_IF_NULL(attr = sym_new_known_notnull(ctx)); \
Expand Down
80 changes: 59 additions & 21 deletions Python/tier2_redundancy_eliminator_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ dummy_func(void) {
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there should be s get_long_const() helper which returns (PyLongObject *)get_const(x) and also includes the assert(PyLong_CheckExact()) call, to make code like this more compact. (Same for float.)

if (temp == NULL) {
goto error;
}
ERROR_IF(temp == NULL, error);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
Expand All @@ -102,9 +100,7 @@ dummy_func(void) {
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
ERROR_IF(temp == NULL, error);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
Expand All @@ -120,9 +116,7 @@ dummy_func(void) {
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
ERROR_IF(temp == NULL, error);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, temp));
// TODO gh-115506:
// replace opcode with constant propagated one and add tests!
Expand All @@ -139,9 +133,7 @@ dummy_func(void) {
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(get_const(left)) +
PyFloat_AS_DOUBLE(get_const(right)));
if (temp == NULL) {
goto error;
}
ERROR_IF(temp == NULL, error);
res = sym_new_const(ctx, temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
Expand All @@ -158,9 +150,7 @@ dummy_func(void) {
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(get_const(left)) -
PyFloat_AS_DOUBLE(get_const(right)));
if (temp == NULL) {
goto error;
}
ERROR_IF(temp == NULL, error);
res = sym_new_const(ctx, temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
Expand All @@ -177,9 +167,7 @@ dummy_func(void) {
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(get_const(left)) *
PyFloat_AS_DOUBLE(get_const(right)));
if (temp == NULL) {
goto error;
}
ERROR_IF(temp == NULL, error);
res = sym_new_const(ctx, temp);
// TODO gh-115506:
// replace opcode with constant propagated one and update tests!
Expand All @@ -189,6 +177,58 @@ dummy_func(void) {
}
}

op(_TO_BOOL, (value -- res)) {
(void)value;
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyBool_Type));
}

op(_TO_BOOL_BOOL, (value -- value)) {
if (sym_matches_type(value, &PyBool_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
else {
sym_set_type(value, &PyBool_Type);
}
}

op(_TO_BOOL_INT, (value -- res)) {
sym_set_type(value, &PyLong_Type);
if (is_const(value)) {
PyObject *load = _PyLong_IsZero((PyLongObject *)get_const(value))
? Py_False : Py_True;
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyBool_Type));
}
}

op(_TO_BOOL_LIST, (value -- res)) {
sym_set_type(value, &PyList_Type);
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyBool_Type));
}

op(_TO_BOOL_NONE, (value -- res)) {
if (get_const(value) == Py_None) {
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_None);
}
sym_set_type(value, &_PyNone_Type);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False));
}

op(_TO_BOOL_STR, (value -- res)) {
sym_set_type(value, &PyUnicode_Type);
if (is_const(value)) {
PyObject *load = get_const(value) == &_Py_STR(empty) ? Py_False : Py_True;
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
}
else {
OUT_OF_SPACE_IF_NULL(res = sym_new_known_type(ctx, &PyBool_Type));
}
}

op(_LOAD_CONST, (-- value)) {
// There should be no LOAD_CONST. It should be all
// replaced by peephole_opt.
Expand Down Expand Up @@ -270,9 +310,7 @@ dummy_func(void) {
(void)callable;

PyFunctionObject *func = (PyFunctionObject *)(this_instr + 2)->operand;
if (func == NULL) {
goto error;
}
ERROR_IF(func == NULL, error);
PyCodeObject *co = (PyCodeObject *)func->func_code;

assert(self_or_null != NULL);
Expand Down
Loading