GH-116422: Tier2 hot/cold splitting (GH-116813)

Splits the "cold" path, deopts and exits, from the "hot" path, reducing the size of most jitted instructions, at the cost of slower exits.
This commit is contained in:
Mark Shannon 2024-03-26 09:35:11 +00:00 committed by GitHub
parent 61599a48f5
commit bf82f77957
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 1662 additions and 1003 deletions

View file

@ -179,7 +179,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(error);
ERROR_NO_POP();
}
next_instr = this_instr;
}
@ -206,7 +206,13 @@ dummy_func(
inst(LOAD_FAST_CHECK, (-- value)) {
value = GETLOCAL(oparg);
ERROR_IF(value == NULL, unbound_local_error);
if (value == NULL) {
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
);
ERROR_IF(1, error);
}
Py_INCREF(value);
}
@ -275,7 +281,7 @@ dummy_func(
if (PyGen_Check(receiver)) {
PyErr_SetObject(PyExc_StopIteration, value);
if (monitor_stop_iteration(tstate, frame, this_instr)) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
PyErr_SetRaisedException(NULL);
}
@ -290,7 +296,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(error);
ERROR_NO_POP();
}
PyErr_SetRaisedException(NULL);
}
@ -826,7 +832,7 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, retval);
if (err) GOTO_ERROR(error);
if (err) ERROR_NO_POP();
STACK_SHRINK(1);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -850,7 +856,7 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, retval);
if (err) GOTO_ERROR(error);
if (err) ERROR_NO_POP();
Py_INCREF(retval);
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -906,7 +912,7 @@ dummy_func(
if (PyAsyncGen_CheckExact(aiter)) {
awaitable = type->tp_as_async->am_anext(aiter);
if (awaitable == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
} else {
if (type->tp_as_async != NULL){
@ -916,7 +922,7 @@ dummy_func(
if (getter != NULL) {
next_iter = (*getter)(aiter);
if (next_iter == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
else {
@ -924,7 +930,7 @@ dummy_func(
"'async for' requires an iterator with "
"__anext__ method, got %.100s",
type->tp_name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
awaitable = _PyCoro_GetAwaitableIter(next_iter);
@ -936,7 +942,7 @@ dummy_func(
Py_TYPE(next_iter)->tp_name);
Py_DECREF(next_iter);
GOTO_ERROR(error);
ERROR_NO_POP();
} else {
Py_DECREF(next_iter);
}
@ -1018,7 +1024,7 @@ dummy_func(
JUMPBY(oparg);
}
else {
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
Py_DECREF(v);
@ -1054,7 +1060,7 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_YIELD,
frame, this_instr, retval);
if (err) GOTO_ERROR(error);
if (err) ERROR_NO_POP();
tstate->exc_info = gen->gi_exc_state.previous_item;
gen->gi_exc_state.previous_item = NULL;
_Py_LeaveRecursiveCallPy(tstate);
@ -1108,7 +1114,7 @@ dummy_func(
else {
assert(PyLong_Check(lasti));
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
assert(exc && PyExceptionInstance_Check(exc));
@ -1184,7 +1190,7 @@ dummy_func(
if (ns == NULL) {
_PyErr_Format(tstate, PyExc_SystemError,
"no locals when deleting %R", name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
err = PyObject_DelItem(ns, name);
// Can't use ERROR_IF here.
@ -1192,7 +1198,7 @@ dummy_func(
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
NAME_ERROR_MSG,
name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
@ -1312,12 +1318,12 @@ dummy_func(
int err = PyDict_Pop(GLOBALS(), name, NULL);
// Can't use ERROR_IF here.
if (err < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (err == 0) {
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
@ -1334,21 +1340,21 @@ 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(error);
ERROR_NO_POP();
}
if (v == NULL) {
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (v == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
}
@ -1364,21 +1370,21 @@ dummy_func(
}
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (v == NULL) {
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (v == NULL) {
_PyEval_FormatExcCheckArg(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
}
@ -1494,7 +1500,13 @@ dummy_func(
inst(DELETE_FAST, (--)) {
PyObject *v = GETLOCAL(oparg);
ERROR_IF(v == NULL, unbound_local_error);
if (v == NULL) {
_PyEval_FormatExcCheckArg(tstate, PyExc_UnboundLocalError,
UNBOUNDLOCAL_ERROR_MSG,
PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg)
);
ERROR_IF(1, error);
}
SETLOCAL(oparg, NULL);
}
@ -1504,7 +1516,7 @@ dummy_func(
PyObject *initial = GETLOCAL(oparg);
PyObject *cell = PyCell_New(initial);
if (cell == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
SETLOCAL(oparg, cell);
}
@ -1516,7 +1528,7 @@ dummy_func(
// Fortunately we don't need its superpower.
if (oldobj == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
GOTO_ERROR(error);
ERROR_NO_POP();
}
PyCell_SET(cell, NULL);
Py_DECREF(oldobj);
@ -1528,14 +1540,14 @@ dummy_func(
assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
if (!value) {
PyObject *cell = GETLOCAL(oparg);
value = PyCell_GET(cell);
if (value == NULL) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
GOTO_ERROR(error);
ERROR_NO_POP();
}
Py_INCREF(value);
}
@ -1615,7 +1627,7 @@ dummy_func(
inst(BUILD_SET, (values[oparg] -- set)) {
set = PySet_New(NULL);
if (set == NULL)
GOTO_ERROR(error);
ERROR_NO_POP();
int err = 0;
for (int i = 0; i < oparg; i++) {
PyObject *item = values[i];
@ -1662,12 +1674,8 @@ dummy_func(
}
inst(BUILD_CONST_KEY_MAP, (values[oparg], keys -- map)) {
if (!PyTuple_CheckExact(keys) ||
PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
_PyErr_SetString(tstate, PyExc_SystemError,
"bad BUILD_CONST_KEY_MAP keys argument");
GOTO_ERROR(error); // Pop the keys and values.
}
assert(PyTuple_CheckExact(keys));
assert(PyTuple_GET_SIZE(keys) == (Py_ssize_t)oparg);
map = _PyDict_FromItems(
&PyTuple_GET_ITEM(keys, 0), 1,
values, 1, oparg);
@ -2502,7 +2510,7 @@ dummy_func(
_PyErr_SetString(tstate, PyExc_TypeError,
"cannot 'yield from' a coroutine object "
"in a non-coroutine generator");
GOTO_ERROR(error);
ERROR_NO_POP();
}
iter = iterable;
}
@ -2513,7 +2521,7 @@ dummy_func(
/* `iterable` is not a generator. */
iter = PyObject_GetIter(iterable);
if (iter == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
DECREF_INPUTS();
}
@ -2550,7 +2558,7 @@ dummy_func(
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
monitor_raise(tstate, frame, this_instr);
_PyErr_Clear(tstate);
@ -2573,7 +2581,7 @@ dummy_func(
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
_PyErr_Clear(tstate);
}
@ -2599,7 +2607,7 @@ dummy_func(
else {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
monitor_raise(tstate, frame, this_instr);
_PyErr_Clear(tstate);
@ -2779,7 +2787,7 @@ dummy_func(
"asynchronous context manager protocol",
Py_TYPE(mgr)->tp_name);
}
GOTO_ERROR(error);
ERROR_NO_POP();
}
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__aexit__));
if (exit == NULL) {
@ -2791,7 +2799,7 @@ dummy_func(
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
GOTO_ERROR(error);
ERROR_NO_POP();
}
DECREF_INPUTS();
res = PyObject_CallNoArgs(enter);
@ -2814,7 +2822,7 @@ dummy_func(
"context manager protocol",
Py_TYPE(mgr)->tp_name);
}
GOTO_ERROR(error);
ERROR_NO_POP();
}
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__exit__));
if (exit == NULL) {
@ -2826,7 +2834,7 @@ dummy_func(
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
GOTO_ERROR(error);
ERROR_NO_POP();
}
DECREF_INPUTS();
res = PyObject_CallNoArgs(enter);
@ -3075,7 +3083,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(error);
ERROR_NO_POP();
}
frame->return_offset = (uint16_t)(next_instr - this_instr);
DISPATCH_INLINED(new_frame);
@ -3298,7 +3306,7 @@ dummy_func(
STAT_INC(CALL, hit);
PyObject *self = _PyType_NewManagedObject(tp);
if (self == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
Py_DECREF(tp);
_PyInterpreterFrame *shim = _PyFrame_PushTrampolineUnchecked(
@ -3335,7 +3343,7 @@ dummy_func(
PyErr_Format(PyExc_TypeError,
"__init__() should return None, not '%.200s'",
Py_TYPE(should_be_none)->tp_name);
GOTO_ERROR(error);
ERROR_NO_POP();
}
}
@ -3472,7 +3480,7 @@ dummy_func(
PyObject *arg = args[0];
Py_ssize_t len_i = PyObject_Length(arg);
if (len_i < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
res = PyLong_FromSsize_t(len_i);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
@ -3498,7 +3506,7 @@ dummy_func(
PyObject *inst = args[0];
int retval = PyObject_IsInstance(inst, cls);
if (retval < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
res = PyBool_FromLong(retval);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
@ -3712,7 +3720,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(error);
ERROR_NO_POP();
}
assert(next_instr - this_instr == 1);
frame->return_offset = 1;
@ -3760,11 +3768,11 @@ dummy_func(
assert(kwargs == NULL || PyDict_CheckExact(kwargs));
if (!PyTuple_CheckExact(callargs)) {
if (check_args_iterable(tstate, func, callargs) < 0) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
PyObject *tuple = PySequence_Tuple(callargs);
if (tuple == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
Py_SETREF(callargs, tuple);
}
@ -3776,7 +3784,7 @@ dummy_func(
int err = _Py_call_instrumentation_2args(
tstate, PY_MONITORING_EVENT_CALL,
frame, this_instr, func, arg);
if (err) GOTO_ERROR(error);
if (err) ERROR_NO_POP();
result = PyObject_Call(func, callargs, kwargs);
if (!PyFunction_Check(func) && !PyMethod_Check(func)) {
@ -3810,7 +3818,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(error);
ERROR_NO_POP();
}
assert(next_instr - this_instr == 1);
frame->return_offset = 1;
@ -3831,7 +3839,7 @@ dummy_func(
Py_DECREF(codeobj);
if (func_obj == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
_PyFunction_SetVersion(
@ -3871,7 +3879,7 @@ dummy_func(
PyFunctionObject *func = (PyFunctionObject *)frame->f_funcobj;
PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func);
if (gen == NULL) {
GOTO_ERROR(error);
ERROR_NO_POP();
}
assert(EMPTY());
_PyFrame_SetStackPointer(frame, stack_pointer);
@ -4169,7 +4177,7 @@ dummy_func(
if (optimized < 0) {
Py_DECREF(previous);
tstate->previous_executor = Py_None;
ERROR_IF(1, error);
GOTO_UNWIND();
}
GOTO_TIER_ONE(target);
}
@ -4199,6 +4207,19 @@ dummy_func(
frame->instr_ptr = (_Py_CODEUNIT *)instr_ptr;
}
tier2 op(_DEOPT, (--)) {
EXIT_TO_TIER1();
}
tier2 op(_SIDE_EXIT, (--)) {
EXIT_TO_TRACE();
}
tier2 op(_ERROR_POP_N, (unused[oparg] --)) {
SYNC_SP();
GOTO_UNWIND();
}
// END BYTECODES //
}