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

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented Feb 14, 2024

Small PR to add type/constant propagation for _BINARY_OP_ADD/SUBTRACT/MULTIPLY_INT.

@bedevere-app bedevere-app bot mentioned this pull request Feb 14, 2024
32 tasks
@Fidget-Spinner Fidget-Spinner changed the title gh-115419: Type and constant propagation for int BINARY_OPs gh-115480: Type and constant propagation for int BINARY_OPs Feb 14, 2024
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

So IIUC, the only effect so far is that some guards may be eliminated, right?

I have a feeling that collapsing LOAD a; LOAD b; ADD into LOAD_CONST (a+b) should never overshoot the output buffer, but I know there are some gnarly corner cases, so fine to put that into a new PR.

@Fidget-Spinner Fidget-Spinner merged commit 4ebf8fb into python:main Feb 15, 2024
@Fidget-Spinner Fidget-Spinner deleted the binary_op_constant_propagate branch February 15, 2024 06:02
@Fidget-Spinner
Copy link
Member Author

So IIUC, the only effect so far is that some guards may be eliminated, right?

Yes.

goto error;
}
res = sym_new_const(ctx, temp);
// 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.

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

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

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

@markshannon
Copy link
Member

I think we should add a FAIL_IF_NULL macro to help with the NULL checks.
Then

    res = sym_new_const(ctx, temp);
    if (res == NULL) {
        goto out_of_space;
    }

becomes

    FAIL_IF_NULL(res = sym_new_const(ctx, temp));

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants