-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
gh-115999: Enable specialization of CALL
instructions in free-threaded builds
#127123
Open
mpage
wants to merge
20
commits into
python:main
Choose a base branch
from
mpage:gh-115999-tlbc-call
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−82
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_CALL_ALLOC_AND_ENTER_INIT will be addressed in a separate PR
This needs to acquire a critical section on the list.
- Modify `get_init_for_simple_managed_python_class` to return both init as well as the type version at the time of lookup. - Modify caching logic to verify that the current version of the type matches the version at the time of lookup. This prevents potentially caching a stale value if we race with an update to __init__. - Only cache __init__ functions that are deferred in free-threaded builds. This ensures that the borrowed reference to __init__ that is stored in the cache is valid if the type version guard in _CHECK_AND_ALLOCATE_OBJECT passes: 1. The type version is cleared before the reference in the MRO to __init__ is destroyed. 2. If the reference in (1) was the last reference then the __init__ method will be queued for deletion the next time GC runs. 3. GC requires stopping the world, which forces a synchronizes-with operation between all threads. 4. If the GC collects the cached __init__, then type's version will have been updated *and* the update will be visible to all threads, so the guard cannot pass. - There are no escaping calls in between loading from the specialization cache and pushing the frame. This is a requirement for the default build.
mpage
changed the title
gh-115999: Enable specialization of
gh-115999: Enable specialization of Nov 22, 2024
CALL
in free-threaded buildsCALL
instructions in free-threaded builds
mpage
requested review from
ericsnowcurrently,
JelleZijlstra and
markshannon
as code owners
November 22, 2024 21:36
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
CALL
family of instructions were mostly thread-safe already and only required a small number of changes, which are documented below.A few changes were needed to make
CALL_ALLOC_AND_ENTER_INIT
thread-safe:_PyType_LookupRefAndVersion
, which returns the type version corresponding to the returned ref._PyType_CacheInitForSpecialization
, which takes an init method and the corresponding type version and only populates the specialization cache if the current type version matches the supplied version. This prevents potentially caching a stale value in free-threaded builds if we race with an update to__init__
.__init__
functions that are deferred in free-threaded builds. This ensures that the reference to__init__
that is stored in the specialization cache is valid if the type version guard in_CHECK_AND_ALLOCATE_OBJECT
passes.A few other miscellaneous changes were also needed:
_PyList_AppendTakeRefAndLock
for use inLIST_APPEND
. This ensures that the list's per-object lock is held while we are appending to it.co_tlbc
for_Py_InitCleanup
.interp->eval_frame
non-atomically and preserves the behavior of_CHECK_PEP_523
documented below.Single-threaded performance
Scaling
The scaling benchmark looks about the same for this PR vs its base:
Thread safety
Thread safety of each instruction in the CALL family is documented below, starting with the uops that are composed to form instructions in the family.
UOPS
The more interesting uops that warrant closer inspection are:
_CHECK_AND_ALLOCATE_OBJECT
This uop loads an
__init__
method from the specialization cache of the operand (a type) if the operand's type version matches the type version stored in the inline cache. The loaded method is guaranteed to be valid because we only store deferred objects in the specialization cache and there are no escaping calls following the load:__init__
is destroyed.__init__
method will be queued for deletion the next time GC runs.__init__
, then type's version will have been updated and the update will be visible to all threads, so the guard cannot pass._CHECK_FUNCTION_VERSION
This uop guards that the top of the stack is a function and that its version matches the version stored in the inline cache. Instructions assume that if the guard passes, the version, and any properties verified by the version, will not change for the remainder of the instruction execution, assuming there are no escaping calls in between the guard and the code that relies on the guard. This property is preserved in free-threaded builds: the world is stopped whenever a function's version changes.
_CHECK_PEP_523
This uop guards that a custom eval frame function is not in use. Instructions assume that if the guard passes, an eval frame function will not be set for the remainder of the instruction's execution, assuming there are no escaping calls in between the guard and code that relies on the guard passing. This property is preserved in free-threaded builds: the world is stopped whenever the eval frame function is set.
The instructions are also composed of uops whose thread safety properties are easier to reason about and require less scrutiny. These are:
_CALL_NON_PY_GENERAL
- Uses existing thread-safe APIs._CHECK_CALL_BOUND_METHOD_EXACT_ARGS
- Only performs exact type checks, which are thread-safe: changing an instance's type stops the world._CHECK_FUNCTION_EXACT_ARGS
- All the loads in the uop are safe to perform non-atomically: settingfunc->func_code
stops the world, theco_argcount
attribute of code objects is immutable._CHECK_IS_NOT_PY_CALLABLE
- Only performs exact type checks._CHECK_METHOD_VERSION
- This loads a function from aPyMethodObject
and guards that its version matches what is stored in the cache.PyMethodObject
s are immutable; their fields can be accessed non-atomically. The thread safety of function version guards was already documented above._CHECK_PERIODIC
- Thread safety was previously addressed as part of the 3.13 release._CHECK_STACK_SPACE
- All the loads in this uop are safe to perform non-atomically: settingfunc->func_code
stops the world, theco_framesize
attribute of code objects is immutable, andtstate->py_recursion_remaining
should only be mutated by the current thread._CREATE_INIT_FRAME
- Uses existing thread-safe APIs._EXPAND_METHOD
- Only loads fromPyMethodObject
s._INIT_CALL_BOUND_METHOD_EXACT_ARGS
- Only loads fromPyMethodObject
s._INIT_CALL_PY_EXACT_ARGS
- Only operates on data that isn't yet visible to other threads._PUSH_FRAME
- Only manipulateststate->current_frame
and fields that are not read by other threads._PY_FRAME_GENERAL
- Reads from fields that are either immutable (co_flags
) or requires stopping the world to change (func_code
)._SAVE_RETURN_OFFSET
- Stores only to the frame'sreturn_offset
which is not read by other threads.Instructions
These instructions perform exact type checks and loads from immutable fields of
PyCFunction
objects:CALL_BUILTIN_FAST
CALL_BUILTIN_FAST_WITH_KEYWORDS
CALL_BUILTIN_O
These instructions perform exact type checks and loads from immutable fields of
PyMethodDescrObject
s:CALL_METHOD_DESCRIPTOR_FAST
CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS
CALL_METHOD_DESCRIPTOR_NOARGS
CALL_METHOD_DESCRIPTOR_O
These instructions are composed of the uops documented above, and are thread-safe transitively:
CALL_ALLOC_AND_ENTER_INIT
CALL_BOUND_METHOD_EXACT_ARGS
CALL_BOUND_METHOD_GENERAL
CALL_NON_PY_GENERAL
CALL_PY_EXACT_ARGS
CALL_PY_GENERAL
These instructions load from the callable cache, which is immutable, perform exact type checks, and use existing thread-safe APIs:
CALL_ISINSTANCE
CALL_LEN
CALL_LIST_APPEND
These instructions use existing thread-safe APIs:
CALL_STR_1
CALL_TUPLE_1
CALL_TYPE_1
Finally, these instructions don't categorize neatly:
CALL_BUILTIN_CLASS
- Performs exact type checks and loads from immutable types.Specialization
Apart from the changes discussed earlier, specialization is already thread-safe. It inspects immutable properties (i.e. those of code objects, method descriptors, or
PyCFunction
s) or properties that require stopping the world to mutate (i.e. properties checked by function version guards).--disable-gil
builds #115999