python / python/cpython

JIT: Set the IP and check for invalidation in `POP_TOP` only if `Py_Dealloc` is called.

Open
#152,106 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The optimizer removes many POP_TOPs, but quite a few remain.

The code for POP_TOP looks like this:

    inst(POP_TOP, (value --)) {
        PyStackRef_XCLOSE(value);
    }

which is fine for the interprete but in the JIT, as POP_TOP is marked as escaping, we need to set the IP before and check for invalidation afterwards.

What we could do, is have a version of POP_TOP that sets the IP and checks for invalidation only if Py_Dealloc is called, so we don't need to add SET_IP and CHECK_VALIDITY.

The new form of POP_TOP would look like this:

    op(_POP_TOP_SET_IP_CHECK_INVALID, (value --)) {
        if (do_decref_and_refcnt_is_zero(value)) {
            SET_IP()
            Py_Dealloc()
            CHECK_VALID();
        }
   }

_POP_TOP_SET_IP_CHECK_INVALID should be inserted in the same pass that we insert _SET_IP and _CHECK_VALIDITY as we know whether we need to insert those uops, and leave POP_TOP alone in those cases.

Overall, this should speed things up as it doesn't increase the overall code size, but it does execute less code in many cases.

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

Inspect the JIT implementation of POP_TOP and the pass that inserts _SET_IP and _CHECK_VALIDITY. Trace do_decref_and_refcnt_is_zero and the existing SET_IP, Py_Dealloc, and CHECK_VALID paths; done means the specialized form is inserted only where needed while POP_TOP remains unchanged elsewhere and the invalidation behavior is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
compilers, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.