GH-131729: Code-gen better liveness analysis (GH-131732)

* Rename 'defined' attribute to 'in_local' to more accurately reflect how it is used

* Make death of variables explicit even for array variables.

* Convert in_memory from boolean to stack offset

* Don't apply liveness analysis to optimizer generated code

* Fix RETURN_VALUE in optimizer
This commit is contained in:
Mark Shannon 2025-03-26 15:21:35 +00:00 committed by GitHub
parent b9ca438daa
commit 1b8bb1ed0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 344 additions and 399 deletions

View file

@ -70,13 +70,13 @@
lhs = res;
stack_pointer[-2] = lhs;
PyStackRef_CLOSE(tmp);
tmp = rhs;
rhs = PyStackRef_NULL;
stack_pointer[-1] = rhs;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(rhs);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = res;
}
DISPATCH();
}
@ -708,13 +708,13 @@
list_st = res;
stack_pointer[-2] = list_st;
PyStackRef_CLOSE(tmp);
tmp = sub_st;
sub_st = PyStackRef_NULL;
stack_pointer[-1] = sub_st;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(sub_st);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = res;
DISPATCH();
}
@ -834,7 +834,6 @@
stack_pointer[-1] = tuple_st;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = res;
DISPATCH();
}
@ -1125,11 +1124,11 @@
}
}
if (err) {
stack_pointer += -oparg;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(set_o);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -oparg;
assert(WITHIN_STACK_BOUNDS());
JUMP_TO_LABEL(error);
}
set = PyStackRef_FromPyObjectStealMortal(set_o);
@ -1271,8 +1270,6 @@
_PyStackRef *callable;
_PyStackRef *self_or_null;
_PyStackRef *args;
_PyStackRef *func;
_PyStackRef *maybe_self;
_PyStackRef res;
// _SPECIALIZE_CALL
{
@ -1295,18 +1292,15 @@
/* Skip 2 cache entries */
// _MAYBE_EXPAND_METHOD
{
args = &stack_pointer[-oparg];
func = &stack_pointer[-2 - oparg];
maybe_self = &stack_pointer[-1 - oparg];
args = &stack_pointer[-oparg];
(void)args;
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *self = ((PyMethodObject *)callable_o)->im_self;
maybe_self[0] = PyStackRef_FromPyObjectNew(self);
self_or_null[0] = PyStackRef_FromPyObjectNew(self);
PyObject *method = ((PyMethodObject *)callable_o)->im_func;
_PyStackRef temp = callable[0];
func[0] = PyStackRef_FromPyObjectNew(method);
callable[0] = PyStackRef_FromPyObjectNew(method);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(temp);
stack_pointer = _PyFrame_GetStackPointer(frame);
@ -1315,8 +1309,6 @@
// _DO_CALL
{
args = &stack_pointer[-oparg];
self_or_null = &stack_pointer[-1 - oparg];
callable = &stack_pointer[-2 - oparg];
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
// oparg counts all of the args, but *not* self:
int total_args = oparg;
@ -1458,7 +1450,7 @@
INSTRUCTION_STATS(CALL_ALLOC_AND_ENTER_INIT);
static_assert(INLINE_CACHE_ENTRIES_CALL == 3, "incorrect cache size");
_PyStackRef *callable;
_PyStackRef *null;
_PyStackRef *self_or_null;
_PyStackRef *args;
_PyStackRef *init;
_PyStackRef *self;
@ -1476,15 +1468,12 @@
// _CHECK_AND_ALLOCATE_OBJECT
{
args = &stack_pointer[-oparg];
null = &stack_pointer[-1 - oparg];
self_or_null = &stack_pointer[-1 - oparg];
callable = &stack_pointer[-2 - oparg];
init = &stack_pointer[-2 - oparg];
self = &stack_pointer[-1 - oparg];
args = &stack_pointer[-oparg];
uint32_t type_version = read_u32(&this_instr[2].cache);
(void)args;
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
if (!PyStackRef_IsNull(null[0])) {
if (!PyStackRef_IsNull(self_or_null[0])) {
UPDATE_MISS_STATS(CALL);
assert(_PyOpcode_Deopt[opcode] == (CALL));
JUMP_TO_PREDICTED(CALL);
@ -1518,18 +1507,17 @@
if (self_o == NULL) {
JUMP_TO_LABEL(error);
}
self[0] = PyStackRef_FromPyObjectSteal(self_o);
self_or_null[0] = PyStackRef_FromPyObjectSteal(self_o);
_PyStackRef temp = callable[0];
init[0] = PyStackRef_FromPyObjectNew(init_func);
callable[0] = PyStackRef_FromPyObjectNew(init_func);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(temp);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
// _CREATE_INIT_FRAME
{
args = &stack_pointer[-oparg];
self = &stack_pointer[-1 - oparg];
init = &stack_pointer[-2 - oparg];
self = self_or_null;
init = callable;
_PyFrame_SetStackPointer(frame, stack_pointer);
_PyInterpreterFrame *shim = _PyFrame_PushTrampolineUnchecked(
tstate, (PyCodeObject *)&_Py_InitCleanup, 1, frame);
@ -2547,7 +2535,7 @@
next_instr += 4;
INSTRUCTION_STATS(CALL_ISINSTANCE);
static_assert(INLINE_CACHE_ENTRIES_CALL == 3, "incorrect cache size");
_PyStackRef *callable;
_PyStackRef callable;
_PyStackRef *self_or_null;
_PyStackRef *args;
_PyStackRef res;
@ -2555,9 +2543,9 @@
/* Skip 2 cache entries */
args = &stack_pointer[-oparg];
self_or_null = &stack_pointer[-1 - oparg];
callable = &stack_pointer[-2 - oparg];
callable = stack_pointer[-2 - oparg];
/* isinstance(o, o2) */
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
int total_args = oparg;
_PyStackRef *arguments = args;
if (!PyStackRef_IsNull(self_or_null[0])) {
@ -2587,8 +2575,9 @@
res = retval ? PyStackRef_True : PyStackRef_False;
assert((!PyStackRef_IsNull(res)) ^ (_PyErr_Occurred(tstate) != NULL));
_PyFrame_SetStackPointer(frame, stack_pointer);
_PyStackRef tmp = callable[0];
callable[0] = res;
_PyStackRef tmp = callable;
callable = res;
stack_pointer[-2 - oparg] = callable;
PyStackRef_CLOSE(tmp);
for (int _i = oparg; --_i >= 0;) {
tmp = args[_i];
@ -2599,7 +2588,6 @@
self_or_null[0] = PyStackRef_NULL;
PyStackRef_XCLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-2 - oparg] = res;
stack_pointer += -1 - oparg;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
@ -2622,8 +2610,6 @@
_PyStackRef *args;
_PyStackRef kwnames;
_PyStackRef kwnames_in;
_PyStackRef *func;
_PyStackRef *maybe_self;
_PyStackRef kwnames_out;
_PyStackRef res;
// _SPECIALIZE_CALL_KW
@ -2649,17 +2635,14 @@
{
kwnames_in = stack_pointer[-1];
args = &stack_pointer[-1 - oparg];
func = &stack_pointer[-3 - oparg];
maybe_self = &stack_pointer[-2 - oparg];
args = &stack_pointer[-1 - oparg];
(void)args;
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *self = ((PyMethodObject *)callable_o)->im_self;
maybe_self[0] = PyStackRef_FromPyObjectNew(self);
self_or_null[0] = PyStackRef_FromPyObjectNew(self);
PyObject *method = ((PyMethodObject *)callable_o)->im_func;
_PyStackRef temp = callable[0];
func[0] = PyStackRef_FromPyObjectNew(method);
callable[0] = PyStackRef_FromPyObjectNew(method);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(temp);
stack_pointer = _PyFrame_GetStackPointer(frame);
@ -2670,8 +2653,6 @@
{
kwnames = kwnames_out;
args = &stack_pointer[-1 - oparg];
self_or_null = &stack_pointer[-2 - oparg];
callable = &stack_pointer[-3 - oparg];
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *kwnames_o = PyStackRef_AsPyObjectBorrow(kwnames);
// oparg counts all of the args, but *not* self:
@ -2696,14 +2677,12 @@
arguments, positional_args, kwnames_o, frame
);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
stack_pointer += -3 - oparg;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(kwnames);
stack_pointer = _PyFrame_GetStackPointer(frame);
// Sync stack explicitly since we leave using DISPATCH_INLINED().
stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS());
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
@ -3958,6 +3937,7 @@
// _INIT_CALL_PY_EXACT_ARGS
{
args = &stack_pointer[-oparg];
self_or_null = &stack_pointer[-1 - oparg];
int has_self = !PyStackRef_IsNull(self_or_null[0]);
STAT_INC(CALL, hit);
new_frame = _PyFrame_PushUnchecked(tstate, callable[0], oparg + has_self, frame);
@ -6027,10 +6007,8 @@
stack_pointer[-1] = iterable;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = iter;
}
}
stack_pointer[-1] = iter;
DISPATCH();
}
@ -6121,17 +6099,14 @@
args = &stack_pointer[-oparg];
self_or_null = &stack_pointer[-1 - oparg];
callable = &stack_pointer[-2 - oparg];
func = &stack_pointer[-2 - oparg];
maybe_self = &stack_pointer[-1 - oparg];
args = &stack_pointer[-oparg];
(void)args;
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *self = ((PyMethodObject *)callable_o)->im_self;
maybe_self[0] = PyStackRef_FromPyObjectNew(self);
self_or_null[0] = PyStackRef_FromPyObjectNew(self);
PyObject *method = ((PyMethodObject *)callable_o)->im_func;
_PyStackRef temp = callable[0];
func[0] = PyStackRef_FromPyObjectNew(method);
callable[0] = PyStackRef_FromPyObjectNew(method);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(temp);
stack_pointer = _PyFrame_GetStackPointer(frame);
@ -6139,9 +6114,8 @@
}
// _MONITOR_CALL
{
args = &stack_pointer[-oparg];
maybe_self = &stack_pointer[-1 - oparg];
func = &stack_pointer[-2 - oparg];
maybe_self = self_or_null;
func = callable;
int is_meth = !PyStackRef_IsNull(maybe_self[0]);
PyObject *function = PyStackRef_AsPyObjectBorrow(func[0]);
PyObject *arg0;
@ -6506,8 +6480,6 @@
_PyStackRef *self_or_null;
_PyStackRef *args;
_PyStackRef kwnames_in;
_PyStackRef *func;
_PyStackRef *maybe_self;
_PyStackRef kwnames_out;
_PyStackRef kwnames;
_PyStackRef res;
@ -6519,17 +6491,14 @@
args = &stack_pointer[-1 - oparg];
self_or_null = &stack_pointer[-2 - oparg];
callable = &stack_pointer[-3 - oparg];
func = &stack_pointer[-3 - oparg];
maybe_self = &stack_pointer[-2 - oparg];
args = &stack_pointer[-1 - oparg];
(void)args;
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *self = ((PyMethodObject *)callable_o)->im_self;
maybe_self[0] = PyStackRef_FromPyObjectNew(self);
self_or_null[0] = PyStackRef_FromPyObjectNew(self);
PyObject *method = ((PyMethodObject *)callable_o)->im_func;
_PyStackRef temp = callable[0];
func[0] = PyStackRef_FromPyObjectNew(method);
callable[0] = PyStackRef_FromPyObjectNew(method);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(temp);
stack_pointer = _PyFrame_GetStackPointer(frame);
@ -6538,9 +6507,6 @@
}
// _MONITOR_CALL_KW
{
args = &stack_pointer[-1 - oparg];
self_or_null = &stack_pointer[-2 - oparg];
callable = &stack_pointer[-3 - oparg];
int is_meth = !PyStackRef_IsNull(self_or_null[0]);
PyObject *arg;
if (is_meth) {
@ -6591,14 +6557,12 @@
arguments, positional_args, kwnames_o, frame
);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
stack_pointer += -3 - oparg;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(kwnames);
stack_pointer = _PyFrame_GetStackPointer(frame);
// Sync stack explicitly since we leave using DISPATCH_INLINED().
stack_pointer += -2 - oparg;
assert(WITHIN_STACK_BOUNDS());
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
if (new_frame == NULL) {
@ -7873,7 +7837,6 @@
stack_pointer[-1] = owner;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = attr;
}
// _PUSH_NULL_CONDITIONAL
{
@ -7942,7 +7905,6 @@
stack_pointer[-1] = owner;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = attr;
}
// _PUSH_NULL_CONDITIONAL
{
@ -8603,7 +8565,6 @@
stack_pointer[-1] = owner;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = attr;
}
/* Skip 5 cache entries */
// _PUSH_NULL_CONDITIONAL
@ -9655,13 +9616,12 @@
global_super_st = self_or_null;
stack_pointer[-2] = global_super_st;
PyStackRef_CLOSE(tmp);
tmp = class_st;
class_st = PyStackRef_NULL;
stack_pointer[-1] = class_st;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(class_st);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
attr = PyStackRef_FromPyObjectSteal(attr_o);
stack_pointer[0] = attr;
@ -9992,7 +9952,6 @@
stack_pointer[-1] = value;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = b;
}
}
// _POP_JUMP_IF_TRUE
@ -10036,7 +9995,6 @@
stack_pointer[-1] = value;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = b;
}
}
// _POP_JUMP_IF_FALSE
@ -11611,7 +11569,6 @@
stack_pointer[-1] = value;
PyStackRef_CLOSE(tmp);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = res;
DISPATCH();
}
@ -11880,13 +11837,11 @@
*values++ = PyStackRef_FromPyObjectNew(items[i]);
}
UNLOCK_OBJECT(seq_o);
stack_pointer += -1;
stack_pointer += -1 + oparg;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(seq);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += oparg;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
@ -11922,13 +11877,11 @@
for (int i = oparg; --i >= 0; ) {
*values++ = PyStackRef_FromPyObjectNew(items[i]);
}
stack_pointer += -1;
stack_pointer += -1 + oparg;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(seq);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += oparg;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
@ -11970,6 +11923,7 @@
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(seq);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer[-1] = val0;
DISPATCH();
}