◐ Shell
reader mode source ↗
Skip to content

bpo-41323: Perform 'peephole' optimizations directly on the CFG.#21517

Merged
markshannon merged 8 commits into
python:masterfrom
markshannon:move-peephole-optimizations-to-cfg
Jul 30, 2020
Merged

bpo-41323: Perform 'peephole' optimizations directly on the CFG.#21517
markshannon merged 8 commits into
python:masterfrom
markshannon:move-peephole-optimizations-to-cfg

Conversation

@markshannon

@markshannon markshannon commented Jul 17, 2020

Copy link
Copy Markdown
Member
  • Saves about 200 lines of code
  • Breaks the coupling between the optimizer and the bytecode and line number table formats.

https://bugs.python.org/issue41323

@markshannon markshannon changed the title bpo-41323: Move 'peephole' optimizations directly on the CFG. Jul 17, 2020
@rhettinger

Copy link
Copy Markdown
Contributor

Mostly, this looks pretty good. Consider removing the peephole.c file altogether. It was originally part of compile.c and got separated out when it got too large. We can more the PyCode_Optimize() function back to compile.c. Ideally, we could drop it from the public api as part of Victor's PEP to overhaul the C API.

One small loss is that the current code has macros that provide meaningful opcode groupings like UNCONDITIONAL_JUMP, CONDITIONAL_JUMP, ABSOLUTE_JUMP, and JUMPS_ON_TRUE. I always found those grouping helpful for reasoning about the code.

@pablogsal pablogsal added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 17, 2020
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @pablogsal for commit 6b7019a 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 17, 2020

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

This looks great and very compact! This is a very good work @markshannon ! I will do a more thorough review over the weekend but I have left some minor comments for now

@markshannon markshannon force-pushed the move-peephole-optimizations-to-cfg branch from 7845d6f to 8551d33 Compare July 20, 2020 09:33
@markshannon

markshannon commented Jul 20, 2020

Copy link
Copy Markdown
Member Author

@rhettinger
I've removed peephole.c as you suggested.

The UNCONDITIONAL_JUMP, CONDITIONAL_JUMP, ABSOLUTE_JUMP, and JUMPS_ON_TRUE don't seem that useful in an instruction based optimizer.
I initially created versions of them, but it turned out they were unused. So I deleted them.
For jump-to-jump elimination, it turns out that the exact opcode is more important that whether a jump is conditional and/or relative.

@markshannon markshannon force-pushed the move-peephole-optimizations-to-cfg branch from 8551d33 to 7bc2522 Compare July 20, 2020 09:49
@markshannon markshannon requested a review from a team as a code owner July 20, 2020 09:49
@pablogsal pablogsal added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 20, 2020
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @pablogsal for commit fda0bed 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 20, 2020
@markshannon

Copy link
Copy Markdown
Member Author

One final tweak. Jumps to empty blocks that were immediately followed by jumps were not being eliminated. The final commit fixes that.

@markshannon markshannon merged commit 6e8128f into python:master Jul 30, 2020
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Aug 4, 2020
…honGH-21517)

* Move 'peephole' optimizations into compile.c and perform them directly on the CFG.
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Aug 20, 2020
…honGH-21517)

* Move 'peephole' optimizations into compile.c and perform them directly on the CFG.
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
…honGH-21517)

* Move 'peephole' optimizations into compile.c and perform them directly on the CFG.
@markshannon markshannon deleted the move-peephole-optimizations-to-cfg branch October 19, 2020 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants