Skip to content

Commit 1023fde

Browse files
committed
Refactor TO_BOOL_LIST, add unit tests
1 parent 4ff2e5e commit 1023fde

9 files changed

Lines changed: 123 additions & 56 deletions

File tree

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 25 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 11 additions & 7 deletions
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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,22 @@ def f(n):
29592959
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
29602960
self.assertIn("_POP_TOP_NOP", uops)
29612961

2962+
def test_to_bool_list(self):
2963+
def f(n):
2964+
for i in range(n):
2965+
lst = [] if i != TIER2_THRESHOLD else [1]
2966+
if lst:
2967+
return 1
2968+
return 0
2969+
2970+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
2971+
self.assertEqual(res, 0)
2972+
self.assertIsNotNone(ex)
2973+
uops = get_opnames(ex)
2974+
self.assertIn("_TO_BOOL_LIST", uops)
2975+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
2976+
self.assertIn("_POP_TOP_NOP", uops)
2977+
29622978
def test_to_bool_int(self):
29632979
def f(n):
29642980
for i in range(n):

Python/bytecodes.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,15 @@ dummy_func(
509509
EXIT_IF(!PySlice_Check(o));
510510
}
511511

512-
macro(TO_BOOL_LIST) = _GUARD_TOS_LIST + unused/1 + unused/2 + _TO_BOOL_LIST;
512+
macro(TO_BOOL_LIST) = _GUARD_TOS_LIST + unused/1 + unused/2 + _TO_BOOL_LIST + POP_TOP;
513513

514-
op(_TO_BOOL_LIST, (value -- res)) {
514+
op(_TO_BOOL_LIST, (value -- res, v)) {
515515
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
516516
assert(PyList_CheckExact(value_o));
517517
STAT_INC(TO_BOOL, hit);
518518
res = PyList_GET_SIZE(value_o) ? PyStackRef_True : PyStackRef_False;
519-
DECREF_INPUTS();
519+
v = value;
520+
DEAD(value);
520521
}
521522

522523
inst(TO_BOOL_NONE, (unused/1, unused/2, value -- res)) {

Python/executor_cases.c.h

Lines changed: 48 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,12 @@ dummy_func(void) {
400400
v = value;
401401
}
402402

403-
op(_TO_BOOL_LIST, (value -- res)) {
404-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
403+
op(_TO_BOOL_LIST, (value -- res, v)) {
404+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
405405
if (!already_bool) {
406406
res = sym_new_type(ctx, &PyBool_Type);
407407
}
408+
v = value;
408409
}
409410

410411
op(_TO_BOOL_NONE, (value -- res)) {

Python/optimizer_cases.c.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)