Skip to content

Commit 0be7147

Browse files
committed
add peephole helper
1 parent cfe3e07 commit 0be7147

2 files changed

Lines changed: 115 additions & 16 deletions

File tree

‎Lib/test/test_capi/test_opt.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5582,8 +5582,7 @@ def testfunc(*args):
55825582
uops = get_opnames(ex)
55835583

55845584
self.assertIn("_BINARY_OP_SUBSCR_INIT_CALL", uops)
5585-
# _POP_TOP_NOP is a sign the optimizer ran and didn't hit contradiction.
5586-
self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 1)
5585+
self.assertNotIn("_POP_TOP_NOP", uops)
55875586

55885587
def test_load_attr_property_frame(self):
55895588
class B:
@@ -6215,6 +6214,39 @@ def __exit__(self, e, v, t): ...
62156214
f1()
62166215
"""), PYTHON_JIT="1")
62176216

6217+
def test_peephole_cancels_adjacent_push_pop(self):
6218+
def negated_constant(n):
6219+
x = 0
6220+
for i in range(n):
6221+
a = 1
6222+
result = -a
6223+
if result < 0:
6224+
x += 1
6225+
return x
6226+
6227+
res, ex = self._run_with_optimizer(negated_constant, TIER2_THRESHOLD)
6228+
self.assertEqual(res, TIER2_THRESHOLD)
6229+
self.assertIsNotNone(ex)
6230+
uops = get_opnames(ex)
6231+
self.assertNotIn("_SWAP", uops)
6232+
self.assertNotIn("_SWAP_2", uops)
6233+
6234+
def test_peephole_removes_redundant_rrot(self):
6235+
def add(a, b):
6236+
return a + b
6237+
6238+
def testfunc(n):
6239+
x = 0
6240+
for _ in range(n):
6241+
x += add(1, 2)
6242+
return x
6243+
6244+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
6245+
self.assertEqual(res, 3 * TIER2_THRESHOLD)
6246+
self.assertIsNotNone(ex)
6247+
uops = get_opnames(ex)
6248+
self.assertNotIn("_RROT_3", uops)
6249+
62186250
def global_identity(x):
62196251
return x
62206252

‎Python/optimizer_analysis.c‎

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,80 @@ const uint16_t op_without_pop[MAX_UOP_ID + 1] = {
719719
[_POP_TOP_UNICODE] = _NOP,
720720
};
721721

722+
static int
723+
previous_non_skip_uop(_PyUOpInstruction *buffer, int pc)
724+
{
725+
while (pc >= 0 && op_skip[buffer[pc].opcode]) {
726+
pc--;
727+
}
728+
return pc;
729+
}
730+
731+
/* Remove redundant stack shuffles left by constant-folding rewrites:
732+
* push push _SWAP(2) pop
733+
* push push push _RROT_3 pop pop
734+
* In both forms, only the push closest to the shuffle survives. */
735+
static bool
736+
remove_folded_stack_shuffle(_PyUOpInstruction *buffer, int pc, int arity)
737+
{
738+
int pops[2];
739+
int idx = pc;
740+
for (int i = 0; i < arity - 1; i++) {
741+
if (idx < 0 || !op_without_pop[buffer[idx].opcode]) {
742+
return false;
743+
}
744+
pops[i] = idx;
745+
idx = previous_non_skip_uop(buffer, idx - 1);
746+
}
747+
int shuf = idx;
748+
if (shuf < 0) {
749+
return false;
750+
}
751+
uint16_t shuf_op = buffer[shuf].opcode;
752+
bool is_shuffle =
753+
(arity == 2 && shuf_op == _SWAP && buffer[shuf].oparg == 2) ||
754+
(arity == 3 && shuf_op == _RROT_3);
755+
if (!is_shuffle) {
756+
return false;
757+
}
758+
759+
int dead_pushes[2];
760+
for (int i = 0; i < arity; i++) {
761+
idx = previous_non_skip_uop(buffer, idx - 1);
762+
if (idx < 0) {
763+
return false;
764+
}
765+
uint16_t push_op = buffer[idx].opcode;
766+
if (push_op == _COPY || !op_without_push[push_op]) {
767+
return false;
768+
}
769+
if (i > 0) {
770+
dead_pushes[i - 1] = idx;
771+
}
772+
}
773+
774+
for (int i = 0; i < arity - 1; i++) {
775+
buffer[dead_pushes[i]].opcode = _NOP;
776+
}
777+
buffer[shuf].opcode = _NOP;
778+
for (int i = 0; i < arity - 1; i++) {
779+
buffer[pops[i]].opcode = _NOP;
780+
}
781+
return true;
782+
}
783+
784+
static bool
785+
remove_adjacent_push_pop(_PyUOpInstruction *buffer, int pc)
786+
{
787+
int last = previous_non_skip_uop(buffer, pc - 1);
788+
if (last < 0 || !op_without_push[buffer[last].opcode]) {
789+
return false;
790+
}
791+
buffer[last].opcode = _NOP;
792+
buffer[pc].opcode = _NOP;
793+
return true;
794+
}
795+
722796

723797
static int
724798
remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
@@ -755,21 +829,14 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
755829
// ...becomes:
756830
// _NOP + _NOP + _POP_TOP + _NOP + _NOP
757831
while (op_without_pop[opcode]) {
758-
_PyUOpInstruction *last = &buffer[pc - 1];
759-
while (op_skip[last->opcode]) {
760-
last--;
761-
}
762-
if (op_without_push[last->opcode] && op_without_pop[opcode]) {
763-
last->opcode = op_without_push[last->opcode];
764-
opcode = buffer[pc].opcode = op_without_pop[opcode];
765-
if (op_without_pop[last->opcode]) {
766-
opcode = last->opcode;
767-
pc = (int)(last - buffer);
768-
}
769-
}
770-
else {
771-
break;
832+
if (remove_folded_stack_shuffle(buffer, pc, 2) ||
833+
remove_folded_stack_shuffle(buffer, pc, 3) ||
834+
remove_adjacent_push_pop(buffer, pc))
835+
{
836+
opcode = buffer[pc].opcode;
837+
continue;
772838
}
839+
break;
773840
}
774841
/* _PUSH_FRAME doesn't escape or error, but it
775842
* does need the IP for the return address */

0 commit comments

Comments
 (0)