Skip to content
Closed
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
Next Next commit
Optimize bytecode multiple times until it cannot be optimized further
  • Loading branch information
pablogsal committed Sep 9, 2019
commit b90592b182ae0bf51dacbd4e57b05262e952517f
24 changes: 15 additions & 9 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,13 @@ def test_elim_jump_to_uncond_jump(self):
# POP_JUMP_IF_FALSE to JUMP_FORWARD --> POP_JUMP_IF_FALSE to non-jump
def f():
if a:
# Intentionally use two-line expression to test issue37213.
if (c
or d):
foo()
if b:
# Intentionally use two-line expression to test issue37213.
if (c
or d):
foo()
else:
bar()
else:
baz()
self.check_jump_targets(f)
Expand All @@ -307,10 +310,13 @@ def test_elim_jump_to_uncond_jump2(self):
# POP_JUMP_IF_FALSE to JUMP_ABSOLUTE --> POP_JUMP_IF_FALSE to non-jump
def f():
while a:
# Intentionally use two-line expression to test issue37213.
if (c
or d):
a = foo()
if b:
# Intentionally use two-line expression to test issue37213.
if (c
or d):
a = foo()
else:
a = bar()
self.check_jump_targets(f)

def test_elim_jump_to_uncond_jump3(self):
Expand Down Expand Up @@ -370,7 +376,7 @@ def f(cond1, cond2):
# There should be one jump for the while loop.
returns = [instr for instr in dis.get_instructions(f)
if instr.opname == 'JUMP_ABSOLUTE']
self.assertEqual(len(returns), 1)
self.assertEqual(len(returns), 0)
returns = [instr for instr in dis.get_instructions(f)
if instr.opname == 'RETURN_VALUE']
self.assertLessEqual(len(returns), 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The peephole optimizer now does multiple runs until the resulting bytecode
cannot be optimized further. Patch by Pablo Galindo.
76 changes: 55 additions & 21 deletions Python/peephole.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,11 @@ markblocks(_Py_CODEUNIT *code, Py_ssize_t len)
return blocks;
}

/* Perform basic peephole optimizations to components of a code object.
The consts object should still be in list form to allow new constants
to be appended.

To keep the optimizer simple, it bails when the lineno table has complex
encoding for gaps >= 255.

Optimizations are restricted to simple transformations occurring within a
single basic block. All transformations keep the code size the same or
smaller. For those that reduce size, the gaps are initially filled with
NOPs. Later those NOPs are removed and the jump addresses retargeted in
a single pass. */

PyObject *
PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
PyObject *lnotab_obj)
static PyObject *
optimize_bytecode_once(PyObject *code, PyObject* consts, PyObject *names,
PyObject *lnotab_obj)
{
Py_ssize_t h, i, nexti, op_start, tgt;
Py_ssize_t h, i, nexti, op_start, tgt, index;
unsigned int j, nops;
unsigned char opcode, nextop;
_Py_CODEUNIT *codestr = NULL;
Expand Down Expand Up @@ -474,10 +461,14 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
unsigned int offset_delta, new_offset;
cum_orig_offset += lnotab[i];
assert(cum_orig_offset % sizeof(_Py_CODEUNIT) == 0);
new_offset = blocks[cum_orig_offset / sizeof(_Py_CODEUNIT)] *
sizeof(_Py_CODEUNIT);
offset_delta = new_offset - last_offset;
assert(offset_delta <= 255);
index = cum_orig_offset / sizeof(_Py_CODEUNIT);
Comment thread
pablogsal marked this conversation as resolved.
if (index >= codelen) {
continue;
} else {
new_offset = blocks[index] * sizeof(_Py_CODEUNIT);
offset_delta = new_offset - last_offset;
assert(offset_delta < 255);
}
lnotab[i] = (unsigned char)offset_delta;
last_offset = new_offset;
}
Expand Down Expand Up @@ -536,3 +527,46 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
PyMem_Free(codestr);
return code;
}

/* Perform basic peephole optimizations to components of a code object.
The consts object should still be in list form to allow new constants
to be appended.

To keep the optimizer simple, it bails when the lineno table has complex
encoding for gaps >= 255.

Optimizations are restricted to simple transformations occurring within a
single basic block. All transformations keep the code size the same or
smaller. For those that reduce size, the gaps are initially filled with
NOPs. Later those NOPs are removed and the jump addresses retargeted in
a single pass. */

PyObject *
PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
Comment thread
pablogsal marked this conversation as resolved.
Outdated
PyObject *lnotab_obj)
{
PyObject* bytecode = code;
PyObject* old_bytecode = NULL;
PyObject* tmp = NULL;
int compare_bytecode;
do {
old_bytecode = PyBytes_FromStringAndSize(PyBytes_AsString(bytecode),
PyBytes_GET_SIZE(bytecode));
tmp = bytecode;
bytecode = optimize_bytecode_once(bytecode, consts, names, lnotab_obj);
if (tmp != code) {
Py_DECREF(tmp);
}
if (!bytecode) {
Py_DECREF(old_bytecode);
return NULL;
}
compare_bytecode = PyObject_RichCompareBool(old_bytecode, bytecode, Py_NE);
Py_DECREF(old_bytecode);
if (compare_bytecode == -1) {
break;
}
} while (compare_bytecode);

return bytecode;
}