Skip to content

Tier 2 Optimizer Eliminate Type Version Guards #119258

Closed
@saulshanabrook

Description

@saulshanabrook

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions