Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
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 ourterms of service andprivacy 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
python-cla-botbot commentedApr 19, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Misc/NEWS.d/next/Core_and_Builtins/2025-04-19-16-22-47.gh-issue-132732.jgqhlF.rstShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
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 markedpure
, 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:_PyStackRefleft=PyStackRef_FromPyObjectBorrow(sym_get_const(ctx,stack_pointer[-2]));_PyStackRefright=PyStackRef_FromPyObjectBorrow(sym_get_const(ctx,stack_pointer[-1]));_PyStackRefres;// 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!
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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 bodyagain. 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 :)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Co-Authored-By: Tomas R. <tomas.roun8@gmail.com>
When you're done making the requested changes, leave the comment: And if you don't make the requested changes, you will be poked with soft cushions! |
In the first place, that's not possible. We only optimize what we specialize in the interpreter. The interpreter will never specialize that to BINARY_OP_ADD_INT. Furthermore, even if it did specialize
If you want to be more assured, how about I merge#132968 to add type assertions to our optimizer? That should make things safer. |
I forgot the guards don't actually guard at the optimization time. My bad. Yeah it seems we need some sort of check. |
@tomasr8 sorry this is going to make your life harder with the removing |
I have made the requested changes; please review again |
Thanks for making the requested changes! @markshannon: please review the changes made to this pull request. |
Note: I mark the object as stackref immortal (but not real immortal!) to simplify the reference management. This means we have no refcounting in the optimizer when constant evaluating stuff. Which makes things easier to reason about, as constants during the lifetime of the optimizer are effectively immortal anyways (the optimizer holds a single reference to all constants). |
Waiting for#134284 to be merged first, then I can use |
brandtbucher left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
It didn't really review the cases generator too closely (since I'm still not very familiar with it) butbased on the code it generates, everything at leastseems correct.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
emitter.emit("/* Start of pure uop copied from bytecodes for constant evaluation */\n") | ||
emitter.emit_tokens(uop, storage, inst=None, emit_braces=False, is_abstract=True) | ||
out.start_line() | ||
emitter.emit("/* End of pure uop copied from bytecodes for constant evaluation */\n") |
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.
Minor: maybe use//
instead of/*
/*/
for these comments, since they're not multi-line?
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.
The bytecodes.c convention seem to inconsistently use/* */
for single-lie comments. E.ghttps://github.com/python/cpython/blob/main/Python/bytecodes.c#L1124
# All new stackrefs are created from new references. | ||
# That's how the stackref contract works. | ||
if not outp.peek: | ||
emitter.emit(f"{outp.name} = sym_new_const_steal(ctx, PyStackRef_AsPyObjectBorrow({outp.name}_stackref));\n") |
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.
It may just be a week of conference sleep schedule, but my brain hurts trying to reason about the refcounting here. Why are we stealing a borrow? Shouldn't we be stealing a steal, or borrowing a borrow? Currently:
- If the tag bit isunset on the stackref, stealing a borrow will leave the refcount on the object unchanged and the tag bit unset. When the symbol is cleared after optimizing, the refcount on the object will be one less, which is correct.
- If the tag bit isset on the stackref, stealing a borrow will leave the refcount on the object itself unchanged and the tag bit still set. When the symbol is cleared after optimizing, the refcount on the object will be one less, which seemsincorrect.
(I haven't looked at the peek code yet.)
Another option, that I might like better, is making all of our constant symbols use stackrefs under-the-hood. Then we could avoid refcounting entirely. But that's a bigger change that could happen later if needed.
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.
The first thing is the tag bit is always unsets for new objects, at least for pure operations. If it's set, then the thing is likely immortal anyways, which we can ignore reference count semantics for. This ignores the FT build's deferred references.sym_new_const
increfs.sym_new_const_steal
doesn't incref. The key part is that all results of pure operations inbytecodes.c
are always new references. So we want to essentially steal the reference on the symbolic side.
On the stackref side, I could just change it to use PyStackRef_AsPyObjectSteal, as this seems more correct.
This is also making me realize that wereally should make it possible to detect refleaks/memory leaks on JIT builds soon. The problem is that new executors are allocated all over the place, leading to things like#120501. |
Uh oh!
There was an error while loading.Please reload this page.