gh-142961: Fix constant folding len(tuple) in JIT (GH-142963)

This commit is contained in:
Ken Jin 2025-12-20 01:43:36 +08:00 committed by GitHub
parent 049c2526bf
commit 786f464c74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 931 additions and 777 deletions

View file

@ -5285,6 +5285,13 @@ dummy_func(
value = PyStackRef_FromPyObjectBorrow(ptr);
}
tier2 op(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a, c)) {
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
c = callable;
INPUTS_DEAD();
}
tier2 op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
PyStackRef_CLOSE(pop2);
PyStackRef_CLOSE(pop1);

View file

@ -16532,6 +16532,106 @@
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW_r03: {
CHECK_CURRENT_CACHED_VALUES(0);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef callable;
_PyStackRef res;
_PyStackRef a;
_PyStackRef c;
arg = stack_pointer[-1];
callable = stack_pointer[-3];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
c = callable;
_tos_cache2 = c;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(3);
stack_pointer += -3;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW_r13: {
CHECK_CURRENT_CACHED_VALUES(1);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef callable;
_PyStackRef res;
_PyStackRef a;
_PyStackRef c;
_PyStackRef _stack_item_0 = _tos_cache0;
arg = _stack_item_0;
callable = stack_pointer[-2];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
c = callable;
_tos_cache2 = c;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(3);
stack_pointer += -2;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW_r23: {
CHECK_CURRENT_CACHED_VALUES(2);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef callable;
_PyStackRef res;
_PyStackRef a;
_PyStackRef c;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
arg = _stack_item_1;
callable = stack_pointer[-1];
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
c = callable;
_tos_cache2 = c;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(3);
stack_pointer += -1;
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW_r33: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
_PyStackRef arg;
_PyStackRef callable;
_PyStackRef res;
_PyStackRef a;
_PyStackRef c;
_PyStackRef _stack_item_0 = _tos_cache0;
_PyStackRef _stack_item_1 = _tos_cache1;
_PyStackRef _stack_item_2 = _tos_cache2;
arg = _stack_item_2;
callable = _stack_item_0;
PyObject *ptr = (PyObject *)CURRENT_OPERAND0_64();
res = PyStackRef_FromPyObjectBorrow(ptr);
a = arg;
c = callable;
_tos_cache2 = c;
_tos_cache1 = a;
_tos_cache0 = res;
SET_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
break;
}
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW_r31: {
CHECK_CURRENT_CACHED_VALUES(3);
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());

View file

@ -529,10 +529,6 @@ dummy_func(void) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}
op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, 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));
}
@ -1263,7 +1259,7 @@ dummy_func(void) {
goto error;
}
if (_Py_IsImmortal(temp)) {
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
REPLACE_OP(this_instr, _SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
0, (uintptr_t)temp);
}
res = sym_new_const(ctx, temp);

View file

@ -2862,7 +2862,7 @@
goto error;
}
if (_Py_IsImmortal(temp)) {
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
REPLACE_OP(this_instr, _SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
0, (uintptr_t)temp);
}
res = sym_new_const(ctx, temp);
@ -3424,8 +3424,7 @@
case _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW: {
JitOptRef value;
PyObject *ptr = (PyObject *)this_instr->operand0;
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
value = sym_new_not_null(ctx);
CHECK_STACK_BOUNDS(-2);
stack_pointer[-3] = value;
stack_pointer += -2;
@ -3433,6 +3432,19 @@
break;
}
case _SHUFFLE_3_LOAD_CONST_INLINE_BORROW: {
JitOptRef res;
JitOptRef a;
JitOptRef c;
res = sym_new_not_null(ctx);
a = sym_new_not_null(ctx);
c = sym_new_not_null(ctx);
stack_pointer[-3] = res;
stack_pointer[-2] = a;
stack_pointer[-1] = c;
break;
}
case _POP_CALL_TWO_LOAD_CONST_INLINE_BORROW: {
JitOptRef value;
PyObject *ptr = (PyObject *)this_instr->operand0;