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

bpo-44840: Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. #27656

Merged
merged 3 commits into from
Aug 9, 2021

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Aug 7, 2021

Makes sure that all exits have line number.

I've tried to keep this change minimal, as it needs to be backported to 3.10 which is nearing release.
For the example code in https://bugs.python.org/issue44840,

def func():
    if A:
        if B:
            if C:
                if D:
                    return False
        else:
            return False
    elif E and F:
        return True

Compiles with main to:

  2           0 LOAD_GLOBAL              0 (A)
              2 POP_JUMP_IF_FALSE       13 (to 26)

  3           4 LOAD_GLOBAL              1 (B)
              6 POP_JUMP_IF_FALSE       11 (to 22)

  4           8 LOAD_GLOBAL              2 (C)
             10 POP_JUMP_IF_FALSE       10 (to 20)

  5          12 LOAD_GLOBAL              3 (D)
             14 POP_JUMP_IF_FALSE       10 (to 20)

  6          16 LOAD_CONST               1 (False)
             18 RETURN_VALUE
 
None    >>   20 JUMP_FORWARD            10 (to 42)

  8     >>   22 LOAD_CONST               1 (False)
             24 RETURN_VALUE

  9     >>   26 LOAD_GLOBAL              4 (E)
             28 POP_JUMP_IF_FALSE       23 (to 46)
             30 LOAD_GLOBAL              5 (F)
             32 POP_JUMP_IF_FALSE       19 (to 38)

 10          34 LOAD_CONST               2 (True)
             36 RETURN_VALUE

  9     >>   38 LOAD_CONST               0 (None)
             40 RETURN_VALUE
        >>   42 LOAD_CONST               0 (None)
             44 RETURN_VALUE
        >>   46 LOAD_CONST               0 (None)
             48 RETURN_VALUE

With this PR, the exit blocks are duplicated after optimization, and before line number propagation,
ensuring that all exit blocks have line numbers:

  2           0 LOAD_GLOBAL              0 (A)
              2 POP_JUMP_IF_FALSE       16 (to 32)

  3           4 LOAD_GLOBAL              1 (B)
              6 POP_JUMP_IF_FALSE       14 (to 28)

  4           8 LOAD_GLOBAL              2 (C)
             10 POP_JUMP_IF_FALSE       10 (to 20)

  5          12 LOAD_GLOBAL              3 (D)
             14 POP_JUMP_IF_FALSE       12 (to 24)

  6          16 LOAD_CONST               1 (False)
             18 RETURN_VALUE

  4     >>   20 LOAD_CONST               0 (None)
             22 RETURN_VALUE

  5     >>   24 LOAD_CONST               0 (None)
             26 RETURN_VALUE

  8     >>   28 LOAD_CONST               1 (False)
             30 RETURN_VALUE

  9     >>   32 LOAD_GLOBAL              4 (E)
             34 POP_JUMP_IF_FALSE       22 (to 44)
             36 LOAD_GLOBAL              5 (F)
             38 POP_JUMP_IF_FALSE       24 (to 48)

 10          40 LOAD_CONST               2 (True)
             42 RETURN_VALUE

  9     >>   44 LOAD_CONST               0 (None)
             46 RETURN_VALUE
        >>   48 LOAD_CONST               0 (None)
             50 RETURN_VALUE

https://bugs.python.org/issue44840

…er CFG optimization.

          Makes sure that all exits have line number.
@markshannon markshannon changed the title Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. bpo-44840: Compiler: Move duplication of exit blocks with no line numbers to after CFG optimization. Aug 7, 2021
@markshannon
Copy link
Member Author

markshannon commented Aug 7, 2021

This is part of PEP 626, so I'm skipping the NEWS.

@markshannon markshannon added the needs backport to 3.10 only security fixes label Aug 9, 2021
@markshannon markshannon merged commit b854557 into python:main Aug 9, 2021
@miss-islington
Copy link
Contributor

Thanks @markshannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington
Copy link
Contributor

Sorry, @markshannon, I could not cleanly backport this to 3.10 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker b854557b49083d8625a433eb36aacb0c87d67c52 3.10

markshannon added a commit to markshannon/cpython that referenced this pull request Aug 9, 2021
…bers to after CFG optimization. (pythonGH-27656)

(cherry picked from commit b854557)
@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Aug 9, 2021
@bedevere-bot
Copy link

GH-27673 is a backport of this pull request to the 3.10 branch.

pablogsal pushed a commit that referenced this pull request Aug 9, 2021
…bers to after CFG optimization. (GH-27656) (#27673)

(cherry picked from commit b854557)
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.

4 participants