mirror of
https://github.com/python/cpython.git
synced 2025-09-03 23:41:18 +00:00
GH-131798: Optimize away isinstance calls in the JIT (GH-134369)
This commit is contained in:
parent
fade146cfb
commit
484e00379b
9 changed files with 312 additions and 61 deletions
|
@ -5287,18 +5287,54 @@ dummy_func(
|
|||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
}
|
||||
|
||||
tier2 pure op (_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
|
||||
tier2 op(_POP_CALL, (callable, null --)) {
|
||||
(void)null; // Silence compiler warnings about unused variables
|
||||
DEAD(null);
|
||||
PyStackRef_CLOSE(callable);
|
||||
}
|
||||
|
||||
tier2 op(_POP_CALL_ONE, (callable, null, pop --)) {
|
||||
PyStackRef_CLOSE(pop);
|
||||
(void)null; // Silence compiler warnings about unused variables
|
||||
DEAD(null);
|
||||
PyStackRef_CLOSE(callable);
|
||||
}
|
||||
|
||||
tier2 op(_POP_CALL_TWO, (callable, null, pop1, pop2 --)) {
|
||||
PyStackRef_CLOSE(pop2);
|
||||
PyStackRef_CLOSE(pop1);
|
||||
(void)null; // Silence compiler warnings about unused variables
|
||||
DEAD(null);
|
||||
PyStackRef_CLOSE(callable);
|
||||
}
|
||||
|
||||
tier2 op(_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
|
||||
PyStackRef_CLOSE(pop);
|
||||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
}
|
||||
|
||||
tier2 pure op(_POP_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, pop1, pop2 -- value)) {
|
||||
tier2 op(_POP_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, pop1, pop2 -- value)) {
|
||||
PyStackRef_CLOSE(pop2);
|
||||
PyStackRef_CLOSE(pop1);
|
||||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
}
|
||||
|
||||
tier2 pure op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
|
||||
tier2 op(_POP_CALL_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null -- value)) {
|
||||
(void)null; // Silence compiler warnings about unused variables
|
||||
DEAD(null);
|
||||
PyStackRef_CLOSE(callable);
|
||||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
}
|
||||
|
||||
tier2 op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop -- value)) {
|
||||
PyStackRef_CLOSE(pop);
|
||||
(void)null; // Silence compiler warnings about unused variables
|
||||
DEAD(null);
|
||||
PyStackRef_CLOSE(callable);
|
||||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
}
|
||||
|
||||
tier2 op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
|
||||
PyStackRef_CLOSE(pop2);
|
||||
PyStackRef_CLOSE(pop1);
|
||||
(void)null; // Silence compiler warnings about unused variables
|
||||
|
|
110
Python/executor_cases.c.h
generated
110
Python/executor_cases.c.h
generated
|
@ -7030,6 +7030,69 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL: {
|
||||
_PyStackRef null;
|
||||
_PyStackRef callable;
|
||||
null = stack_pointer[-1];
|
||||
callable = stack_pointer[-2];
|
||||
(void)null;
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(callable);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_ONE: {
|
||||
_PyStackRef pop;
|
||||
_PyStackRef null;
|
||||
_PyStackRef callable;
|
||||
pop = stack_pointer[-1];
|
||||
null = stack_pointer[-2];
|
||||
callable = stack_pointer[-3];
|
||||
stack_pointer += -1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(pop);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
(void)null;
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(callable);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_TWO: {
|
||||
_PyStackRef pop2;
|
||||
_PyStackRef pop1;
|
||||
_PyStackRef null;
|
||||
_PyStackRef callable;
|
||||
pop2 = stack_pointer[-1];
|
||||
pop1 = stack_pointer[-2];
|
||||
null = stack_pointer[-3];
|
||||
callable = stack_pointer[-4];
|
||||
stack_pointer += -1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(pop2);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
stack_pointer += -1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(pop1);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
(void)null;
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(callable);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_TOP_LOAD_CONST_INLINE_BORROW: {
|
||||
_PyStackRef pop;
|
||||
_PyStackRef value;
|
||||
|
@ -7071,6 +7134,53 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_LOAD_CONST_INLINE_BORROW: {
|
||||
_PyStackRef null;
|
||||
_PyStackRef callable;
|
||||
_PyStackRef value;
|
||||
null = stack_pointer[-1];
|
||||
callable = stack_pointer[-2];
|
||||
PyObject *ptr = (PyObject *)CURRENT_OPERAND0();
|
||||
(void)null;
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(callable);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
stack_pointer[0] = value;
|
||||
stack_pointer += 1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW: {
|
||||
_PyStackRef pop;
|
||||
_PyStackRef null;
|
||||
_PyStackRef callable;
|
||||
_PyStackRef value;
|
||||
pop = stack_pointer[-1];
|
||||
null = stack_pointer[-2];
|
||||
callable = stack_pointer[-3];
|
||||
PyObject *ptr = (PyObject *)CURRENT_OPERAND0();
|
||||
stack_pointer += -1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(pop);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
(void)null;
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
PyStackRef_CLOSE(callable);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
value = PyStackRef_FromPyObjectBorrow(ptr);
|
||||
stack_pointer[0] = value;
|
||||
stack_pointer += 1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW: {
|
||||
_PyStackRef pop2;
|
||||
_PyStackRef pop1;
|
||||
|
|
|
@ -552,11 +552,13 @@ const uint16_t op_without_push[MAX_UOP_ID + 1] = {
|
|||
[_POP_TOP_LOAD_CONST_INLINE] = _POP_TOP,
|
||||
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = _POP_TOP,
|
||||
[_POP_TWO_LOAD_CONST_INLINE_BORROW] = _POP_TWO,
|
||||
[_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW] = _POP_CALL_TWO,
|
||||
};
|
||||
|
||||
const bool op_skip[MAX_UOP_ID + 1] = {
|
||||
[_NOP] = true,
|
||||
[_CHECK_VALIDITY] = true,
|
||||
[_SET_IP] = true,
|
||||
};
|
||||
|
||||
const uint16_t op_without_pop[MAX_UOP_ID + 1] = {
|
||||
|
@ -565,6 +567,15 @@ const uint16_t op_without_pop[MAX_UOP_ID + 1] = {
|
|||
[_POP_TOP_LOAD_CONST_INLINE_BORROW] = _LOAD_CONST_INLINE_BORROW,
|
||||
[_POP_TWO] = _POP_TOP,
|
||||
[_POP_TWO_LOAD_CONST_INLINE_BORROW] = _POP_TOP_LOAD_CONST_INLINE_BORROW,
|
||||
[_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW] = _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
|
||||
[_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW] = _POP_CALL_LOAD_CONST_INLINE_BORROW,
|
||||
[_POP_CALL_TWO] = _POP_CALL_ONE,
|
||||
[_POP_CALL_ONE] = _POP_CALL,
|
||||
};
|
||||
|
||||
const uint16_t op_without_pop_null[MAX_UOP_ID + 1] = {
|
||||
[_POP_CALL] = _POP_TOP,
|
||||
[_POP_CALL_LOAD_CONST_INLINE_BORROW] = _POP_TOP_LOAD_CONST_INLINE_BORROW,
|
||||
};
|
||||
|
||||
|
||||
|
@ -601,19 +612,29 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
|
|||
// _LOAD_FAST + _POP_TWO_LOAD_CONST_INLINE_BORROW + _POP_TOP
|
||||
// ...becomes:
|
||||
// _NOP + _POP_TOP + _NOP
|
||||
while (op_without_pop[opcode]) {
|
||||
while (op_without_pop[opcode] || op_without_pop_null[opcode]) {
|
||||
_PyUOpInstruction *last = &buffer[pc - 1];
|
||||
while (op_skip[last->opcode]) {
|
||||
last--;
|
||||
}
|
||||
if (!op_without_push[last->opcode]) {
|
||||
break;
|
||||
if (op_without_push[last->opcode]) {
|
||||
last->opcode = op_without_push[last->opcode];
|
||||
opcode = buffer[pc].opcode = op_without_pop[opcode];
|
||||
if (op_without_pop[last->opcode]) {
|
||||
opcode = last->opcode;
|
||||
pc = last - buffer;
|
||||
}
|
||||
}
|
||||
last->opcode = op_without_push[last->opcode];
|
||||
opcode = buffer[pc].opcode = op_without_pop[opcode];
|
||||
if (op_without_pop[last->opcode]) {
|
||||
opcode = last->opcode;
|
||||
pc = last - buffer;
|
||||
else if (last->opcode == _PUSH_NULL) {
|
||||
// Handle _POP_CALL and _POP_CALL_LOAD_CONST_INLINE_BORROW separately.
|
||||
// This looks for a preceding _PUSH_NULL instruction and
|
||||
// simplifies to _POP_TOP(_LOAD_CONST_INLINE_BORROW).
|
||||
last->opcode = _NOP;
|
||||
opcode = buffer[pc].opcode = op_without_pop_null[opcode];
|
||||
assert(opcode);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* _PUSH_FRAME doesn't escape or error, but it
|
||||
|
|
|
@ -551,6 +551,14 @@ dummy_func(void) {
|
|||
value = sym_new_const(ctx, ptr);
|
||||
}
|
||||
|
||||
op(_POP_CALL_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused -- value)) {
|
||||
value = sym_new_const(ctx, ptr);
|
||||
}
|
||||
|
||||
op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused -- value)) {
|
||||
value = sym_new_const(ctx, ptr);
|
||||
}
|
||||
|
||||
op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused, unused -- value)) {
|
||||
value = sym_new_const(ctx, ptr);
|
||||
}
|
||||
|
|
38
Python/optimizer_cases.c.h
generated
38
Python/optimizer_cases.c.h
generated
|
@ -2601,6 +2601,24 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL: {
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_ONE: {
|
||||
stack_pointer += -3;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_TWO: {
|
||||
stack_pointer += -4;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_TOP_LOAD_CONST_INLINE_BORROW: {
|
||||
JitOptSymbol *value;
|
||||
PyObject *ptr = (PyObject *)this_instr->operand0;
|
||||
|
@ -2618,6 +2636,26 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_LOAD_CONST_INLINE_BORROW: {
|
||||
JitOptSymbol *value;
|
||||
PyObject *ptr = (PyObject *)this_instr->operand0;
|
||||
value = sym_new_const(ctx, ptr);
|
||||
stack_pointer[-2] = value;
|
||||
stack_pointer += -1;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW: {
|
||||
JitOptSymbol *value;
|
||||
PyObject *ptr = (PyObject *)this_instr->operand0;
|
||||
value = sym_new_const(ctx, ptr);
|
||||
stack_pointer[-3] = value;
|
||||
stack_pointer += -2;
|
||||
assert(WITHIN_STACK_BOUNDS());
|
||||
break;
|
||||
}
|
||||
|
||||
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW: {
|
||||
JitOptSymbol *value;
|
||||
PyObject *ptr = (PyObject *)this_instr->operand0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue