Skip to content

Commit 853e11e

Browse files
committed
Refactor TO_BOOL_LIST, add unit tests
1 parent 2ca15cd commit 853e11e

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
@@ -3049,6 +3049,22 @@ def f(n):
30493049
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
30503050
self.assertIn("_POP_TOP_NOP", uops)
30513051

3052+
def test_to_bool_list(self):
3053+
def f(n):
3054+
for i in range(n):
3055+
lst = [] if i != TIER2_THRESHOLD else [1]
3056+
if lst:
3057+
return 1
3058+
return 0
3059+
3060+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
3061+
self.assertEqual(res, 0)
3062+
self.assertIsNotNone(ex)
3063+
uops = get_opnames(ex)
3064+
self.assertIn("_TO_BOOL_LIST", uops)
3065+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
3066+
self.assertIn("_POP_TOP_NOP", uops)
3067+
30523068
def test_to_bool_always_true(self):
30533069
def testfunc(n):
30543070
class A:

Python/bytecodes.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,15 @@ dummy_func(
514514
EXIT_IF(!PySlice_Check(o));
515515
}
516516

517-
macro(TO_BOOL_LIST) = _GUARD_TOS_LIST + unused/1 + unused/2 + _TO_BOOL_LIST;
517+
macro(TO_BOOL_LIST) = _GUARD_TOS_LIST + unused/1 + unused/2 + _TO_BOOL_LIST + POP_TOP;
518518

519-
op(_TO_BOOL_LIST, (value -- res)) {
519+
op(_TO_BOOL_LIST, (value -- res, v)) {
520520
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
521521
assert(PyList_CheckExact(value_o));
522522
STAT_INC(TO_BOOL, hit);
523523
res = PyList_GET_SIZE(value_o) ? PyStackRef_True : PyStackRef_False;
524-
DECREF_INPUTS();
524+
v = value;
525+
DEAD(value);
525526
}
526527

527528
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
@@ -409,11 +409,12 @@ dummy_func(void) {
409409
v = value;
410410
}
411411

412-
op(_TO_BOOL_LIST, (value -- res)) {
413-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
412+
op(_TO_BOOL_LIST, (value -- res, v)) {
413+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
414414
if (!already_bool) {
415415
res = sym_new_type(ctx, &PyBool_Type);
416416
}
417+
v = value;
417418
}
418419

419420
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)