gh-148211: decompose [_POP_CALL_X/_SHUFFLE_2]_LOAD_CONST_INLINE_BORROW in JIT (GH-148313)

This commit is contained in:
Neko Asakura 2026-04-10 21:57:01 +08:00 committed by GitHub
parent d3b7b93cbb
commit aea0b91d65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 1040 additions and 1411 deletions

View file

@ -5936,21 +5936,6 @@ dummy_func(
value = PyStackRef_FromPyObjectBorrow(ptr);
}
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(_INSERT_2_LOAD_CONST_INLINE_BORROW, (ptr/4, left, right -- res, l, r)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
l = left;
@ -5958,12 +5943,6 @@ dummy_func(
INPUTS_DEAD();
}
tier2 op(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
INPUTS_DEAD();
}
tier2 op(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a, c)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
@ -5971,15 +5950,6 @@ dummy_func(
INPUTS_DEAD();
}
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
DEAD(null);
PyStackRef_CLOSE(callable);
value = PyStackRef_FromPyObjectBorrow(ptr);
}
tier2 op(_START_EXECUTOR, (executor/4 --)) {
#ifndef _Py_JIT
assert(current_executor == (_PyExecutorObject*)executor);

View file

@ -21413,66 +21413,6 @@
break;
}
case _POP_CALL_LOAD_CONST_INLINE_BORROW_r21: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef null;
_PyStackRef callable;
_PyStackRef value;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
null = _stack_item_1;
callable = _stack_item_0;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
(void)null;
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(callable);
stack_pointer = _PyFrame_GetStackPointer(frame);
value = PyStackRef_FromPyObjectBorrow(ptr);
_tos_cache0 = value;
_tos_cache1 = PyStackRef_ZERO_BITS;
_tos_cache2 = PyStackRef_ZERO_BITS;
SET_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW_r31: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef pop;
_PyStackRef null;
_PyStackRef callable;
_PyStackRef value;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
_PyStackRef _stack_item_2 = _tos_cache2;
pop = _stack_item_2;
null = _stack_item_1;
callable = _stack_item_0;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
stack_pointer[0] = callable;
stack_pointer[1] = null;
stack_pointer += 2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(pop);
stack_pointer = _PyFrame_GetStackPointer(frame);
(void)null;
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(callable);
stack_pointer = _PyFrame_GetStackPointer(frame);
value = PyStackRef_FromPyObjectBorrow(ptr);
_tos_cache0 = value;
_tos_cache1 = PyStackRef_ZERO_BITS;
_tos_cache2 = PyStackRef_ZERO_BITS;
SET_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _INSERT_2_LOAD_CONST_INLINE_BORROW_r03: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
@ -21546,86 +21486,6 @@
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r02: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
arg = stack_pointer[-1];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -3;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r12: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
arg = _stack_item_0;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r22: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
arg = _stack_item_1;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW_r32: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef res;
_PyStackRef a;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
_PyStackRef _stack_item_2 = _tos_cache2;
arg = _stack_item_2;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW_r03: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
@ -21726,49 +21586,6 @@
break;
}
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW_r31: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef pop2;
_PyStackRef pop1;
_PyStackRef null;
_PyStackRef callable;
_PyStackRef value;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
_PyStackRef _stack_item_2 = _tos_cache2;
pop2 = _stack_item_2;
pop1 = _stack_item_1;
null = _stack_item_0;
callable = stack_pointer[-1];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
stack_pointer[0] = null;
stack_pointer[1] = pop1;
stack_pointer += 2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(pop2);
stack_pointer = _PyFrame_GetStackPointer(frame);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(pop1);
stack_pointer = _PyFrame_GetStackPointer(frame);
(void)null;
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyStackRef_CLOSE(callable);
stack_pointer = _PyFrame_GetStackPointer(frame);
value = PyStackRef_FromPyObjectBorrow(ptr);
_tos_cache0 = value;
_tos_cache1 = PyStackRef_ZERO_BITS;
_tos_cache2 = PyStackRef_ZERO_BITS;
SET_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _START_EXECUTOR_r00: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());

View file

@ -657,9 +657,6 @@ 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_ONE_LOAD_CONST_INLINE_BORROW] = _POP_CALL_ONE,
[_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW] = _POP_CALL_TWO,
[_SHUFFLE_2_LOAD_CONST_INLINE_BORROW] = _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
};
const bool op_skip[MAX_UOP_ID + 1] = {
@ -679,15 +676,12 @@ 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,
};
@ -739,9 +733,9 @@ remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size)
}
}
else if (last->opcode == _PUSH_NULL) {
// Handle _POP_CALL and _POP_CALL_LOAD_CONST_INLINE_BORROW separately.
// Handle _POP_CALL separately.
// This looks for a preceding _PUSH_NULL instruction and
// simplifies to _POP_TOP(_LOAD_CONST_INLINE_BORROW).
// simplifies to _POP_TOP.
last->opcode = _NOP;
opcode = buffer[pc].opcode = op_without_pop_null[opcode];
assert(opcode);

View file

@ -763,19 +763,6 @@ dummy_func(void) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}
op(_POP_CALL_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused -- value)) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}
op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused, unused -- value)) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}
op(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, arg -- res, a)) {
res = PyJitRef_Borrow(sym_new_const(ctx, ptr));
a = arg;
}
op(_POP_TOP, (value -- )) {
PyTypeObject *typ = sym_get_type(value);
if (PyJitRef_IsBorrowed(value) ||
@ -1252,8 +1239,11 @@ dummy_func(void) {
PyObject* type = (PyObject *)sym_get_type(arg);
if (type) {
res = sym_new_const(ctx, type);
ADD_OP(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)type);
ADD_OP(_SWAP, 3, 0);
ADD_OP(_POP_TOP, 0, 0);
ADD_OP(_POP_TOP, 0, 0);
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)type);
ADD_OP(_SWAP, 2, 0);
}
else {
res = sym_new_not_null(ctx);
@ -1290,7 +1280,8 @@ dummy_func(void) {
out = Py_True;
}
sym_set_const(res, out);
ADD_OP(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out);
ADD_OP(_POP_CALL_TWO, 0, 0);
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out);
}
}

View file

@ -3789,8 +3789,11 @@
PyObject* type = (PyObject *)sym_get_type(arg);
if (type) {
res = sym_new_const(ctx, type);
ADD_OP(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)type);
ADD_OP(_SWAP, 3, 0);
ADD_OP(_POP_TOP, 0, 0);
ADD_OP(_POP_TOP, 0, 0);
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)type);
ADD_OP(_SWAP, 2, 0);
}
else {
res = sym_new_not_null(ctx);
@ -4123,7 +4126,8 @@
out = Py_True;
}
sym_set_const(res, out);
ADD_OP(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out);
ADD_OP(_POP_CALL_TWO, 0, 0);
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)out);
}
CHECK_STACK_BOUNDS(-3);
stack_pointer[-4] = res;
@ -4988,27 +4992,6 @@
break;
}
case _POP_CALL_LOAD_CONST_INLINE_BORROW: {
JitOptRef value;
PyObject *ptr = (PyObject *)this_instr->operand0;
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
CHECK_STACK_BOUNDS(-1);
stack_pointer[-2] = value;
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW: {
JitOptRef value;
value = sym_new_not_null(ctx);
CHECK_STACK_BOUNDS(-2);
stack_pointer[-3] = value;
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
case _INSERT_2_LOAD_CONST_INLINE_BORROW: {
JitOptRef res;
JitOptRef l;
@ -5025,22 +5008,6 @@
break;
}
case _SHUFFLE_2_LOAD_CONST_INLINE_BORROW: {
JitOptRef arg;
JitOptRef res;
JitOptRef a;
arg = stack_pointer[-1];
PyObject *ptr = (PyObject *)this_instr->operand0;
res = PyJitRef_Borrow(sym_new_const(ctx, ptr));
a = arg;
CHECK_STACK_BOUNDS(-1);
stack_pointer[-3] = res;
stack_pointer[-2] = a;
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW: {
JitOptRef res;
JitOptRef a;
@ -5054,17 +5021,6 @@
break;
}
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW: {
JitOptRef value;
PyObject *ptr = (PyObject *)this_instr->operand0;
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
CHECK_STACK_BOUNDS(-3);
stack_pointer[-4] = value;
stack_pointer += -3;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
break;
}
case _START_EXECUTOR: {
break;
}