Skip to content

Commit 676b8a2

Browse files
committed
Don't stop the world on setting vectorcall
1 parent f86eed6 commit 676b8a2

8 files changed

Lines changed: 37 additions & 31 deletions

File tree

‎Include/internal/pycore_function.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
1616

1717

1818
#define FUNC_VERSION_UNSET 0
19-
#define FUNC_VERSION_CLEARED 1
20-
#define FUNC_VERSION_FIRST_VALID 2
19+
#define FUNC_VERSION_FIRST_VALID 1
2120

2221
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);
2322

‎Modules/_testinternalcapi.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,8 @@ set_vectorcall_nop(PyObject *self, PyObject *func)
31933193
return NULL;
31943194
}
31953195

3196-
((PyFunctionObject*)func)->vectorcall = vectorcall_nop;
3196+
FT_ATOMIC_STORE_PTR_RELAXED(((PyFunctionObject*)func)->vectorcall,
3197+
vectorcall_nop);
31973198
Py_RETURN_NONE;
31983199
}
31993200

‎Modules/_testinternalcapi/test_cases.c.h‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/funcobject.c‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
323323
assert(version >= FUNC_VERSION_FIRST_VALID);
324324
// This should only be called from MAKE_FUNCTION. No code is specialized
325325
// based on the version, so we do not need to stop the world to set it.
326-
func->func_version = version;
326+
FT_ATOMIC_STORE_UINT32_RELAXED(func->func_version, version);
327327
#ifndef Py_GIL_DISABLED
328328
PyInterpreterState *interp = _PyInterpreterState_GET();
329329
struct _func_version_cache_item *slot = get_cache_item(interp, version);
@@ -347,7 +347,7 @@ func_clear_version(PyInterpreterState *interp, PyFunctionObject *func)
347347
// Leave slot->code alone, there may be use for it.
348348
}
349349
#endif
350-
func->func_version = FUNC_VERSION_CLEARED;
350+
func->func_version = FUNC_VERSION_UNSET;
351351
}
352352

353353
void
@@ -370,7 +370,7 @@ _PyFunction_ClearCodeByVersion(uint32_t version)
370370
uint32_t
371371
_PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
372372
{
373-
return func->func_version;
373+
return FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version);
374374
}
375375

376376
PyObject *
@@ -452,10 +452,16 @@ PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
452452
{
453453
assert(func != NULL);
454454
PyInterpreterState *interp = _PyInterpreterState_GET();
455-
_PyEval_StopTheWorld(interp);
455+
456+
#ifdef Py_GIL_DISABLED
457+
// As documented functions using PyFunction_SetVectorcall shouldn't be
458+
// altering the behavior of the unmodified vectorcall function. We can
459+
// race with the update but it will be seen eventually.
460+
FT_ATOMIC_STORE_UINT32_RELAXED(func->func_version, FUNC_VERSION_UNSET);
461+
#else
456462
func_clear_version(interp, func);
457-
func->vectorcall = vectorcall;
458-
_PyEval_StartTheWorld(interp);
463+
#endif
464+
FT_ATOMIC_STORE_PTR_RELAXED(func->vectorcall, vectorcall);
459465
}
460466

461467
PyObject *

‎Python/bytecodes.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ dummy_func(
13541354
EXIT_IF(getitem_o == NULL);
13551355
assert(PyFunction_Check(getitem_o));
13561356
uint32_t cached_version = FT_ATOMIC_LOAD_UINT32_RELAXED(ht->_spec_cache.getitem_version);
1357-
EXIT_IF(((PyFunctionObject *)getitem_o)->func_version != cached_version);
1357+
EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)getitem_o)->func_version) != cached_version);
13581358
PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(getitem_o);
13591359
assert(code->co_argcount == 2);
13601360
EXIT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize));
@@ -3032,7 +3032,7 @@ dummy_func(
30323032
assert((oparg & 1) == 0);
30333033
assert(Py_IS_TYPE(fget, &PyFunction_Type));
30343034
PyFunctionObject *f = (PyFunctionObject *)fget;
3035-
EXIT_IF(f->func_version != func_version);
3035+
EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(f->func_version) != func_version);
30363036
PyCodeObject *code = (PyCodeObject *)f->func_code;
30373037
EXIT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize));
30383038
STAT_INC(LOAD_ATTR, hit);
@@ -5476,7 +5476,7 @@ dummy_func(
54765476
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
54775477
EXIT_IF(!PyFunction_Check(callable_o));
54785478
PyFunctionObject *func = (PyFunctionObject *)callable_o;
5479-
EXIT_IF(func->func_version != func_version);
5479+
EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version) != func_version);
54805480
}
54815481

54825482
macro(CALL_KW_PY) =
@@ -5495,7 +5495,7 @@ dummy_func(
54955495
EXIT_IF(Py_TYPE(callable_o) != &PyMethod_Type);
54965496
PyObject *func = ((PyMethodObject *)callable_o)->im_func;
54975497
EXIT_IF(!PyFunction_Check(func));
5498-
EXIT_IF(((PyFunctionObject *)func)->func_version != func_version);
5498+
EXIT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(((PyFunctionObject *)func)->func_version) != func_version);
54995499
EXIT_IF(!PyStackRef_IsNull(null));
55005500
}
55015501

‎Python/executor_cases.c.h‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Python/generated_cases.c.h‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Python/specialize.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
17631763
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523);
17641764
return -1;
17651765
}
1766-
if (func->vectorcall != _PyFunction_Vectorcall) {
1766+
if (FT_ATOMIC_LOAD_PTR_RELAXED(func->vectorcall) != _PyFunction_Vectorcall) {
17671767
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL);
17681768
return -1;
17691769
}
@@ -1806,7 +1806,7 @@ specialize_py_call_kw(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
18061806
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523);
18071807
return -1;
18081808
}
1809-
if (func->vectorcall != _PyFunction_Vectorcall) {
1809+
if (FT_ATOMIC_LOAD_PTR_RELAXED(func->vectorcall) != _PyFunction_Vectorcall) {
18101810
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL);
18111811
return -1;
18121812
}
@@ -2775,7 +2775,7 @@ _Py_Specialize_CallFunctionEx(_PyStackRef func_st, _Py_CODEUNIT *instr)
27752775
assert(_PyOpcode_Caches[CALL_FUNCTION_EX] == INLINE_CACHE_ENTRIES_CALL_FUNCTION_EX);
27762776

27772777
if (Py_TYPE(func) == &PyFunction_Type &&
2778-
((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) {
2778+
FT_ATOMIC_LOAD_PTR_RELAXED(((PyFunctionObject *)func)->vectorcall) == _PyFunction_Vectorcall) {
27792779
if (!_PyInterpreterState_IsSpecializationEnabled(_PyInterpreterState_GET())) {
27802780
goto failure;
27812781
}

0 commit comments

Comments
 (0)