Skip to content

Commit a32b2c2

Browse files
committed
Optimize _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW
1 parent 1a07a01 commit a32b2c2

File tree

6 files changed

+180
-49
lines changed

6 files changed

+180
-49
lines changed

Include/internal/pycore_uop_ids.h

Lines changed: 46 additions & 43 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: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5287,18 +5287,39 @@ dummy_func(
52875287
value = PyStackRef_FromPyObjectBorrow(ptr);
52885288
}
52895289

5290-
tier2 pure op (_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
5290+
tier2 op(_POP_CALL, (callable, null --)) {
5291+
(void)null; // Silence compiler warnings about unused variables
5292+
DEAD(null);
5293+
PyStackRef_CLOSE(callable);
5294+
}
5295+
5296+
tier2 op(_POP_CALL_ONE, (callable, null, pop --)) {
5297+
PyStackRef_CLOSE(pop);
5298+
(void)null; // Silence compiler warnings about unused variables
5299+
DEAD(null);
5300+
PyStackRef_CLOSE(callable);
5301+
}
5302+
5303+
tier2 op(_POP_CALL_TWO, (callable, null, pop1, pop2 --)) {
5304+
PyStackRef_CLOSE(pop2);
5305+
PyStackRef_CLOSE(pop1);
5306+
(void)null; // Silence compiler warnings about unused variables
5307+
DEAD(null);
5308+
PyStackRef_CLOSE(callable);
5309+
}
5310+
5311+
tier2 op(_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
52915312
PyStackRef_CLOSE(pop);
52925313
value = PyStackRef_FromPyObjectBorrow(ptr);
52935314
}
52945315

5295-
tier2 pure op(_POP_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, pop1, pop2 -- value)) {
5316+
tier2 op(_POP_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, pop1, pop2 -- value)) {
52965317
PyStackRef_CLOSE(pop2);
52975318
PyStackRef_CLOSE(pop1);
52985319
value = PyStackRef_FromPyObjectBorrow(ptr);
52995320
}
53005321

5301-
tier2 pure op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
5322+
tier2 op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
53025323
PyStackRef_CLOSE(pop2);
53035324
PyStackRef_CLOSE(pop1);
53045325
(void)null; // Silence compiler warnings about unused variables

Python/executor_cases.c.h

Lines changed: 63 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,17 @@ const uint16_t op_without_push[MAX_UOP_ID + 1] = {
533533
[_POP_TOP_LOAD_CONST_INLINE] = _POP_TOP,
534534
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = _POP_TOP,
535535
[_POP_TWO_LOAD_CONST_INLINE_BORROW] = _POP_TWO,
536+
[_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW] = _POP_CALL_TWO,
536537
};
537538

538539
const uint16_t op_without_pop[MAX_UOP_ID + 1] = {
539540
[_POP_TOP] = _NOP,
540541
[_POP_TOP_LOAD_CONST_INLINE] = _LOAD_CONST_INLINE,
541542
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = _LOAD_CONST_INLINE_BORROW,
542543
[_POP_TWO_LOAD_CONST_INLINE_BORROW] = _POP_TOP_LOAD_CONST_INLINE_BORROW,
544+
[_POP_CALL_TWO] = _POP_CALL_ONE,
545+
[_POP_CALL_ONE] = _POP_CALL,
546+
// [_POP_CALL] = _POP_TOP, # TOS is NULL, this is handled separately in remove_unneeded_uops
543547
};
544548

545549

@@ -587,6 +591,16 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
587591
last->opcode = op_without_push[last->opcode];
588592
opcode = buffer[pc].opcode = op_without_pop[opcode];
589593
}
594+
if (opcode == _POP_CALL) {
595+
_PyUOpInstruction *last = &buffer[pc - 1];
596+
while (last->opcode == _NOP) {
597+
last--;
598+
}
599+
if (last->opcode == _PUSH_NULL) {
600+
last->opcode = _NOP;
601+
opcode = buffer[pc].opcode = _POP_TOP;
602+
}
603+
}
590604
/* _PUSH_FRAME doesn't escape or error, but it
591605
* does need the IP for the return address */
592606
bool needs_ip = opcode == _PUSH_FRAME;

Python/optimizer_cases.c.h

Lines changed: 18 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)