Skip to content

gh-115419: Change default sym to not_null #116562

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

Merged

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented Mar 10, 2024

Changes the default symbol to not-null because in reality, most of the stuff in CPython pushes non-null to the stack.

@Fidget-Spinner Fidget-Spinner changed the title Eliminate guard for constant promoted function gh-https://github.com/python/cpython/pull/116562: Eliminate guard for constant promoted function Mar 10, 2024
@Fidget-Spinner Fidget-Spinner changed the title gh-https://github.com/python/cpython/pull/116562: Eliminate guard for constant promoted function gh-115419: Eliminate guard for constant promoted function Mar 10, 2024
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, no time to review yet, but a suggestion for the test.

Comment on lines 797 to 800
"""Note: this function must be executed in a subprocess, because when other tests are run,
the globals of this module might get affected, causing globals to constant promotion
to fail.
"""
Copy link
Member

@gvanrossum gvanrossum Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I betcha you can do without a subprocess. Instead, just create a namespace and use that, like several other tests do. The namespace directory acts as the globals for the function defined in it. Try it!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry my Python is actually somewhat rusty -- which namespace are you referring to? I don't remember types.SimpleNamespace being that powerful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a dict -- look at test_type_inconsistency() in the same file for a complete example. Because it says exec(src, ns, ns) the ns dict is used as the globals for the function, and you can add/update its "globals" using ns["some_name"] = some_value.

@markshannon
Copy link
Member

There are two parts to this, AFAICT.

  1. Changing the default value in the abstract interpreter generator from "unknown" to "not-NULL".
  2. Skipping the function version check if the function is a constant.

1 seems like a worthwhile change, but 2 is incorrect as functions are mutable.

Could you move 1 to its own PR?
To remove function version checks you will need to add function watchers, or directly invalidate traces in function mutations.

@Fidget-Spinner Fidget-Spinner changed the title gh-115419: Eliminate guard for constant promoted function gh-115419: Change default sym to not_null Mar 11, 2024
@gvanrossum
Copy link
Member

FWIW This may be off-topic and a pipe dream, but I have a feeling that we ought to consider deprecating certain rare mutable attributes. E.g. assignments to func.__code__, func.__defaults__, func.__globals__ etc., and obj.__class__, cls.__bases__ etc. If all these are immutable we can skip a bunch of nuisance guards. These are all esoteric assignments. Of course we'll have to go through the deprecation process. In the meantime watchers can maybe help. (Do we even have function watchers?)

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure how to go about making sure that we didn't miss any opcodes that might push NULL. I checked for ones with NULL in their name, and they're all covered. How did you even find _LOAD_ATTR?

(void)owner;
OUT_OF_SPACE_IF_NULL(attr = sym_new_not_null(ctx));
if (oparg & 1) {
OUT_OF_SPACE_IF_NULL(self_or_null = sym_new_unknown(ctx));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point in the future we will be able to tell whether the attribute is a method or not, and hence we will be able to set it to either null or not_null -- but not today, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No not for _LOAD_ATTR - this opcode is special, it means we dont know anything and it's only decideable at runtime.

@Fidget-Spinner
Copy link
Member Author

The test suite crashed without _LOAD_ATTR manual definition

@Fidget-Spinner Fidget-Spinner merged commit 617aca9 into python:main Mar 13, 2024
@Fidget-Spinner Fidget-Spinner deleted the constant_propagation_functions branch March 13, 2024 12:57
vstinner pushed a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants