Skip to content

Commit 685fd74

Browse files
authored
GH-131798: Remove type checks for _TO_BOOL_STR (GH-131816)
1 parent 39fa19a commit 685fd74

10 files changed

+185
-125
lines changed

Include/internal/pycore_opcode_metadata.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

+97-96
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

+5-1
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

+18
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,24 @@ def testfunc(n):
15811581
self.assertNotIn("_COMPARE_OP_INT", uops)
15821582
self.assertIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
15831583

1584+
def test_remove_guard_for_known_type_str(self):
1585+
def f(n):
1586+
for i in range(n):
1587+
false = i == TIER2_THRESHOLD
1588+
empty = "X"[:false]
1589+
empty += "" # Make JIT realize this is a string.
1590+
if empty:
1591+
return 1
1592+
return 0
1593+
1594+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
1595+
self.assertEqual(res, 0)
1596+
self.assertIsNotNone(ex)
1597+
uops = get_opnames(ex)
1598+
self.assertIn("_TO_BOOL_STR", uops)
1599+
self.assertNotIn("_GUARD_TOS_UNICODE", uops)
1600+
1601+
15841602
def global_identity(x):
15851603
return x
15861604

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow JIT to omit str guard in truthiness test when str type is known.

Python/bytecodes.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,14 @@ dummy_func(
512512
res = PyStackRef_False;
513513
}
514514

515-
inst(TO_BOOL_STR, (unused/1, unused/2, value -- res)) {
515+
op(_GUARD_TOS_UNICODE, (value -- value)) {
516516
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
517517
EXIT_IF(!PyUnicode_CheckExact(value_o));
518+
}
519+
520+
op(_TO_BOOL_STR, (value -- res)) {
518521
STAT_INC(TO_BOOL, hit);
522+
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
519523
if (value_o == &_Py_STR(empty)) {
520524
assert(_Py_IsImmortal(value_o));
521525
DEAD(value);
@@ -528,6 +532,9 @@ dummy_func(
528532
}
529533
}
530534

535+
macro(TO_BOOL_STR) =
536+
_GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR;
537+
531538
op(_REPLACE_WITH_TRUE, (value -- res)) {
532539
PyStackRef_CLOSE(value);
533540
res = PyStackRef_True;

Python/executor_cases.c.h

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)