8000 GH-131729: Code-gen better liveness analysis by markshannon · Pull Request #131732 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-131729: Code-gen better liveness analysis #131732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ def test_overlap(self):
def test_predictions(self):
input = """
inst(OP1, (arg -- res)) {
DEAD(arg);
res = Py_None;
}
inst(OP3, (arg -- res)) {
DEAD(arg);
DEOPT_IF(xxx);
re 10000 s = Py_None;
}
Expand All @@ -303,7 +305,9 @@ def test_predictions(self):
next_instr += 1;
INSTRUCTION_STATS(OP1);
PREDICTED_OP1:;
_PyStackRef arg;
_PyStackRef res;
arg = stack_pointer[-1];
res = Py_None;
stack_pointer[-1] = res;
DISPATCH();
Expand All @@ -320,7 +324,9 @@ def test_predictions(self):
next_instr += 1;
INSTRUCTION_STATS(OP3);
static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
_PyStackRef arg;
_PyStackRef res;
arg = stack_pointer[-1];
if (xxx) {
UPDATE_MISS_STATS(OP1);
assert(_PyOpcode_Deopt[opcode] == (OP1));
Expand All @@ -336,11 +342,13 @@ def test_predictions(self):
def test_sync_sp(self):
input = """
inst(A, (arg -- res)) {
DEAD(arg);
SYNC_SP();
escaping_call();
res = Py_None;
}
inst(B, (arg -- res)) {
DEAD(arg);
res = Py_None;
SYNC_SP();
escaping_call();
Expand All @@ -355,7 +363,9 @@ def test_sync_sp(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(A);
_PyStackRef arg;
_PyStackRef res;
arg = stack_pointer[-1];
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
_PyFrame_SetStackPointer(frame, stack_pointer);
Expand All @@ -376,7 +386,9 @@ def test_sync_sp(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(B);
_PyStackRef arg;
_PyStackRef res;
arg = stack_pointer[-1];
res = Py_None;
stack_pointer[-1] = res;
_PyFrame_SetStackPointer(frame, stack_pointer);
Expand Down Expand Up @@ -522,6 +534,7 @@ def test_error_if_pop_with_result(self):
def test_cache_effect(self):
input = """
inst(OP, (counter/1, extra/2, value --)) {
DEAD(value);
}
"""
output = """
Expand All @@ -535,6 +548,8 @@ def test_cache_effect(self):
frame->instr_ptr = next_instr;
next_instr += 4;
INSTRUCTION_STATS(OP);
_PyStackRef value;
value = stack_pointer[-1];
uint16_t counter = read_u16(&this_instr[1].cache);
(void)counter;
uint32_t extra = read_u32(&this_instr[2].cache);
Expand Down Expand Up @@ -793,6 +808,9 @@ def test_array_input(self):
input = """
inst(OP, (below, values[oparg*2], above --)) {
SPAM(values, oparg);
DEAD(below);
DEAD(values);
DEAD(above);
}
"""
output = """
Expand All @@ -804,8 +822,12 @@ def test_array_input(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP);
_PyStackRef below;
_PyStackRef *values;
_PyStackRef above;
above = stack_pointer[-1];
values = &stack_pointer[-1 - oparg*2];
below = stack_pointer[-2 - oparg*2];
SPAM(values, oparg);
stack_pointer += -2 - oparg*2;
assert(WITHIN_STACK_BOUNDS());
Expand Down Expand Up @@ -879,6 +901,8 @@ def test_array_input_output(self):
def test_array_error_if(self):
input = """
inst(OP, (extra, values[oparg] --)) {
DEAD(extra);
DEAD(values);
ERROR_IF(oparg == 0, somewhere);
}
"""
Expand All @@ -891,6 +915,10 @@ def test_array_error_if(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP);
_PyStackRef extra;
_PyStackRef *values;
values = &stack_pointer[-oparg];
extra = stack_pointer[-1 - oparg];
if (oparg == 0) {
stack_pointer += -1 - oparg;
assert(WITHIN_STACK_BOUNDS());
Expand Down Expand Up @@ -1056,6 +1084,7 @@ def test_array_of_one(self):
input = """
inst(OP, (arg[1] -- out[1])) {
out[0] = arg[0];
DEAD(arg);
}
"""
output = """
Expand Down
39 changes: 25 additions & 14 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,7 @@ dummy_func(
PyStackRef_CLOSE(value);
}
}
DEAD(values);
if (err) {
Py_DECREF(set_o);
ERROR_IF(true, error);
Expand Down Expand Up @@ -3583,15 +3584,15 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION_FT */
}

op(_MAYBE_EXPAND_METHOD, (callable[1], self_or_null[1], args[oparg] -- func[1], maybe_self[1], args[oparg])) {
op(_MAYBE_EXPAND_METHOD, (callable[1], self_or_null[1], args[oparg] -- callable[1], self_or_null[1], args[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);
PyStackRef_CLOSE(temp);
}
}
Expand All @@ -3618,6 +3619,9 @@ dummy_func(
tstate, callable[0], locals,
arguments, total_args, NULL, frame
);
DEAD(args);
DEAD(self_or_null);
DEAD(callable);
// Manipulate stack directly since we leave using DISPATCH_INLINED().
SYNC_SP();
// The frame has stolen all the arguments from the stack,
Expand Down Expand Up @@ -3950,10 +3954,10 @@ dummy_func(
_CALL_TUPLE_1 +
_CHECK_PERIODIC;

op(_CHECK_AND_ALLOCATE_OBJECT, (type_version/2, callable[1], null[1], args[oparg] -- init[1], self[1], args[oparg])) {
op(_CHECK_AND_ALLOCATE_OBJECT, (type_version/2, callable[1], self_or_null[1], args[oparg] -- callable[1], self_or_null[1], args[oparg])) {
(void)args;
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
DEOPT_IF(!PyStackRef_IsNull(null[0]));
DEOPT_IF(!PyStackRef_IsNull(self_or_null[0]));
DEOPT_IF(!PyType_Check(callable_o));
PyTypeObject *tp = (PyTypeObject *)callable_o;
DEOPT_IF(FT_ATOMIC_LOAD_UINT32_RELAXED(tp->tp_version_tag) != type_version);
Expand All @@ -3969,9 +3973,9 @@ dummy_func(
if (self_o == NULL) {
ERROR_NO_POP();
}
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);
PyStackRef_CLOSE(temp);
}

Expand All @@ -3982,10 +3986,11 @@ dummy_func(
assert(_PyFrame_GetBytecode(shim)[1].op.code == RETURN_VALUE);
/* Push self onto stack of shim */
shim->localsplus[0] = PyStackRef_DUP(self[0]);
DEAD(init);
DEAD(self);
_PyInterpreterFrame *temp = _PyEvalFramePushAndInit(
tstate, init[0], NULL, args-1, oparg+1, NULL, shim);
DEAD(init);
DEAD(self);
DEAD(args);
SYNC_SP();
if (temp == NULL) {
_PyEval_FrameClearAndPop(tstate, shim);
Expand Down Expand Up @@ -4187,9 +4192,9 @@ dummy_func(
res = PyStackRef_FromPyObjectSteal(res_o);
}

inst(CALL_ISINSTANCE, (unused/1, unused/2, callable[1], self_or_null[1], args[oparg] -- res)) {
inst(CALL_ISINSTANCE, (unused/1, unused/2, callable, self_or_null[1], args[oparg] -- res)) {
/* isinstance(o, o2) */
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable[0]);
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);

int total_args = oparg;
_PyStackRef *arguments = args;
Expand Down Expand Up @@ -4420,15 +4425,15 @@ dummy_func(
ERROR_IF(err, error);
}

op(_MAYBE_EXPAND_METHOD_KW, (callable[1], self_or_null[1], args[oparg], kwnames_in -- func[1], maybe_self[1], args[oparg], kwnames_out)) {
op(_MAYBE_EXPAND_METHOD_KW, (callable[1], self_or_null[1], args[oparg], kwnames_in -- callable[1], self_or_null[1], args[oparg], kwnames_out)) {
(void)args;
if (PyStackRef_TYPE(callable[0]) == &PyMethod_Type && PyStackRef_IsNull(self_or_null[0])) { BB07
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);
PyStackRef_CLOSE(temp);
}
kwnames_out = kwnames_in;
Expand Down Expand Up @@ -4458,6 +4463,9 @@ dummy_func(
tstate, callable[0], locals,
arguments, positional_args, kwnames_o, frame
);
DEAD(args);
DEAD(self_or_null);
DEAD(callable);
PyStackRef_CLOSE(kwnames);
// Sync stack explicitly since we leave using DISPATCH_INLINED().
SYNC_SP();
Expand Down Expand Up @@ -4525,6 +4533,9 @@ dummy_func(
PyStackRef_CLOSE(kwnames);
// The frame has stolen all the arguments from the stack,
// so there is no need to clean them up.
DEAD(args);
DEAD(self_or_null);
DEAD(callable);
SYNC_SP();
ERROR_IF(temp == NULL, error);
new_frame = temp;
Expand Down
Loading
Loading
0