10000 gh-115999: Add free-threaded specialization for FOR_ITER by Yhg1s · Pull Request #128798 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115999: Add free-threaded specialization for FOR_ITER #128798

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

Merged
merged 15 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add specialization for range iterators and generators, both about as
thread-safe as without spcialization (i.e. not much to none at all).
  • Loading branch information
Yhg1s committed Jan 13, 2025
commit 1433cd3e47f62c7bdda8abcba7b66a1345c14414
28 changes: 17 additions & 11 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_range.h"
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_pyatomic_ft_wrappers.h"


/* Support objects whose length is > PY_SSIZE_T_MAX.
Expand Down Expand Up @@ -816,10 +817,12 @@ PyTypeObject PyRange_Type = {
static PyObject *
rangeiter_next(_PyRangeIterObject *r)
{
if (r->len > 0) {
long result = r->start;
r->start = result + r->step;
r->len--;
long len = FT_ATOMIC_LOAD_LONG_RELAXED(r->len);
if (len > 0) {
long result = FT_ATOMIC_LOAD_LONG_RELAXED(r->start);
FT_ATOMIC_STORE_LONG_RELAXED(r->start, result + r->step);
// Relaxed ops for maximum speed and minimum thread-safety.
FT_ATOMIC_STORE_LONG_RELAXED(r->len, len - 1);
return PyLong_FromLong(result);
}
return NULL;
Expand All @@ -828,7 +831,7 @@ rangeiter_next(_PyRangeIterObject *r)
static PyObject *
rangeiter_len(_PyRangeIterObject *r, PyObject *Py_UNUSED(ignored))
{
return PyLong_FromLong(r->len);
return PyLong_FromLong(FT_ATOMIC_LOAD_LONG_RELAXED(r->len));
}

PyDoc_STRVAR(length_hint_doc,
Expand All @@ -841,10 +844,11 @@ rangeiter_reduce(_PyRangeIterObject *r, PyObject *Py_UNUSED(ignored))
PyObject *range;

/* create a range object for pickling */
start = PyLong_FromLong(r->start);
long lstart = FT_ATOMIC_LOAD_LONG_RELAXED(r->start);
start = PyLong_FromLong(lstart);
if (start == NULL)
goto err;
stop = PyLong_FromLong(r->start + r->len * r->step);
stop = PyLong_FromLong(lstart + FT_ATOMIC_LOAD_LONG_RELAXED(r->len) * r->step);
if (stop == NULL)
goto err;
step = PyLong_FromLong(r->step);
Expand All @@ -871,12 +875,14 @@ rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
if (index == -1 && PyErr_Occurred())
return NULL;
/* silently clip the index value */
long len = FT_ATOMIC_LOAD_LONG_RELAXED(r->len);
if (index < 0)
index = 0;
else if (index > r->len)
index = r->len; /* exhausted iterator */
r->start += index * r->step;
r->len -= index;
else if (index > len)
index = len; /* exhausted iterator */
FT_ATOMIC_STORE_LONG_RELAXED(r->start,
FT_ATOMIC_LOAD_LONG_RELAXED(r->start) + index * r->step);
FT_ATOMIC_STORE_LONG_RELAXED(r->len, len - index);
Py_RETURN_NONE;
}

Expand Down
13 changes: 8 additions & 5 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3179,7 +3179,7 @@ dummy_func(
_PyRangeIterObject *r = (_PyRangeIterObject *)PyStackRef_AsPyObjectBorrow(iter);
assert(Py_TYPE(r) == &PyRangeIter_Type);
STAT_INC(FOR_ITER, hit);
if (r->len <= 0) {
if (FT_ATOMIC_LOAD_LONG_RELAXED(r->len) <= 0) {
// Jump over END_FOR instruction.
JUMPBY(oparg + 1);
DISPATCH();
Expand All @@ -3190,16 +3190,19 @@ dummy_func(
op(_GUARD_NOT_EXHAUSTED_RANGE, (iter -- iter)) {
_PyRangeIterObject *r = (_PyRangeIterObject *)PyStackRef_AsPyObjectBorrow(iter);
assert(Py_TYPE(r) == &PyRangeIter_Type);
EXIT_IF(r->len <= 0);
EXIT_IF(FT_ATOMIC_LOAD_LONG_RELAXED(r->len) <= 0);
}

op(_ITER_NEXT_RANGE, (iter -- iter, next)) {
_PyRangeIterObject *r = (_PyRangeIterObject *)PyStackRef_AsPyObjectBorrow(iter);
assert(Py_TYPE(r) == &PyRangeIter_Type);
#ifndef Py_GIL_DISABLED
assert(r->len > 0);
long value = r->start;
r->start = value + r->step;
r->len--;
#endif
long value = FT_ATOMIC_LOAD_LONG_RELAXED(r->start);
FT_ATOMIC_STORE_LONG_RELAXED(r->start, value + r->step);
FT_ATOMIC_STORE_LONG_RELAXED(r->len,
FT_ATOMIC_LOAD_LONG_RELAXED(r->len) - 1);
PyObject *res = PyLong_FromLong(value);
ERROR_IF(res == NULL, error);
next = PyStackRef_FromPyObjectSteal(res);
Expand Down
11 changes: 7 additions & 4 deletions Python/executor_cases.c.h

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

11 changes: 7 additions & 4 deletions Python/generated_cases.c.h

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

4 changes: 2 additions & 2 deletions Python/specialize.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -2644,8 +2644,8 @@
}
#endif // Py_STATS

void

Check failure on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘_Py_IsOwnedByCurrentThread’ [-Werror=implicit-function-declaration]

Check failure on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘_Py_IsOwnedByCurrentThread’ [-Werror=implicit-function-declaration]

Check warning on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'_Py_IsOwnedByCurrentThread' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-aarch64)

implicit declaration of function ‘_Py_IsOwnedByCurrentThread’ [-Werror=implicit-function-declaration]

Check warning on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (arm64)

'_Py_IsOwnedByCurrentThread' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'_Py_IsOwnedByCurrentThread' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'_Py_IsOwnedByCurrentThread' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2647 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘_Py_IsOwnedByCurrentThread’ [-Werror=implicit-function-declaration]
_Py_Specialize_ForIter(_PyStackRef iter, _Py_CODEUNIT *instr, int oparg)

Check failure on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

8000

implicit declaration of function ‘_PyObject_GC_IS_SHARED’; did you mean ‘_PyObject_GC_IS_TRACKED’? [-Werror=implicit-function-declaration]

Check failure on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘_PyObject_GC_IS_SHARED’; did you mean ‘_PyObject_GC_IS_TRACKED’? [-Werror=implicit-function-declaration]

Check warning on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'_PyObject_GC_IS_SHARED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-aarch64)

implicit declaration of function ‘_PyObject_GC_IS_SHARED’; did you mean ‘_PyObject_GC_IS_TRACKED’? [-Werror=implicit-function-declaration]

Check warning on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (arm64)

'_PyObject_GC_IS_SHARED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'_PyObject_GC_IS_SHARED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

'_PyObject_GC_IS_SHARED' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2648 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘_PyObject_GC_IS_SHARED’; did you mean ‘_PyObject_GC_IS_TRACKED’? [-Werror=implicit-function-declaration]
{
assert(ENABLE_SPECIALIZATION_FT);
assert(_PyOpcode_Caches[FOR_ITER] == INLINE_CACHE_ENTRIES_FOR_ITER);
Expand All @@ -2668,12 +2668,13 @@
specialize(instr, FOR_ITER_TUPLE);
return;
}
#ifndef Py_GIL_DISABLED
else if (tp == &PyRangeIter_Type) {
specialize(instr, FOR_ITER_RANGE);
return;
}
else if (tp == &PyGen_Type && oparg <= SHRT_MAX) {
// Generators are very much not thread-safe, so don't worry about
// the specialization not being thread-safe.
assert(instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == END_FOR ||
instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == INSTRUMENTED_END_FOR
);
Expand All @@ -2686,7 +2687,6 @@
specialize(instr, FOR_ITER_GEN);
return;
}
#endif
SPECIALIZATION_FAIL(FOR_ITER,
_PySpecialization_ClassifyIterator(iter_o));
unspecialize(instr);
Expand Down
Loading
0