Skip to content

Commit a059e85

Browse files
authored
gh-131798: Add _IS_NONE to the JIT optimizer (GH-148369)
1 parent 1c89817 commit a059e85

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Lib/test/test_capi/test_opt.py

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

4665+
def test_is_none_narrows_to_constant(self):
4666+
def testfunc(n):
4667+
value = None
4668+
hits = 0
4669+
for _ in range(n):
4670+
if value is None:
4671+
hits += 1
4672+
return hits
4673+
4674+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
4675+
self.assertEqual(res, TIER2_THRESHOLD)
4676+
self.assertIsNotNone(ex)
4677+
uops = get_opnames(ex)
4678+
4679+
self.assertNotIn("_IS_NONE", uops)
4680+
self.assertIn("_GUARD_IS_NONE_POP", uops)
4681+
self.assertIn("_POP_TOP_NOP", uops)
4682+
46654683
def test_is_false_narrows_to_constant(self):
46664684
def f(n):
46674685
def return_false():

Python/optimizer_bytecodes.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,16 @@ dummy_func(void) {
724724
r = right;
725725
}
726726

727+
op(_IS_NONE, (value -- b)) {
728+
if (sym_is_const(ctx, value)) {
729+
PyObject *value_o = sym_get_const(ctx, value);
730+
b = sym_new_const(ctx, Py_IsNone(value_o) ? Py_True : Py_False);
731+
}
732+
else {
733+
b = sym_new_type(ctx, &PyBool_Type);
734+
}
735+
}
736+
727737
op(_CONTAINS_OP, (left, right -- b, l, r)) {
728738
b = sym_new_type(ctx, &PyBool_Type);
729739
l = left;

Python/optimizer_cases.c.h

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