python / python/cpython

Add effective peephole optimizer for uop traces.

Open
#152,215 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.16 interpreter-core topic-JIT type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Currently we lack an effective peephole optimizer for uops traces.
We do some ad-hoc removal of LOAD/POP pairs, but many enhancements are missed.

For example, this silly program:

def add(a, b):
    return a + b

def loop(n=100_000):
    t = 0
    for _ in range(n):
        t += add(1,2)

if __name__ == "__main__":
    loop()

generates this trace:

Image

which contains the sequence:

_LOAD_FAST_BORROW 0
_LOAD_FAST_BORROW_1
_LOAD_CONST_INLINE_BORROW K 
_RROT_3
_POP_TOP_NOP
_POP_TOP_NOP

which should be reduced to

_LOAD_CONST_INLINE_BORROW K 
Linked PRs
  • gh-153035

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the uop trace optimizer and the existing ad-hoc LOAD/POP removal, then reproduce the supplied add/loop trace. Compare the current sequence with the proposed reduced sequence; done means the optimizer handles this case and has coverage for the transformation. Review linked PR gh-153035 before starting, since work is already associated with it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.