-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Changes from 20 commits
6d9b6a2
30e25d2
92160de
4754921
6a20bb0
ea7206d
d5375f1
a2ca192
2ab08f2
3534246
acda1c6
fdfa678
ad2e15c
0003d00
8ebd331
4c1ad6c
57ba52d
8ebd73d
8651ebe
4c7837f
4b0a850
d8a67c2
3e8d85e
de0e2ee
1cb6260
b3aa63c
6b591c3
a5fdc59
bc65932
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5527,9 +5527,12 @@ _PyTypes_AfterFork(void) | |
} | ||
|
||
/* Internal API to look for a name through the MRO. | ||
This returns a borrowed reference, and doesn't set an exception! */ | ||
This returns a strong reference, and doesn't set an exception! | ||
If nonzero, version is set to the value of type->tp_version at the time of | ||
the lookup. | ||
*/ | ||
PyObject * | ||
_PyType_LookupRef(PyTypeObject *type, PyObject *name) | ||
_PyType_LookupRefAndVersion(PyTypeObject *type, PyObject *name, unsigned int *version) | ||
{ | ||
PyObject *res; | ||
int error; | ||
|
@@ -5552,6 +5555,9 @@ _PyType_LookupRef(PyTypeObject *type, PyObject *name) | |
// If the sequence is still valid then we're done | ||
if (value == NULL || _Py_TryIncref(value)) { | ||
if (_PySeqLock_EndRead(&entry->sequence, sequence)) { | ||
if (version != NULL) { | ||
*version = entry_version; | ||
} | ||
return value; | ||
} | ||
Py_XDECREF(value); | ||
|
@@ -5573,6 +5579,9 @@ _PyType_LookupRef(PyTypeObject *type, PyObject *name) | |
OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name)); | ||
OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name)); | ||
Py_XINCREF(entry->value); | ||
if (version != NULL) { | ||
*version = entry->version; | ||
} | ||
return entry->value; | ||
} | ||
#endif | ||
|
@@ -5586,12 +5595,12 @@ _PyType_LookupRef(PyTypeObject *type, PyObject *name) | |
// anyone else can modify our mro or mutate the type. | ||
|
||
int has_version = 0; | ||
int version = 0; | ||
unsigned int assigned_version = 0; | ||
BEGIN_TYPE_LOCK(); | ||
res = find_name_in_mro(type, name, &error); | ||
if (MCACHE_CACHEABLE_NAME(name)) { | ||
has_version = assign_version_tag(interp, type); | ||
version = type->tp_version_tag; | ||
assigned_version = type->tp_version_tag; | ||
} | ||
END_TYPE_LOCK(); | ||
|
||
|
@@ -5608,28 +5617,67 @@ _PyType_LookupRef(PyTypeObject *type, PyObject *name) | |
if (error == -1) { | ||
PyErr_Clear(); | ||
} | ||
if (version != NULL) { | ||
// 0 is not a valid version | ||
*version = 0; | ||
} | ||
return NULL; | ||
} | ||
|
||
if (has_version) { | ||
#if Py_GIL_DISABLED | ||
update_cache_gil_disabled(entry, name, version, res); | ||
update_cache_gil_disabled(entry, name, assigned_version, res); | ||
#else | ||
PyObject *old_value = update_cache(entry, name, version, res); | ||
PyObject *old_value = update_cache(entry, name, assigned_version, res); | ||
Py_DECREF(old_value); | ||
#endif | ||
} | ||
if (version != NULL) { | ||
// 0 is not a valid version | ||
*version = has_version ? assigned_version : 0; | ||
} | ||
return res; | ||
} | ||
|
||
/* Internal API to look for a name through the MRO. | ||
This returns a strong reference, and doesn't set an exception! | ||
*/ | ||
PyObject * | ||
_PyType_LookupRef(PyTypeObject *type, PyObject *name) | ||
{ | ||
return _PyType_LookupRefAndVersion(type, name, NULL); | ||
} | ||
|
||
/* Internal API to look for a name through the MRO. | ||
This returns a borrowed reference, and doesn't set an exception! */ | ||
PyObject * | ||
_PyType_Lookup(PyTypeObject *type, PyObject *name) | ||
{ | ||
PyObject *res = _PyType_LookupRef(type, name); | ||
PyObject *res = _PyType_LookupRefAndVersion(type, name, NULL); | ||
Py_XDECREF(res); | ||
return res; | ||
} | ||
|
||
int | ||
_PyType_CacheInitForSpecialization(PyHeapTypeObject *type, PyObject *init, | ||
unsigned int tp_version) | ||
{ | ||
if (!init || !tp_version) { | ||
return 0; | ||
} | ||
int can_cache; | ||
BEGIN_TYPE_LOCK(); | ||
can_cache = ((PyTypeObject*)type)->tp_version_tag == tp_version; | ||
#ifdef Py_GIL_DISABLED | ||
can_cache = can_cache && _PyObject_HasDeferredRefcount(init); | ||
#endif | ||
if (can_cache) { | ||
FT_ATOMIC_STORE_PTR_RELAXED(type->_spec_cache.init, init); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be "release" so that the contents of (There might be other synchronization of the thread-local specialization process that makes relaxed okay, but it seems easier to reason about to me if it's at least "release") |
||
} | ||
END_TYPE_LOCK(); | ||
return can_cache; | ||
} | ||
|
||
static void | ||
set_flags(PyTypeObject *self, unsigned long mask, unsigned long flags) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3308,15 +3308,15 @@ dummy_func( | |
}; | ||
|
||
specializing op(_SPECIALIZE_CALL, (counter/1, callable[1], self_or_null[1], args[oparg] -- callable[1], self_or_null[1], args[oparg])) { | ||
#if ENABLE_SPECIALIZATION | ||
#if ENABLE_SPECIALIZATION_FT | ||
if (ADAPTIVE_COUNTER_TRIGGERS(counter)) { | ||
next_instr = this_instr; | ||
_Py_Specialize_Call(callable[0], next_instr, oparg + !PyStackRef_IsNull(self_or_null[0])); | ||
DISPATCH_SAME_OPARG(); | ||
} | ||
OPCODE_DEFERRED_INC(CALL); | ||
ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter); | ||
#endif /* ENABLE_SPECIALIZATION */ | ||
#endif /* ENABLE_SPECIALIZATION_FT */ | ||
} | ||
|
||
op(_MAYBE_EXPAND_METHOD, (callable[1], self_or_null[1], args[oparg] -- func[1], maybe_self[1], args[oparg])) { | ||
|
@@ -3701,10 +3701,10 @@ dummy_func( | |
DEOPT_IF(!PyStackRef_IsNull(null[0])); | ||
DEOPT_IF(!PyType_Check(callable_o)); | ||
PyTypeObject *tp = (PyTypeObject *)callable_o; | ||
DEOPT_IF(tp->tp_version_tag != type_version); | ||
DEOPT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(tp->tp_version_tag) != type_version); | ||
assert(tp->tp_flags & Py_TPFLAGS_INLINE_VALUES); | ||
PyHeapTypeObject *cls = (PyHeapTypeObject *)callable_o; | ||
PyFunctionObject *init_func = (PyFunctionObject *)cls->_spec_cache.init; | ||
PyFunctionObject *init_func = (PyFunctionObject *)FT_ATOMIC_LOAD_PTR_RELAXED(cls->_spec_cache.init); | ||
PyCodeObject *code = (PyCodeObject *)init_func->func_code; | ||
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize + _Py_InitCleanup.co_framesize)); | ||
STAT_INC(CALL, hit); | ||
|
@@ -3722,6 +3722,7 @@ dummy_func( | |
_PyInterpreterFrame *shim = _PyFrame_PushTrampolineUnchecked( | ||
tstate, (PyCodeObject *)&_Py_InitCleanup, 1, frame); | ||
assert(_PyFrame_GetBytecode(shim)[0].op.code == EXIT_INIT_CHECK); | ||
assert(_PyFrame_GetBytecode(shim)[1].op.code == RETURN_VALUE); | ||
/* Push self onto stack of shim */ | ||
shim->localsplus[0] = PyStackRef_DUP(self[0]); | ||
DEAD(init); | ||
|
@@ -3980,7 +3981,11 @@ dummy_func( | |
assert(self_o != NULL); | ||
DEOPT_IF(!PyList_Check(self_o)); | ||
STAT_INC(CALL, hit); | ||
#ifdef Py_GIL_DISABLED | ||
int err = _PyList_AppendTakeRefAndLock((PyListObject *)self_o, PyStackRef_AsPyObjectSteal(arg)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use the I'm a bit uneasy about the critical section after the In other words, this adds a potentially escaping call in a place where we previously didn't have one. The |
||
#else | ||
int err = _PyList_AppendTakeRef((PyListObject *)self_o, PyStackRef_AsPyObjectSteal(arg)); | ||
#endif | ||
PyStackRef_CLOSE(self); | ||
PyStackRef_CLOSE(callable); | ||
ERROR_IF(err, error); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I think we should be deferring functions defined in classes, even if the classes are nested. They already require GC for collection because classes are full of cycles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #127274