Skip to content

Commit c33f2ab

Browse files
committed
refact _CHECK_STACK_SPACE && fix tests
1 parent f05c6af commit c33f2ab

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ def testfunc(n):
11521152
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
11531153
_testinternalcapi.get_co_framesize(dummy18.__code__)), uops_and_operands)
11541154

1155+
@unittest.skip("reopen when we combine multiple stack space checks into one")
11551156
def test_combine_stack_space_complex(self):
11561157
def dummy0(x):
11571158
return x
@@ -1189,8 +1190,19 @@ def testfunc(n):
11891190
self.assertEqual(uop_names.count("_RETURN_VALUE"), 15)
11901191

11911192
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1192-
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 15)
1193+
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1194+
largest_stack = (
1195+
_testinternalcapi.get_co_framesize(dummy6.__code__) +
1196+
_testinternalcapi.get_co_framesize(dummy5.__code__) +
1197+
_testinternalcapi.get_co_framesize(dummy2.__code__) +
1198+
_testinternalcapi.get_co_framesize(dummy1.__code__) +
1199+
_testinternalcapi.get_co_framesize(dummy0.__code__)
1200+
)
1201+
self.assertIn(
1202+
("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands
1203+
)
11931204

1205+
@unittest.skip("reopen when we combine multiple stack space checks into one")
11941206
def test_combine_stack_space_checks_large_framesize(self):
11951207
# Create a function with a large framesize. This ensures _CHECK_STACK_SPACE is
11961208
# actually doing its job. Note that the resulting trace hits
@@ -1235,12 +1247,22 @@ def testfunc(n):
12351247

12361248
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex]
12371249
uop_names = [uop[0] for uop in uops_and_operands]
1238-
self.assertGreaterEqual(uop_names.count("_PUSH_FRAME"), 1)
1239-
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0)
1240-
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"),
1241-
uop_names.count("_PUSH_FRAME"))
1242-
self.assertIn(("_CHECK_STACK_SPACE_OPERAND",
1243-
_testinternalcapi.get_co_framesize(dummy15.__code__)), uops_and_operands)
1250+
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2)
1251+
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1)
1252+
1253+
# this hits a different case during trace projection in refcount test runs only,
1254+
# so we need to account for both possibilities
1255+
self.assertIn(uop_names.count("_CHECK_STACK_SPACE"), [0, 1])
1256+
if uop_names.count("_CHECK_STACK_SPACE") == 0:
1257+
largest_stack = (
1258+
_testinternalcapi.get_co_framesize(dummy15.__code__) +
1259+
_testinternalcapi.get_co_framesize(dummy_large.__code__)
1260+
)
1261+
else:
1262+
largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__)
1263+
self.assertIn(
1264+
("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands
1265+
)
12441266

12451267
def test_combine_stack_space_checks_recursion(self):
12461268
def dummy15(x):

Python/bytecodes.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5362,7 +5362,6 @@ dummy_func(
53625362
tier2 op(_CHECK_STACK_SPACE_OPERAND, (framesize/2 --)) {
53635363
assert(framesize <= INT_MAX);
53645364
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, framesize));
5365-
DEOPT_IF(tstate->py_recursion_remaining <= 1);
53665365
}
53675366

53685367
op(_SAVE_RETURN_OFFSET, (--)) {

Python/executor_cases.c.h

Lines changed: 0 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,15 +1088,13 @@ dummy_func(void) {
10881088
}
10891089

10901090
op(_CHECK_STACK_SPACE, (unused, unused, unused[oparg] -- unused, unused, unused[oparg])) {
1091-
assert((this_instr + 1)->opcode == _CHECK_RECURSION_REMAINING);
10921091
assert((this_instr + 4)->opcode == _PUSH_FRAME);
10931092
PyCodeObject *co = get_code_with_logging((this_instr + 4));
10941093
if (co == NULL) {
10951094
ctx->done = true;
10961095
break;
10971096
}
10981097
ADD_OP(_CHECK_STACK_SPACE_OPERAND, 0, co->co_framesize);
1099-
REPLACE_OP((this_instr + 1), _NOP, 0, 0);
11001098
}
11011099

11021100
op (_CHECK_STACK_SPACE_OPERAND, (framesize/2 -- )) {

Python/optimizer_cases.c.h

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)