Skip to content

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

Merged
Fidget-Spinner merged 2 commits into
python:mainfrom
Fidget-Spinner:binary_op_constant_propagate
Feb 15, 2024
Merged

gh-115480: Type and constant propagation for int BINARY_OPs#115478
Fidget-Spinner merged 2 commits into
python:mainfrom
Fidget-Spinner:binary_op_constant_propagate

Conversation

@Fidget-Spinner

@Fidget-SpinnerFidget-Spinner commented Feb 14, 2024

Copy link
Copy Markdown
Member

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
Comment threadPython/tier2_redundancy_eliminator_bytecodes.c

@gvanrossumgvanrossum left a comment

Copy link
Copy Markdown
Member

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:mainFeb 15, 2024
@Fidget-Spinner
Fidget-Spinner deleted the binary_op_constant_propagate branch February 15, 2024 06:02
@Fidget-Spinner

Copy link
Copy Markdown
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!

@markshannonmarkshannonFeb 15, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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),

@markshannonmarkshannonFeb 21, 2024

Copy link
Copy Markdown
Member

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