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

GH-113710: Add a peephole optimization pass. #114487

Merged
merged 4 commits into from
Jan 24, 2024

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Jan 23, 2024

This PR adds a simple peephole optimizer.

  • Converts _LOAD_CONST to the inline versions: _LOAD_CONST_INLINE and _LOAD_CONST_INLINE_BORROW.
  • Invalidates all executors when the frame eval function is changed
  • Removes all PEP 523 checks from tier 2 code.

Benchmarking and stats show a ~0.4% speedup (which could just be noise).

There is a ~0.7% reduction in tier 2 instructions executed (from removing the PEP 523 checks).
The change to loading constants will have limited impact on the tier 2 interpreter, but the effect should be more pronounced for the JIT as the operand can be inlined.

@markshannon
Copy link
Member Author

markshannon commented Jan 23, 2024

One other thing to note (that I found a bit surprising) is that 94% of the _LOAD_CONSTs are converted into _LOAD_CONST_INLINE_BORROW. This could get us back a significant fraction of the cost of immortal objects.

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.

LG except for a stray blank line. I guess the trick to store the code object in the operand of frame push/pop uops is the only thing here that's even remotely clever, which is good.

static void
peephole_opt(PyCodeObject *co, _PyUOpInstruction *buffer, int buffer_size)
{

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@markshannon markshannon merged commit 384429d into python:main Jan 24, 2024
30 checks passed
@markshannon markshannon deleted the remove-pep-523-checks branch February 1, 2024 19:49
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…14487)

* Convert _LOAD_CONST to inline versions

* Remove PEP 523 checks
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…14487)

* Convert _LOAD_CONST to inline versions

* Remove PEP 523 checks
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.

2 participants