Skip to content

gh-115480: Type and constant propagation for int BINARY_OPs #115478

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
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ sym_new_const(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val)
return temp;
}

static inline bool
is_const(_Py_UOpsSymType *sym)
{
return sym->const_val != NULL;
}

static inline PyObject *
get_const(_Py_UOpsSymType *sym)
{
return sym->const_val;
}

static _Py_UOpsSymType*
sym_new_null(_Py_UOpsAbstractInterpContext *ctx)
{
Expand Down
62 changes: 56 additions & 6 deletions Python/tier2_redundancy_eliminator_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,62 @@ dummy_func(void) {


op(_BINARY_OP_ADD_INT, (left, right -- res)) {
// TODO constant propagation
(void)left;
(void)right;
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left),
Copy link
Member

@markshannon markshannon Feb 21, 2024

Choose a reason for hiding this comment

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

This may leak, if temp is mortal.
Likewise for the other _BINARY_OP_... below.

Symbols hold a strong reference to constants, so there is no leak.

(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Copy link
Member

Choose a reason for hiding this comment

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

Missing NULL check

// TODO replace opcode with constant propagated one and add tests!
Copy link
Member

@markshannon markshannon Feb 15, 2024

Choose a reason for hiding this comment

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

In future, please make a new issue for this sort of TODOs.

}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
}

op(_BINARY_OP_SUBTRACT_INT, (left, right -- res)) {
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Copy link
Member

Choose a reason for hiding this comment

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

Missing NULL check

// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
}

op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
Copy link
Member

Choose a reason for hiding this comment

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

Missing NULL check

// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
}

Expand Down
68 changes: 58 additions & 10 deletions Python/tier2_redundancy_eliminator_cases.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,28 @@
}

case _BINARY_OP_MULTIPLY_INT: {
_Py_UOpsSymType *right;
_Py_UOpsSymType *left;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
right = stack_pointer[-1];
left = stack_pointer[-2];
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Multiply((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
stack_pointer[-2] = res;
stack_pointer += -1;
break;
Expand All @@ -194,22 +213,51 @@
_Py_UOpsSymType *res;
right = stack_pointer[-1];
left = stack_pointer[-2];
// TODO constant propagation
(void)left;
(void)right;
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Add((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
stack_pointer[-2] = res;
stack_pointer += -1;
break;
}

case _BINARY_OP_SUBTRACT_INT: {
_Py_UOpsSymType *right;
_Py_UOpsSymType *left;
_Py_UOpsSymType *res;
res = sym_new_unknown(ctx);
if (res == NULL) goto out_of_space;
right = stack_pointer[-1];
left = stack_pointer[-2];
if (is_const(left) && is_const(right)) {
assert(PyLong_CheckExact(get_const(left)));
assert(PyLong_CheckExact(get_const(right)));
PyObject *temp = _PyLong_Subtract((PyLongObject *)get_const(left),
(PyLongObject *)get_const(right));
if (temp == NULL) {
goto error;
}
res = sym_new_const(ctx, temp);
// TODO replace opcode with constant propagated one and add tests!
}
else {
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
stack_pointer[-2] = res;
stack_pointer += -1;
break;
Expand Down