Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4554,6 +4554,24 @@ def return_true():
# v + 1 should be constant folded
self.assertNotIn("_BINARY_OP", uops)

def test_is_none_narrows_to_constant(self):
def testfunc(n):
value = None
hits = 0
for _ in range(n):
if value is None:
hits += 1
return hits

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertNotIn("_IS_NONE", uops)
self.assertIn("_GUARD_IS_NONE_POP", uops)
self.assertIn("_POP_TOP_NOP", uops)

def test_is_false_narrows_to_constant(self):
def f(n):
def return_false():
Expand Down
10 changes: 10 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,16 @@ dummy_func(void) {
r = right;
}

op(_IS_NONE, (value -- b)) {
if (sym_is_const(ctx, value)) {
PyObject *value_o = sym_get_const(ctx, value);
b = sym_new_const(ctx, Py_IsNone(value_o) ? Py_True : Py_False);
}
else {
b = sym_new_type(ctx, &PyBool_Type);
}
}
Comment on lines +714 to +721
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace this whole chunk with

        b = sym_new_type(ctx, &PyBool_Type);
        REPLACE_OPCODE_IF_EVALUATES_PURE(value, b);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Currently, I have the following code in my local environment:

    op(_IS_NONE, (value -- b)) {
        b = sym_new_type(ctx, &PyBool_Type);
        REPLACE_OPCODE_IF_EVALUATES_PURE(value, b);
    }

However, when running optimizer_generator.py, it throws the following error:

File "E:/cpython/Python/bytecodes.c", line 3519

                ^
SyntaxError: 'value' is cleared on some paths, but not all

next step, should I replace

cpython/Python/bytecodes.c

Lines 3511 to 3520 in 72006a7

op(_IS_NONE, (value -- b)) {
if (PyStackRef_IsNone(value)) {
b = PyStackRef_True;
DEAD(value);
}
else {
b = PyStackRef_False;
DECREF_INPUTS();
}
}

with the following?

        op(_IS_NONE, (value -- b)) {
            b = PyStackRef_IsNone(value) ? PyStackRef_True : PyStackRef_False;
            DECREF_INPUTS();
        }

I apologize if this is a basic question

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you're in the right file? You should be editing optimizer_bytecodes.c not bytecodes.c

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, looks this😥
image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably a bug in the cases generator then. Let's just use your current approach for now but fixed.


op(_CONTAINS_OP, (left, right -- b, l, r)) {
b = sym_new_type(ctx, &PyBool_Type);
l = left;
Expand Down
10 changes: 9 additions & 1 deletion Python/optimizer_cases.c.h

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

Loading