Skip to content
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

Incorrect output due to Tier 2 _LOAD_CONST_INLINE #117051

Open
gvanrossum opened this issue Mar 20, 2024 · 2 comments
Open

Incorrect output due to Tier 2 _LOAD_CONST_INLINE #117051

gvanrossum opened this issue Mar 20, 2024 · 2 comments
Assignees
Labels
3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@gvanrossum
Copy link
Member

gvanrossum commented Mar 20, 2024

Bug report

I've constructed a sneaky way to break the remove_globals() optimization, by executing a code object in two different globals dicts. (Credit goes to @brandtbucher who started the conversation about this topic, though this particular example is mine.)

Take this code:

src = """
def f():
    global x
    def inner(i):
        a = 1
        for _ in range(100):
            a = x + i
        return a
    return inner

func = f()
"""

co = compile(src, __file__, "exec")

ns1 = {"x": 1000}
eval(co, ns1)
func1 = ns1["func"]
for i in range(1000):
    x = func1(i)
print(x)

ns2 = {"x": 2000}
eval(co, ns2)
func2 = ns2["func"]
for i in range(1000):
    x = func2(i)
print(x)

This should print

1999
2999

(try it with Python 3.12, or Python 3.13 without JIT or -Xuops).

However with current main (EDIT: when using JIT or -Xuops) it prints

1999
1999

:-(

@gvanrossum gvanrossum added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 20, 2024
@gvanrossum gvanrossum added the 3.13 bugs and security fixes label Mar 20, 2024
@gvanrossum
Copy link
Member Author

gvanrossum commented Mar 20, 2024

git bisect points its sneaky little finger at GH-116460.

A workaround would be to comment out the remove_globals() call in _Py_uop_analyze_and_optimize().

@gvanrossum
Copy link
Member Author

I was thinking that another workaround would be to leave in the first _GUARD_GLOBALS_VERSION. In fact, I'm not sure why that isn't kept by remove_globals()? (The algorithm there is hard for me to follow.) Of course it ought to be moved to the top, before the start of the loop, which would require #117062 first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants