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
Add POP_JUMP_IF_TRUE; drop NONE variants
  • Loading branch information
gvanrossum committed Jul 10, 2023
commit 11d0e11889b6445d2fe3531aa3070f40a7006f1b
16 changes: 16 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,22 @@ def testfunc(n):
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
while not i >= n:
i += 1

opt = _testinternalcapi.get_uop_optimizer()

with temporary_optimizer(opt):
testfunc(10)

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


if __name__ == "__main__":
unittest.main()
22 changes: 0 additions & 22 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2787,28 +2787,6 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
break;
}

case _POP_JUMP_IF_NONE:
{
if (Py_IsNone(stack_pointer[-1])) {
pc = oparg;
}
else {
Py_DECREF(stack_pointer[-1]);
}
stack_pointer--;
break;
}

case _POP_JUMP_IF_NOT_NONE:
{
if (!Py_IsNone(stack_pointer[-1])) {
pc = oparg;
Py_DECREF(stack_pointer[-1]);
}
stack_pointer--;
break;
}

case SAVE_IP:
{
frame->prev_instr = ip_offset + oparg;
Expand Down
52 changes: 24 additions & 28 deletions Python/opcode_metadata.h

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

14 changes: 10 additions & 4 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,25 +428,31 @@ translate_bytecode_to_trace(
oparg = (oparg << 8) | instr->op.arg;
}
if (opcode == ENTER_EXECUTOR) {
_PyExecutorObject *executor = (_PyExecutorObject *)code->co_executors->executors[oparg&255];
_PyExecutorObject *executor =
(_PyExecutorObject *)code->co_executors->executors[oparg&255];
opcode = executor->vm_data.opcode;
DPRINTF(2, " * ENTER_EXECUTOR -> %s\n", _PyOpcode_OpName[opcode]);
oparg = (oparg & 0xffffff00) | executor->vm_data.oparg;
}
switch (opcode) {

case POP_JUMP_IF_FALSE:
case POP_JUMP_IF_TRUE:
{
// Assume jump unlikely (TODO: handle jump likely case)
// Reserve 5 entries (1 here, 2 stub, plus SAVE_IP + EXIT_TRACE)
if (trace_length + 5 > max_length) {
DPRINTF(1, "Ran out of space for POP_JUMP_IF_FALSE\n");
goto done;
}
_Py_CODEUNIT *target_instr = instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]] + oparg;
_Py_CODEUNIT *target_instr =
instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]] + oparg;
max_length -= 2; // Really the start of the stubs
ADD_TO_TRACE(_POP_JUMP_IF_FALSE, max_length);
ADD_TO_STUB(max_length, SAVE_IP, target_instr - (_Py_CODEUNIT *)code->co_code_adaptive);
int uopcode = opcode == POP_JUMP_IF_TRUE ?
_POP_JUMP_IF_TRUE : _POP_JUMP_IF_FALSE;
ADD_TO_TRACE(uopcode, max_length);
ADD_TO_STUB(max_length, SAVE_IP,
target_instr - (_Py_CODEUNIT *)code->co_code_adaptive);
ADD_TO_STUB(max_length + 1, EXIT_TRACE, 0);
break;
}
Expand Down
2 changes: 0 additions & 2 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,8 +1348,6 @@ def add(name: str) -> None:
add("SAVE_IP")
add("_POP_JUMP_IF_FALSE")
add("_POP_JUMP_IF_TRUE")
add("_POP_JUMP_IF_NONE")
add("_POP_JUMP_IF_NOT_NONE")

for instr in self.instrs.values():
if instr.kind == "op" and instr.is_viable_uop():
Expand Down