summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authormpage <mpage@meta.com>2024-10-08 14:04:35 (GMT)
committerGitHub <noreply@github.com>2024-10-08 14:04:35 (GMT)
commite99f159be4f70cf9e40865d638e79fa426968827 (patch)
treea1e4d25793ea84055150e13a09608086a4102288 /Include
parent65ce228d63878d8b6d0005f682e89ad9d5289c4b (diff)
downloadcpython-e99f159be4f70cf9e40865d638e79fa426968827.zip
cpython-e99f159be4f70cf9e40865d638e79fa426968827.tar.gz
cpython-e99f159be4f70cf9e40865d638e79fa426968827.tar.bz2
gh-115999: Stop the world when invalidating function versions (#124997)
Stop the world when invalidating function versions The tier1 interpreter specializes `CALL` instructions based on the values of certain function attributes (e.g. `__code__`, `__defaults__`). The tier1 interpreter uses function versions to verify that the attributes of a function during execution of a specialization match those seen during specialization. A function's version is initialized in `MAKE_FUNCTION` and is invalidated when any of the critical function attributes are changed. The tier1 interpreter stores the function version in the inline cache during specialization. A guard is used by the specialized instruction to verify that the version of the function on the operand stack matches the cached version (and therefore has all of the expected attributes). It is assumed that once the guard passes, all attributes will remain unchanged while executing the rest of the specialized instruction. Stopping the world when invalidating function versions ensures that all critical function attributes will remain unchanged after the function version guard passes in free-threaded builds. It's important to note that this is only true if the remainder of the specialized instruction does not enter and exit a stop-the-world point. We will stop the world the first time any of the following function attributes are mutated: - defaults - vectorcall - kwdefaults - closure - code This should happen rarely and only happens once per function, so the performance impact on majority of code should be minimal. Additionally, refactor the API for manipulating function versions to more clearly match the stated semantics.
Diffstat (limited to 'Include')
-rw-r--r--Include/internal/pycore_function.h10
-rw-r--r--Include/internal/pycore_runtime_init.h3
2 files changed, 12 insertions, 1 deletions
diff --git a/Include/internal/pycore_function.h b/Include/internal/pycore_function.h
index 6d44e93..c45d281 100644
--- a/Include/internal/pycore_function.h
+++ b/Include/internal/pycore_function.h
@@ -18,6 +18,10 @@ extern PyObject* _PyFunction_Vectorcall(
#define FUNC_MAX_WATCHERS 8
+#define FUNC_VERSION_UNSET 0
+#define FUNC_VERSION_CLEARED 1
+#define FUNC_VERSION_FIRST_VALID 2
+
#define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
struct _func_version_cache_item {
@@ -41,6 +45,12 @@ struct _py_func_state {
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);
+static inline int
+_PyFunction_IsVersionValid(uint32_t version)
+{
+ return version >= FUNC_VERSION_FIRST_VALID;
+}
+
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
PyAPI_FUNC(void) _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version);
void _PyFunction_ClearCodeByVersion(uint32_t version);
diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h
index e6adb98..a17ba46 100644
--- a/Include/internal/pycore_runtime_init.h
+++ b/Include/internal/pycore_runtime_init.h
@@ -11,6 +11,7 @@ extern "C" {
#include "pycore_ceval_state.h" // _PyEval_RUNTIME_PERF_INIT
#include "pycore_faulthandler.h" // _faulthandler_runtime_state_INIT
#include "pycore_floatobject.h" // _py_float_format_unknown
+#include "pycore_function.h"
#include "pycore_object.h" // _PyObject_HEAD_INIT
#include "pycore_obmalloc_init.h" // _obmalloc_global_state_INIT
#include "pycore_parser.h" // _parser_runtime_state_INIT
@@ -243,7 +244,7 @@ extern PyTypeObject _PyExc_MemoryError;
.dict_state = _dict_state_INIT, \
.mem_free_queue = _Py_mem_free_queue_INIT(INTERP.mem_free_queue), \
.func_state = { \
- .next_version = 1, \
+ .next_version = FUNC_VERSION_FIRST_VALID, \
}, \
.types = { \
.next_version_tag = _Py_TYPE_BASE_VERSION_TAG, \