gh-111520: Integrate the Tier 2 interpreter in the Tier 1 interpreter (#111428)

- There is no longer a separate Python/executor.c file.
- Conventions in Python/bytecodes.c are slightly different -- don't use `goto error`,
  you must use `GOTO_ERROR(error)` (same for others like `unused_local_error`).
- The `TIER_ONE` and `TIER_TWO` symbols are only valid in the generated (.c.h) files.
- In Lib/test/support/__init__.py, `Py_C_RECURSION_LIMIT` is imported from `_testcapi`.
- On Windows, in debug mode, stack allocation grows from 8MiB to 12MiB.
- **Beware!** This changes the env vars to enable uops and their debugging
  to `PYTHON_UOPS` and `PYTHON_LLTRACE`.
This commit is contained in:
Guido van Rossum 2023-11-01 13:13:02 -07:00 committed by GitHub
parent 5d6db168b9
commit 7e135a48d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 509 additions and 487 deletions

View File

@ -2355,8 +2355,15 @@ def adjust_int_max_str_digits(max_digits):
#For recursion tests, easily exceeds default recursion limit
EXCEEDS_RECURSION_LIMIT = 5000
# The default C recursion limit (from Include/cpython/pystate.h).
Py_C_RECURSION_LIMIT = 1500
def _get_c_recursion_limit():
try:
import _testcapi
return _testcapi.Py_C_RECURSION_LIMIT
except (ImportError, AttributeError):
return 1500 # (from Include/cpython/pystate.h)
# The default C recursion limit.
Py_C_RECURSION_LIMIT = _get_c_recursion_limit()
#Windows doesn't have os.uname() but it doesn't support s390x.
skip_on_s390x = unittest.skipIf(hasattr(os, 'uname') and os.uname().machine == 's390x',

View File

@ -118,17 +118,19 @@ class TestGeneratedCases(unittest.TestCase):
with open(self.temp_output_filename) as temp_output:
lines = temp_output.readlines()
while lines and lines[0].startswith("// "):
while lines and lines[0].startswith(("// ", "#", " #", "\n")):
lines.pop(0)
while lines and lines[-1].startswith(("#", "\n")):
lines.pop(-1)
actual = "".join(lines)
# if actual.rstrip() != expected.rstrip():
# if actual.strip() != expected.strip():
# print("Actual:")
# print(actual)
# print("Expected:")
# print(expected)
# print("End")
self.assertEqual(actual.rstrip(), expected.rstrip())
self.assertEqual(actual.strip(), expected.strip())
def test_inst_no_args(self):
input = """

View File

@ -412,7 +412,6 @@ PYTHON_OBJS= \
Python/crossinterp.o \
Python/dynamic_annotations.o \
Python/errors.o \
Python/executor.o \
Python/flowgraph.o \
Python/frame.o \
Python/frozenmain.o \
@ -1603,12 +1602,6 @@ Python/ceval.o: \
$(srcdir)/Python/generated_cases.c.h \
$(srcdir)/Python/opcode_targets.h
Python/executor.o: \
$(srcdir)/Include/internal/pycore_opcode_metadata.h \
$(srcdir)/Include/internal/pycore_optimizer.h \
$(srcdir)/Python/ceval_macros.h \
$(srcdir)/Python/executor_cases.c.h
Python/flowgraph.o: \
$(srcdir)/Include/internal/pycore_opcode_metadata.h

View File

@ -0,0 +1,9 @@
Merge the Tier 1 (bytecode) and Tier 2 (micro-ops) interpreters together,
moving the Tier 2 interpreter loop and switch into
``_PyEval_EvalFrameDefault()`` in ``Python/ceval.c``.
The ``Python/executor.c`` file is gone.
Also the ``TIER_ONE`` and ``TIER_TWO`` macros are now handled
by the code generator.
**Beware!** This changes the environment variables to enable micro-ops and
their debugging to ``PYTHON_UOPS`` and ``PYTHON_LLTRACE``.

View File

@ -200,7 +200,6 @@
<ClCompile Include="..\Python\dynamic_annotations.c" />
<ClCompile Include="..\Python\dynload_win.c" />
<ClCompile Include="..\Python\errors.c" />
<ClCompile Include="..\Python\executor.c" />
<ClCompile Include="..\Python\fileutils.c" />
<ClCompile Include="..\Python\flowgraph.c" />
<ClCompile Include="..\Python\formatter_unicode.c" />

View File

@ -127,9 +127,6 @@
<ClCompile Include="..\Python\errors.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\executor.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Objects\exceptions.c">
<Filter>Source Files</Filter>
</ClCompile>

View File

@ -95,7 +95,7 @@
<Link>
<SubSystem>Console</SubSystem>
<StackReserveSize Condition="$(Configuration) != 'Debug'">2000000</StackReserveSize>
<StackReserveSize Condition="$(Configuration) == 'Debug'">8000000</StackReserveSize>
<StackReserveSize Condition="$(Configuration) == 'Debug'">12000000</StackReserveSize>
</Link>
</ItemDefinitionGroup>
<ItemGroup>

View File

@ -552,7 +552,6 @@
<ClCompile Include="..\Python\dynamic_annotations.c" />
<ClCompile Include="..\Python\dynload_win.c" />
<ClCompile Include="..\Python\errors.c" />
<ClCompile Include="..\Python\executor.c" />
<ClCompile Include="..\Python\fileutils.c" />
<ClCompile Include="..\Python\flowgraph.c" />
<ClCompile Include="..\Python\formatter_unicode.c" />

View File

@ -1253,9 +1253,6 @@
<ClCompile Include="..\Python\errors.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\executor.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\fileutils.c">
<Filter>Python</Filter>
</ClCompile>

View File

@ -63,6 +63,7 @@ static size_t jump;
static uint16_t invert, counter, index, hint;
#define unused 0 // Used in a macro def, can't be static
static uint32_t type_version;
static _PyUOpExecutorObject *current_executor;
static PyObject *
dummy_func(
@ -171,7 +172,7 @@ dummy_func(
uintptr_t code_version = _PyFrame_GetCode(frame)->_co_instrumentation_version;
if (code_version != global_version) {
if (_Py_Instrument(_PyFrame_GetCode(frame), tstate->interp)) {
goto error;
GOTO_ERROR(error);
}
next_instr = this_instr;
}
@ -268,7 +269,7 @@ dummy_func(
if (PyGen_Check(receiver)) {
PyErr_SetObject(PyExc_StopIteration, value);
if (monitor_stop_iteration(tstate, frame, this_instr)) {
goto error;
GOTO_ERROR(error);
}
PyErr_SetRaisedException(NULL);
}
@ -284,7 +285,7 @@ dummy_func(
if (PyGen_Check(receiver) || PyCoro_CheckExact(receiver)) {
PyErr_SetObject(PyExc_StopIteration, value);
if (monitor_stop_iteration(tstate, frame, this_instr)) {
goto error;
GOTO_ERROR(error);
}
PyErr_SetRaisedException(NULL);
}
@ -826,7 +827,7 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, retval);
if (err) goto error;
if (err) GOTO_ERROR(error);
STACK_SHRINK(1);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -850,7 +851,7 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, retval);
if (err) goto error;
if (err) GOTO_ERROR(error);
Py_INCREF(retval);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -906,7 +907,7 @@ dummy_func(
if (PyAsyncGen_CheckExact(aiter)) {
awaitable = type->tp_as_async->am_anext(aiter);
if (awaitable == NULL) {
goto error;
GOTO_ERROR(error);
}
} else {
if (type->tp_as_async != NULL){
@ -916,7 +917,7 @@ dummy_func(
if (getter != NULL) {
next_iter = (*getter)(aiter);
if (next_iter == NULL) {
goto error;
GOTO_ERROR(error);
}
}
else {
@ -924,7 +925,7 @@ dummy_func(
"'async for' requires an iterator with "
"__anext__ method, got %.100s",
type->tp_name);
goto error;
GOTO_ERROR(error);
}
awaitable = _PyCoro_GetAwaitableIter(next_iter);
@ -936,7 +937,7 @@ dummy_func(
Py_TYPE(next_iter)->tp_name);
Py_DECREF(next_iter);
goto error;
GOTO_ERROR(error);
} else {
Py_DECREF(next_iter);
}
@ -1019,7 +1020,7 @@ dummy_func(
JUMPBY(oparg);
}
else {
goto error;
GOTO_ERROR(error);
}
}
Py_DECREF(v);
@ -1054,7 +1055,7 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_YIELD,
frame, this_instr, retval);
if (err) goto error;
if (err) GOTO_ERROR(error);
tstate->exc_info = gen->gi_exc_state.previous_item;
gen->gi_exc_state.previous_item = NULL;
_Py_LeaveRecursiveCallPy(tstate);
@ -1108,7 +1109,7 @@ dummy_func(
else {
assert(PyLong_Check(lasti));
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
goto error;
GOTO_ERROR(error);
}
}
assert(exc && PyExceptionInstance_Check(exc));
@ -1187,7 +1188,7 @@ dummy_func(
if (ns == NULL) {
_PyErr_Format(tstate, PyExc_SystemError,
"no locals when deleting %R", name);
goto error;
GOTO_ERROR(error);
}
err = PyObject_DelItem(ns, name);
// Can't use ERROR_IF here.
@ -1195,7 +1196,7 @@ dummy_func(
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
NAME_ERROR_MSG,
name);
goto error;
GOTO_ERROR(error);
}
}
@ -1319,7 +1320,7 @@ dummy_func(
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
}
goto error;
GOTO_ERROR(error);
}
}
@ -1336,7 +1337,7 @@ dummy_func(
inst(LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) {
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name);
@ -1344,17 +1345,17 @@ dummy_func(
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
goto error;
GOTO_ERROR(error);
}
else {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
goto error;
GOTO_ERROR(error);
}
}
}
@ -1370,7 +1371,7 @@ dummy_func(
}
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name);
@ -1378,17 +1379,17 @@ dummy_func(
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
goto error;
GOTO_ERROR(error);
}
else {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
goto error;
GOTO_ERROR(error);
}
}
}
@ -1510,7 +1511,7 @@ dummy_func(
PyObject *initial = GETLOCAL(oparg);
PyObject *cell = PyCell_New(initial);
if (cell == NULL) {
goto error;
GOTO_ERROR(error);
}
SETLOCAL(oparg, cell);
}
@ -1522,7 +1523,7 @@ dummy_func(
// Fortunately we don't need its superpower.
if (oldobj == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
goto error;
GOTO_ERROR(error);
}
PyCell_SET(cell, NULL);
Py_DECREF(oldobj);
@ -1535,7 +1536,7 @@ dummy_func(
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) {
Py_DECREF(class_dict);
goto error;
GOTO_ERROR(error);
}
Py_DECREF(class_dict);
if (!value) {
@ -1543,7 +1544,7 @@ dummy_func(
value = PyCell_GET(cell);
if (value == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
goto error;
GOTO_ERROR(error);
}
Py_INCREF(value);
}
@ -1622,7 +1623,7 @@ dummy_func(
inst(BUILD_SET, (values[oparg] -- set)) {
set = PySet_New(NULL);
if (set == NULL)
goto error;
GOTO_ERROR(error);
int err = 0;
for (int i = 0; i < oparg; i++) {
PyObject *item = values[i];
@ -1690,7 +1691,7 @@ dummy_func(
PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
_PyErr_SetString(tstate, PyExc_SystemError,
"bad BUILD_CONST_KEY_MAP keys argument");
goto error; // Pop the keys and values.
GOTO_ERROR(error); // Pop the keys and values.
}
map = _PyDict_FromItems(
&PyTuple_GET_ITEM(keys, 0), 1,
@ -2363,13 +2364,16 @@ dummy_func(
JUMPBY(1-original_oparg);
frame->instr_ptr = next_instr;
Py_INCREF(executor);
if (executor->execute == _PyUopExecute) {
current_executor = (_PyUOpExecutorObject *)executor;
GOTO_TIER_TWO();
}
frame = executor->execute(executor, frame, stack_pointer);
if (frame == NULL) {
frame = tstate->current_frame;
goto resume_with_error;
}
next_instr = frame->instr_ptr;
goto resume_frame;
goto enter_tier_one;
}
inst(POP_JUMP_IF_FALSE, (unused/1, cond -- )) {
@ -2469,7 +2473,7 @@ dummy_func(
_PyErr_SetString(tstate, PyExc_TypeError,
"cannot 'yield from' a coroutine object "
"in a non-coroutine generator");
goto error;
GOTO_ERROR(error);
}
iter = iterable;
}
@ -2480,7 +2484,7 @@ dummy_func(
/* `iterable` is not a generator. */
iter = PyObject_GetIter(iterable);
if (iter == NULL) {
goto error;
GOTO_ERROR(error);
}
DECREF_INPUTS();
}
@ -2518,7 +2522,7 @@ dummy_func(
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
goto error;
GOTO_ERROR(error);
}
monitor_raise(tstate, frame, this_instr);
_PyErr_Clear(tstate);
@ -2548,7 +2552,7 @@ dummy_func(
else {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
goto error;
GOTO_ERROR(error);
}
monitor_raise(tstate, frame, this_instr);
_PyErr_Clear(tstate);
@ -2743,7 +2747,7 @@ dummy_func(
"asynchronous context manager protocol",
Py_TYPE(mgr)->tp_name);
}
goto error;
GOTO_ERROR(error);
}
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
if (exit == NULL) {
@ -2755,7 +2759,7 @@ dummy_func(
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
goto error;
GOTO_ERROR(error);
}
DECREF_INPUTS();
res = _PyObject_CallNoArgs(enter);
@ -2779,7 +2783,7 @@ dummy_func(
"context manager protocol",
Py_TYPE(mgr)->tp_name);
}
goto error;
GOTO_ERROR(error);
}
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__exit__));
if (exit == NULL) {
@ -2791,7 +2795,7 @@ dummy_func(
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
goto error;
GOTO_ERROR(error);
}
DECREF_INPUTS();
res = _PyObject_CallNoArgs(enter);
@ -3041,7 +3045,7 @@ dummy_func(
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
goto error;
GOTO_ERROR(error);
}
frame->return_offset = (uint16_t)(next_instr - this_instr);
DISPATCH_INLINED(new_frame);
@ -3258,7 +3262,7 @@ dummy_func(
STAT_INC(CALL, hit);
PyObject *self = _PyType_NewManagedObject(tp);
if (self == NULL) {
goto error;
GOTO_ERROR(error);
}
Py_DECREF(tp);
_PyInterpreterFrame *shim = _PyFrame_PushTrampolineUnchecked(
@ -3295,7 +3299,7 @@ dummy_func(
PyErr_Format(PyExc_TypeError,
"__init__() should return None, not '%.200s'",
Py_TYPE(should_be_none)->tp_name);
goto error;
GOTO_ERROR(error);
}
}
@ -3334,7 +3338,7 @@ dummy_func(
// This is slower but CPython promises to check all non-vectorcall
// function calls.
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
goto error;
GOTO_ERROR(error);
}
PyObject *arg = args[0];
res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg);
@ -3419,7 +3423,7 @@ dummy_func(
PyObject *arg = args[0];
Py_ssize_t len_i = PyObject_Length(arg);
if (len_i < 0) {
goto error;
GOTO_ERROR(error);
}
res = PyLong_FromSsize_t(len_i);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
@ -3444,7 +3448,7 @@ dummy_func(
PyObject *inst = args[0];
int retval = PyObject_IsInstance(inst, cls);
if (retval < 0) {
goto error;
GOTO_ERROR(error);
}
res = PyBool_FromLong(retval);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
@ -3494,7 +3498,7 @@ dummy_func(
// This is slower but CPython promises to check all non-vectorcall
// function calls.
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
goto error;
GOTO_ERROR(error);
}
res = _PyCFunction_TrampolineCall(cfunc, self, arg);
_Py_LeaveRecursiveCallTstate(tstate);
@ -3554,7 +3558,7 @@ dummy_func(
// This is slower but CPython promises to check all non-vectorcall
// function calls.
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
goto error;
GOTO_ERROR(error);
}
res = _PyCFunction_TrampolineCall(cfunc, self, NULL);
_Py_LeaveRecursiveCallTstate(tstate);
@ -3641,7 +3645,7 @@ dummy_func(
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
goto error;
GOTO_ERROR(error);
}
assert(next_instr - this_instr == 1);
frame->return_offset = 1;
@ -3689,11 +3693,11 @@ dummy_func(
assert(kwargs == NULL || PyDict_CheckExact(kwargs));
if (!PyTuple_CheckExact(callargs)) {
if (check_args_iterable(tstate, func, callargs) < 0) {
goto error;
GOTO_ERROR(error);
}
PyObject *tuple = PySequence_Tuple(callargs);
if (tuple == NULL) {
goto error;
GOTO_ERROR(error);
}
Py_SETREF(callargs, tuple);
}
@ -3707,7 +3711,7 @@ dummy_func(
int err = _Py_call_instrumentation_2args(
tstate, PY_MONITORING_EVENT_CALL,
frame, this_instr, func, arg);
if (err) goto error;
if (err) GOTO_ERROR(error);
result = PyObject_Call(func, callargs, kwargs);
if (result == NULL) {
_Py_call_instrumentation_exc2(
@ -3738,7 +3742,7 @@ dummy_func(
// Need to manually shrink the stack since we exit with DISPATCH_INLINED.
STACK_SHRINK(oparg + 3);
if (new_frame == NULL) {
goto error;
GOTO_ERROR(error);
}
assert(next_instr - this_instr == 1);
frame->return_offset = 1;
@ -3759,7 +3763,7 @@ dummy_func(
Py_DECREF(codeobj);
if (func_obj == NULL) {
goto error;
GOTO_ERROR(error);
}
_PyFunction_SetVersion(
@ -3799,7 +3803,7 @@ dummy_func(
PyFunctionObject *func = (PyFunctionObject *)frame->f_funcobj;
PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func);
if (gen == NULL) {
goto error;
GOTO_ERROR(error);
}
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -3988,24 +3992,25 @@ dummy_func(
op(_POP_JUMP_IF_FALSE, (flag -- )) {
if (Py_IsFalse(flag)) {
pc = oparg;
next_uop = current_executor->trace + oparg;
}
}
op(_POP_JUMP_IF_TRUE, (flag -- )) {
if (Py_IsTrue(flag)) {
pc = oparg;
next_uop = current_executor->trace + oparg;
}
}
op(_JUMP_TO_TOP, (--)) {
pc = 0;
next_uop = current_executor->trace;
CHECK_EVAL_BREAKER();
}
op(_SET_IP, (--)) {
TIER_TWO_ONLY
frame->instr_ptr = ip_offset + oparg;
// TODO: Put the code pointer in `operand` to avoid indirection via `frame`
frame->instr_ptr = _PyCode_CODE(_PyFrame_GetCode(frame)) + oparg;
}
op(_SAVE_RETURN_OFFSET, (--)) {
@ -4019,10 +4024,7 @@ dummy_func(
op(_EXIT_TRACE, (--)) {
TIER_TWO_ONLY
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
return frame;
GOTO_TIER_ONE();
}
op(_INSERT, (unused[oparg], top -- top, unused[oparg])) {

View File

@ -202,15 +202,15 @@ maybe_lltrace_resume_frame(_PyInterpreterFrame *frame, _PyInterpreterFrame *skip
if (r < 0) {
return -1;
}
int lltrace = r;
int lltrace = r * 5; // Levels 1-4 only trace uops
if (!lltrace) {
// When tracing executed uops, also trace bytecode
char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG");
if (uop_debug != NULL && *uop_debug >= '0') {
lltrace = (*uop_debug - '0') >= 5; // TODO: Parse an int and all that
// Can also be controlled by environment variable
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
if (python_lltrace != NULL && *python_lltrace >= '0') {
lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that
}
}
if (lltrace) {
if (lltrace >= 5) {
lltrace_resume_frame(frame);
}
return lltrace;
@ -611,10 +611,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
return _PyEval_EvalFrame(tstate, f->f_frame, throwflag);
}
#define TIER_ONE 1
#include "ceval_macros.h"
int _Py_CheckRecursiveCallPy(
PyThreadState *tstate)
{
@ -680,9 +678,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
#ifdef Py_STATS
int lastopcode = 0;
#endif
// opcode is an 8-bit value to improve the code generated by MSVC
// for the big switch below (in combination with the EXTRA_CASES macro).
uint8_t opcode; /* Current opcode */
int opcode; /* Current opcode */
int oparg; /* Current opcode argument, if any */
#ifdef LLTRACE
int lltrace = 0;
@ -730,6 +726,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
goto resume_with_error;
}
/* State shared between Tier 1 and Tier 2 interpreter */
_PyUOpExecutorObject *current_executor = NULL;
/* Local "register" variables.
* These are cached values from the frame and code object. */
@ -766,7 +765,9 @@ resume_frame:
/* Start instructions */
#if !USE_COMPUTED_GOTOS
dispatch_opcode:
switch (opcode)
// Cast to an 8-bit value to improve the code generated by MSVC
// (in combination with the EXTRA_CASES macro).
switch ((uint8_t)opcode)
#endif
{
@ -914,7 +915,7 @@ exception_unwind:
}
/* Resume normal execution */
#ifdef LLTRACE
if (lltrace) {
if (lltrace >= 5) {
lltrace_resume_frame(frame);
}
#endif
@ -943,6 +944,135 @@ resume_with_error:
stack_pointer = _PyFrame_GetStackPointer(frame);
goto error;
// The Tier 2 interpreter is also here!
enter_tier_two:
#undef LOAD_IP
#define LOAD_IP(UNUSED) (void)0
#undef GOTO_ERROR
#define GOTO_ERROR(LABEL) goto LABEL ## _tier_two
#undef DEOPT_IF
#define DEOPT_IF(COND, INSTNAME) \
if ((COND)) { \
goto deoptimize;\
}
#ifdef Py_STATS
// Disable these macros that apply to Tier 1 stats when we are in Tier 2
#undef STAT_INC
#define STAT_INC(opname, name) ((void)0)
#undef STAT_DEC
#define STAT_DEC(opname, name) ((void)0)
#undef CALL_STAT_INC
#define CALL_STAT_INC(name) ((void)0)
#endif
#undef ENABLE_SPECIALIZATION
#define ENABLE_SPECIALIZATION 0
#ifdef Py_DEBUG
#define DPRINTF(level, ...) \
if (lltrace >= (level)) { printf(__VA_ARGS__); }
#else
#define DPRINTF(level, ...)
#endif
OPT_STAT_INC(traces_executed);
_PyUOpInstruction *next_uop = current_executor->trace;
#ifdef Py_DEBUG
uint64_t operand; // Used by several DPRINTF() calls
#endif
#ifdef Py_STATS
uint64_t trace_uop_execution_counter = 0;
#endif
for (;;) {
opcode = next_uop->opcode;
oparg = next_uop->oparg;
#ifdef Py_DEBUG
operand = next_uop->operand;
#endif
DPRINTF(3,
"%4d: uop %s, oparg %d, operand %" PRIu64 ", stack_level %d\n",
(int)(next_uop - current_executor->trace),
opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode],
oparg,
operand,
(int)(stack_pointer - _PyFrame_Stackbase(frame)));
next_uop++;
OPT_STAT_INC(uops_executed);
#ifdef Py_STATS
trace_uop_execution_counter++;
#endif
switch (opcode) {
#include "executor_cases.c.h"
default:
#ifdef Py_DEBUG
{
fprintf(stderr, "Unknown uop %d, oparg %d, operand %" PRIu64 "\n",
opcode, oparg, operand);
Py_FatalError("Unknown uop");
}
#else
Py_UNREACHABLE();
#endif
}
}
// Jump here from ERROR_IF(..., unbound_local_error)
unbound_local_error_tier_two:
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
);
goto error_tier_two;
// JUMP to any of these from ERROR_IF(..., error)
pop_4_error_tier_two:
STACK_SHRINK(1);
pop_3_error_tier_two:
STACK_SHRINK(1);
pop_2_error_tier_two:
STACK_SHRINK(1);
pop_1_error_tier_two:
STACK_SHRINK(1);
error_tier_two:
DPRINTF(2, "Error: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
frame->return_offset = 0; // Don't leave this random
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(current_executor);
goto resume_with_error;
// Jump here from DEOPT_IF()
deoptimize:
// On DEOPT_IF we just repeat the last instruction.
// This presumes nothing was popped from the stack (nor pushed).
DPRINTF(2, "DEOPT: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
frame->return_offset = 0; // Dispatch to frame->instr_ptr
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(current_executor);
// Fall through
// Jump here from ENTER_EXECUTOR
enter_tier_one:
next_instr = frame->instr_ptr;
goto resume_frame;
// Jump here from _EXIT_TRACE
exit_trace:
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(current_executor);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
goto enter_tier_one;
}
#if defined(__GNUC__)
# pragma GCC diagnostic pop

View File

@ -1,4 +1,4 @@
// Macros and other things needed by ceval.c, executor.c, and bytecodes.c
// Macros and other things needed by ceval.c, and bytecodes.c
/* Computed GOTOs, or
the-optimization-commonly-but-improperly-known-as-"threaded code"
@ -80,7 +80,7 @@
/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */
#ifdef LLTRACE
#define PRE_DISPATCH_GOTO() if (lltrace) { \
#define PRE_DISPATCH_GOTO() if (lltrace >= 5) { \
lltrace_instruction(frame, stack_pointer, next_instr); }
#else
#define PRE_DISPATCH_GOTO() ((void)0)
@ -112,11 +112,14 @@
goto start_frame; \
} while (0)
// Use this instead of 'goto error' so Tier 2 can go to a different label
#define GOTO_ERROR(LABEL) goto LABEL
#define CHECK_EVAL_BREAKER() \
_Py_CHECK_EMSCRIPTEN_SIGNALS_PERIODICALLY(); \
if (_Py_atomic_load_uintptr_relaxed(&tstate->interp->ceval.eval_breaker) & _PY_EVAL_EVENTS_MASK) { \
if (_Py_HandlePending(tstate) != 0) { \
goto error; \
GOTO_ERROR(error); \
} \
}
@ -322,7 +325,7 @@ do { \
}\
else { \
result = PyFloat_FromDouble(dval); \
if ((result) == NULL) goto error; \
if ((result) == NULL) GOTO_ERROR(error); \
_Py_DECREF_NO_DEALLOC(left); \
_Py_DECREF_NO_DEALLOC(right); \
} \
@ -374,27 +377,14 @@ static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) {
/* Implementation of "macros" that modify the instruction pointer,
* stack pointer, or frame pointer.
* These need to treated differently by tier 1 and 2. */
#if TIER_ONE
* These need to treated differently by tier 1 and 2.
* The Tier 1 version is here; Tier 2 is inlined in ceval.c. */
#define LOAD_IP(OFFSET) do { \
next_instr = frame->instr_ptr + (OFFSET); \
} while (0)
#define STORE_SP() \
_PyFrame_SetStackPointer(frame, stack_pointer)
#define LOAD_SP() \
stack_pointer = _PyFrame_GetStackPointer(frame);
#endif
#if TIER_TWO
#define LOAD_IP(UNUSED) \
do { ip_offset = (_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive; } while (0)
/* There's no STORE_IP(), it's inlined by the code generator. */
#define STORE_SP() \
_PyFrame_SetStackPointer(frame, stack_pointer)
@ -402,8 +392,8 @@ _PyFrame_SetStackPointer(frame, stack_pointer)
#define LOAD_SP() \
stack_pointer = _PyFrame_GetStackPointer(frame);
#endif
/* Tier-switching macros. */
#define GOTO_TIER_TWO() goto enter_tier_two;
#define GOTO_TIER_ONE() goto exit_trace;

View File

@ -1,149 +0,0 @@
#include "Python.h"
#include "opcode.h"
#include "pycore_bitutils.h"
#include "pycore_call.h"
#include "pycore_ceval.h"
#include "pycore_dict.h"
#include "pycore_emscripten_signal.h"
#include "pycore_intrinsics.h"
#include "pycore_long.h"
#include "pycore_object.h"
#include "pycore_opcode_metadata.h"
#include "pycore_opcode_utils.h"
#include "pycore_pyerrors.h"
#include "pycore_range.h"
#include "pycore_setobject.h" // _PySet_Update()
#include "pycore_sliceobject.h"
#include "pycore_uops.h"
#define TIER_TWO 2
#include "ceval_macros.h"
#undef DEOPT_IF
#define DEOPT_IF(COND, INSTNAME) \
if ((COND)) { \
UOP_STAT_INC(INSTNAME, miss); \
goto deoptimize; \
}
#ifdef Py_STATS
// Disable these macros that apply to Tier 1 stats when we are in Tier 2
#undef STAT_INC
#define STAT_INC(opname, name) ((void)0)
#undef STAT_DEC
#define STAT_DEC(opname, name) ((void)0)
#undef CALL_STAT_INC
#define CALL_STAT_INC(name) ((void)0)
#endif
#undef ENABLE_SPECIALIZATION
#define ENABLE_SPECIALIZATION 0
_PyInterpreterFrame *
_PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **stack_pointer)
{
#ifdef Py_DEBUG
char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG");
int lltrace = 0;
if (uop_debug != NULL && *uop_debug >= '0') {
lltrace = *uop_debug - '0'; // TODO: Parse an int and all that
}
#define DPRINTF(level, ...) \
if (lltrace >= (level)) { printf(__VA_ARGS__); }
#else
#define DPRINTF(level, ...)
#endif
DPRINTF(3,
"Entering _PyUopExecute for %s (%s:%d) at byte offset %ld\n",
PyUnicode_AsUTF8(_PyFrame_GetCode(frame)->co_qualname),
PyUnicode_AsUTF8(_PyFrame_GetCode(frame)->co_filename),
_PyFrame_GetCode(frame)->co_firstlineno,
2 * (long)(frame->instr_ptr -
(_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive));
PyThreadState *tstate = _PyThreadState_GET();
_PyUOpExecutorObject *self = (_PyUOpExecutorObject *)executor;
CHECK_EVAL_BREAKER();
OPT_STAT_INC(traces_executed);
_Py_CODEUNIT *ip_offset = (_Py_CODEUNIT *)_PyFrame_GetCode(frame)->co_code_adaptive;
int pc = 0;
int opcode;
int oparg;
uint64_t operand;
#ifdef Py_STATS
uint64_t trace_uop_execution_counter = 0;
#endif
for (;;) {
opcode = self->trace[pc].opcode;
oparg = self->trace[pc].oparg;
operand = self->trace[pc].operand;
DPRINTF(3,
"%4d: uop %s, oparg %d, operand %" PRIu64 ", stack_level %d\n",
pc,
opcode < 256 ? _PyOpcode_OpName[opcode] : _PyOpcode_uop_name[opcode],
oparg,
operand,
(int)(stack_pointer - _PyFrame_Stackbase(frame)));
pc++;
OPT_STAT_INC(uops_executed);
UOP_STAT_INC(opcode, execution_count);
#ifdef Py_STATS
trace_uop_execution_counter++;
#endif
switch (opcode) {
#include "executor_cases.c.h"
default:
{
fprintf(stderr, "Unknown uop %d, operand %" PRIu64 "\n", opcode, operand);
Py_FatalError("Unknown uop");
}
}
}
unbound_local_error:
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
);
goto error;
pop_4_error:
STACK_SHRINK(1);
pop_3_error:
STACK_SHRINK(1);
pop_2_error:
STACK_SHRINK(1);
pop_1_error:
STACK_SHRINK(1);
error:
// On ERROR_IF we return NULL as the frame.
// The caller recovers the frame from tstate->current_frame.
DPRINTF(2, "Error: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
frame->return_offset = 0; // Don't leave this random
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
return NULL;
deoptimize:
// On DEOPT_IF we just repeat the last instruction.
// This presumes nothing was popped from the stack (nor pushed).
DPRINTF(2, "DEOPT: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
frame->return_offset = 0; // Dispatch to frame->instr_ptr
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
return frame;
}

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,11 @@
// Python/bytecodes.c
// Do not edit!
#ifdef TIER_TWO
#error "This file is for Tier 1 only"
#endif
#define TIER_ONE 1
TARGET(NOP) {
frame->instr_ptr = next_instr;
next_instr += 1;
@ -61,7 +66,7 @@
uintptr_t code_version = _PyFrame_GetCode(frame)->_co_instrumentation_version;
if (code_version != global_version) {
if (_Py_Instrument(_PyFrame_GetCode(frame), tstate->interp)) {
goto error;
GOTO_ERROR(error);
}
next_instr = this_instr;
}
@ -250,7 +255,7 @@
if (PyGen_Check(receiver)) {
PyErr_SetObject(PyExc_StopIteration, value);
if (monitor_stop_iteration(tstate, frame, this_instr)) {
goto error;
GOTO_ERROR(error);
}
PyErr_SetRaisedException(NULL);
}
@ -286,7 +291,7 @@
if (PyGen_Check(receiver) || PyCoro_CheckExact(receiver)) {
PyErr_SetObject(PyExc_StopIteration, value);
if (monitor_stop_iteration(tstate, frame, this_instr)) {
goto error;
GOTO_ERROR(error);
}
PyErr_SetRaisedException(NULL);
}
@ -1190,7 +1195,7 @@
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, retval);
if (err) goto error;
if (err) GOTO_ERROR(error);
STACK_SHRINK(1);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -1250,7 +1255,7 @@
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, retval);
if (err) goto error;
if (err) GOTO_ERROR(error);
Py_INCREF(retval);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -1320,7 +1325,7 @@
if (PyAsyncGen_CheckExact(aiter)) {
awaitable = type->tp_as_async->am_anext(aiter);
if (awaitable == NULL) {
goto error;
GOTO_ERROR(error);
}
} else {
if (type->tp_as_async != NULL){
@ -1330,7 +1335,7 @@
if (getter != NULL) {
next_iter = (*getter)(aiter);
if (next_iter == NULL) {
goto error;
GOTO_ERROR(error);
}
}
else {
@ -1338,7 +1343,7 @@
"'async for' requires an iterator with "
"__anext__ method, got %.100s",
type->tp_name);
goto error;
GOTO_ERROR(error);
}
awaitable = _PyCoro_GetAwaitableIter(next_iter);
@ -1350,7 +1355,7 @@
Py_TYPE(next_iter)->tp_name);
Py_DECREF(next_iter);
goto error;
GOTO_ERROR(error);
} else {
Py_DECREF(next_iter);
}
@ -1454,7 +1459,7 @@
JUMPBY(oparg);
}
else {
goto error;
GOTO_ERROR(error);
}
}
Py_DECREF(v);
@ -1502,7 +1507,7 @@
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_YIELD,
frame, this_instr, retval);
if (err) goto error;
if (err) GOTO_ERROR(error);
tstate->exc_info = gen->gi_exc_state.previous_item;
gen->gi_exc_state.previous_item = NULL;
_Py_LeaveRecursiveCallPy(tstate);
@ -1575,7 +1580,7 @@
else {
assert(PyLong_Check(lasti));
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
goto error;
GOTO_ERROR(error);
}
}
assert(exc && PyExceptionInstance_Check(exc));
@ -1704,7 +1709,7 @@
if (ns == NULL) {
_PyErr_Format(tstate, PyExc_SystemError,
"no locals when deleting %R", name);
goto error;
GOTO_ERROR(error);
}
err = PyObject_DelItem(ns, name);
// Can't use ERROR_IF here.
@ -1712,7 +1717,7 @@
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
NAME_ERROR_MSG,
name);
goto error;
GOTO_ERROR(error);
}
DISPATCH();
}
@ -1907,7 +1912,7 @@
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
}
goto error;
GOTO_ERROR(error);
}
DISPATCH();
}
@ -1938,7 +1943,7 @@
mod_or_class_dict = stack_pointer[-1];
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name);
@ -1946,17 +1951,17 @@
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
goto error;
GOTO_ERROR(error);
}
else {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
goto error;
GOTO_ERROR(error);
}
}
}
@ -1978,7 +1983,7 @@
}
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name);
@ -1986,17 +1991,17 @@
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
goto error;
GOTO_ERROR(error);
}
else {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
goto error;
GOTO_ERROR(error);
}
if (v == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
goto error;
GOTO_ERROR(error);
}
}
}
@ -2164,7 +2169,7 @@
PyObject *initial = GETLOCAL(oparg);
PyObject *cell = PyCell_New(initial);
if (cell == NULL) {
goto error;
GOTO_ERROR(error);
}
SETLOCAL(oparg, cell);
DISPATCH();
@ -2180,7 +2185,7 @@
// Fortunately we don't need its superpower.
if (oldobj == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
goto error;
GOTO_ERROR(error);
}
PyCell_SET(cell, NULL);
Py_DECREF(oldobj);
@ -2200,7 +2205,7 @@
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) {
Py_DECREF(class_dict);
goto error;
GOTO_ERROR(error);
}
Py_DECREF(class_dict);
if (!value) {
@ -2208,7 +2213,7 @@
value = PyCell_GET(cell);
if (value == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
goto error;
GOTO_ERROR(error);
}
Py_INCREF(value);
}
@ -2363,7 +2368,7 @@
values = stack_pointer - oparg;
set = PySet_New(NULL);
if (set == NULL)
goto error;
GOTO_ERROR(error);
int err = 0;
for (int i = 0; i < oparg; i++) {
PyObject *item = values[i];
@ -2459,7 +2464,7 @@
PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
_PyErr_SetString(tstate, PyExc_SystemError,
"bad BUILD_CONST_KEY_MAP keys argument");
goto error; // Pop the keys and values.
GOTO_ERROR(error); // Pop the keys and values.
}
map = _PyDict_FromItems(
&PyTuple_GET_ITEM(keys, 0), 1,
@ -3432,13 +3437,16 @@
JUMPBY(1-original_oparg);
frame->instr_ptr = next_instr;
Py_INCREF(executor);
if (executor->execute == _PyUopExecute) {
current_executor = (_PyUOpExecutorObject *)executor;
GOTO_TIER_TWO();
}
frame = executor->execute(executor, frame, stack_pointer);
if (frame == NULL) {
frame = tstate->current_frame;
goto resume_with_error;
}
next_instr = frame->instr_ptr;
goto resume_frame;
goto enter_tier_one;
}
TARGET(POP_JUMP_IF_FALSE) {
@ -3673,7 +3681,7 @@
_PyErr_SetString(tstate, PyExc_TypeError,
"cannot 'yield from' a coroutine object "
"in a non-coroutine generator");
goto error;
GOTO_ERROR(error);
}
iter = iterable;
}
@ -3684,7 +3692,7 @@
/* `iterable` is not a generator. */
iter = PyObject_GetIter(iterable);
if (iter == NULL) {
goto error;
GOTO_ERROR(error);
}
Py_DECREF(iterable);
}
@ -3723,7 +3731,7 @@
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
goto error;
GOTO_ERROR(error);
}
monitor_raise(tstate, frame, this_instr);
_PyErr_Clear(tstate);
@ -3758,7 +3766,7 @@
else {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
goto error;
GOTO_ERROR(error);
}
monitor_raise(tstate, frame, this_instr);
_PyErr_Clear(tstate);
@ -3941,7 +3949,7 @@
"asynchronous context manager protocol",
Py_TYPE(mgr)->tp_name);
}
goto error;
GOTO_ERROR(error);
}
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
if (exit == NULL) {
@ -3953,7 +3961,7 @@
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
goto error;
GOTO_ERROR(error);
}
Py_DECREF(mgr);
res = _PyObject_CallNoArgs(enter);
@ -3988,7 +3996,7 @@
"context manager protocol",
Py_TYPE(mgr)->tp_name);
}
goto error;
GOTO_ERROR(error);
}
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__exit__));
if (exit == NULL) {
@ -4000,7 +4008,7 @@
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
goto error;
GOTO_ERROR(error);
}
Py_DECREF(mgr);
res = _PyObject_CallNoArgs(enter);
@ -4341,7 +4349,7 @@
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
goto error;
GOTO_ERROR(error);
}
frame->return_offset = (uint16_t)(next_instr - this_instr);
DISPATCH_INLINED(new_frame);
@ -4708,7 +4716,7 @@
STAT_INC(CALL, hit);
PyObject *self = _PyType_NewManagedObject(tp);
if (self == NULL) {
goto error;
GOTO_ERROR(error);
}
Py_DECREF(tp);
_PyInterpreterFrame *shim = _PyFrame_PushTrampolineUnchecked(
@ -4750,7 +4758,7 @@
PyErr_Format(PyExc_TypeError,
"__init__() should return None, not '%.200s'",
Py_TYPE(should_be_none)->tp_name);
goto error;
GOTO_ERROR(error);
}
STACK_SHRINK(1);
DISPATCH();
@ -4815,7 +4823,7 @@
// This is slower but CPython promises to check all non-vectorcall
// function calls.
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
goto error;
GOTO_ERROR(error);
}
PyObject *arg = args[0];
res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg);
@ -4942,7 +4950,7 @@
PyObject *arg = args[0];
Py_ssize_t len_i = PyObject_Length(arg);
if (len_i < 0) {
goto error;
GOTO_ERROR(error);
}
res = PyLong_FromSsize_t(len_i);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
@ -4981,7 +4989,7 @@
PyObject *inst = args[0];
int retval = PyObject_IsInstance(inst, cls);
if (retval < 0) {
goto error;
GOTO_ERROR(error);
}
res = PyBool_FromLong(retval);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
@ -5053,7 +5061,7 @@
// This is slower but CPython promises to check all non-vectorcall
// function calls.
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
goto error;
GOTO_ERROR(error);
}
res = _PyCFunction_TrampolineCall(cfunc, self, arg);
_Py_LeaveRecursiveCallTstate(tstate);
@ -5141,7 +5149,7 @@
// This is slower but CPython promises to check all non-vectorcall
// function calls.
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
goto error;
GOTO_ERROR(error);
}
res = _PyCFunction_TrampolineCall(cfunc, self, NULL);
_Py_LeaveRecursiveCallTstate(tstate);
@ -5263,7 +5271,7 @@
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
goto error;
GOTO_ERROR(error);
}
assert(next_instr - this_instr == 1);
frame->return_offset = 1;
@ -5330,11 +5338,11 @@
assert(kwargs == NULL || PyDict_CheckExact(kwargs));
if (!PyTuple_CheckExact(callargs)) {
if (check_args_iterable(tstate, func, callargs) < 0) {
goto error;
GOTO_ERROR(error);
}
PyObject *tuple = PySequence_Tuple(callargs);
if (tuple == NULL) {
goto error;
GOTO_ERROR(error);
}
Py_SETREF(callargs, tuple);
}
@ -5348,7 +5356,7 @@
int err = _Py_call_instrumentation_2args(
tstate, PY_MONITORING_EVENT_CALL,
frame, this_instr, func, arg);
if (err) goto error;
if (err) GOTO_ERROR(error);
result = PyObject_Call(func, callargs, kwargs);
if (result == NULL) {
_Py_call_instrumentation_exc2(
@ -5379,7 +5387,7 @@
// Need to manually shrink the stack since we exit with DISPATCH_INLINED.
STACK_SHRINK(oparg + 3);
if (new_frame == NULL) {
goto error;
GOTO_ERROR(error);
}
assert(next_instr - this_instr == 1);
frame->return_offset = 1;
@ -5412,7 +5420,7 @@
Py_DECREF(codeobj);
if (func_obj == NULL) {
goto error;
GOTO_ERROR(error);
}
_PyFunction_SetVersion(
@ -5467,7 +5475,7 @@
PyFunctionObject *func = (PyFunctionObject *)frame->f_funcobj;
PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func);
if (gen == NULL) {
goto error;
GOTO_ERROR(error);
}
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -5765,3 +5773,5 @@
assert(0 && "Executing RESERVED instruction.");
Py_UNREACHABLE();
}
#undef TIER_ONE

View File

@ -433,10 +433,10 @@ translate_bytecode_to_trace(
int trace_stack_depth = 0;
#ifdef Py_DEBUG
char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG");
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
int lltrace = 0;
if (uop_debug != NULL && *uop_debug >= '0') {
lltrace = *uop_debug - '0'; // TODO: Parse an int and all that
if (python_lltrace != NULL && *python_lltrace >= '0') {
lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that
}
#endif
@ -881,10 +881,10 @@ remove_unneeded_uops(_PyUOpInstruction *trace, int trace_length)
if (dest < last_instr) {
int new_trace_length = move_stubs(trace, dest, last_instr, trace_length);
#ifdef Py_DEBUG
char *uop_debug = Py_GETENV("PYTHONUOPSDEBUG");
char *python_lltrace = Py_GETENV("PYTHON_LLTRACE");
int lltrace = 0;
if (uop_debug != NULL && *uop_debug >= '0') {
lltrace = *uop_debug - '0'; // TODO: Parse an int and all that
if (python_lltrace != NULL && *python_lltrace >= '0') {
lltrace = *python_lltrace - '0'; // TODO: Parse an int and all that
}
if (lltrace >= 2) {
printf("Optimized trace (length %d+%d = %d, saved %d):\n",
@ -939,6 +939,15 @@ uop_optimize(
return 1;
}
/* Dummy execute() function for Uop Executor.
* The actual implementation is inlined in ceval.c,
* in _PyEval_EvalFrameDefault(). */
_PyInterpreterFrame *
_PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject **stack_pointer)
{
Py_FatalError("Tier 2 is now inlined into Tier 1");
}
static void
uop_opt_dealloc(PyObject *self) {
PyObject_Free(self);

View File

@ -1229,7 +1229,7 @@ init_interp_main(PyThreadState *tstate)
// Turn on experimental tier 2 (uops-based) optimizer
if (is_main_interp) {
char *envvar = Py_GETENV("PYTHONUOPS");
char *envvar = Py_GETENV("PYTHON_UOPS");
int enabled = envvar != NULL && *envvar > '0';
if (_Py_get_xoption(&config->xoptions, L"uops") != NULL) {
enabled = 1;

View File

@ -758,6 +758,12 @@ class Generator(Analyzer):
self.write_provenance_header()
self.out.write_raw("\n")
self.out.write_raw("#ifdef TIER_TWO\n")
self.out.write_raw(" #error \"This file is for Tier 1 only\"\n")
self.out.write_raw("#endif\n")
self.out.write_raw("#define TIER_ONE 1\n")
# Write and count instructions of all kinds
n_macros = 0
for thing in self.everything:
@ -773,6 +779,9 @@ class Generator(Analyzer):
case _:
assert_never(thing)
self.out.write_raw("\n")
self.out.write_raw("#undef TIER_ONE\n")
print(
f"Wrote {n_macros} cases to {output_filename}",
file=sys.stderr,
@ -786,6 +795,13 @@ class Generator(Analyzer):
with open(executor_filename, "w") as f:
self.out = Formatter(f, 8, emit_line_directives)
self.write_provenance_header()
self.out.write_raw("\n")
self.out.write_raw("#ifdef TIER_ONE\n")
self.out.write_raw(" #error \"This file is for Tier 2 only\"\n")
self.out.write_raw("#endif\n")
self.out.write_raw("#define TIER_TWO 2\n")
for instr in self.instrs.values():
if instr.is_viable_uop():
n_uops += 1
@ -795,6 +811,10 @@ class Generator(Analyzer):
if instr.check_eval_breaker:
self.out.emit("CHECK_EVAL_BREAKER();")
self.out.emit("break;")
self.out.write_raw("\n")
self.out.write_raw("#undef TIER_TWO\n")
print(
f"Wrote {n_uops} cases to {executor_filename}",
file=sys.stderr,

View File

@ -162,7 +162,7 @@ class Instruction:
f"{func}(&this_instr[{active.offset + 1}].cache);"
)
else:
out.emit(f"{typ}{ceffect.name} = ({typ.strip()})operand;")
out.emit(f"{typ}{ceffect.name} = ({typ.strip()})next_uop[-1].operand;")
# Write the body, substituting a goto for ERROR_IF() and other stuff
assert dedent <= 0
@ -197,6 +197,8 @@ class Instruction:
ninputs, symbolic = list_effect_size(ieffs)
if ninputs:
label = f"pop_{ninputs}_{label}"
if tier == TIER_TWO:
label = label + "_tier_two"
if symbolic:
out.write_raw(
f"{space}if ({cond}) {{ STACK_SHRINK({symbolic}); goto {label}; }}\n"