Skip to content

Make the specializing interpreter thread-safe in --disable-gil builds #115999

Description

@swtaarrs

Feature or enhancement

Proposal:

In free-threaded builds, the specializing adaptive interpreter needs to be made thread-safe. We should start with a small PR to simply disable it in free-threaded builds, which will be correct but will incur a performance penalty. Then we can work out how to properly support specialization in a free-threaded build.

These two commits from Sam's nogil-3.12 branch can serve as inspiration:

  1. specialize: make specialization thread-safe
  2. specialize: optimize for single-threaded programs

There are two primary concerns to balance while implementing this functionality on main:

  1. Runtime overhead: There should be no performance impact on normal builds, and minimal performance impact on single-threaded code running in free-threaded builds.
  2. Reducing code duplication/divergence: We should come up with a design that is minimally disruptive to ongoing work on the specializing interpreter. It should be easy for other devs to keep the free-threaded build working without having to know too much about it.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

Specialization Families

- [x] BINARY_OP
- [x] BINARY_SUBSCR - @corona10
- [x] CALL - @mpage
- [x] CALL_KW - @mpage
- [x] COMPARE_OP - @Yhg1s
- [x] CONTAINS_OP - @corona10
- [x] FOR_ITER - @Yhg1s
- [x] LOAD_ATTR - @mpage
- [x] LOAD_CONST
- [x] LOAD_GLOBAL - @mpage
- [x] LOAD_SUPER_ATTR - @nascheme
- [x] RESUME
- [x] SEND - @nascheme
- [x] STORE_ATTR - -@nascheme
- [x] STORE_SUBSCR - @colesbury
- [x] TO_BOOL - @corona10
- [x] UNPACK_SEQUENCE - @Eclips4

Linked PRs

Activity

  1. self-assigned this
    on Feb 27, 2024
  2. brandtbucher commented on Feb 27, 2024

    @brandtbucher
    Member

    (subscribing myself)

  3. added a commit that references this issue on Mar 1, 2024
  4. removed their assignment
    on Mar 1, 2024
  5. swtaarrs commented on Mar 1, 2024

    @swtaarrs
    MemberAuthor

    This is now a performance (rather than correctness) issue for free-threaded builds, so I'm going to focus on more time-sensitive issues for a while.

  6. added a commit that references this issue on Mar 4, 2024
  7. added a commit that references this issue on Mar 25, 2024
  8. added a commit that references this issue on Apr 17, 2024
  9. corona10 commented on Apr 20, 2024

    @corona10
    Member

    @swtaarrs Out of curiosity, is there any progress or plan for this issue?

  10. Fidget-Spinner commented on Apr 24, 2024

    @Fidget-Spinner
    Member

    @corona10 I'm planning to work on this after I get the deferred reference stack in. However, there are no concrete plans as of now. I'm really happy for you or anyone else to propose a design for the specializing interpreter with free-threaded safety!

  11. corona10 commented on Apr 25, 2024

    @corona10
    Member

    @Fidget-Spinner cc @swtaarrs
    Nice. I was also thinking about how to make it thread-safe in a seamless way since I agree with @swtaarrs.
    But there is no good idea yet to solve the issue right now since I am not in a full-time position for this task :)
    So it will be happy to see you have a good plan.
    (I am curious that we can make them per-thread mechanism...)

    By the way, in the short term, can we enable the specializer to be used only for the main thread if we can not solve the issue before 3.13 is released?
    We can easily track the performance degradation between the default build because most of pyperformance benchmark are based on a single thread :)

  12. Fidget-Spinner commented on Apr 26, 2024

    @Fidget-Spinner
    Member

    @corona10 for 3.13, I think generally we're focusing on scalability across multicore rather than single-threaded perf for 3.13. It's a bit too near to feature freeze for me to feel safe re-enabling specialization at this point. There are a lot of unsolved problems still even with specialization only on the main thread. Consider the following:

    Two threads sharing the same code object, A and B. A is main thread.
    Thread B is in LOAD_ATTR_METHOD_WITH_VALUES's action (after guards, it is in the middle of loading from a method)
    Thread A is in LOAD_ATTR_METHOD_WITH_VALUES's guard, but then deopts, meaning the method reference is now most likely dead/invalid.
    Thread B loads from LOAD_ATTR_METHOD_WITH_VALUE's method, it is now holding a dangling pointer.
    Thread B pushes dangling pointer to the stack. Everything crashes.
    

    I'm reading a few papers to get some inspiration and also looking at how CRuby and other runtimes deal with this. Will post back when I have an actual plan.

  13. self-assigned this
    on Aug 8, 2024
  14. 151 remaining items

  15. added a commit that references this issue on May 26, 2025
  16. added a commit that references this issue on Jul 4, 2025
  17. added a commit that references this issue on Jul 11, 2025
  18. added 3 commits that reference this issue on Jul 12, 2025
  19. added a commit that references this issue on Jul 13, 2025
  20. added 3 commits that reference this issue on Aug 4, 2025
  21. added a commit that references this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions