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

Tier 2 Optimizer Eliminate Type Version Guards #119258

Closed
saulshanabrook opened this issue May 20, 2024 · 0 comments
Closed

Tier 2 Optimizer Eliminate Type Version Guards #119258

saulshanabrook opened this issue May 20, 2024 · 0 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@saulshanabrook
Copy link
Contributor

saulshanabrook commented May 20, 2024

Feature or Enhancement:

Proposal:

Note: I made this issue at the PyCon sprints discussing with @Fidget-Spinner and in collaboration with @dpdani

The tier 2 optimizer should eliminate type version guards if it is safe to do.

It is safe if it has previously checked the type version guard at runtime and we haven't had an escape opt since then.

Note that this will need Py_Decref to be deferred before the 3.13 final release, to be usable, because otherwise there could be an escape anywhere we decrement the reference count.

Example

For example, if we have this code:

class A:
    attr = 1

def thing(a):
    return a.attr + a.attr

for _ in range(1000):
    thing(A())

then when thing is executed it should only run the type guard once.

If we disassemble this code then we see it emits the LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES bytecode:

>>> import dis
>>> dis.dis(thing, adaptive=True)
  8           RESUME_CHECK             0

  9           LOAD_FAST                0 (a)
              LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 0 (attr)
              LOAD_FAST                0 (a)
              LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 0 (attr)
              BINARY_OP_ADD_INT        0 (+)
              RETURN_VALUE

If we look at the definition of LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES, we see it uses _GUARD_TYPE_VERSION (in bytecodes.c:

        macro(LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES) =
            unused/1 +
            _GUARD_TYPE_VERSION +
            _GUARD_DORV_VALUES_INST_ATTR_FROM_DICT +
            _GUARD_KEYS_VERSION +
            _LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES;

So if this is executed one after the other, without any unknown function calls in the middle, it should remove the second _GUARD_TYPE_VERSION call.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@saulshanabrook saulshanabrook added the type-feature A feature request or enhancement label May 20, 2024
@Eclips4 Eclips4 added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 20, 2024
@python python deleted a comment May 25, 2024
Fidget-Spinner added a commit that referenced this issue Jun 8, 2024
…-119365)

Co-authored-by: parmeggiani <parmeggiani@spaziodati.eu>
Co-authored-by: dpdani <git@danieleparmeggiani.me>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Co-authored-by: Ken Jin <kenjin@python.org>
Fidget-Spinner added a commit that referenced this issue Jun 20, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
…er (pythonGH-119365)

Co-authored-by: parmeggiani <parmeggiani@spaziodati.eu>
Co-authored-by: dpdani <git@danieleparmeggiani.me>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Co-authored-by: Ken Jin <kenjin@python.org>
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
…er (pythonGH-119365)

Co-authored-by: parmeggiani <parmeggiani@spaziodati.eu>
Co-authored-by: dpdani <git@danieleparmeggiani.me>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Co-authored-by: Ken Jin <kenjin@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
3 participants