Skip to content
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

GH-123391: Code generators: Track stack and locals in if statements. #123397

Closed
wants to merge 16 commits into from
Closed
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
65 changes: 63 additions & 2 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_effect_sizes(self):
stack.pop(y)
stack.pop(x)
for out in outputs:
stack.push(Local.local(out))
stack.push(Local.undefined(out))
self.assertEqual(stack.base_offset.to_c(), "-1 - oparg - oparg*2")
self.assertEqual(stack.top_offset.to_c(), "1 - oparg - oparg*2 + oparg*4")

Expand Down Expand Up @@ -281,6 +281,67 @@ def test_predictions(self):
"""
self.run_cases_test(input, output)

def test_sync_sp(self):
input = """
inst(A, (arg -- res)) {
SYNC_SP();
escaping_call();
res = Py_None;
}
inst(B, (arg -- res)) {
res = Py_None;
SYNC_SP();
escaping_call();
}
"""
output = """
TARGET(A) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(A);
_PyStackRef res;
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
escaping_call();
res = Py_None;
stack_pointer[0] = res;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}

TARGET(B) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(B);
_PyStackRef res;
res = Py_None;
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
escaping_call();
stack_pointer[0] = res;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
self.run_cases_test(input, output)


def test_pep7_condition(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test the else branch as well?

input = """
inst(OP, (arg1 -- out)) {
if (arg1)
out = 0;
else {
out = 1;
}
}
"""
output = ""
with self.assertRaises(SyntaxError):
self.run_cases_test(input, output)

def test_error_if_plain(self):
input = """
inst(OP, (--)) {
Expand Down Expand Up @@ -810,7 +871,7 @@ def test_deopt_and_exit(self):
}
"""
output = ""
with self.assertRaises(Exception):
with self.assertRaises(SyntaxError):
self.run_cases_test(input, output)

def test_array_of_one(self):
Expand Down
20 changes: 15 additions & 5 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,9 @@ dummy_func(
int err = _Py_call_instrumentation_arg(
tstate, PY_MONITORING_EVENT_PY_RETURN,
frame, this_instr, PyStackRef_AsPyObjectBorrow(val));
if (err) ERROR_NO_POP();
if (err) {
ERROR_NO_POP();
}
}

macro(INSTRUMENTED_RETURN_VALUE) =
Expand Down Expand Up @@ -1168,7 +1170,9 @@ dummy_func(
tstate, PY_MONITORING_EVENT_PY_YIELD,
frame, this_instr, PyStackRef_AsPyObjectBorrow(val));
LOAD_SP();
if (err) ERROR_NO_POP();
if (err) {
ERROR_NO_POP();
}
if (frame->instr_ptr != this_instr) {
next_instr = frame->instr_ptr;
DISPATCH();
Expand Down Expand Up @@ -1276,10 +1280,12 @@ dummy_func(
DECREF_INPUTS();
ERROR_IF(true, error);
}
if (PyDict_CheckExact(ns))
if (PyDict_CheckExact(ns)) {
err = PyDict_SetItem(ns, name, PyStackRef_AsPyObjectBorrow(v));
else
}
else {
err = PyObject_SetItem(ns, name, PyStackRef_AsPyObjectBorrow(v));
}
DECREF_INPUTS();
ERROR_IF(err, error);
}
Expand Down Expand Up @@ -1993,6 +1999,8 @@ dummy_func(
attr_o = PyObject_GetAttr(PyStackRef_AsPyObjectBorrow(owner), name);
DECREF_INPUTS();
ERROR_IF(attr_o == NULL, error);
/* We need to define self_or_null on all paths */
self_or_null = PyStackRef_NULL;
}
attr = PyStackRef_FromPyObjectSteal(attr_o);
}
Expand Down Expand Up @@ -4297,7 +4305,9 @@ dummy_func(
int err = _Py_call_instrumentation_2args(
tstate, PY_MONITORING_EVENT_CALL,
frame, this_instr, func, arg);
if (err) ERROR_NO_POP();
if (err) {
ERROR_NO_POP();
}
result = PyStackRef_FromPyObjectSteal(PyObject_Call(func, callargs, kwargs));

if (!PyFunction_Check(func) && !PyMethod_Check(func)) {
Expand Down
12 changes: 8 additions & 4 deletions Python/executor_cases.c.h

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

32 changes: 23 additions & 9 deletions Python/generated_cases.c.h

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

25 changes: 18 additions & 7 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ dummy_func(void) {
res = sym_new_type(ctx, &PyFloat_Type);
}
}
res = sym_new_unknown(ctx);
else {
res = sym_new_unknown(ctx);
}
}

op(_BINARY_OP_ADD_INT, (left, right -- res)) {
Expand Down Expand Up @@ -337,41 +339,47 @@ dummy_func(void) {
}

op(_TO_BOOL, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
int opt = optimize_to_bool(this_instr, ctx, value, &res);
if (!opt) {
res = sym_new_type(ctx, &PyBool_Type);
}
}

op(_TO_BOOL_BOOL, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
int opt = optimize_to_bool(this_instr, ctx, value, &res);
if (!opt) {
sym_set_type(value, &PyBool_Type);
res = value;
}
}

op(_TO_BOOL_INT, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
int opt = optimize_to_bool(this_instr, ctx, value, &res);
if (!opt) {
sym_set_type(value, &PyLong_Type);
res = sym_new_type(ctx, &PyBool_Type);
}
}

op(_TO_BOOL_LIST, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
int opt = optimize_to_bool(this_instr, ctx, value, &res);
if (!opt) {
sym_set_type(value, &PyList_Type);
res = sym_new_type(ctx, &PyBool_Type);
}
}

op(_TO_BOOL_NONE, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
int opt = optimize_to_bool(this_instr, ctx, value, &res);
if (!opt) {
sym_set_const(value, Py_None);
res = sym_new_const(ctx, Py_False);
}
}

op(_TO_BOOL_STR, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
int opt = optimize_to_bool(this_instr, ctx, value, &res);
if (!opt) {
res = sym_new_type(ctx, &PyBool_Type);
sym_set_type(value, &PyUnicode_Type);
}
Expand Down Expand Up @@ -482,6 +490,9 @@ dummy_func(void) {
if (oparg & 1) {
self_or_null = sym_new_unknown(ctx);
}
else {
self_or_null = NULL;
}
}

op(_LOAD_ATTR_MODULE, (index/1, owner -- attr, null if (oparg & 1))) {
Expand Down
Loading
Loading