mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-131798: Remove bounds check when indexing into tuples with a constant index (#137607)
* Remove bounds check when indexing into tuples with a constant index * Add news entry * fixup after rebase
This commit is contained in:
parent
c3febba73b
commit
713684de53
10 changed files with 1232 additions and 940 deletions
|
|
@ -968,9 +968,16 @@ dummy_func(
|
|||
}
|
||||
|
||||
macro(BINARY_OP_SUBSCR_TUPLE_INT) =
|
||||
_GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT + _POP_TOP_INT + POP_TOP;
|
||||
_GUARD_TOS_INT +
|
||||
_GUARD_NOS_TUPLE +
|
||||
_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS +
|
||||
unused/5 +
|
||||
_BINARY_OP_SUBSCR_TUPLE_INT +
|
||||
_POP_TOP_INT +
|
||||
POP_TOP;
|
||||
|
||||
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
|
||||
// A guard that checks that the tuple subscript is within bounds
|
||||
op(_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS, (tuple_st, sub_st -- tuple_st, sub_st)) {
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
|
||||
|
|
@ -981,7 +988,17 @@ dummy_func(
|
|||
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub));
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
DEOPT_IF(index >= PyTuple_GET_SIZE(tuple));
|
||||
}
|
||||
|
||||
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
|
||||
STAT_INC(BINARY_OP, hit);
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
|
||||
assert(res_o != NULL);
|
||||
res = PyStackRef_FromPyObjectNew(res_o);
|
||||
|
|
|
|||
196
Python/executor_cases.c.h
generated
196
Python/executor_cases.c.h
generated
|
|
@ -4891,14 +4891,76 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _BINARY_OP_SUBSCR_TUPLE_INT_r23: {
|
||||
case _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS_r02: {
|
||||
CHECK_CURRENT_CACHED_VALUES(0);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
sub_st = stack_pointer[-1];
|
||||
tuple_st = stack_pointer[-2];
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
if (!_PyLong_IsNonNegativeCompact((PyLongObject *)sub)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
SET_CURRENT_CACHED_VALUES(0);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
if (index >= PyTuple_GET_SIZE(tuple)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
SET_CURRENT_CACHED_VALUES(0);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
_tos_cache1 = sub_st;
|
||||
_tos_cache0 = tuple_st;
|
||||
SET_CURRENT_CACHED_VALUES(2);
|
||||
stack_pointer += -2;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
break;
|
||||
}
|
||||
|
||||
case _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS_r12: {
|
||||
CHECK_CURRENT_CACHED_VALUES(1);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
_PyStackRef _stack_item_0 = _tos_cache0;
|
||||
sub_st = _stack_item_0;
|
||||
tuple_st = stack_pointer[-1];
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
if (!_PyLong_IsNonNegativeCompact((PyLongObject *)sub)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
_tos_cache0 = sub_st;
|
||||
SET_CURRENT_CACHED_VALUES(1);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
if (index >= PyTuple_GET_SIZE(tuple)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
_tos_cache0 = sub_st;
|
||||
SET_CURRENT_CACHED_VALUES(1);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
_tos_cache1 = sub_st;
|
||||
_tos_cache0 = tuple_st;
|
||||
SET_CURRENT_CACHED_VALUES(2);
|
||||
stack_pointer += -1;
|
||||
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
break;
|
||||
}
|
||||
|
||||
case _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS_r22: {
|
||||
CHECK_CURRENT_CACHED_VALUES(2);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
_PyStackRef res;
|
||||
_PyStackRef ts;
|
||||
_PyStackRef ss;
|
||||
_PyStackRef _stack_item_0 = _tos_cache0;
|
||||
_PyStackRef _stack_item_1 = _tos_cache1;
|
||||
sub_st = _stack_item_1;
|
||||
|
|
@ -4922,7 +4984,133 @@
|
|||
SET_CURRENT_CACHED_VALUES(2);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
_tos_cache1 = sub_st;
|
||||
_tos_cache0 = tuple_st;
|
||||
SET_CURRENT_CACHED_VALUES(2);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
break;
|
||||
}
|
||||
|
||||
case _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS_r33: {
|
||||
CHECK_CURRENT_CACHED_VALUES(3);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
_PyStackRef _stack_item_0 = _tos_cache0;
|
||||
_PyStackRef _stack_item_1 = _tos_cache1;
|
||||
_PyStackRef _stack_item_2 = _tos_cache2;
|
||||
sub_st = _stack_item_2;
|
||||
tuple_st = _stack_item_1;
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
if (!_PyLong_IsNonNegativeCompact((PyLongObject *)sub)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
_tos_cache2 = sub_st;
|
||||
_tos_cache1 = tuple_st;
|
||||
_tos_cache0 = _stack_item_0;
|
||||
SET_CURRENT_CACHED_VALUES(3);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
if (index >= PyTuple_GET_SIZE(tuple)) {
|
||||
UOP_STAT_INC(uopcode, miss);
|
||||
_tos_cache2 = sub_st;
|
||||
_tos_cache1 = tuple_st;
|
||||
_tos_cache0 = _stack_item_0;
|
||||
SET_CURRENT_CACHED_VALUES(3);
|
||||
JUMP_TO_JUMP_TARGET();
|
||||
}
|
||||
_tos_cache2 = sub_st;
|
||||
_tos_cache1 = tuple_st;
|
||||
_tos_cache0 = _stack_item_0;
|
||||
SET_CURRENT_CACHED_VALUES(3);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
break;
|
||||
}
|
||||
|
||||
case _BINARY_OP_SUBSCR_TUPLE_INT_r03: {
|
||||
CHECK_CURRENT_CACHED_VALUES(0);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
_PyStackRef res;
|
||||
_PyStackRef ts;
|
||||
_PyStackRef ss;
|
||||
sub_st = stack_pointer[-1];
|
||||
tuple_st = stack_pointer[-2];
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
STAT_INC(BINARY_OP, hit);
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
|
||||
assert(res_o != NULL);
|
||||
res = PyStackRef_FromPyObjectNew(res_o);
|
||||
ts = tuple_st;
|
||||
ss = sub_st;
|
||||
_tos_cache2 = ss;
|
||||
_tos_cache1 = ts;
|
||||
_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 _BINARY_OP_SUBSCR_TUPLE_INT_r13: {
|
||||
CHECK_CURRENT_CACHED_VALUES(1);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
_PyStackRef res;
|
||||
_PyStackRef ts;
|
||||
_PyStackRef ss;
|
||||
_PyStackRef _stack_item_0 = _tos_cache0;
|
||||
sub_st = _stack_item_0;
|
||||
tuple_st = stack_pointer[-1];
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
STAT_INC(BINARY_OP, hit);
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
|
||||
assert(res_o != NULL);
|
||||
res = PyStackRef_FromPyObjectNew(res_o);
|
||||
ts = tuple_st;
|
||||
ss = sub_st;
|
||||
_tos_cache2 = ss;
|
||||
_tos_cache1 = ts;
|
||||
_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 _BINARY_OP_SUBSCR_TUPLE_INT_r23: {
|
||||
CHECK_CURRENT_CACHED_VALUES(2);
|
||||
assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE());
|
||||
_PyStackRef sub_st;
|
||||
_PyStackRef tuple_st;
|
||||
_PyStackRef res;
|
||||
_PyStackRef ts;
|
||||
_PyStackRef ss;
|
||||
_PyStackRef _stack_item_0 = _tos_cache0;
|
||||
_PyStackRef _stack_item_1 = _tos_cache1;
|
||||
sub_st = _stack_item_1;
|
||||
tuple_st = _stack_item_0;
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
STAT_INC(BINARY_OP, hit);
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
|
||||
assert(res_o != NULL);
|
||||
res = PyStackRef_FromPyObjectNew(res_o);
|
||||
|
|
|
|||
12
Python/generated_cases.c.h
generated
12
Python/generated_cases.c.h
generated
|
|
@ -1042,8 +1042,7 @@
|
|||
JUMP_TO_PREDICTED(BINARY_OP);
|
||||
}
|
||||
}
|
||||
/* Skip 5 cache entries */
|
||||
// _BINARY_OP_SUBSCR_TUPLE_INT
|
||||
// _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS
|
||||
{
|
||||
sub_st = value;
|
||||
tuple_st = nos;
|
||||
|
|
@ -1062,7 +1061,16 @@
|
|||
assert(_PyOpcode_Deopt[opcode] == (BINARY_OP));
|
||||
JUMP_TO_PREDICTED(BINARY_OP);
|
||||
}
|
||||
}
|
||||
/* Skip 5 cache entries */
|
||||
// _BINARY_OP_SUBSCR_TUPLE_INT
|
||||
{
|
||||
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
|
||||
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
|
||||
assert(PyLong_CheckExact(sub));
|
||||
assert(PyTuple_CheckExact(tuple));
|
||||
STAT_INC(BINARY_OP, hit);
|
||||
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
|
||||
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
|
||||
assert(res_o != NULL);
|
||||
res = PyStackRef_FromPyObjectNew(res_o);
|
||||
|
|
|
|||
|
|
@ -332,6 +332,19 @@ dummy_func(void) {
|
|||
i = sub_st;
|
||||
}
|
||||
|
||||
op(_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS, (tuple_st, sub_st -- tuple_st, sub_st)) {
|
||||
assert(sym_matches_type(tuple_st, &PyTuple_Type));
|
||||
if (sym_is_const(ctx, sub_st)) {
|
||||
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
|
||||
long index = PyLong_AsLong(sym_get_const(ctx, sub_st));
|
||||
assert(index >= 0);
|
||||
int tuple_length = sym_tuple_length(tuple_st);
|
||||
if (tuple_length != -1 && index < tuple_length) {
|
||||
REPLACE_OP(this_instr, _NOP, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
|
||||
assert(sym_matches_type(tuple_st, &PyTuple_Type));
|
||||
if (sym_is_const(ctx, sub_st)) {
|
||||
|
|
|
|||
18
Python/optimizer_cases.c.h
generated
18
Python/optimizer_cases.c.h
generated
|
|
@ -799,6 +799,24 @@
|
|||
break;
|
||||
}
|
||||
|
||||
case _GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS: {
|
||||
JitOptRef sub_st;
|
||||
JitOptRef tuple_st;
|
||||
sub_st = stack_pointer[-1];
|
||||
tuple_st = stack_pointer[-2];
|
||||
assert(sym_matches_type(tuple_st, &PyTuple_Type));
|
||||
if (sym_is_const(ctx, sub_st)) {
|
||||
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
|
||||
long index = PyLong_AsLong(sym_get_const(ctx, sub_st));
|
||||
assert(index >= 0);
|
||||
int tuple_length = sym_tuple_length(tuple_st);
|
||||
if (tuple_length != -1 && index < tuple_length) {
|
||||
REPLACE_OP(this_instr, _NOP, 0, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case _BINARY_OP_SUBSCR_TUPLE_INT: {
|
||||
JitOptRef sub_st;
|
||||
JitOptRef tuple_st;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue