Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
POP TOP
  • Loading branch information
savannahostrowski committed Aug 5, 2025
commit e75ca80cff4fac57cebd1aee8ee2f84db93620cb
49 changes: 49 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,55 @@ def f(n):
# But all of the appends we care about are still there:
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))

def test_replace_with_true_pop_top_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for _ in range(n):
a = 42
result = bool(a)
if result:
x += 1
return x
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_REPLACE_WITH_TRUE", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)

def test_to_bool_none_pop_top_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for _ in range(n):
a = None
result = bool(a)
if result:
x += 1
return x
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, 0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_TO_BOOL_NONE", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)

def test_get_len_pop_top_load_const_inline_borrow(self):
def testfunc(n):
x = 0
for _ in range(n):
a = "foo"
result = len(a)
if result:
x += 1
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_GET_LEN", uops)
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)

def test_compare_op_pop_two_load_const_inline_borrow(self):
def testfunc(n):
x = 0
Expand Down
1 change: 1 addition & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ dummy_func(void) {
}

op(_TO_BOOL_NONE, (value -- res)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(value);
Comment thread
savannahostrowski marked this conversation as resolved.
Outdated
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
if (!already_bool) {
sym_set_const(value, Py_None);
Expand Down
190 changes: 112 additions & 78 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading