Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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