Skip to content

Commit cd98657

Browse files
authored
gh-155486: Allow non-compact integers in TO_BOOL_INT (GH-155531)
1 parent 716cbae commit cd98657

10 files changed

Lines changed: 162 additions & 8 deletions

File tree

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

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

Include/internal/pycore_uop_metadata.h

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

Lib/test/test_capi/test_opt.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,27 @@ def f(n):
48754875
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
48764876
self.assertIn("_POP_TOP_NOP", uops)
48774877

4878+
def test_to_bool_noncompact_int(self):
4879+
# gh-155486: non-compact exact integer should remain specialized
4880+
# as _TO_BOOL_INT in Tier 2.
4881+
def f(n, value=1 << 100):
4882+
for _ in range(n):
4883+
if not value:
4884+
return 0
4885+
# The second check should reuse the exact-int type established by the first guard.
4886+
if not value:
4887+
return 0
4888+
return 1
4889+
4890+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
4891+
self.assertEqual(res, 1)
4892+
self.assertIsNotNone(ex)
4893+
uops = get_opnames(ex)
4894+
self.assertIn("_TO_BOOL_INT", uops)
4895+
self.assertLessEqual(count_ops(ex, "_GUARD_TOS_EXACT_INT"), 1)
4896+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
4897+
self.assertIn("_POP_TOP_NOP", uops)
4898+
48784899
def test_to_bool_list(self):
48794900
def f(n):
48804901
for i in range(n):

Modules/_testinternalcapi/test_cases.c.h

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

Python/bytecodes.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ dummy_func(
526526
}
527527

528528
macro(TO_BOOL_INT) =
529-
_GUARD_TOS_INT + unused/1 + unused/2 + _TO_BOOL_INT + _POP_TOP_INT;
529+
_GUARD_TOS_EXACT_INT + unused/1 + unused/2 + _TO_BOOL_INT + _POP_TOP_INT;
530530

531531
op(_GUARD_NOS_LIST, (nos, unused -- nos, unused)) {
532532
PyObject *o = PyStackRef_AsPyObjectBorrow(nos);
@@ -643,6 +643,11 @@ dummy_func(
643643
EXIT_IF(!_PyLong_CheckExactAndCompact(value_o));
644644
}
645645

646+
op(_GUARD_TOS_EXACT_INT, (value -- value)) {
647+
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
648+
EXIT_IF(!PyLong_CheckExact(value_o));
649+
}
650+
646651
op(_GUARD_NOS_OVERFLOWED, (left, unused -- left, unused)) {
647652
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
648653
assert(Py_TYPE(left_o) == &PyLong_Type);

Python/executor_cases.c.h

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

Python/generated_cases.c.h

Lines changed: 2 additions & 2 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ dummy_func(void) {
217217
}
218218
}
219219

220+
op(_GUARD_TOS_EXACT_INT, (value -- value)) {
221+
if (sym_matches_type(value, &PyLong_Type)) {
222+
ADD_OP(_NOP, 0, 0);
223+
}
224+
sym_set_type(value, &PyLong_Type);
225+
}
226+
220227
op(_GUARD_NOS_INT, (left, unused -- left, unused)) {
221228
if (sym_is_compact_int(left)) {
222229
ADD_OP(_NOP, 0, 0);

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)