Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 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
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,942 changes: 973 additions & 969 deletions Include/internal/pycore_uop_ids.h

Large diffs are not rendered by default.

30 changes: 30 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.

142 changes: 142 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4028,6 +4028,148 @@ 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_attr_promotion_failure(self):
# We're not testing for any specific uops here, just
# testing it doesn't crash.
Expand Down
17 changes: 17 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,23 @@ 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, set, frozenset, bytes, bytearray. */
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
STAT_INC(TO_BOOL, hit);
res = Py_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
56 changes: 56 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.

25 changes: 25 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,16 @@ dummy_func(void) {
op(_TO_BOOL, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
if (sym_matches_type(value, &PyDict_Type)) {
REPLACE_OP(this_instr, _TO_BOOL_DICT, 0, 0);
}
else if (sym_matches_type(value, &PyTuple_Type) ||
sym_matches_type(value, &PySet_Type) ||
sym_matches_type(value, &PyFrozenSet_Type) ||
sym_matches_type(value, &PyBytes_Type) ||
sym_matches_type(value, &PyByteArray_Type)) {
REPLACE_OP(this_instr, _TO_BOOL_SIZED, 0, 0);
}
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
res = sym_new_truthiness(ctx, value, true);
}
}
Expand Down Expand Up @@ -600,6 +610,21 @@ dummy_func(void) {
}
}

op(_TO_BOOL_DICT, (value -- res)) {
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
sym_set_type(value, &PyDict_Type);
res = sym_new_truthiness(ctx, value, true);
}
}

op(_TO_BOOL_SIZED, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
}
}

op(_UNARY_NOT, (value -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(value, res);
sym_set_type(value, &PyBool_Type);
Expand Down
35 changes: 35 additions & 0 deletions Python/optimizer_cases.c.h

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

Loading
Loading