-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-132732: Automatically constant evaluate pure operations #132733
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
base: main
Are you sure you want to change the base?
Conversation
Misc/NEWS.d/next/Core_and_Builtins/2025-04-19-16-22-47.gh-issue-132732.jgqhlF.rst
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really neat!
Other than two opcodes I found that shouldn't be marked pure
, I just have one thought:
Rather than rewriting the bodies like this to use the symbols-manipulating functions (which seems error-prone), would we be able to just use stackrefs to do this?
For example, _BINARY_OP_ADD_INT
is defined like this:
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
// ...
res = PyStackRef_FromPyObjectSteal(res_o);
Rather than rewriting uses of these functions, could it be easier to just do something like this, since we're guranteed not to escape?
if (sym_is_const(ctx, stack_pointer[-2]) && sym_is_const(ctx, stack_pointer[-1])) {
// Generated code to turn constant symbols into stackrefs:
_PyStackRef left = PyStackRef_FromPyObjectBorrow(sym_get_const(ctx, stack_pointer[-2]));
_PyStackRef right = PyStackRef_FromPyObjectBorrow(sym_get_const(ctx, stack_pointer[-1]));
_PyStackRef res;
// Now the actual body, same as it appears in executor_cases.c.h:
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
// ...
res = PyStackRef_FromPyObjectSteal(res_o);
// Generated code to turn stackrefs into constant symbols:
stack_pointer[-1] = sym_new_const(ctx, PyStackRef_AsPyObjectSteal(res));
}
I'm not too familiar with the design of the cases generator though, so maybe this is way harder or something. Either way, I'm excited to see this get in!
Seems feasible. I could try to rewrite all occurences of the variable with a stackref-producing const one. Let me try that. |
I've verified no refleak on |
There's a lot going on in this PR, probably too much for one PR. Could we start with a PR to fix up the |
Could we have the default code generator generate a function for the body of the pure instruction and then call that from the three interpreters? |
Hm, I think I’d prefer not to. Sounds like it could hurt performance, especially for the JIT (where things can’t inline). |
I think a good progression would be:
|
I thought about this and I think we can inline if we autogenerate a header file and include that directly. But then we're at the mercy of the compiler in both the normal interpreter and the JIT deciding to inline or not to inline the body again. Which I truly do not want. |
@brandtbucher @markshannon what can I do to get this PR moving? @tomasr8 if youd like to review, here's a summary of the PR:
|
Thanks for the ping! I actually wanted to try/review this PR, I was just very busy this week with work :/ I'll have a look this weekend :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only had time to skim the PR, I'll do a more thorough review this weekend :)
Co-Authored-By: Tomas R. <[email protected]>
I've added the required functions to the allowlist, so I removed the is_abstract workaround. |
@markshannon it seems this is wrong. |
I have a fix in a separate PR for the longobject GC issues. |
I think we only specialize for, and are interested in compact ints (or tagged ints in the future), so maybe replace |
For now, I'm avoiding changing the int operations in this PR. I will add back constant evaluation for them in the future once we fix this in either bytecodes.c or the long object. |
I've implemented code for deopt_if, error_if and addressed all review comments. Is there anything left? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a maintenance perspective, I'm still not happy about this approach, due to the amount (and complexity) of code this adds to the code generators. It also adds a lot of bulk to the generated code as all the BINARY_OP...
variants get a copy of the code in bytecodes.c as well as the code in optimizer_bytecodes.c (although that's much less an issue than the maintenance one).
How about specifying the function to do the evaluation in the macro?
In other words, instead of REPLACE_OPCODE_IF_EVALUATES_PURE(left, right)
we would write REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, PyNumber_Add)
.
Then we wouldn't need to parse the original opcode, just wrap the call to PyNumber_Add
That sadly wouldn't work for the next thing we plan to add this to:
This has already paid for itself from a maintenance perspective:
If you're worried about the bulk of the generated code, I can open an issue for someone to make a follow up PR to generate function templates from the bytecodes.c file, similar to what we already do for the tail calling interpreter. This would allow us to just call the function. That should go into a follow-up commit though, because it requires changes that affect more than just the optimizer (it will also touch executor_cases and such). |
|
Does this work for |
Doesn't that defeat the purpose of this DSL addition, because we'd have to modify |
It should, let me try |
No need to modify I know that doing this all automatically seems purer and more elegant, and it probably is, but the code generator is a bit of a pain point in terms of maintenance. If we can keep it simple with a bit of extra work elsewhere it is usually worth it. One other thing. Ultimately we will want to move this functionality out of the |
In case it wasn't clear. The helper functions would be: |
Is this the same as this?
|
Uh oh!
There was an error while loading. Please reload this page.