Skip to content

gh-115685: Type/values propagate for TO_BOOL in tier 2 #115686

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

Merged
merged 16 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
41 changes: 21 additions & 20 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 @@ -4081,6 +4081,11 @@ dummy_func(
TIER_TWO_ONLY
value = ptr;
}
pure op (_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
TIER_TWO_ONLY;
Py_DECREF(pop);
value = ptr;
}

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.

15 changes: 15 additions & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ sym_set_null(_Py_UOpsSymType *sym)
sym_set_flag(sym, KNOWN);
}

static inline void
sym_set_const(_Py_UOpsSymType *sym, PyObject *const_val)
{
sym_set_type(sym, Py_TYPE(const_val));
sym_set_flag(sym, TRUE_CONST | KNOWN | NOT_NULL);
Py_XSETREF(sym->const_val, Py_NewRef(const_val));
Copy link
Member Author

Choose a reason for hiding this comment

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

This is temporary, just ignore this for now. I will remove it in #115817.

assert(sym_is_const(sym));
}

static inline _Py_UOpsSymType*
sym_new_unknown(_Py_UOpsAbstractInterpContext *ctx)
Expand Down Expand Up @@ -596,6 +604,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
87 changes: 63 additions & 24 deletions Python/tier2_redundancy_eliminator_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ dummy_func(void) {
assert(PyLong_CheckExact(sym_get_const(right)));
PyObject *temp = _PyLong_Add((PyLongObject *)sym_get_const(left),
(PyLongObject *)sym_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 @@ -111,9 +109,7 @@ dummy_func(void) {
assert(PyLong_CheckExact(sym_get_const(right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)sym_get_const(left),
(PyLongObject *)sym_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 @@ -129,9 +125,7 @@ dummy_func(void) {
assert(PyLong_CheckExact(sym_get_const(right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)sym_get_const(left),
(PyLongObject *)sym_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 @@ -148,10 +142,8 @@ dummy_func(void) {
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(sym_get_const(left)) +
PyFloat_AS_DOUBLE(sym_get_const(right)));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
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 update tests!
}
Expand All @@ -167,10 +159,8 @@ dummy_func(void) {
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(sym_get_const(left)) -
PyFloat_AS_DOUBLE(sym_get_const(right)));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
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 update tests!
}
Expand All @@ -186,10 +176,8 @@ dummy_func(void) {
PyObject *temp = PyFloat_FromDouble(
PyFloat_AS_DOUBLE(sym_get_const(left)) *
PyFloat_AS_DOUBLE(sym_get_const(right)));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
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 update tests!
}
Expand All @@ -198,6 +186,59 @@ dummy_func(void) {
}
}

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

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 (sym_is_const(value)) {
PyObject *load = _PyLong_IsZero((PyLongObject *)sym_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 (sym_get_const(value) == Py_None) {
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_False);
}
sym_set_const(value, Py_None);
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 (sym_is_const(value)) {
PyObject *load = sym_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 @@ -312,9 +353,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