Skip to content

Commit 6dde5ca

Browse files
committed
Optimize _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW
1 parent f3acbb7 commit 6dde5ca

File tree

8 files changed

+168
-48
lines changed

8 files changed

+168
-48
lines changed

Include/internal/pycore_uop_ids.h

Lines changed: 45 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ def testfunc(n):
19581958
self.assertNotIn("_CALL_ISINSTANCE", uops)
19591959
self.assertNotIn("_GUARD_THIRD_NULL", uops)
19601960
self.assertNotIn("_GUARD_CALLABLE_ISINSTANCE", uops)
1961-
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)
1961+
self.assertIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
19621962

19631963
def test_call_list_append(self):
19641964
def testfunc(n):
@@ -1991,7 +1991,7 @@ def testfunc(n):
19911991
self.assertNotIn("_CALL_ISINSTANCE", uops)
19921992
self.assertNotIn("_TO_BOOL_BOOL", uops)
19931993
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
1994-
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)
1994+
self.assertIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
19951995

19961996
def test_call_isinstance_is_false(self):
19971997
def testfunc(n):
@@ -2009,7 +2009,7 @@ def testfunc(n):
20092009
self.assertNotIn("_CALL_ISINSTANCE", uops)
20102010
self.assertNotIn("_TO_BOOL_BOOL", uops)
20112011
self.assertNotIn("_GUARD_IS_FALSE_POP", uops)
2012-
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)
2012+
self.assertIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
20132013

20142014
def test_call_isinstance_subclass(self):
20152015
def testfunc(n):
@@ -2027,7 +2027,7 @@ def testfunc(n):
20272027
self.assertNotIn("_CALL_ISINSTANCE", uops)
20282028
self.assertNotIn("_TO_BOOL_BOOL", uops)
20292029
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
2030-
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)
2030+
self.assertIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
20312031

20322032
def test_call_isinstance_unknown_object(self):
20332033
def testfunc(n):

Python/bytecodes.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5342,6 +5342,22 @@ dummy_func(
53425342
value = PyStackRef_FromPyObjectImmortal(ptr);
53435343
}
53445344

5345+
tier2 pure op(_POP_TWO, (unused, unused --)) {
5346+
// noop
5347+
}
5348+
5349+
tier2 pure op(_POP_THREE, (unused, unused, unused --)) {
5350+
// noop
5351+
}
5352+
5353+
tier2 pure op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop -- value)) {
5354+
PyStackRef_CLOSE(pop);
5355+
(void)null; // Silence compiler warnings about unused variables
5356+
DEAD(null);
5357+
PyStackRef_CLOSE(callable);
5358+
value = PyStackRef_FromPyObjectImmortal(ptr);
5359+
}
5360+
53455361
tier2 pure op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
53465362
PyStackRef_CLOSE(pop2);
53475363
PyStackRef_CLOSE(pop1);

Python/executor_cases.c.h

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_analysis.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,26 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
552552
}
553553
break;
554554
case _POP_TOP:
555+
case _POP_TWO:
556+
case _POP_THREE:
555557
case _POP_TOP_LOAD_CONST_INLINE:
556558
case _POP_TOP_LOAD_CONST_INLINE_BORROW:
557559
case _POP_TWO_LOAD_CONST_INLINE_BORROW:
560+
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW:
561+
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW:
558562
optimize_pop_top_again:
559563
{
560564
_PyUOpInstruction *last = &buffer[pc-1];
561565
while (last->opcode == _NOP) {
562566
last--;
563567
}
564568
switch (last->opcode) {
569+
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW:
570+
last->opcode = _POP_THREE;
571+
break;
572+
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW:
573+
last->opcode = _POP_TWO;
574+
break;
565575
case _POP_TWO_LOAD_CONST_INLINE_BORROW:
566576
last->opcode = _POP_TOP;
567577
break;
@@ -579,17 +589,31 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
579589
if (opcode == _POP_TOP) {
580590
opcode = buffer[pc].opcode = _NOP;
581591
}
592+
else if (opcode == _POP_TWO) {
593+
opcode = buffer[pc].opcode = _POP_TOP;
594+
}
595+
else if (opcode == _POP_THREE) {
596+
opcode = buffer[pc].opcode = _POP_TWO;
597+
}
582598
else if (opcode == _POP_TOP_LOAD_CONST_INLINE) {
583599
opcode = buffer[pc].opcode = _LOAD_CONST_INLINE;
584600
}
585601
else if (opcode == _POP_TOP_LOAD_CONST_INLINE_BORROW) {
586602
opcode = buffer[pc].opcode = _LOAD_CONST_INLINE_BORROW;
587603
}
588-
else {
589-
assert(opcode == _POP_TWO_LOAD_CONST_INLINE_BORROW);
604+
else if (opcode == _POP_TWO_LOAD_CONST_INLINE_BORROW) {
590605
opcode = buffer[pc].opcode = _POP_TOP_LOAD_CONST_INLINE_BORROW;
591606
goto optimize_pop_top_again;
592607
}
608+
else if (opcode == _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW) {
609+
opcode = buffer[pc].opcode = _POP_TWO_LOAD_CONST_INLINE_BORROW;
610+
goto optimize_pop_top_again;
611+
}
612+
else {
613+
assert(opcode == _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW);
614+
opcode = buffer[pc].opcode = _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW;
615+
goto optimize_pop_top_again;
616+
}
593617
}
594618
_Py_FALLTHROUGH;
595619
}

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ dummy_func(void) {
550550
value = sym_new_const(ctx, ptr);
551551
}
552552

553+
op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused -- value)) {
554+
value = sym_new_const(ctx, ptr);
555+
}
556+
553557
op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused, unused -- value)) {
554558
value = sym_new_const(ctx, ptr);
555559
}

Python/optimizer_cases.c.h

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)