Skip to content

Commit 1fc22e4

Browse files
committed
gh-131798: Add _IS_NONE to the JIT optimizer
1 parent dd88e77 commit 1fc22e4

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,6 +4554,24 @@ def return_true():
45544554
# v + 1 should be constant folded
45554555
self.assertNotIn("_BINARY_OP", uops)
45564556

4557+
def test_is_none_narrows_to_constant(self):
4558+
def testfunc(n):
4559+
value = None
4560+
hits = 0
4561+
for _ in range(n):
4562+
if value is None:
4563+
hits += 1
4564+
return hits
4565+
4566+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
4567+
self.assertEqual(res, TIER2_THRESHOLD)
4568+
self.assertIsNotNone(ex)
4569+
uops = get_opnames(ex)
4570+
4571+
self.assertNotIn("_IS_NONE", uops)
4572+
self.assertIn("_GUARD_IS_NONE_POP", uops)
4573+
self.assertIn("_POP_TOP_NOP", uops)
4574+
45574575
def test_is_false_narrows_to_constant(self):
45584576
def f(n):
45594577
def return_false():

Python/optimizer_bytecodes.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,20 @@ dummy_func(void) {
710710
r = right;
711711
}
712712

713+
op(_IS_NONE, (value -- b)) {
714+
if (sym_is_const(ctx, value)) {
715+
PyObject *value_o = sym_get_const(ctx, value);
716+
assert(value_o != NULL);
717+
b = sym_new_const(ctx, Py_IsNone(value_o) ? Py_True : Py_False);
718+
}
719+
else if (sym_has_type(value)) {
720+
b = sym_new_const(ctx, sym_matches_type(value, &_PyNone_Type) ? Py_True : Py_False);
721+
}
722+
else {
723+
b = sym_new_type(ctx, &PyBool_Type);
724+
}
725+
}
726+
713727
op(_CONTAINS_OP, (left, right -- b, l, r)) {
714728
b = sym_new_type(ctx, &PyBool_Type);
715729
l = left;

Python/optimizer_cases.c.h

Lines changed: 13 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)