GH-90690: Remove PRECALL instruction (GH-92925)

This commit is contained in:
Mark Shannon 2022-05-19 11:05:26 +01:00 committed by GitHub
parent 41638967a0
commit e48ac9c100
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 547 additions and 704 deletions

View file

@ -1439,10 +1439,6 @@ eval_frame_handle_pending(PyThreadState *tstate)
#define JUMPTO(x) (next_instr = first_instr + (x))
#define JUMPBY(x) (next_instr += (x))
// Skip from a PRECALL over a CALL to the next instruction:
#define SKIP_CALL() \
JUMPBY(INLINE_CACHE_ENTRIES_PRECALL + 1 + INLINE_CACHE_ENTRIES_CALL)
/* Get opcode and oparg from original instructions, not quickened form. */
#define TRACING_NEXTOPARG() do { \
NEXTOPARG(); \
@ -1687,7 +1683,7 @@ pop_frame(PyThreadState *tstate, _PyInterpreterFrame *frame)
return prev_frame;
}
/* It is only between the PRECALL instruction and the following CALL,
/* It is only between the KW_NAMES instruction and the following CALL,
* that this has any meaning.
*/
typedef struct {
@ -4541,7 +4537,7 @@ handle_eval_breaker:
TARGET(LOAD_METHOD) {
PREDICTED(LOAD_METHOD);
/* Designed to work in tandem with PRECALL. */
/* Designed to work in tandem with CALL. */
PyObject *name = GETITEM(names, oparg);
PyObject *obj = TOP();
PyObject *meth = NULL;
@ -4566,7 +4562,7 @@ handle_eval_breaker:
/* meth is not an unbound method (but a regular attr, or
something was returned by a descriptor protocol). Set
the second element of the stack to NULL, to signal
PRECALL that it's not a method call.
CALL that it's not a method call.
NULL | meth | arg1 | ... | argN
*/
@ -4710,57 +4706,11 @@ handle_eval_breaker:
NOTRACE_DISPATCH();
}
TARGET(PRECALL) {
PREDICTED(PRECALL);
/* Designed to work in tamdem with LOAD_METHOD. */
/* `meth` is NULL when LOAD_METHOD thinks that it's not
a method call.
Stack layout:
... | NULL | callable | arg1 | ... | argN
^- TOP()
^- (-oparg)
^- (-oparg-1)
^- (-oparg-2)
`callable` will be POPed by call_function.
NULL will will be POPed manually later.
If `meth` isn't NULL, it's a method call. Stack layout:
... | method | self | arg1 | ... | argN
^- TOP()
^- (-oparg)
^- (-oparg-1)
^- (-oparg-2)
`self` and `method` will be POPed by call_function.
We'll be passing `oparg + 1` to call_function, to
make it accept the `self` as a first argument.
*/
int is_meth = is_method(stack_pointer, oparg);
int nargs = oparg + is_meth;
/* Move ownership of reference from stack to call_shape
* and make sure that NULL is cleared from stack */
PyObject *function = PEEK(nargs + 1);
if (!is_meth && Py_TYPE(function) == &PyMethod_Type) {
PyObject *meth = ((PyMethodObject *)function)->im_func;
PyObject *self = ((PyMethodObject *)function)->im_self;
Py_INCREF(meth);
Py_INCREF(self);
PEEK(oparg+1) = self;
PEEK(oparg+2) = meth;
Py_DECREF(function);
}
JUMPBY(INLINE_CACHE_ENTRIES_PRECALL);
DISPATCH();
}
TARGET(PRECALL_BOUND_METHOD) {
DEOPT_IF(is_method(stack_pointer, oparg), PRECALL);
TARGET(CALL_BOUND_METHOD_EXACT_ARGS) {
DEOPT_IF(is_method(stack_pointer, oparg), CALL);
PyObject *function = PEEK(oparg + 1);
DEOPT_IF(Py_TYPE(function) != &PyMethod_Type, PRECALL);
STAT_INC(PRECALL, hit);
DEOPT_IF(Py_TYPE(function) != &PyMethod_Type, CALL);
STAT_INC(CALL, hit);
PyObject *meth = ((PyMethodObject *)function)->im_func;
PyObject *self = ((PyMethodObject *)function)->im_self;
Py_INCREF(meth);
@ -4768,17 +4718,7 @@ handle_eval_breaker:
PEEK(oparg + 1) = self;
PEEK(oparg + 2) = meth;
Py_DECREF(function);
JUMPBY(INLINE_CACHE_ENTRIES_PRECALL);
DISPATCH();
}
TARGET(PRECALL_PYFUNC) {
int nargs = oparg + is_method(stack_pointer, oparg);
PyObject *function = PEEK(nargs + 1);
DEOPT_IF(Py_TYPE(function) != &PyFunction_Type, PRECALL);
STAT_INC(PRECALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_PRECALL);
DISPATCH();
goto call_exact_args;
}
TARGET(KW_NAMES) {
@ -4789,11 +4729,22 @@ handle_eval_breaker:
}
TARGET(CALL) {
int is_meth;
int total_args, is_meth;
call_function:
is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyObject *function = PEEK(total_args + 1);
PyObject *function = PEEK(oparg + 1);
if (!is_meth && Py_TYPE(function) == &PyMethod_Type) {
PyObject *meth = ((PyMethodObject *)function)->im_func;
PyObject *self = ((PyMethodObject *)function)->im_self;
Py_INCREF(meth);
Py_INCREF(self);
PEEK(oparg+1) = self;
PEEK(oparg+2) = meth;
Py_DECREF(function);
is_meth = 1;
}
total_args = oparg + is_meth;
function = PEEK(total_args + 1);
int positional_args = total_args - KWNAMES_LEN();
// Check if the call can be inlined or not
if (Py_TYPE(function) == &PyFunction_Type && tstate->interp->eval_frame == NULL) {
@ -4850,27 +4801,6 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_ADAPTIVE) {
_PyPrecallCache *cache = (_PyPrecallCache *)next_instr;
if (cache->counter == 0) {
next_instr--;
int is_meth = is_method(stack_pointer, oparg);
int nargs = oparg + is_meth;
PyObject *callable = PEEK(nargs + 1);
int err = _Py_Specialize_Precall(callable, next_instr, nargs,
call_shape.kwnames, oparg);
if (err < 0) {
goto error;
}
NOTRACE_DISPATCH_SAME_OPARG();
}
else {
STAT_INC(PRECALL, deferred);
cache->counter--;
JUMP_TO_INSTRUCTION(PRECALL);
}
}
TARGET(CALL_ADAPTIVE) {
_PyCallCache *cache = (_PyCallCache *)next_instr;
if (cache->counter == 0) {
@ -4893,6 +4823,7 @@ handle_eval_breaker:
}
TARGET(CALL_PY_EXACT_ARGS) {
call_exact_args:
assert(call_shape.kwnames == NULL);
DEOPT_IF(tstate->interp->eval_frame, CALL);
_PyCallCache *cache = (_PyCallCache *)next_instr;
@ -4968,16 +4899,16 @@ handle_eval_breaker:
goto start_frame;
}
TARGET(PRECALL_NO_KW_TYPE_1) {
TARGET(CALL_NO_KW_TYPE_1) {
assert(call_shape.kwnames == NULL);
assert(cframe.use_tracing == 0);
assert(oparg == 1);
DEOPT_IF(is_method(stack_pointer, 1), PRECALL);
DEOPT_IF(is_method(stack_pointer, 1), CALL);
PyObject *obj = TOP();
PyObject *callable = SECOND();
DEOPT_IF(callable != (PyObject *)&PyType_Type, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(callable != (PyObject *)&PyType_Type, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyObject *res = Py_NewRef(Py_TYPE(obj));
Py_DECREF(callable);
Py_DECREF(obj);
@ -4986,15 +4917,15 @@ handle_eval_breaker:
NOTRACE_DISPATCH();
}
TARGET(PRECALL_NO_KW_STR_1) {
TARGET(CALL_NO_KW_STR_1) {
assert(call_shape.kwnames == NULL);
assert(cframe.use_tracing == 0);
assert(oparg == 1);
DEOPT_IF(is_method(stack_pointer, 1), PRECALL);
DEOPT_IF(is_method(stack_pointer, 1), CALL);
PyObject *callable = PEEK(2);
DEOPT_IF(callable != (PyObject *)&PyUnicode_Type, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(callable != (PyObject *)&PyUnicode_Type, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyObject *arg = TOP();
PyObject *res = PyObject_Str(arg);
Py_DECREF(arg);
@ -5008,14 +4939,14 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_TUPLE_1) {
TARGET(CALL_NO_KW_TUPLE_1) {
assert(call_shape.kwnames == NULL);
assert(oparg == 1);
DEOPT_IF(is_method(stack_pointer, 1), PRECALL);
DEOPT_IF(is_method(stack_pointer, 1), CALL);
PyObject *callable = PEEK(2);
DEOPT_IF(callable != (PyObject *)&PyTuple_Type, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(callable != (PyObject *)&PyTuple_Type, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyObject *arg = TOP();
PyObject *res = PySequence_Tuple(arg);
Py_DECREF(arg);
@ -5029,16 +4960,16 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_BUILTIN_CLASS) {
TARGET(CALL_BUILTIN_CLASS) {
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
int kwnames_len = KWNAMES_LEN();
PyObject *callable = PEEK(total_args + 1);
DEOPT_IF(!PyType_Check(callable), PRECALL);
DEOPT_IF(!PyType_Check(callable), CALL);
PyTypeObject *tp = (PyTypeObject *)callable;
DEOPT_IF(tp->tp_vectorcall == NULL, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(tp->tp_vectorcall == NULL, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
STACK_SHRINK(total_args);
PyObject *res = tp->tp_vectorcall((PyObject *)tp, stack_pointer,
total_args-kwnames_len, call_shape.kwnames);
@ -5057,18 +4988,18 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_BUILTIN_O) {
TARGET(CALL_NO_KW_BUILTIN_O) {
assert(cframe.use_tracing == 0);
/* Builtin METH_O functions */
assert(call_shape.kwnames == NULL);
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
DEOPT_IF(total_args != 1, PRECALL);
DEOPT_IF(total_args != 1, CALL);
PyObject *callable = PEEK(total_args + 1);
DEOPT_IF(!PyCFunction_CheckExact(callable), PRECALL);
DEOPT_IF(PyCFunction_GET_FLAGS(callable) != METH_O, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(!PyCFunction_CheckExact(callable), CALL);
DEOPT_IF(PyCFunction_GET_FLAGS(callable) != METH_O, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable);
// This is slower but CPython promises to check all non-vectorcall
// function calls.
@ -5091,18 +5022,18 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_BUILTIN_FAST) {
TARGET(CALL_NO_KW_BUILTIN_FAST) {
assert(cframe.use_tracing == 0);
/* Builtin METH_FASTCALL functions, without keywords */
assert(call_shape.kwnames == NULL);
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyObject *callable = PEEK(total_args + 1);
DEOPT_IF(!PyCFunction_CheckExact(callable), PRECALL);
DEOPT_IF(!PyCFunction_CheckExact(callable), CALL);
DEOPT_IF(PyCFunction_GET_FLAGS(callable) != METH_FASTCALL,
PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable);
STACK_SHRINK(total_args);
/* res = func(self, args, nargs) */
@ -5131,17 +5062,17 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_BUILTIN_FAST_WITH_KEYWORDS) {
TARGET(CALL_BUILTIN_FAST_WITH_KEYWORDS) {
assert(cframe.use_tracing == 0);
/* Builtin METH_FASTCALL | METH_KEYWORDS functions */
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyObject *callable = PEEK(total_args + 1);
DEOPT_IF(!PyCFunction_CheckExact(callable), PRECALL);
DEOPT_IF(!PyCFunction_CheckExact(callable), CALL);
DEOPT_IF(PyCFunction_GET_FLAGS(callable) !=
(METH_FASTCALL | METH_KEYWORDS), PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
(METH_FASTCALL | METH_KEYWORDS), CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
STACK_SHRINK(total_args);
/* res = func(self, args, nargs, kwnames) */
_PyCFunctionFastWithKeywords cfunc =
@ -5170,18 +5101,18 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_LEN) {
TARGET(CALL_NO_KW_LEN) {
assert(cframe.use_tracing == 0);
assert(call_shape.kwnames == NULL);
/* len(o) */
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
DEOPT_IF(total_args != 1, PRECALL);
DEOPT_IF(total_args != 1, CALL);
PyObject *callable = PEEK(total_args + 1);
PyInterpreterState *interp = _PyInterpreterState_GET();
DEOPT_IF(callable != interp->callable_cache.len, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(callable != interp->callable_cache.len, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyObject *arg = TOP();
Py_ssize_t len_i = PyObject_Length(arg);
if (len_i < 0) {
@ -5200,18 +5131,18 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_ISINSTANCE) {
TARGET(CALL_NO_KW_ISINSTANCE) {
assert(cframe.use_tracing == 0);
assert(call_shape.kwnames == NULL);
/* isinstance(o, o2) */
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyObject *callable = PEEK(total_args + 1);
DEOPT_IF(total_args != 2, PRECALL);
DEOPT_IF(total_args != 2, CALL);
PyInterpreterState *interp = _PyInterpreterState_GET();
DEOPT_IF(callable != interp->callable_cache.isinstance, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(callable != interp->callable_cache.isinstance, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyObject *cls = POP();
PyObject *inst = TOP();
int retval = PyObject_IsInstance(inst, cls);
@ -5233,18 +5164,18 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_LIST_APPEND) {
TARGET(CALL_NO_KW_LIST_APPEND) {
assert(cframe.use_tracing == 0);
assert(call_shape.kwnames == NULL);
assert(oparg == 1);
PyObject *callable = PEEK(3);
PyInterpreterState *interp = _PyInterpreterState_GET();
DEOPT_IF(callable != interp->callable_cache.list_append, PRECALL);
DEOPT_IF(callable != interp->callable_cache.list_append, CALL);
PyObject *list = SECOND();
DEOPT_IF(!PyList_Check(list), PRECALL);
STAT_INC(PRECALL, hit);
// PRECALL + CALL + POP_TOP
JUMPBY(INLINE_CACHE_ENTRIES_PRECALL + 1 + INLINE_CACHE_ENTRIES_CALL + 1);
DEOPT_IF(!PyList_Check(list), CALL);
STAT_INC(CALL, hit);
// CALL + POP_TOP
JUMPBY(INLINE_CACHE_ENTRIES_CALL + 1);
assert(_Py_OPCODE(next_instr[-1]) == POP_TOP);
PyObject *arg = POP();
if (_PyList_AppendTakeRef((PyListObject *)list, arg) < 0) {
@ -5256,21 +5187,21 @@ handle_eval_breaker:
NOTRACE_DISPATCH();
}
TARGET(PRECALL_NO_KW_METHOD_DESCRIPTOR_O) {
TARGET(CALL_NO_KW_METHOD_DESCRIPTOR_O) {
assert(call_shape.kwnames == NULL);
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyMethodDescrObject *callable =
(PyMethodDescrObject *)PEEK(total_args + 1);
DEOPT_IF(total_args != 2, PRECALL);
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), PRECALL);
DEOPT_IF(total_args != 2, CALL);
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
PyMethodDef *meth = callable->d_method;
DEOPT_IF(meth->ml_flags != METH_O, PRECALL);
DEOPT_IF(meth->ml_flags != METH_O, CALL);
PyObject *arg = TOP();
PyObject *self = SECOND();
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyCFunction cfunc = meth->ml_meth;
// This is slower but CPython promises to check all non-vectorcall
// function calls.
@ -5292,19 +5223,19 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS) {
TARGET(CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS) {
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyMethodDescrObject *callable =
(PyMethodDescrObject *)PEEK(total_args + 1);
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), PRECALL);
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
PyMethodDef *meth = callable->d_method;
DEOPT_IF(meth->ml_flags != (METH_FASTCALL|METH_KEYWORDS), PRECALL);
DEOPT_IF(meth->ml_flags != (METH_FASTCALL|METH_KEYWORDS), CALL);
PyTypeObject *d_type = callable->d_common.d_type;
PyObject *self = PEEK(total_args);
DEOPT_IF(!Py_IS_TYPE(self, d_type), PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(!Py_IS_TYPE(self, d_type), CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
int nargs = total_args-1;
STACK_SHRINK(nargs);
_PyCFunctionFastWithKeywords cfunc =
@ -5329,20 +5260,20 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS) {
TARGET(CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS) {
assert(call_shape.kwnames == NULL);
assert(oparg == 0 || oparg == 1);
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
DEOPT_IF(total_args != 1, PRECALL);
DEOPT_IF(total_args != 1, CALL);
PyMethodDescrObject *callable = (PyMethodDescrObject *)SECOND();
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), PRECALL);
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
PyMethodDef *meth = callable->d_method;
PyObject *self = TOP();
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), PRECALL);
DEOPT_IF(meth->ml_flags != METH_NOARGS, PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), CALL);
DEOPT_IF(meth->ml_flags != METH_NOARGS, CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
PyCFunction cfunc = meth->ml_meth;
// This is slower but CPython promises to check all non-vectorcall
// function calls.
@ -5363,20 +5294,20 @@ handle_eval_breaker:
DISPATCH();
}
TARGET(PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST) {
TARGET(CALL_NO_KW_METHOD_DESCRIPTOR_FAST) {
assert(call_shape.kwnames == NULL);
int is_meth = is_method(stack_pointer, oparg);
int total_args = oparg + is_meth;
PyMethodDescrObject *callable =
(PyMethodDescrObject *)PEEK(total_args + 1);
/* Builtin METH_FASTCALL methods, without keywords */
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), PRECALL);
DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), CALL);
PyMethodDef *meth = callable->d_method;
DEOPT_IF(meth->ml_flags != METH_FASTCALL, PRECALL);
DEOPT_IF(meth->ml_flags != METH_FASTCALL, CALL);
PyObject *self = PEEK(total_args);
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), PRECALL);
STAT_INC(PRECALL, hit);
SKIP_CALL();
DEOPT_IF(!Py_IS_TYPE(self, callable->d_common.d_type), CALL);
STAT_INC(CALL, hit);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
_PyCFunctionFast cfunc =
(_PyCFunctionFast)(void(*)(void))meth->ml_meth;
int nargs = total_args-1;