Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Include/internal/pycore_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions Include/internal/pycore_pyatomic_ft_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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) \
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 14 additions & 13 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
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);
Expand All @@ -347,7 +347,7 @@
// Leave slot->code alone, there may be use for it.
}
#endif
func->func_version = FUNC_VERSION_CLEARED;
func->func_version = FUNC_VERSION_UNSET;
}

void
Expand All @@ -370,7 +370,7 @@
uint32_t
_PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
{
return func->func_version;
return FT_ATOMIC_LOAD_UINT32_ACQUIRE(func->func_version);
}

PyObject *
Expand Down Expand Up @@ -451,11 +451,17 @@
PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
{
assert(func != NULL);
PyInterpreterState *interp = _PyInterpreterState_GET();

Check warning on line 454 in Objects/funcobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-26.04)

unused variable ‘interp’ [-Wunused-variable]

Check warning on line 454 in Objects/funcobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-26.04-arm)

unused variable ‘interp’ [-Wunused-variable]
_PyEval_StopTheWorld(interp);

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_RELEASE(func->func_version, FUNC_VERSION_UNSET);
#else
func_clear_version(interp, func);
func->vectorcall = vectorcall;
_PyEval_StartTheWorld(interp);
#endif
}

PyObject *
Expand Down
21 changes: 11 additions & 10 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) =
Expand All @@ -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));
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -5705,15 +5705,15 @@ 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)) {
PyObject *func = PyStackRef_AsPyObjectBorrow(func_st);
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);
Expand Down Expand Up @@ -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)) {
Expand Down
Loading
Loading