Skip to content
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

gh-117139: Set up the tagged evaluation stack #117186

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
7ca9b11
Tag objects in ceval, type, gen, frame
Fidget-Spinner Mar 23, 2024
09dbeca
partially convert cases
Fidget-Spinner Mar 23, 2024
84142a5
fix the rest
Fidget-Spinner Mar 23, 2024
25bf135
fixups
Fidget-Spinner Mar 23, 2024
d484628
fix all remaining warnings
Fidget-Spinner Mar 26, 2024
744357e
fix tests
Fidget-Spinner Mar 26, 2024
4cc9fb8
fix frames
Fidget-Spinner Mar 26, 2024
8423e75
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Mar 26, 2024
6b9ad92
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 26, 2024
0bb6def
fix mypy errors
Fidget-Spinner Mar 26, 2024
8fe65c1
Merge branch 'tagged_evaluation_stack' of github.com:Fidget-Spinner/c…
Fidget-Spinner Mar 26, 2024
8412d46
fix mypy for real this time
Fidget-Spinner Mar 26, 2024
973c41c
fix windows builds
Fidget-Spinner Mar 26, 2024
4428279
fix JIT builds
Fidget-Spinner Mar 26, 2024
a4917ae
untag into temp stack for vectorcall
Fidget-Spinner Mar 27, 2024
3f7f25a
address review
Fidget-Spinner Mar 27, 2024
adfbd43
remove .obj field
Fidget-Spinner Mar 27, 2024
868b78e
add ways to test this
Fidget-Spinner Mar 27, 2024
6024a4b
minor fixups
Fidget-Spinner Mar 27, 2024
b1a614b
fix all remaining uses of PyObject **
Fidget-Spinner Mar 29, 2024
e4fa6da
minor fix
Fidget-Spinner Mar 29, 2024
7bead7f
fixed gdb
Fidget-Spinner Mar 29, 2024
eb9e9a5
move tags to internal header
Fidget-Spinner Mar 29, 2024
59b237a
replace incref decrefs with tagged versions
Fidget-Spinner Mar 29, 2024
b566e39
no-op tags on default build
Fidget-Spinner Mar 29, 2024
a036f57
fix operator associativity
Fidget-Spinner Mar 29, 2024
02408cc
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Mar 29, 2024
6b1bcf3
rename to tag/untag
Fidget-Spinner Mar 29, 2024
340f5e1
rename as requested
Fidget-Spinner Mar 29, 2024
7471fe3
default to tagged pointers in cases generator (part 1)
Fidget-Spinner Mar 29, 2024
8be49a5
(broken) convert default to tagged ptr
Fidget-Spinner Mar 29, 2024
5e2018c
swap order of tags and refcounts
Fidget-Spinner Mar 29, 2024
adff67f
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Apr 1, 2024
c09ddee
minor fixes (still broken)
Fidget-Spinner Apr 1, 2024
515536e
bunch of fixes
Fidget-Spinner Apr 1, 2024
7d7ef29
fix everything
Fidget-Spinner Apr 1, 2024
4be812f
fix some tests
Fidget-Spinner Apr 1, 2024
2f972c5
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Apr 7, 2024
72ff61b
fix up warnings
Fidget-Spinner Apr 7, 2024
2e9b411
Rename _PyTaggedPtr -> _PyStackRef
Fidget-Spinner Apr 7, 2024
b5e4c83
make ownership clear in tagging and untagging
Fidget-Spinner Apr 7, 2024
175700c
further cleanup
Fidget-Spinner Apr 7, 2024
b75c171
rename tagged -> stackref
Fidget-Spinner Apr 7, 2024
ed3a3e6
Introduced owned references
Fidget-Spinner Apr 7, 2024
6e7a917
fix syntaxerror on nogil
Fidget-Spinner Apr 7, 2024
4675ce8
Fix mypy errors
Fidget-Spinner Apr 7, 2024
629a303
fix slices
Fidget-Spinner Apr 10, 2024
4170db1
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Apr 10, 2024
038cb8a
Merge Sam's deferred refcounting in
Fidget-Spinner Apr 10, 2024
65da5a0
Fix STORE_SUBSCR, and friends
Fidget-Spinner Apr 10, 2024
6a9e1bc
traverse the stack in GC
Fidget-Spinner Apr 11, 2024
bc2a6ec
fix bugs in frame pushing and popping
Fidget-Spinner Apr 12, 2024
3bb3de2
fix for shim frames
Fidget-Spinner Apr 12, 2024
8afd8b6
stackref -> tagged
Fidget-Spinner Apr 12, 2024
87fda3e
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Apr 12, 2024
3269f20
Fix _PyFrame_Copy
Fidget-Spinner Apr 12, 2024
693b64b
fix memleak and address sam's comment
Fidget-Spinner Apr 12, 2024
03af3a4
all tests pass except references
Fidget-Spinner Apr 13, 2024
aef8e3c
Fix some refleaks
Fidget-Spinner Apr 13, 2024
87112bb
fix another references
Fidget-Spinner Apr 13, 2024
a34cec8
clean up a little
Fidget-Spinner Apr 13, 2024
5b4ddb6
undo gc changes
Fidget-Spinner Apr 13, 2024
019bc3d
rename stack to stackref
Fidget-Spinner Apr 16, 2024
b38e507
defer stack for bound methods
Fidget-Spinner Apr 16, 2024
933b5b4
fix a bunch of ownership, and deferred methods
Fidget-Spinner Apr 24, 2024
11af18d
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Apr 24, 2024
1bbf021
fix upstream merge conflicts
Fidget-Spinner Apr 24, 2024
bd6889b
Merge remote-tracking branch 'upstream/main' into tagged_evaluation_s…
Fidget-Spinner Apr 25, 2024
bafd342
fix merge conflicts
Fidget-Spinner Apr 25, 2024
752a49b
lint
Fidget-Spinner Apr 25, 2024
c6be5aa
use old code, fix decref on traceback
Fidget-Spinner Apr 26, 2024
d59145b
make bound methods work for keyword calls as well
Fidget-Spinner Apr 26, 2024
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
10 changes: 10 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ static inline struct _Py_object_freelists* _Py_object_freelists_GET(void)
#endif
}

extern PyInterpreterState *_Py_interp_for_stop_the_world(struct _stoptheworld_state *stw);

// Loops over threads for a stop-the-world event.
// For global: all threads in all interpreters
// For per-interpreter: all threads in the interpreter
#define _Py_FOR_EACH_THREAD(stw, i, t) \
for (i = _Py_interp_for_stop_the_world((stw)); \
i != NULL; i = ((stw->is_global) ? i->next : NULL)) \
for (t = i->threads.head; t; t = t->next)

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 7 additions & 5 deletions Include/internal/pycore_tagged.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef union {
#define Py_STACK_TAG(obj) ((_PyStackRef){.bits = ((uintptr_t)(obj))})
#endif

#define Py_STACK_TAG_UNSAFE(obj) ((_PyStackRef){.bits = ((uintptr_t)(obj))})

#if defined(Py_TAG_TEST)
#define Py_STACK_UNTAG_OWNED(tagged) Py_STACK_UNTAG_BORROWED(tagged)
Expand All @@ -60,6 +61,7 @@ typedef union {
assert(_PyObject_HasDeferredRefcount(Py_STACK_UNTAG_BORROWED(tagged)));
return Py_NewRef(Py_STACK_UNTAG_BORROWED(tagged));
}
assert(!_PyObject_HasDeferredRefcount(Py_STACK_UNTAG_BORROWED(tagged)));
return Py_STACK_UNTAG_BORROWED(tagged);
}
#define Py_STACK_UNTAG_OWNED(tagged) _Py_Stack_Untag_Owned(tagged)
Expand Down Expand Up @@ -128,11 +130,11 @@ _Py_untag_stack_owned(PyObject **dst, const _PyStackRef *src, size_t length) {
#if defined(Py_GIL_DISABLED)
static inline void
_Py_IncRef_StackRef(_PyStackRef tagged) {
if ((tagged.bits & Py_TAG_DEFERRED) == Py_TAG_DEFERRED) {
assert(_PyObject_HasDeferredRefcount(Py_STACK_UNTAG_BORROWED(tagged)));
return;
}
assert(!_PyObject_HasDeferredRefcount(Py_STACK_UNTAG_BORROWED(tagged)));
// if ((tagged.bits & Py_TAG_DEFERRED) == Py_TAG_DEFERRED) {
// assert(_PyObject_HasDeferredRefcount(Py_STACK_UNTAG_BORROWED(tagged)));
// return;
// }
// assert(!_PyObject_HasDeferredRefcount(Py_STACK_UNTAG_BORROWED(tagged)));
return Py_INCREF(Py_STACK_UNTAG_BORROWED(tagged));
}
#define Py_INCREF_STACKREF(op) _Py_IncRef_StackRef(op)
Expand Down
44 changes: 34 additions & 10 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ static _PyInterpreterFrame *
_PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals, _PyStackRef const* args,
size_t argcount, PyObject *kwnames);
static _PyInterpreterFrame *
_PyEvalFramePushAndInit_UnTagged(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals, PyObject *const* args,
size_t argcount, PyObject *kwnames);
static _PyInterpreterFrame *
_PyEvalFramePushAndInit_Ex(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals, Py_ssize_t nargs, PyObject *callargs, PyObject *kwargs);
Expand Down Expand Up @@ -1481,7 +1485,7 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
for (i = 0; i < kwcount; i++) {
PyObject **co_varnames;
PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
PyObject *value = Py_STACK_UNTAG_BORROWED(args[i+argcount]);
_PyStackRef value_tagged = args[i+argcount];
Py_ssize_t j;

if (keyword == NULL || !PyUnicode_Check(keyword)) {
Expand Down Expand Up @@ -1554,16 +1558,15 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
goto kw_fail;
}

if (PyDict_SetItem(kwdict, keyword, value) == -1) {
if (PyDict_SetItem(kwdict, keyword, Py_STACK_UNTAG_OWNED(value_tagged)) == -1) {
goto kw_fail;
}
Py_DECREF(value);
Py_DECREF_STACKREF(value_tagged);
continue;

kw_fail:
for (;i < kwcount; i++) {
PyObject *value = Py_STACK_UNTAG_BORROWED(args[i+argcount]);
Py_DECREF(value);
Py_DECREF_STACKREF(args[i+argcount]);
}
goto fail_post_args;

Expand All @@ -1574,7 +1577,7 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
func->func_qualname, keyword);
goto kw_fail;
}
localsplus[j] = Py_STACK_TAG(value);
localsplus[j] = value_tagged;
}
}

Expand Down Expand Up @@ -1737,6 +1740,27 @@ _PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
return NULL;
}

static _PyInterpreterFrame *
_PyEvalFramePushAndInit_UnTagged(PyThreadState *tstate, PyFunctionObject *func,
PyObject *locals, PyObject *const* args,
size_t argcount, PyObject *kwnames)
{
#if defined(Py_GIL_DISABLED) || defined(Py_TAG_TEST)
_PyStackRef *tagged_args_buffer = PyMem_Malloc(sizeof(_PyStackRef) * argcount);
if (tagged_args_buffer == NULL) {
PyErr_NoMemory();
return NULL;
}
for (size_t i = 0; i < argcount; i++) {
tagged_args_buffer[i] = Py_STACK_TAG(args[i]);
}
return _PyEvalFramePushAndInit(tstate, func, locals, (_PyStackRef const *)tagged_args_buffer, argcount, kwnames);
#else
assert(Py_TAG == 0);
return _PyEvalFramePushAndInit(tstate, func, locals, (_PyStackRef const *)args, argcount, kwnames);
#endif
}

/* Same as _PyEvalFramePushAndInit but takes an args tuple and kwargs dict.
Steals references to func, callargs and kwargs.
*/
Expand All @@ -1761,9 +1785,9 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, PyFunctionObject *func,
Py_INCREF(PyTuple_GET_ITEM(callargs, i));
}
}
_PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit(
_PyInterpreterFrame *new_frame = _PyEvalFramePushAndInit_UnTagged(
tstate, (PyFunctionObject *)func, locals,
(_PyStackRef const *)newargs, nargs, kwnames
newargs, nargs, kwnames
);
if (has_dict) {
_PyStack_UnpackDict_FreeNoDecRef(newargs, kwnames);
Expand Down Expand Up @@ -1799,8 +1823,8 @@ _PyEval_Vector(PyThreadState *tstate, PyFunctionObject *func,
Py_INCREF(args[i+argcount]);
}
}
_PyInterpreterFrame *frame = _PyEvalFramePushAndInit(
tstate, func, locals, (_PyStackRef * const)args, argcount, kwnames);
_PyInterpreterFrame *frame = _PyEvalFramePushAndInit_UnTagged(
tstate, func, locals, args, argcount, kwnames);
if (frame == NULL) {
return NULL;
}
Expand Down
24 changes: 24 additions & 0 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "pycore_tstate.h" // _PyThreadStateImpl
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
#include "pydtrace.h"
#include "pycore_tagged.h"

#ifdef Py_GIL_DISABLED

Expand Down Expand Up @@ -308,6 +309,27 @@ gc_visit_heaps(PyInterpreterState *interp, mi_block_visit_fun *visitor,
return err;
}

static void
gc_visit_thread_stacks(struct _stoptheworld_state *stw)
{
HEAD_LOCK(&_PyRuntime);
PyInterpreterState *interp;
PyThreadState *tstate;
_Py_FOR_EACH_THREAD(stw, interp, tstate) {
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
_PyStackChunk *curr_chunk = tstate->datastack_chunk;
while (curr_chunk != NULL) {
for (size_t curr_i = 0; curr_i < curr_chunk->top; curr_i++) {
_PyStackRef curr_o = Py_STACK_TAG_UNSAFE(curr_chunk->data[curr_i]);
if ((curr_o.bits & Py_TAG_DEFERRED) == Py_TAG_DEFERRED) {
gc_add_refs(Py_STACK_UNTAG_OWNED(curr_o), 1);
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved
}
}
curr_chunk = curr_chunk->previous;
}
}
HEAD_UNLOCK(&_PyRuntime);
}

static void
merge_queued_objects(_PyThreadStateImpl *tstate, struct collection_state *state)
{
Expand Down Expand Up @@ -566,6 +588,8 @@ deduce_unreachable_heap(PyInterpreterState *interp,
gc_visit_heaps(interp, &validate_gc_objects, &state->base);
#endif

gc_visit_thread_stacks(&interp->stoptheworld);

// Transitively mark reachable objects by clearing the
// _PyGC_BITS_UNREACHABLE flag.
if (gc_visit_heaps(interp, &mark_heap_visitor, &state->base) < 0) {
Expand Down
13 changes: 2 additions & 11 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2079,23 +2079,14 @@ decrement_stoptheworld_countdown(struct _stoptheworld_state *stw)
// we start with the first interpreter and then iterate over all interpreters.
// For per-interpreter stop-the-world events, we only operate on the one
// interpreter.
static PyInterpreterState *
interp_for_stop_the_world(struct _stoptheworld_state *stw)
PyInterpreterState *
_Py_interp_for_stop_the_world(struct _stoptheworld_state *stw)
{
return (stw->is_global
? PyInterpreterState_Head()
: _Py_CONTAINER_OF(stw, PyInterpreterState, stoptheworld));
}

// Loops over threads for a stop-the-world event.
// For global: all threads in all interpreters
// For per-interpreter: all threads in the interpreter
#define _Py_FOR_EACH_THREAD(stw, i, t) \
for (i = interp_for_stop_the_world((stw)); \
i != NULL; i = ((stw->is_global) ? i->next : NULL)) \
for (t = i->threads.head; t; t = t->next)


// Try to transition threads atomically from the "detached" state to the
// "gc stopped" state. Returns true if all threads are in the "gc stopped"
static bool
Expand Down