Skip to content

Commit e75ca80

Browse files
POP TOP
1 parent 8e7023d commit e75ca80

File tree

4 files changed

+182
-100
lines changed

4 files changed

+182
-100
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,55 @@ def f(n):
16141614
# But all of the appends we care about are still there:
16151615
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
16161616

1617+
def test_replace_with_true_pop_top_load_const_inline_borrow(self):
1618+
def testfunc(n):
1619+
x = 0
1620+
for _ in range(n):
1621+
a = 42
1622+
result = bool(a)
1623+
if result:
1624+
x += 1
1625+
return x
1626+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1627+
self.assertEqual(res, TIER2_THRESHOLD)
1628+
self.assertIsNotNone(ex)
1629+
uops = get_opnames(ex)
1630+
self.assertNotIn("_REPLACE_WITH_TRUE", uops)
1631+
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
1632+
1633+
def test_to_bool_none_pop_top_load_const_inline_borrow(self):
1634+
def testfunc(n):
1635+
x = 0
1636+
for _ in range(n):
1637+
a = None
1638+
result = bool(a)
1639+
if result:
1640+
x += 1
1641+
return x
1642+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1643+
self.assertEqual(res, 0)
1644+
self.assertIsNotNone(ex)
1645+
uops = get_opnames(ex)
1646+
self.assertNotIn("_TO_BOOL_NONE", uops)
1647+
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
1648+
1649+
def test_get_len_pop_top_load_const_inline_borrow(self):
1650+
def testfunc(n):
1651+
x = 0
1652+
for _ in range(n):
1653+
a = "foo"
1654+
result = len(a)
1655+
if result:
1656+
x += 1
1657+
return x
1658+
1659+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1660+
self.assertEqual(res, TIER2_THRESHOLD)
1661+
self.assertIsNotNone(ex)
1662+
uops = get_opnames(ex)
1663+
self.assertNotIn("_GET_LEN", uops)
1664+
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
1665+
16171666
def test_compare_op_pop_two_load_const_inline_borrow(self):
16181667
def testfunc(n):
16191668
x = 0

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ dummy_func(void) {
362362
}
363363

364364
op(_TO_BOOL_NONE, (value -- res)) {
365+
REPLACE_OPCODE_IF_EVALUATES_PURE(value);
365366
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
366367
if (!already_bool) {
367368
sym_set_const(value, Py_None);

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)