Skip to content

Commit 3852c28

Browse files
committed
Get tos caching working with exit-at-end. Add some more tracing info
1 parent 9a8a5eb commit 3852c28

File tree

11 files changed

+1246
-75
lines changed

11 files changed

+1246
-75
lines changed

Include/internal/pycore_stackref.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ PyStackRef_IsValid(_PyStackRef ref)
310310
return ref.bits >= Py_INT_TAG;
311311
}
312312

313+
static inline bool
314+
PyStackRef_IsWrapped(_PyStackRef ref)
315+
{
316+
#ifdef Py_DEBUG
317+
return (ref.bits & Py_TAG_INVALID) != 0 && !PyStackRef_IsError(ref);
318+
#else
319+
Py_FatalError("Cannot determine if value is wrapped if Py_DEBUG is not set");
320+
#endif
321+
}
322+
313323
static inline bool
314324
PyStackRef_IsTaggedInt(_PyStackRef i)
315325
{

Include/internal/pycore_uop_metadata.h

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

Python/bytecodes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5349,6 +5349,9 @@ dummy_func(
53495349
tier2 op(_START_EXECUTOR, (executor/4 --)) {
53505350
#ifndef _Py_JIT
53515351
current_executor = (_PyExecutorObject*)executor;
5352+
#endif
5353+
#ifdef Py_DEBUG
5354+
assert(STACK_LEVEL() + current_cached_values == oparg);
53525355
#endif
53535356
assert(tstate->jit_exit == NULL || tstate->jit_exit->executor == current_executor);
53545357
tstate->current_executor = (PyObject *)executor;

Python/ceval.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,23 @@ dump_item(_PyStackRef item)
164164
printf("%" PRId64, (int64_t)PyStackRef_UntagInt(item));
165165
return;
166166
}
167-
if (PyStackRef_IsValid(item)) {
168-
PyObject *obj = PyStackRef_AsPyObjectBorrow(item);
169-
if (obj == NULL) {
170-
printf("<nil>");
171-
return;
172-
}
173-
// Don't call __repr__(), it might recurse into the interpreter.
174-
printf("<%s at %p>", Py_TYPE(obj)->tp_name, (void *)obj);
167+
if (PyStackRef_IsWrapped(item)) {
168+
void *ptr = PyStackRef_Unwrap(item);
169+
printf("Wrapped(pointer %p)", ptr);
170+
return;
175171
}
176-
else {
177-
/* Already handled NULL */
178-
if (PyStackRef_IsError(item)) {
179-
printf("ERROR");
180-
}
181-
else {
182-
// Wrapped item
183-
void *ptr = PyStackRef_Unwrap(item);
184-
printf("Wrapped(pointer %p)", ptr);
185-
}
172+
if (PyStackRef_IsError(item)) {
173+
printf("ERROR");
174+
return;
175+
}
176+
assert(PyStackRef_IsValid(item));
177+
PyObject *obj = PyStackRef_AsPyObjectBorrow(item);
178+
if (obj == NULL) {
179+
printf("<nil>");
180+
return;
186181
}
182+
// Don't call __repr__(), it might recurse into the interpreter.
183+
printf("<%s at %p>", Py_TYPE(obj)->tp_name, (void *)obj);
187184
}
188185

189186
static void
@@ -214,11 +211,21 @@ dump_stack(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer)
214211
}
215212
printf("]\n");
216213
}
217-
fflush(stdout);
218214
PyErr_SetRaisedException(exc);
219215
_PyFrame_GetStackPointer(frame);
220216
}
221217

218+
static void
219+
dump_cache_item(_PyStackRef cache, int position, int depth)
220+
{
221+
if (position < depth) {
222+
dump_item(cache);
223+
}
224+
else {
225+
printf("---");
226+
}
227+
}
228+
222229
static void
223230
lltrace_instruction(_PyInterpreterFrame *frame,
224231
_PyStackRef *stack_pointer,
@@ -1185,6 +1192,13 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11851192
#ifdef Py_DEBUG
11861193
if (frame->lltrace >= 3) {
11871194
dump_stack(frame, stack_pointer);
1195+
printf(" cache=[");
1196+
dump_cache_item(_tos_cache0, 0, current_cached_values);
1197+
printf(", ");
1198+
dump_cache_item(_tos_cache1, 1, current_cached_values);
1199+
printf(", ");
1200+
dump_cache_item(_tos_cache2, 2, current_cached_values);
1201+
printf("]\n");
11881202
if (next_uop->opcode == _START_EXECUTOR_r00) {
11891203
printf("%4d uop: ", 0);
11901204
}
@@ -1193,6 +1207,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11931207
}
11941208
_PyUOpPrint(next_uop);
11951209
printf("\n");
1210+
fflush(stdout);
11961211
}
11971212
#endif
11981213
next_uop++;

Python/ceval_macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ GETITEM(PyObject *v, Py_ssize_t i) {
212212
#define WITHIN_STACK_BOUNDS() \
213213
(frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
214214

215+
#define WITHIN_STACK_BOUNDS_WITH_CACHE() \
216+
(frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= 0 && (STACK_LEVEL() + current_cached_values) <= STACK_SIZE()))
217+
215218
/* Data access macros */
216219
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)
217220
#define FRAME_CO_NAMES (_PyFrame_GetCode(frame)->co_names)

0 commit comments

Comments
 (0)