From f86eed663a737b452cc9672590b274704aaf88f1 Mon Sep 17 00:00:00 2001 From: Dino Viehland Date: Thu, 3 Dec 2026 17:22:32 -0800 Subject: [PATCH 1/3] Vectorcall should be read w/ relaxed atomics --- Modules/_testinternalcapi/test_cases.c.h | 19 ++++++++++--------- Python/bytecodes.c | 13 +++++++------ Python/executor_cases.c.h | 22 +++++++++++++--------- Python/generated_cases.c.h | 19 ++++++++++--------- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8bb3f857437792a..0a5c639f75a32eb 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -1878,7 +1878,7 @@ } if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -2781,7 +2781,8 @@ { func_st = stack_pointer[-4]; PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - if (Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + if (Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { UPDATE_MISS_STATS(CALL_FUNCTION_EX); assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); @@ -2937,7 +2938,7 @@ assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); } - if (((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall) { UPDATE_MISS_STATS(CALL_FUNCTION_EX); assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); @@ -2951,7 +2952,7 @@ PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); assert(Py_TYPE(func) == &PyFunction_Type); - assert(((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall); + assert(FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); assert(kwargs == NULL || PyDict_CheckExact(kwargs)); Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); @@ -3129,7 +3130,7 @@ else { if (Py_TYPE(func) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); @@ -3444,7 +3445,7 @@ int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -7184,7 +7185,7 @@ } if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -7346,7 +7347,7 @@ else { if (Py_TYPE(func) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); @@ -7499,7 +7500,7 @@ int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c6f1f61f3e08fe1..02cb18c8edd181b 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -4358,7 +4358,7 @@ dummy_func( // Check if the call can be inlined or not if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -5402,7 +5402,7 @@ dummy_func( // Check if the call can be inlined or not if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -5648,7 +5648,7 @@ dummy_func( else { if (Py_TYPE(func) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); @@ -5705,7 +5705,7 @@ dummy_func( op(_CHECK_IS_PY_CALLABLE_EX, (func_st, unused, unused, unused -- func_st, unused, unused, unused)) { PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); EXIT_IF(Py_TYPE(func) != &PyFunction_Type); - EXIT_IF(((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall); + EXIT_IF(FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall); } op(_PY_FRAME_EX, (func_st, null, callargs_st, kwargs_st -- ex_frame)) { @@ -5713,7 +5713,7 @@ dummy_func( PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); assert(Py_TYPE(func) == &PyFunction_Type); - assert(((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall); + assert(FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); assert(kwargs == NULL || PyDict_CheckExact(kwargs)); Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); @@ -5743,7 +5743,8 @@ dummy_func( op(_CHECK_IS_NOT_PY_CALLABLE_EX, (func_st, unused, unused, unused -- func_st, unused, unused, unused)) { PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - EXIT_IF(Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall); + EXIT_IF(Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall); } op(_CALL_FUNCTION_EX_NON_PY_GENERAL, (func_st, null, callargs_st, kwargs_st -- result)) { diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5df8120e7424b93..e211b7d92066cf6 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -20267,7 +20267,7 @@ SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); } - if (((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); @@ -20295,7 +20295,7 @@ SET_CURRENT_CACHED_VALUES(1); JUMP_TO_JUMP_TARGET(); } - if (((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(1); @@ -20326,7 +20326,7 @@ SET_CURRENT_CACHED_VALUES(2); JUMP_TO_JUMP_TARGET(); } - if (((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); _tos_cache1 = _stack_item_1; _tos_cache0 = _stack_item_0; @@ -20360,7 +20360,7 @@ SET_CURRENT_CACHED_VALUES(3); JUMP_TO_JUMP_TARGET(); } - if (((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); _tos_cache2 = _stack_item_2; _tos_cache1 = _stack_item_1; @@ -20393,7 +20393,7 @@ PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); assert(Py_TYPE(func) == &PyFunction_Type); - assert(((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall); + assert(FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); assert(kwargs == NULL || PyDict_CheckExact(kwargs)); Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); @@ -20429,7 +20429,8 @@ _PyStackRef func_st; func_st = stack_pointer[-4]; PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - if (Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + if (Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); @@ -20451,7 +20452,8 @@ _PyStackRef _stack_item_0 = _tos_cache0; func_st = stack_pointer[-3]; PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - if (Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + if (Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(1); @@ -20475,7 +20477,8 @@ _PyStackRef _stack_item_1 = _tos_cache1; func_st = stack_pointer[-2]; PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - if (Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + if (Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); _tos_cache1 = _stack_item_1; _tos_cache0 = _stack_item_0; @@ -20501,7 +20504,8 @@ _PyStackRef _stack_item_2 = _tos_cache2; func_st = stack_pointer[-1]; PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - if (Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + if (Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { UOP_STAT_INC(uopcode, miss); _tos_cache2 = _stack_item_2; _tos_cache1 = _stack_item_1; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3af7863e80d2c6a..b6c65bdbe4f8799 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1878,7 +1878,7 @@ } if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -2781,7 +2781,8 @@ { func_st = stack_pointer[-4]; PyObject *func = PyStackRef_AsPyObjectBorrow(func_st); - if (Py_TYPE(func) == &PyFunction_Type && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + if (Py_TYPE(func) == &PyFunction_Type && + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { UPDATE_MISS_STATS(CALL_FUNCTION_EX); assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); @@ -2937,7 +2938,7 @@ assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); } - if (((PyFunctionObject *)func)->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) != _PyFunction_Vectorcall) { UPDATE_MISS_STATS(CALL_FUNCTION_EX); assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); @@ -2951,7 +2952,7 @@ PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); assert(Py_TYPE(func) == &PyFunction_Type); - assert(((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall); + assert(FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); assert(kwargs == NULL || PyDict_CheckExact(kwargs)); Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); @@ -3129,7 +3130,7 @@ else { if (Py_TYPE(func) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); @@ -3444,7 +3445,7 @@ int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -7184,7 +7185,7 @@ } if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); @@ -7346,7 +7347,7 @@ else { if (Py_TYPE(func) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); PyObject *kwargs = PyStackRef_IsNull(kwargs_st) ? NULL : PyStackRef_AsPyObjectSteal(kwargs_st); @@ -7499,7 +7500,7 @@ int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); if (Py_TYPE(callable_o) == &PyFunction_Type && !IS_PEP523_HOOKED(tstate) && - ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)callable_o)->vectorcall) == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : Py_NewRef(PyFunction_GET_GLOBALS(callable_o)); From 676b8a2c057f5b847b7645292fa5ccfc77cb5ed9 Mon Sep 17 00:00:00 2001 From: Dino Viehland Date: Fri, 4 Dec 2026 10:38:12 -0800 Subject: [PATCH 2/3] Don't stop the world on setting vectorcall --- Include/internal/pycore_function.h | 3 +-- Modules/_testinternalcapi.c | 3 ++- Modules/_testinternalcapi/test_cases.c.h | 8 ++++---- Objects/funcobject.c | 18 ++++++++++++------ Python/bytecodes.c | 8 ++++---- Python/executor_cases.c.h | 14 +++++++------- Python/generated_cases.c.h | 8 ++++---- Python/specialize.c | 6 +++--- 8 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Include/internal/pycore_function.h b/Include/internal/pycore_function.h index 2184f40956d4e24..d580f45619596b5 100644 --- a/Include/internal/pycore_function.h +++ b/Include/internal/pycore_function.h @@ -16,8 +16,7 @@ PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall( #define FUNC_VERSION_UNSET 0 -#define FUNC_VERSION_CLEARED 1 -#define FUNC_VERSION_FIRST_VALID 2 +#define FUNC_VERSION_FIRST_VALID 1 extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr); diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 586b687f233ce02..c325bc7371ba4a8 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3193,7 +3193,8 @@ set_vectorcall_nop(PyObject *self, PyObject *func) return NULL; } - ((PyFunctionObject*)func)->vectorcall = vectorcall_nop; + FT_ATOMIC_STORE_PTR_RELAXED(((PyFunctionObject*)func)->vectorcall, + vectorcall_nop); Py_RETURN_NONE; } diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 0a5c639f75a32eb..7d88055c55ea5d2 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -757,7 +757,7 @@ } assert(PyFunction_Check(getitem_o)); uint32_t cached_version = FT_ATOMIC_LOAD_UINT32_RELAXED(ht->_spec_cache.getitem_version); - if (((PyFunctionObject *)getitem_o)->func_version != cached_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)getitem_o)->func_version) != cached_version) { UPDATE_MISS_STATS(BINARY_OP); assert(_PyOpcode_Deopt[opcode] == (BINARY_OP)); JUMP_TO_PREDICTED(BINARY_OP); @@ -3541,7 +3541,7 @@ assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); } - if (((PyFunctionObject *)func)->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)func)->func_version) != func_version) { UPDATE_MISS_STATS(CALL_KW); assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); @@ -3752,7 +3752,7 @@ JUMP_TO_PREDICTED(CALL_KW); } PyFunctionObject *func = (PyFunctionObject *)callable_o; - if (func->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version) != func_version) { UPDATE_MISS_STATS(CALL_KW); assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); @@ -9449,7 +9449,7 @@ assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - if (f->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version) { UPDATE_MISS_STATS(LOAD_ATTR); assert(_PyOpcode_Deopt[opcode] == (LOAD_ATTR)); JUMP_TO_PREDICTED(LOAD_ATTR); diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 2e0bcd7853b9259..603b23bf2cd0811 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -323,7 +323,7 @@ _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version) assert(version >= FUNC_VERSION_FIRST_VALID); // This should only be called from MAKE_FUNCTION. No code is specialized // based on the version, so we do not need to stop the world to set it. - func->func_version = version; + FT_ATOMIC_STORE_UINT32_RELAXED(func->func_version, version); #ifndef Py_GIL_DISABLED PyInterpreterState *interp = _PyInterpreterState_GET(); struct _func_version_cache_item *slot = get_cache_item(interp, version); @@ -347,7 +347,7 @@ func_clear_version(PyInterpreterState *interp, PyFunctionObject *func) // Leave slot->code alone, there may be use for it. } #endif - func->func_version = FUNC_VERSION_CLEARED; + func->func_version = FUNC_VERSION_UNSET; } void @@ -370,7 +370,7 @@ _PyFunction_ClearCodeByVersion(uint32_t version) uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func) { - return func->func_version; + return FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version); } PyObject * @@ -452,10 +452,16 @@ PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall) { assert(func != NULL); PyInterpreterState *interp = _PyInterpreterState_GET(); - _PyEval_StopTheWorld(interp); + +#ifdef Py_GIL_DISABLED + // As documented functions using PyFunction_SetVectorcall shouldn't be + // altering the behavior of the unmodified vectorcall function. We can + // race with the update but it will be seen eventually. + FT_ATOMIC_STORE_UINT32_RELAXED(func->func_version, FUNC_VERSION_UNSET); +#else func_clear_version(interp, func); - func->vectorcall = vectorcall; - _PyEval_StartTheWorld(interp); +#endif + FT_ATOMIC_STORE_PTR_RELAXED(func->vectorcall, vectorcall); } PyObject * diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 02cb18c8edd181b..b563b2006ce8105 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1354,7 +1354,7 @@ dummy_func( EXIT_IF(getitem_o == NULL); assert(PyFunction_Check(getitem_o)); uint32_t cached_version = FT_ATOMIC_LOAD_UINT32_RELAXED(ht->_spec_cache.getitem_version); - EXIT_IF(((PyFunctionObject *)getitem_o)->func_version != cached_version); + EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)getitem_o)->func_version) != cached_version); PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(getitem_o); assert(code->co_argcount == 2); EXIT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize)); @@ -3032,7 +3032,7 @@ dummy_func( assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - EXIT_IF(f->func_version != func_version); + EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version); PyCodeObject *code = (PyCodeObject *)f->func_code; EXIT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize)); STAT_INC(LOAD_ATTR, hit); @@ -5476,7 +5476,7 @@ dummy_func( PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable); EXIT_IF(!PyFunction_Check(callable_o)); PyFunctionObject *func = (PyFunctionObject *)callable_o; - EXIT_IF(func->func_version != func_version); + EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version) != func_version); } macro(CALL_KW_PY) = @@ -5495,7 +5495,7 @@ dummy_func( EXIT_IF(Py_TYPE(callable_o) != &PyMethod_Type); PyObject *func = ((PyMethodObject *)callable_o)->im_func; EXIT_IF(!PyFunction_Check(func)); - EXIT_IF(((PyFunctionObject *)func)->func_version != func_version); + EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)func)->func_version) != func_version); EXIT_IF(!PyStackRef_IsNull(null)); } diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index e211b7d92066cf6..388b746fb865ebd 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -8192,7 +8192,7 @@ } assert(PyFunction_Check(getitem_o)); uint32_t cached_version = FT_ATOMIC_LOAD_UINT32_RELAXED(ht->_spec_cache.getitem_version); - if (((PyFunctionObject *)getitem_o)->func_version != cached_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)getitem_o)->func_version) != cached_version) { UOP_STAT_INC(uopcode, miss); _tos_cache1 = _stack_item_1; _tos_cache0 = container; @@ -12844,7 +12844,7 @@ assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - if (f->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version) { UOP_STAT_INC(uopcode, miss); SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); @@ -12880,7 +12880,7 @@ assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - if (f->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version) { UOP_STAT_INC(uopcode, miss); _tos_cache0 = owner; SET_CURRENT_CACHED_VALUES(1); @@ -12917,7 +12917,7 @@ assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - if (f->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version) { UOP_STAT_INC(uopcode, miss); _tos_cache1 = owner; _tos_cache0 = _stack_item_0; @@ -12958,7 +12958,7 @@ assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - if (f->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version) { UOP_STAT_INC(uopcode, miss); _tos_cache2 = owner; _tos_cache1 = _stack_item_1; @@ -20038,7 +20038,7 @@ JUMP_TO_JUMP_TARGET(); } PyFunctionObject *func = (PyFunctionObject *)callable_o; - if (func->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version) != func_version) { UOP_STAT_INC(uopcode, miss); _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(1); @@ -20074,7 +20074,7 @@ SET_CURRENT_CACHED_VALUES(1); JUMP_TO_JUMP_TARGET(); } - if (((PyFunctionObject *)func)->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)func)->func_version) != func_version) { UOP_STAT_INC(uopcode, miss); _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(1); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index b6c65bdbe4f8799..13e2cc0e672a406 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -757,7 +757,7 @@ } assert(PyFunction_Check(getitem_o)); uint32_t cached_version = FT_ATOMIC_LOAD_UINT32_RELAXED(ht->_spec_cache.getitem_version); - if (((PyFunctionObject *)getitem_o)->func_version != cached_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)getitem_o)->func_version) != cached_version) { UPDATE_MISS_STATS(BINARY_OP); assert(_PyOpcode_Deopt[opcode] == (BINARY_OP)); JUMP_TO_PREDICTED(BINARY_OP); @@ -3541,7 +3541,7 @@ assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); } - if (((PyFunctionObject *)func)->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)func)->func_version) != func_version) { UPDATE_MISS_STATS(CALL_KW); assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); @@ -3752,7 +3752,7 @@ JUMP_TO_PREDICTED(CALL_KW); } PyFunctionObject *func = (PyFunctionObject *)callable_o; - if (func->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version) != func_version) { UPDATE_MISS_STATS(CALL_KW); assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); @@ -9448,7 +9448,7 @@ assert((oparg & 1) == 0); assert(Py_IS_TYPE(fget, &PyFunction_Type)); PyFunctionObject *f = (PyFunctionObject *)fget; - if (f->func_version != func_version) { + if (FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version) { UPDATE_MISS_STATS(LOAD_ATTR); assert(_PyOpcode_Deopt[opcode] == (LOAD_ATTR)); JUMP_TO_PREDICTED(LOAD_ATTR); diff --git a/Python/specialize.c b/Python/specialize.c index b5cf4ebc31142ab..59e94bce6195f8f 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -1763,7 +1763,7 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs, SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523); return -1; } - if (func->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(func->vectorcall) != _PyFunction_Vectorcall) { SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL); return -1; } @@ -1806,7 +1806,7 @@ specialize_py_call_kw(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs, SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523); return -1; } - if (func->vectorcall != _PyFunction_Vectorcall) { + if (FT_ATOMIC_LOAD_PTR_RELAXED(func->vectorcall) != _PyFunction_Vectorcall) { SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL); return -1; } @@ -2775,7 +2775,7 @@ _Py_Specialize_CallFunctionEx(_PyStackRef func_st, _Py_CODEUNIT *instr) assert(_PyOpcode_Caches[CALL_FUNCTION_EX] == INLINE_CACHE_ENTRIES_CALL_FUNCTION_EX); if (Py_TYPE(func) == &PyFunction_Type && - ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { + FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) { if (!_PyInterpreterState_IsSpecializationEnabled(_PyInterpreterState_GET())) { goto failure; } From d88ca11ae5c5de091aed90e47318650a233e4281 Mon Sep 17 00:00:00 2001 From: Dino Viehland Date: Fri, 4 Dec 2026 11:04:39 -0800 Subject: [PATCH 3/3] More synchronization around func_version --- Include/internal/pycore_pyatomic_ft_wrappers.h | 6 ++++++ Objects/funcobject.c | 6 +++--- Python/specialize.c | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Include/internal/pycore_pyatomic_ft_wrappers.h b/Include/internal/pycore_pyatomic_ft_wrappers.h index de3a78ec02ec02d..fe069ac12b866c0 100644 --- a/Include/internal/pycore_pyatomic_ft_wrappers.h +++ b/Include/internal/pycore_pyatomic_ft_wrappers.h @@ -47,6 +47,8 @@ extern "C" { _Py_atomic_load_uint8_relaxed(&value) #define FT_ATOMIC_LOAD_UINT16_RELAXED(value) \ _Py_atomic_load_uint16_relaxed(&value) +#define FT_ATOMIC_LOAD_UINT32_ACQUIRE(value) \ + _Py_atomic_load_uint32_acquire(&value) #define FT_ATOMIC_LOAD_UINT32_RELAXED(value) \ _Py_atomic_load_uint32_relaxed(&value) #define FT_ATOMIC_LOAD_UINT64_ACQUIRE(value) \ @@ -77,6 +79,8 @@ extern "C" { _Py_atomic_store_uint16_relaxed(&value, new_value) #define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) \ _Py_atomic_store_uint32_relaxed(&value, new_value) +#define FT_ATOMIC_STORE_UINT32_RELEASE(value, new_value) \ + _Py_atomic_store_uint32_release(&value, new_value) #define FT_ATOMIC_AND_UINT64(value, new_value) \ (void)_Py_atomic_and_uint64(&value, new_value) #define FT_ATOMIC_OR_UINT64(value, new_value) \ @@ -160,6 +164,7 @@ extern "C" { #define FT_ATOMIC_LOAD_INT8_RELAXED(value) value #define FT_ATOMIC_LOAD_UINT8_RELAXED(value) value #define FT_ATOMIC_LOAD_UINT16_RELAXED(value) value +#define FT_ATOMIC_LOAD_UINT32_ACQUIRE(value) value #define FT_ATOMIC_LOAD_UINT32_RELAXED(value) value #define FT_ATOMIC_LOAD_UINT64_ACQUIRE(value) value #define FT_ATOMIC_LOAD_UINT64_RELAXED(value) value @@ -174,6 +179,7 @@ extern "C" { #define FT_ATOMIC_STORE_UINT8_RELEASE(value, new_value) value = new_value #define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value #define FT_ATOMIC_STORE_UINT16_RELAXED(value, new_value) value = new_value +#define FT_ATOMIC_STORE_UINT32_RELEASE(value, new_value) value = new_value #define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) value = new_value #define FT_ATOMIC_AND_UINT64(value, new_value) (void)(value &= new_value) #define FT_ATOMIC_OR_UINT64(value, new_value) (void)(value |= new_value) diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 603b23bf2cd0811..995f1406d8b9c3a 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -370,7 +370,7 @@ _PyFunction_ClearCodeByVersion(uint32_t version) uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func) { - return FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version); + return FT_ATOMIC_LOAD_UINT32_ACQUIRE(func->func_version); } PyObject * @@ -453,15 +453,15 @@ PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall) assert(func != NULL); PyInterpreterState *interp = _PyInterpreterState_GET(); + FT_ATOMIC_STORE_PTR_RELAXED(func->vectorcall, vectorcall); #ifdef Py_GIL_DISABLED // As documented functions using PyFunction_SetVectorcall shouldn't be // altering the behavior of the unmodified vectorcall function. We can // race with the update but it will be seen eventually. - FT_ATOMIC_STORE_UINT32_RELAXED(func->func_version, FUNC_VERSION_UNSET); + FT_ATOMIC_STORE_UINT32_RELEASE(func->func_version, FUNC_VERSION_UNSET); #else func_clear_version(interp, func); #endif - FT_ATOMIC_STORE_PTR_RELAXED(func->vectorcall, vectorcall); } PyObject * diff --git a/Python/specialize.c b/Python/specialize.c index 59e94bce6195f8f..a5119e32068d82d 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -1763,6 +1763,7 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs, SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523); return -1; } + int version = _PyFunction_GetVersionForCurrentState(func); if (FT_ATOMIC_LOAD_PTR_RELAXED(func->vectorcall) != _PyFunction_Vectorcall) { SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL); return -1; @@ -1775,7 +1776,6 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs, if (kind == SIMPLE_FUNCTION) { argcount = code->co_argcount; } - int version = _PyFunction_GetVersionForCurrentState(func); if (!_PyFunction_IsVersionValid(version)) { SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OUT_OF_VERSIONS); return -1; @@ -1806,6 +1806,7 @@ specialize_py_call_kw(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs, SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523); return -1; } + int version = _PyFunction_GetVersionForCurrentState(func); if (FT_ATOMIC_LOAD_PTR_RELAXED(func->vectorcall) != _PyFunction_Vectorcall) { SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL); return -1; @@ -1814,7 +1815,6 @@ specialize_py_call_kw(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs, SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CODE_NOT_OPTIMIZED); return -1; } - int version = _PyFunction_GetVersionForCurrentState(func); if (!_PyFunction_IsVersionValid(version)) { SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OUT_OF_VERSIONS); return -1;