Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d35071a
Add specializations for TO_BOOL
eendebakpt Apr 8, 2026
44f5c49
type information for kwargs
eendebakpt Apr 8, 2026
b80a79b
add tests
eendebakpt Apr 8, 2026
ba88f27
Merge branch 'main' into to_bool_specialization
eendebakpt Apr 8, 2026
29311d8
review comments
eendebakpt Apr 10, 2026
e9c1e24
Merge branch 'main' into to_bool_specialization
eendebakpt Apr 10, 2026
9de1c2c
fix merge conflict
eendebakpt Apr 10, 2026
91334af
Merge branch 'main' into to_bool_specialization
eendebakpt Apr 12, 2026
08a4535
Merge branch 'main' into to_bool_specialization
eendebakpt Apr 14, 2026
62fae72
add regression test for set
eendebakpt Apr 14, 2026
52f7cc6
review comments
eendebakpt Apr 14, 2026
05c6089
handle set case
eendebakpt Apr 14, 2026
d655709
remove dead code
eendebakpt Apr 14, 2026
3984a31
Merge branch 'main' into to_bool_specialization
eendebakpt Apr 22, 2026
0606e89
regenerate
eendebakpt Apr 22, 2026
578a6e0
Merge branch 'to_bool_specialization_v2' into to_bool_specialization
eendebakpt Apr 30, 2026
1e01851
Merge remote-tracking branch 'origin/main' into to_bool_specialization
eendebakpt Apr 30, 2026
bb35e19
fix merge conflicts and add recording uop
eendebakpt Apr 30, 2026
f9a8bc5
cleanup
eendebakpt Apr 30, 2026
ee0325c
Merge remote-tracking branch 'origin/main' into to_bool_specializatio…
eendebakpt Apr 30, 2026
c7daa03
Restore test_opt.py from origin/main, re-applying TO_BOOL tests
eendebakpt Apr 30, 2026
e9a1e9a
add more tests, remove defensive code
eendebakpt May 1, 2026
09c07a3
news entry
eendebakpt May 1, 2026
c64d69d
Merge remote-tracking branch 'origin/main' into to_bool_specialization
eendebakpt May 1, 2026
06247eb
Merge branch 'main' into to_bool_specialization
eendebakpt May 1, 2026
a30ca62
Merge branch 'main' into to_bool_specialization
eendebakpt May 1, 2026
fa17d1d
Merge branch 'main' into to_bool_specialization
eendebakpt May 3, 2026
cd540b8
Merge branch 'main' into pr-148271
eendebakpt May 18, 2026
729ecc9
add frozendict
eendebakpt May 18, 2026
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
1,920 changes: 963 additions & 957 deletions Include/internal/pycore_uop_ids.h

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

168 changes: 168 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4250,6 +4250,174 @@ class A:
uops = get_opnames(ex)
self.assertNotIn("_REPLACE_WITH_TRUE", uops)

def test_to_bool_kwargs_dict(self):
"""**kwargs is known to be dict, so TO_BOOL specializes to _TO_BOOL_DICT."""
def inner(**kwargs):
cnt = 0
for i in range(TIER2_THRESHOLD):
if kwargs:
cnt += 1
return cnt

def f(n):
return inner(a=1, b=2)

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_DICT", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_kwargs_empty_dict(self):
"""**kwargs is known to be dict even when empty."""
def inner(**kwargs):
cnt = 0
for i in range(TIER2_THRESHOLD):
if not kwargs:
cnt += 1
return cnt

def f(n):
return inner()

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_DICT", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_varargs_tuple(self):
"""*args is known to be tuple, so TO_BOOL specializes to _TO_BOOL_SIZED."""
def inner(*args):
cnt = 0
for i in range(TIER2_THRESHOLD):
if args:
cnt += 1
return cnt

def f(n):
return inner(1, 2, 3)

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_SIZED", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_varargs_empty_tuple(self):
"""*args is known to be tuple even when empty."""
def inner(*args):
cnt = 0
for i in range(TIER2_THRESHOLD):
if not args:
cnt += 1
return cnt

def f(n):
return inner()

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_SIZED", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_args_and_kwargs(self):
"""Combined *args and **kwargs both get correct types."""
def inner(*args, **kwargs):
cnt = 0
for i in range(TIER2_THRESHOLD):
if args and kwargs:
cnt += 1
return cnt

def f(n):
return inner(1, 2, a=3)

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_SIZED", uops)
self.assertIn("_TO_BOOL_DICT", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_args_kwargs_with_regular_params(self):
"""*args/**kwargs slot calculation is correct with regular params."""
def inner(x, y, *args, key=None, **kwargs):
cnt = 0
for i in range(TIER2_THRESHOLD):
if args and kwargs:
cnt += 1
return cnt

def f(n):
return inner(1, 2, 3, 4, key="v", extra=5)

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_SIZED", uops)
self.assertIn("_TO_BOOL_DICT", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_kwargs_only_no_varargs(self):
"""**kwargs without *args gets correct dict type."""
def inner(x, **kwargs):
cnt = 0
for i in range(TIER2_THRESHOLD):
if kwargs:
cnt += 1
return cnt

def f(n):
return inner(1, a=2)

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
ex_inner = get_first_executor(inner)
self.assertIsNotNone(ex_inner)
uops = get_opnames(ex_inner)
self.assertIn("_TO_BOOL_DICT", uops)
self.assertNotIn("_TO_BOOL", uops)

def test_to_bool_set_with_dummy_entries(self):
"""Sets with dummy entries (after discard) must evaluate as falsy.

PySetObject does not use PyObject_VAR_HEAD; reading ob_size at that
offset accidentally reads `fill`, which counts both live *and* dummy
(deleted) entries. A set whose items have all been discarded must
still be falsy even after the JIT specialises TO_BOOL to
_TO_BOOL_ANY_SET.
"""
def f(n):
cnt = 0
for _ in range(n):
s = {1}
s.discard(1) # leaves a dummy entry; fill == 1, used == 0
if s: # emits TO_BOOL; specialises to _TO_BOOL_ANY_SET
cnt += 1
return cnt

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
# `if s` must be false for every iteration -- the set is empty after
# discard(), even though the dummy entry makes fill != 0.
self.assertEqual(res, 0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_TO_BOOL_ANY_SET", uops)

def test_attr_promotion_failure(self):
# We're not testing for any specific uops here, just
# testing it doesn't crash.
Expand Down
27 changes: 27 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,33 @@ dummy_func(
_REPLACE_WITH_TRUE +
POP_TOP;

tier2 op(_TO_BOOL_DICT, (value -- res)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can merge this with _TO_BOOL_SIZED by using the fact that both do a fixed offset lookup.
In tier2 optimizer you can set the offset for where the size is stored and do size = (Py_ssize_t)((char *)obj + offset) and check that directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. It goes into the internals of PyDict (e.g. not using PyDict_GET_SIZE, but doing manual offset calculations) and we also need to store the offset somewhere. So I think this is too much of a complication to get rid of a tier2 opcode.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also need to store the offset somewhere.

You can store it in the instruction operand0.

PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
assert(PyDict_CheckExact(value_o));
STAT_INC(TO_BOOL, hit);
res = PyDict_GET_SIZE(value_o) ? PyStackRef_True : PyStackRef_False;
PyStackRef_CLOSE(value);
}

tier2 op(_TO_BOOL_SIZED, (value -- res)) {
/* Covers types whose truthiness is Py_SIZE(obj) != 0:
tuple, bytes, bytearray. Not set/frozenset: PySetObject is
not a PyVarObject and its ob_size slot aliases `fill`, which
counts dummy entries -- use _TO_BOOL_ANY_SET for those. */
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
STAT_INC(TO_BOOL, hit);
res = Py_SIZE(value_o) ? PyStackRef_True : PyStackRef_False;
PyStackRef_CLOSE(value);
}

tier2 op(_TO_BOOL_ANY_SET, (value -- res)) {
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
assert(PyAnySet_Check(value_o));
STAT_INC(TO_BOOL, hit);
res = PySet_GET_SIZE(value_o) ? PyStackRef_True : PyStackRef_False;
PyStackRef_CLOSE(value);
}

macro(UNARY_INVERT) = _UNARY_INVERT + POP_TOP;

op(_UNARY_INVERT, (value -- res, v)) {
Expand Down
86 changes: 86 additions & 0 deletions Python/executor_cases.c.h

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

Loading
Loading