Skip to content

gh-106529: Generate uops for POP_JUMP_IF_[NOT_]NONE #106796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2023
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
30 changes: 30 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,36 @@ def testfunc(n):
uops = {opname for opname, _ in ex}
self.assertIn("_POP_JUMP_IF_FALSE", uops)

def test_pop_jump_if_none(self):
def testfunc(a):
for x in a:
if x is None:
x = 0

opt = _testinternalcapi.get_uop_optimizer()
with temporary_optimizer(opt):
testfunc([1, 2, 3])

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = {opname for opname, _ in ex}
self.assertIn("_POP_JUMP_IF_TRUE", uops)

def test_pop_jump_if_not_none(self):
def testfunc(a):
for x in a:
if x is not None:
x = 0

opt = _testinternalcapi.get_uop_optimizer()
with temporary_optimizer(opt):
testfunc([1, 2, 3])

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = {opname for opname, _ in ex}
self.assertIn("_POP_JUMP_IF_FALSE", uops)

def test_pop_jump_if_true(self):
def testfunc(n):
i = 0
Expand Down
17 changes: 17 additions & 0 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,26 @@ translate_bytecode_to_trace(

switch (opcode) {

case POP_JUMP_IF_NONE:
{
RESERVE(2, 2);
ADD_TO_TRACE(IS_NONE, 0);
opcode = POP_JUMP_IF_TRUE;
goto pop_jump_if_bool;
}

case POP_JUMP_IF_NOT_NONE:
{
RESERVE(2, 2);
ADD_TO_TRACE(IS_NONE, 0);
opcode = POP_JUMP_IF_FALSE;
goto pop_jump_if_bool;
}

case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
{
pop_jump_if_bool:
// Assume jump unlikely (TODO: handle jump likely case)
RESERVE(1, 2);
_Py_CODEUNIT *target_instr =
Expand Down