Skip to content

Commit 7d9c91c

Browse files
committed
gh-155486: Allow non-compact integers in TO_BOOL_INT
1 parent 219768f commit 7d9c91c

9 files changed

Lines changed: 145 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,23 @@ 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+
return 1
4886+
4887+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
4888+
self.assertEqual(res, 1)
4889+
self.assertIsNotNone(ex)
4890+
uops = get_opnames(ex)
4891+
self.assertIn("_TO_BOOL_INT", uops)
4892+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
4893+
self.assertIn("_POP_TOP_NOP", uops)
4894+
48784895
def test_to_bool_list(self):
48794896
def f(n):
48804897
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_cases.c.h

Lines changed: 4 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)