Skip to content

Commit 67f42ab

Browse files
committed
Optimize _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW
1 parent b430e92 commit 67f42ab

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
@@ -5293,6 +5293,22 @@ dummy_func(
52935293
value = PyStackRef_FromPyObjectBorrow(ptr);
52945294
}
52955295

5296+
tier2 pure op(_POP_TWO, (unused, unused --)) {
5297+
// noop
5298+
}
5299+
5300+
tier2 pure op(_POP_THREE, (unused, unused, unused --)) {
5301+
// noop
5302+
}
5303+
5304+
tier2 pure op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop -- value)) {
5305+
PyStackRef_CLOSE(pop);
5306+
(void)null; // Silence compiler warnings about unused variables
5307+
DEAD(null);
5308+
PyStackRef_CLOSE(callable);
5309+
value = PyStackRef_FromPyObjectImmortal(ptr);
5310+
}
5311+
52965312
tier2 pure op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
52975313
PyStackRef_CLOSE(pop2);
52985314
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
@@ -536,6 +536,10 @@ dummy_func(void) {
536536
value = sym_new_const(ctx, ptr);
537537
}
538538

539+
op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused -- value)) {
540+
value = sym_new_const(ctx, ptr);
541+
}
542+
539543
op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused, unused -- value)) {
540544
value = sym_new_const(ctx, ptr);
541545
}

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)