◐ Shell
reader mode source ↗
Skip to content

gh-144145: Track nullness of properties in the Tier 2 JIT optimizer#144122

Merged
Fidget-Spinner merged 20 commits into
python:mainfrom
cocolato:dev_143414
Jan 30, 2026
Merged

gh-144145: Track nullness of properties in the Tier 2 JIT optimizer#144122
Fidget-Spinner merged 20 commits into
python:mainfrom
cocolato:dev_143414

Conversation

@cocolato

@cocolato cocolato commented Jan 21, 2026

Copy link
Copy Markdown
Member

This PR implements symbolic tracking for __slots__ object attributes in the Tier 2 JIT optimizer.

Currently, when the JIT optimizer encounters _LOAD_ATTR_SLOT, it creates a new unknown symbol (<!NULL>) even if the same slot was previously written in the same trace. This prevents the optimizer from:

  1. Knowing the type of values loaded from slots
  2. Eliminating redundant type guards
  3. Performing further type-based optimizations

Before(set PYTHON_OPT_DEBUG=5):

51 abs: _LOAD_ATTR_SLOT (4, target=48, operand0=0x10, ...)
    stack=[..., <Point at 0x...440>]
52 abs: _POP_TOP
    stack=[..., <!NULL at 0x7fb981d874c0>, <Point at 0x...440>]
                ↑↑↑ NOT NULL TYPE

Now:

51 abs: _LOAD_ATTR_SLOT (4, target=48, operand0=0x10, ...)
    stack=[..., <Point slots[2] v131145 at 0x...440>]
52 abs: _POP_TOP
    stack=[..., <compact_int at 0x7f02ba1d6490>, <Point slots[2]>]
                ↑↑↑ we know the exact type

@markshannon

Copy link
Copy Markdown
Member

This PR seems unrelated to the issue. It tracks the internal state of objects, not the uniqueness of references.
Please create a new issue.

Tracking the internal state of objects is tricky, as we need to keep track of where code might escape and invalidate any knowledge prior to the escape. So far, we have only tracked properties of stateless objects, so it isn't an issue.
There is value in tracking the state of objects, but it needs to be done carefully.

In future, I would also advise waiting to see if there is general support on the issue before spending time on the implementation.

@cocolato

cocolato commented Jan 22, 2026

Copy link
Copy Markdown
Member Author

Thanks for reply!

This PR seems unrelated to the issue. It tracks the internal state of objects, not the uniqueness of references.
Please create a new issue.

Sorry, I'm linking to this issue because I discussed the implementation logic with @Fidget-Spinner in this issue, and I think this should be a pre-task for unique reference tracking (if I'm wrong, please point it out, thanks!). I'll create a new issue to link to this PR.

Tracking the internal state of objects is tricky, as we need to keep track of where code might escape and invalidate any knowledge prior to the escape. So far, we have only tracked properties of stateless objects, so it isn't an issue.
There is value in tracking the state of objects, but it needs to be done carefully.

Sorry again for overlooking object escape. I think may be Ken Jin's original requirement was just to add tracking for the Slots object(if I'm wrong, please point it out again, thanks!). Perhaps I should remove the optimization in the _LOAD_ATTR_SLOT section?

In future, I would also advise waiting to see if there is general support on the issue before spending time on the implementation.

Thanks for the reminder. I'll be more careful in the future.

@cocolato cocolato changed the title gh-143414: Add __slots__ object property tracking for the Tier 2 JIT optimizer Jan 22, 2026
@cocolato cocolato marked this pull request as draft January 22, 2026 10:56
@Fidget-Spinner

Copy link
Copy Markdown
Member

Sorry again for overlooking object escape. I think may be Ken Jin's original requirement was just to add tracking for the Slots object(if I'm wrong, please point it out again, thanks!). Perhaps I should remove the optimization in the _LOAD_ATTR_SLOT section?

Yeah we need to track for escapes. I mentioned it here how to do it #143414 (comment). It got lost in the noise though, so no worries.

@markshannon

In future, I would also advise waiting to see if there is general support on the issue before spending time on the implementation.

I advised @cocolato to embark on this issue. I think there is value in doing it. They waited for me to give my go-ahead before doing it. So I think they didn't do anything wrong.

@cocolato

Copy link
Copy Markdown
Member Author

Thanks! I will refactor this to also support #144141.

@cocolato

Copy link
Copy Markdown
Member Author

@Fidget-Spinner Hi, one thing i want to know. Should this PR flag which property escape during escape handling UOPs (e.g. _CALL_*)? This would allow us to reset only those objects instead of resetting all __slot__ objects tracked in s_arena. However, this will made this PR more complex.

@Fidget-Spinner

Copy link
Copy Markdown
Member

Sorry I'm busy for these 2 days. I'll try to review on Saturday. If Mark reviews it earlier, we can go with his ideas.

@Fidget-Spinner

Copy link
Copy Markdown
Member

@Fidget-Spinner Hi, one thing i want to know. Should this PR flag which property escape during escape handling UOPs (e.g. _CALL_*)? This would allow us to reset only those objects instead of resetting all __slot__ objects tracked in s_arena. However, this will made this PR more complex.

No. The problem is any uop that escapes must be handled. For example, anything with POP_TOP if not eliminated is escaping, because it might call __del__ which can trigger arbitrary code.

27 hidden items Load more…
@cocolato

Copy link
Copy Markdown
Member Author

Updated, added _STORE_ATTR_INSTANCE_VALUE_NULL and now only use is_init_shim flag to avoid _CREATE_INIT_FRAME escaping.

@Fidget-Spinner

Fidget-Spinner commented Jan 30, 2026

Copy link
Copy Markdown
Member

Cool, this LGTM now I think. This deserves a news entry. Potentially call it:

Track nullness of attributes of objects in the JIT optimizer. Patch by Hai Zhu.

I know it's not just nullness, but in all practicality due to the escapes, it's just nullness for now.

@Fidget-Spinner Fidget-Spinner changed the title gh-144145: Add object property tracking for the Tier 2 JIT optimizer Jan 30, 2026

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

Thanks

Hide details View details @Fidget-Spinner Fidget-Spinner merged commit 1dc12b2 into python:main Jan 30, 2026
75 checks passed
@Fidget-Spinner

Copy link
Copy Markdown
Member

Thanks! Do you want to handle STORE_ATTR_WITH_HINT Too?

@cocolato

Copy link
Copy Markdown
Member Author

Thanks! Do you want to handle STORE_ATTR_WITH_HINT Too?

Sure, I will work on it :)

@cocolato

cocolato commented Feb 2, 2026

Copy link
Copy Markdown
Member Author

@Fidget-Spinner I noticed a slight decrease in JIT performance yesterday: https://doesjitgobrrr.com, which seems related to this PR. Should we look into it?

@Fidget-Spinner

Copy link
Copy Markdown
Member

@cocolato I suggest waiting for one more day. If it's real, please investigate. Thank you!

@markshannon

Copy link
Copy Markdown
Member

I'm going to have to ask for this PR to be reverted, because:

We also need to decide whether we want to track whether execution has escaped.
Tracking escapes may allow us some extra optimizations, but it adds a lot of complexity.
Until the front-end and optimizer is more robust, I really don't want to be making the optimizer more complex.

@markshannon

Copy link
Copy Markdown
Member

@cocolato if you are looking for ways to boost performance, I've created a meta-issue with some ideas: #144388

cocolato added a commit to cocolato/cpython that referenced this pull request Feb 2, 2026
@cocolato

cocolato commented Feb 2, 2026

Copy link
Copy Markdown
Member Author

Sorry about that, revert it by #144391. I will do some more safe changes in the future.

@markshannon

Copy link
Copy Markdown
Member

Don't worry. Occasionally things need to be reverted.

I think something like this is worth doing, but we should discuss it a bit more on the issue first.

markshannon pushed a commit that referenced this pull request Feb 2, 2026
…44391)

Revert "gh-144145: Track nullness of properties in the Tier 2 JIT optimizer (GH-144122)"

This reverts commit 1dc12b2.
Aniketsy pushed a commit to Aniketsy/cpython that referenced this pull request Feb 3, 2026
…pythonGH-144391)

Revert "pythongh-144145: Track nullness of properties in the Tier 2 JIT optimizer (pythonGH-144122)"

This reverts commit 1dc12b2.
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
…pythonGH-144391)

Revert "pythongh-144145: Track nullness of properties in the Tier 2 JIT optimizer (pythonGH-144122)"

This reverts commit 1dc12b2.
@cocolato cocolato deleted the dev_143414 branch March 17, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants