Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Fidget-Spinner wants to merge25 commits intopython:main
base:main
Choose a base branch
Loading
fromFidget-Spinner:pure

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-SpinnerFidget-Spinner commentedApr 19, 2025
edited by bedevere-appbot
Loading

@python-cla-bot
Copy link

python-cla-botbot commentedApr 19, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Member

@brandtbucherbrandtbucher left a 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!

@Fidget-Spinner
Copy link
MemberAuthor

Rather than rewriting uses of these functions, could it be easier to just do something like this, since we're guranteed not to escape?

Seems feasible. I could try to rewrite all occurences of the variable with a stackref-producing const one. Let me try that.

@Fidget-Spinner
Copy link
MemberAuthor

I've verified no refleak ontest_capi.test_opt locally apart from#132731 which is pre-existing.

@markshannon
Copy link
Member

There's a lot going on in this PR, probably too much for one PR.

Could we start with a PR to fix up thepure annotations so that they are on the correct instructions and maybe add thepure_guard annotation that Brandt suggested?

@markshannon
Copy link
Member

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?

@brandtbucher
Copy link
Member

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).

@brandtbucher
Copy link
Member

I think a good progression would be:

  • Implement thepure attribute, and the optimizer changes. Remove thepure attributes where they don’t belong (so nothing breaks) and leave the existing ones as proof that the implementation works. (This PR)
  • Audit the existing non-pure bytecodes and addpure where it makes sense. (Follow-up PR)
  • Implement thepure_guard attribute, and annotate any bytecodes that can use it. (Follow-up PR)

@Fidget-Spinner
Copy link
MemberAuthor

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 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.

@Fidget-Spinner
Copy link
MemberAuthor

@brandtbucher@markshannon what can I do to get this PR moving?

@tomasr8 if youd like to review, here's a summary of the PR:

  1. If a bytecode operation is pure (no side effects) we can mark it as pure in bytecodes.c.
  2. In the optimizer, we automatically generate the body that does evaluation of the symbolic constants by copy pasting the bytecodes.c definition into the optimizer's C code. Of course we check that the inputs are constants first.
  3. All changes to the cases generator is for the second point.
tomasr8 reacted with heart emoji

@tomasr8
Copy link
Member

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 :)

Fidget-Spinner reacted with heart emoji

Copy link
Member

@tomasr8tomasr8 left a 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 :)

Fidget-Spinner reacted with heart emoji
Co-Authored-By: Tomas R. <tomas.roun8@gmail.com>
@bedevere-app
Copy link

When you're done making the requested changes, leave the comment:I have made the requested changes; please review again.

And if you don't make the requested changes, you will be poked with soft cushions!

@Fidget-Spinner
Copy link
MemberAuthor

We don't want to be evaluating Disaster() +1 when optimizing BINARY_OP_ADD_INT

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 specializeDisaster() to that by some oddity, we check_GUARD_TOS_INT and_GUARD_NOS_INT first in the specializer.Disaster() would never pass these checks.

Unfortunately this approach has a critical flaw. It is possible for the optimizer to see values that the executing code never would. For example, through a combination of statistical branch profiling and global to constant conversion.

If you want to be more assured, how about I merge#132968 to add type assertions to our optimizer? That should make things safer.

@Fidget-Spinner
Copy link
MemberAuthor

We don't want to be evaluating Disaster() +1 when optimizing BINARY_OP_ADD_INT

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 specializeDisaster() to that by some oddity, we check_GUARD_TOS_INT and_GUARD_NOS_INT first in the specializer.Disaster() would never pass these checks.

I forgot the guards don't actually guard at the optimization time. My bad. Yeah it seems we need some sort of check.

brandtbucher reacted with thumbs up emoji

@Fidget-Spinner
Copy link
MemberAuthor

@tomasr8 sorry this is going to make your life harder with the removingsym_is_const andsym_matches_type PR. I think you can still removesym_matches_type, butsym_is_const is now a little harder.

@Fidget-Spinner
Copy link
MemberAuthor

I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

@markshannon: please review the changes made to this pull request.

@Fidget-Spinner
Copy link
MemberAuthor

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).

@Fidget-Spinner
Copy link
MemberAuthor

Waiting for#134284 to be merged first, then I can usePyStackRef_FromPyObjectBorrow

Copy link
Member

@brandtbucherbrandtbucher left a comment
edited
Loading

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.

Comment on lines +211 to +214
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")
Copy link
Member

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?

Copy link
MemberAuthor

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")
Copy link
Member

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.

Copy link
MemberAuthor

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.

@brandtbucher
Copy link
Member

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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@brandtbucherbrandtbucherbrandtbucher left review comments

@StanFromIrelandStanFromIrelandStanFromIreland left review comments

@tomasr8tomasr8tomasr8 approved these changes

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@Fidget-Spinner@markshannon@brandtbucher@tomasr8@StanFromIreland

[8]ページ先頭

©2009-2025 Movatter.jp