Skip to content

Commit 059bd4d

Browse files
authored
pythonGH-108614: Remove non-debug uses of #if TIER_ONE and #if TIER_TWO from _POP_FRAME op. (pythonGH-108685)
1 parent 194c6fb commit 059bd4d

File tree

7 files changed

+123
-66
lines changed

7 files changed

+123
-66
lines changed

Python/bytecodes.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -768,24 +768,25 @@ dummy_func(
768768
// different frame, and it's accounted for by _PUSH_FRAME.
769769
op(_POP_FRAME, (retval --)) {
770770
assert(EMPTY());
771-
_PyFrame_SetStackPointer(frame, stack_pointer);
772-
_Py_LeaveRecursiveCallPy(tstate);
773-
// GH-99729: We need to unlink the frame *before* clearing it:
774-
_PyInterpreterFrame *dying = frame;
775771
#if TIER_ONE
776772
assert(frame != &entry_frame);
777773
#endif
774+
STORE_SP();
775+
_Py_LeaveRecursiveCallPy(tstate);
776+
// GH-99729: We need to unlink the frame *before* clearing it:
777+
_PyInterpreterFrame *dying = frame;
778778
frame = tstate->current_frame = dying->previous;
779779
_PyEval_FrameClearAndPop(tstate, dying);
780780
frame->prev_instr += frame->return_offset;
781781
_PyFrame_StackPush(frame, retval);
782-
#if TIER_ONE
783-
goto resume_frame;
784-
#endif
785-
#if TIER_TWO
786-
stack_pointer = _PyFrame_GetStackPointer(frame);
787-
ip_offset = (_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive;
788-
#endif
782+
LOAD_SP();
783+
LOAD_IP();
784+
#if LLTRACE && TIER_ONE
785+
lltrace = maybe_lltrace_resume_frame(frame, &entry_frame, GLOBALS());
786+
if (lltrace < 0) {
787+
goto exit_unwind;
788+
}
789+
#endif
789790
}
790791

791792
macro(RETURN_VALUE) =

Python/ceval.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,34 @@ lltrace_resume_frame(_PyInterpreterFrame *frame)
189189
fflush(stdout);
190190
PyErr_SetRaisedException(exc);
191191
}
192+
193+
static int
194+
maybe_lltrace_resume_frame(_PyInterpreterFrame *frame, _PyInterpreterFrame *skip_frame, PyObject *globals)
195+
{
196+
if (globals == NULL) {
197+
return 0;
198+
}
199+
if (frame == skip_frame) {
200+
return 0;
201+
}
202+
int r = PyDict_Contains(globals, &_Py_ID(__lltrace__));
203+
if (r < 0) {
204+
return -1;
205+
}
206+
int lltrace = r;
207+
if (!lltrace) {
208+
// When tracing executed uops, also trace bytecode
209+
char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG");
210+
if (uop_debug != NULL && *uop_debug >= '0') {
211+
lltrace = (*uop_debug - '0') >= 5; // TODO: Parse an int and all that
212+
}
213+
}
214+
if (lltrace) {
215+
lltrace_resume_frame(frame);
216+
}
217+
return lltrace;
218+
}
219+
192220
#endif
193221

194222
static void monitor_raise(PyThreadState *tstate,
@@ -576,6 +604,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
576604
return _PyEval_EvalFrame(tstate, f->f_frame, throwflag);
577605
}
578606

607+
#define TIER_ONE 1
579608
#include "ceval_macros.h"
580609

581610

@@ -714,24 +743,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
714743
SET_LOCALS_FROM_FRAME();
715744

716745
#ifdef LLTRACE
717-
{
718-
if (frame != &entry_frame && GLOBALS()) {
719-
int r = PyDict_Contains(GLOBALS(), &_Py_ID(__lltrace__));
720-
if (r < 0) {
721-
goto exit_unwind;
722-
}
723-
lltrace = r;
724-
if (!lltrace) {
725-
// When tracing executed uops, also trace bytecode
726-
char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG");
727-
if (uop_debug != NULL && *uop_debug >= '0') {
728-
lltrace = (*uop_debug - '0') >= 5; // TODO: Parse an int and all that
729-
}
730-
}
731-
}
732-
if (lltrace) {
733-
lltrace_resume_frame(frame);
734-
}
746+
lltrace = maybe_lltrace_resume_frame(frame, &entry_frame, GLOBALS());
747+
if (lltrace < 0) {
748+
goto exit_unwind;
735749
}
736750
#endif
737751

@@ -752,7 +766,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
752766
#endif
753767
{
754768

755-
#define TIER_ONE 1
756769
#include "generated_cases.c.h"
757770

758771
/* INSTRUMENTED_LINE has to be here, rather than in bytecodes.c,

Python/ceval_macros.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,39 @@ static inline int _Py_EnterRecursivePy(PyThreadState *tstate) {
373373
static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) {
374374
tstate->py_recursion_remaining++;
375375
}
376+
377+
378+
/* Implementation of "macros" that modify the instruction pointer,
379+
* stack pointer, or frame pointer.
380+
* These need to treated differently by tier 1 and 2. */
381+
382+
#if TIER_ONE
383+
384+
#define LOAD_IP() \
385+
do { next_instr = frame->prev_instr+1; } while (0)
386+
387+
#define STORE_SP() \
388+
_PyFrame_SetStackPointer(frame, stack_pointer)
389+
390+
#define LOAD_SP() \
391+
stack_pointer = _PyFrame_GetStackPointer(frame);
392+
393+
#endif
394+
395+
396+
#if TIER_TWO
397+
398+
#define LOAD_IP() \
399+
do { ip_offset = (_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive; } while (0)
400+
401+
#define STORE_SP() \
402+
_PyFrame_SetStackPointer(frame, stack_pointer)
403+
404+
#define LOAD_SP() \
405+
stack_pointer = _PyFrame_GetStackPointer(frame);
406+
407+
#endif
408+
409+
410+
411+

Python/executor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "pycore_sliceobject.h"
1818
#include "pycore_uops.h"
1919

20+
#define TIER_TWO 2
2021
#include "ceval_macros.h"
2122

2223

@@ -83,7 +84,6 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
8384
OBJECT_STAT_INC(optimization_uops_executed);
8485
switch (opcode) {
8586

86-
#define TIER_TWO 2
8787
#include "executor_cases.c.h"
8888

8989
default:

Python/executor_cases.c.h

Lines changed: 12 additions & 11 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: 28 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/cases_generator/stacking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ def write_macro_instr(
369369
next_instr_is_set = write_components(parts, out, TIER_ONE, mac.cache_offset)
370370
except AssertionError as err:
371371
raise AssertionError(f"Error writing macro {mac.name}") from err
372-
if not parts[-1].instr.always_exits and not next_instr_is_set:
373-
if mac.cache_offset:
372+
if not parts[-1].instr.always_exits:
373+
if not next_instr_is_set and mac.cache_offset:
374374
out.emit(f"next_instr += {mac.cache_offset};")
375375
out.emit("DISPATCH();")
376376

0 commit comments

Comments
 (0)