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,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)) {
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 on lines +541 to +550
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.

Would this be better?

PyTypeObject *tp = sym_get_type(value);
if (tp == &PyTuple_Type || tp == ...)

res = sym_new_truthiness(ctx, value, true);
}
}
Expand Down Expand Up @@ -600,6 +610,21 @@ dummy_func(void) {
}
}

op(_TO_BOOL_DICT, (value -- res)) {
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