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 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
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
96 changes: 75 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,74 @@ dummy_func(void) {
}
}

op(_TO_BOOL, (value -- res)) {
sym_set_type(value, &PyBool_Type);
if (is_const(value)) {
int err = PyObject_IsTrue(get_const(value));
ERROR_IF(err < 0, error);
res = sym_new_const(ctx, err ? Py_True : Py_False);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
}

op(_TO_BOOL_BOOL, (value -- value)) {
sym_set_type(value, &PyBool_Type);
}

op(_TO_BOOL_INT, (value -- res)) {
sym_set_type(value, &PyLong_Type);
if (is_const(value)) {
PyObject *temp = NULL;
if (_PyLong_IsZero((PyLongObject *)get_const(value))) {
assert(_Py_IsImmortal(get_const(value)));
temp = Py_False;
}
else {
temp = Py_True;
}
res = sym_new_const(ctx, temp);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
}

op(_TO_BOOL_LIST, (value -- res)) {
sym_set_type(value, &PyList_Type);
if (is_const(value)) {
res = sym_new_const(ctx, Py_SIZE(get_const(value)) ? Py_True : Py_False);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
}

op(_TO_BOOL_NONE, (value -- res)) {
sym_set_type(value, &_PyNone_Type);
res = sym_new_const(ctx, Py_False);
}

op(_TO_BOOL_STR, (value -- res)) {
sym_set_type(value, &PyUnicode_Type);
if (is_const(value)) {
PyObject *temp = NULL;
if (get_const(value) == &_Py_STR(empty)) {
assert(_Py_IsImmortal(get_const(value)));
temp = Py_False;
}
else {
assert(Py_SIZE(get_const(value)));
temp = Py_True;
}
res = sym_new_const(ctx, temp);
}
else {
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 +326,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
100 changes: 69 additions & 31 deletions Python/tier2_redundancy_eliminator_cases.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,45 +104,97 @@
}

case _TO_BOOL: {
_Py_UOpsSymType *value;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
value = stack_pointer[-1];
sym_set_type(value, &PyBool_Type);
if (is_const(value)) {
int err = PyObject_IsTrue(get_const(value));
ERROR_IF(err < 0, error);
res = sym_new_const(ctx, err ? Py_True : Py_False);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
stack_pointer[-1] = res;
break;
}

case _TO_BOOL_BOOL: {
_Py_UOpsSymType *value;
value = stack_pointer[-1];
sym_set_type(value, &PyBool_Type);
break;
}

case _TO_BOOL_INT: {
_Py_UOpsSymType *value;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
value = stack_pointer[-1];
sym_set_type(value, &PyLong_Type);
if (is_const(value)) {
PyObject *temp = NULL;
if (_PyLong_IsZero((PyLongObject *)get_const(value))) {
assert(_Py_IsImmortal(get_const(value)));
temp = Py_False;
}
else {
temp = Py_True;
}
res = sym_new_const(ctx, temp);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
stack_pointer[-1] = res;
break;
}

case _TO_BOOL_LIST: {
_Py_UOpsSymType *value;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
value = stack_pointer[-1];
sym_set_type(value, &PyList_Type);
if (is_const(value)) {
res = sym_new_const(ctx, Py_SIZE(get_const(value)) ? Py_True : Py_False);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
stack_pointer[-1] = res;
break;
}

case _TO_BOOL_NONE: {
_Py_UOpsSymType *value;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
value = stack_pointer[-1];
sym_set_type(value, &_PyNone_Type);
res = sym_new_const(ctx, Py_False);
stack_pointer[-1] = res;
break;
}

case _TO_BOOL_STR: {
_Py_UOpsSymType *value;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
value = stack_pointer[-1];
sym_set_type(value, &PyUnicode_Type);
if (is_const(value)) {
PyObject *temp = NULL;
if (get_const(value) == &_Py_STR(empty)) {
assert(_Py_IsImmortal(get_const(value)));
temp = Py_False;
}
else {
assert(Py_SIZE(get_const(value)));
temp = Py_True;
}
res = sym_new_const(ctx, temp);
}
else {
res = sym_new_known_type(ctx, &PyBool_Type);
}
stack_pointer[-1] = res;
break;
}
Expand Down Expand Up @@ -188,9 +240,7 @@
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 @@ -214,9 +264,7 @@
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Add((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 @@ -240,9 +288,7 @@
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 Down Expand Up @@ -281,9 +327,7 @@
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 @@ -308,9 +352,7 @@
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 @@ -335,9 +377,7 @@
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 Down Expand Up @@ -1376,9 +1416,7 @@
int argcount = oparg;
(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);
assert(args != NULL);
Expand Down