Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
102 changes: 38 additions & 64 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,6 @@ def return_hello():
# Constant narrowing allows constant folding for second comparison
self.assertLessEqual(count_ops(ex, "_COMPARE_OP_STR"), 1)

@unittest.skip("gh-139109 WIP")
def test_combine_stack_space_checks_sequential(self):
def dummy12(x):
return x - 1
Expand All @@ -1046,12 +1045,14 @@ def testfunc(n):
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
self.assertEqual(uop_names.count("_RETURN_VALUE"), 2)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
# sequential calls: max(12, 13) == 13
largest_stack = _testinternalcapi.get_co_framesize(dummy13.__code__)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
# Each call gets its own _CHECK_STACK_SPACE_OPERAND
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 2)
# Each _CHECK_STACK_SPACE_OPERAND has the framesize of its function
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy12.__code__)), uops_and_operands)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy13.__code__)), uops_and_operands)

@unittest.skip("gh-139109 WIP")
def test_combine_stack_space_checks_nested(self):
def dummy12(x):
return x + 3
Expand All @@ -1074,15 +1075,12 @@ def testfunc(n):
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
self.assertEqual(uop_names.count("_RETURN_VALUE"), 2)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
# nested calls: 15 + 12 == 27
largest_stack = (
_testinternalcapi.get_co_framesize(dummy15.__code__) +
_testinternalcapi.get_co_framesize(dummy12.__code__)
)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 2)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy15.__code__)), uops_and_operands)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy12.__code__)), uops_and_operands)

@unittest.skip("gh-139109 WIP")
def test_combine_stack_space_checks_several_calls(self):
def dummy12(x):
return x + 3
Expand Down Expand Up @@ -1110,15 +1108,14 @@ def testfunc(n):
self.assertEqual(uop_names.count("_PUSH_FRAME"), 4)
self.assertEqual(uop_names.count("_RETURN_VALUE"), 4)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
# max(12, 18 + max(12, 13)) == 31
largest_stack = (
_testinternalcapi.get_co_framesize(dummy18.__code__) +
_testinternalcapi.get_co_framesize(dummy13.__code__)
)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 4)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy12.__code__)), uops_and_operands)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy13.__code__)), uops_and_operands)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy18.__code__)), uops_and_operands)

@unittest.skip("gh-139109 WIP")
def test_combine_stack_space_checks_several_calls_different_order(self):
# same as `several_calls` but with top-level calls reversed
def dummy12(x):
Expand Down Expand Up @@ -1147,15 +1144,14 @@ def testfunc(n):
self.assertEqual(uop_names.count("_PUSH_FRAME"), 4)
self.assertEqual(uop_names.count("_RETURN_VALUE"), 4)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
# max(18 + max(12, 13), 12) == 31
largest_stack = (
_testinternalcapi.get_co_framesize(dummy18.__code__) +
_testinternalcapi.get_co_framesize(dummy13.__code__)
)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 4)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy12.__code__)), uops_and_operands)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy13.__code__)), uops_and_operands)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy18.__code__)), uops_and_operands)

@unittest.skip("gh-139109 WIP")
def test_combine_stack_space_complex(self):
def dummy0(x):
return x
Expand Down Expand Up @@ -1193,19 +1189,8 @@ def testfunc(n):
self.assertEqual(uop_names.count("_RETURN_VALUE"), 15)

self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
largest_stack = (
_testinternalcapi.get_co_framesize(dummy6.__code__) +
_testinternalcapi.get_co_framesize(dummy5.__code__) +
_testinternalcapi.get_co_framesize(dummy2.__code__) +
_testinternalcapi.get_co_framesize(dummy1.__code__) +
_testinternalcapi.get_co_framesize(dummy0.__code__)
)
self.assertIn(
("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands
)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 15)

@unittest.skip("gh-139109 WIP")
Comment thread
cocolato marked this conversation as resolved.
def test_combine_stack_space_checks_large_framesize(self):
# Create a function with a large framesize. This ensures _CHECK_STACK_SPACE is
# actually doing its job. Note that the resulting trace hits
Expand Down Expand Up @@ -1250,24 +1235,13 @@ def testfunc(n):

uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
uop_names = [uop[0] for uop in uops_and_operands]
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)

# this hits a different case during trace projection in refcount test runs only,
# so we need to account for both possibilities
self.assertIn(uop_names.count("_CHECK_STACK_SPACE"), [0, 1])
if uop_names.count("_CHECK_STACK_SPACE") == 0:
largest_stack = (
_testinternalcapi.get_co_framesize(dummy15.__code__) +
_testinternalcapi.get_co_framesize(dummy_large.__code__)
)
else:
largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__)
self.assertIn(
("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands
)
self.assertGreaterEqual(uop_names.count("_PUSH_FRAME"), 1)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"),
uop_names.count("_PUSH_FRAME"))
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
_testinternalcapi.get_co_framesize(dummy15.__code__)), uops_and_operands)

@unittest.skip("gh-139109 WIP")
def test_combine_stack_space_checks_recursion(self):
def dummy15(x):
while x > 0:
Expand All @@ -1290,12 +1264,12 @@ def testfunc(n):

uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
uop_names = [uop[0] for uop in uops_and_operands]
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
self.assertEqual(uop_names.count("_PUSH_FRAME"), 12)
self.assertEqual(uop_names.count("_RETURN_VALUE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 1)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 12)
framesize = _testinternalcapi.get_co_framesize(dummy15.__code__)
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", framesize), uops_and_operands)

def test_many_nested(self):
# overflow the trace_stack
Expand Down
12 changes: 9 additions & 3 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1088,13 +1088,19 @@ dummy_func(void) {
}

op(_CHECK_STACK_SPACE, (unused, unused, unused[oparg] -- unused, unused, unused[oparg])) {
assert((this_instr + 1)->opcode == _CHECK_RECURSION_REMAINING);
Comment thread
cocolato marked this conversation as resolved.
Outdated
assert((this_instr + 4)->opcode == _PUSH_FRAME);
PyCodeObject *co = get_code_with_logging((this_instr + 4));
if (co == NULL) {
ctx->done = true;
break;
}
ADD_OP(_CHECK_STACK_SPACE_OPERAND, 0, co->co_framesize);
REPLACE_OP((this_instr + 1), _NOP, 0, 0);
Comment thread
cocolato marked this conversation as resolved.
Outdated
}

op (_CHECK_STACK_SPACE_OPERAND, (framesize/2 -- )) {
(void)framesize;
/* We should never see _CHECK_STACK_SPACE_OPERANDs.
* They are only created at the end of this pass. */
Py_UNREACHABLE();
}

op(_PUSH_FRAME, (new_frame -- )) {
Expand Down
10 changes: 9 additions & 1 deletion Python/optimizer_cases.c.h

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

Loading