Skip to content

Conversation

@Fidget-Spinner
Copy link
Member

@Fidget-SpinnerFidget-Spinner commented Feb 14, 2024

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

@bedevere-appbedevere-appbot mentioned this pull request Feb 14, 2024
32 tasks
@Fidget-SpinnerFidget-Spinner changed the title gh-115419: Type and constant propagation for int BINARY_OPsgh-115480: Type and constant propagation for int BINARY_OPsFeb 14, 2024
Copy link
Member

@gvanrossumgvanrossum 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-SpinnerFidget-Spinner merged commit 4ebf8fb into python:mainFeb 15, 2024
@Fidget-SpinnerFidget-Spinner deleted the binary_op_constant_propagate branch February 15, 2024 06:02
@Fidget-Spinner
Copy link
MemberAuthor

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

@markshannonmarkshannonFeb 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

@markshannonmarkshannonFeb 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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@Fidget-Spinner@markshannon@gvanrossum