gh-145214: Narrow _GUARD_TOS_ANY_{SET,DICT} by using probable type (gh-145215)

This commit is contained in:
Donghee Na 2026-03-03 09:58:38 +09:00 committed by GitHub
parent ea90b032a0
commit 6908372fb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 1728 additions and 1061 deletions

View file

@ -46,6 +46,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
#define sym_set_recorded_gen_func(SYM, VAL) _Py_uop_sym_set_recorded_gen_func(ctx, SYM, VAL)
#define sym_get_probable_func_code _Py_uop_sym_get_probable_func_code
#define sym_get_probable_value _Py_uop_sym_get_probable_value
#define sym_get_probable_type _Py_uop_sym_get_probable_type
#define sym_set_stack_depth(DEPTH, SP) _Py_uop_sym_set_stack_depth(ctx, DEPTH, SP)
extern int
@ -1324,28 +1325,36 @@ dummy_func(void) {
if (sym_matches_type(tos, &PyList_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PyList_Type);
else {
sym_set_type(tos, &PyList_Type);
}
}
op(_GUARD_NOS_LIST, (nos, unused -- nos, unused)) {
if (sym_matches_type(nos, &PyList_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(nos, &PyList_Type);
else {
sym_set_type(nos, &PyList_Type);
}
}
op(_GUARD_TOS_TUPLE, (tos -- tos)) {
if (sym_matches_type(tos, &PyTuple_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PyTuple_Type);
else {
sym_set_type(tos, &PyTuple_Type);
}
}
op(_GUARD_NOS_TUPLE, (nos, unused -- nos, unused)) {
if (sym_matches_type(nos, &PyTuple_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(nos, &PyTuple_Type);
else {
sym_set_type(nos, &PyTuple_Type);
}
}
op(_GUARD_NOS_DICT, (nos, unused -- nos, unused)) {
@ -1359,7 +1368,6 @@ dummy_func(void) {
PyTypeObject *tp = sym_get_type(nos);
if (tp == &PyDict_Type || tp == &PyFrozenDict_Type) {
ADD_OP(_NOP, 0, 0);
sym_set_type(nos, tp);
}
}
@ -1367,65 +1375,133 @@ dummy_func(void) {
PyTypeObject *tp = sym_get_type(tos);
if (tp == &PyDict_Type || tp == &PyFrozenDict_Type) {
ADD_OP(_NOP, 0, 0);
sym_set_type(tos, tp);
}
else {
// Narrowing the guard based on the probable type.
tp = sym_get_probable_type(tos);
if (tp == &PyDict_Type) {
ADD_OP(_GUARD_TOS_DICT, 0, 0);
}
else if (tp == &PyFrozenDict_Type) {
ADD_OP(_GUARD_TOS_FROZENDICT, 0, 0);
}
}
}
op(_GUARD_TOS_DICT, (tos -- tos)) {
if (sym_matches_type(tos, &PyDict_Type)) {
ADD_OP(_NOP, 0, 0);
}
else {
sym_set_type(tos, &PyDict_Type);
}
}
op(_GUARD_TOS_FROZENDICT, (tos -- tos)) {
if (sym_matches_type(tos, &PyFrozenDict_Type)) {
ADD_OP(_NOP, 0, 0);
}
else {
sym_set_type(tos, &PyFrozenDict_Type);
}
}
op(_GUARD_TOS_ANY_SET, (tos -- tos)) {
if (sym_matches_type(tos, &PySet_Type) ||
sym_matches_type(tos, &PyFrozenSet_Type))
{
PyTypeObject *tp = sym_get_type(tos);
if (tp == &PySet_Type || tp == &PyFrozenSet_Type) {
ADD_OP(_NOP, 0, 0);
}
else {
// Narrowing the guard based on the probable type.
tp = sym_get_probable_type(tos);
if (tp == &PySet_Type) {
ADD_OP(_GUARD_TOS_SET, 0, 0);
}
else if (tp == &PyFrozenSet_Type) {
ADD_OP(_GUARD_TOS_FROZENSET, 0, 0);
}
}
}
op(_GUARD_TOS_SET, (tos -- tos)) {
if (sym_matches_type(tos, &PySet_Type)) {
ADD_OP(_NOP, 0, 0);
}
else {
sym_set_type(tos, &PySet_Type);
}
}
op(_GUARD_TOS_FROZENSET, (tos -- tos)) {
if (sym_matches_type(tos, &PyFrozenSet_Type)) {
ADD_OP(_NOP, 0, 0);
}
else {
sym_set_type(tos, &PyFrozenSet_Type);
}
}
op(_GUARD_TOS_SLICE, (tos -- tos)) {
if (sym_matches_type(tos, &PySlice_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PySlice_Type);
else {
sym_set_type(tos, &PySlice_Type);
}
}
op(_GUARD_NOS_NULL, (null, unused -- null, unused)) {
if (sym_is_null(null)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_null(null);
else {
sym_set_null(null);
}
}
op(_GUARD_NOS_NOT_NULL, (nos, unused -- nos, unused)) {
if (sym_is_not_null(nos)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_non_null(nos);
else {
sym_set_non_null(nos);
}
}
op(_GUARD_THIRD_NULL, (null, unused, unused -- null, unused, unused)) {
if (sym_is_null(null)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_null(null);
else {
sym_set_null(null);
}
}
op(_GUARD_CALLABLE_TYPE_1, (callable, unused, unused -- callable, unused, unused)) {
if (sym_get_const(ctx, callable) == (PyObject *)&PyType_Type) {
ADD_OP(_NOP, 0, 0);
}
sym_set_const(callable, (PyObject *)&PyType_Type);
else {
sym_set_const(callable, (PyObject *)&PyType_Type);
}
}
op(_GUARD_CALLABLE_TUPLE_1, (callable, unused, unused -- callable, unused, unused)) {
if (sym_get_const(ctx, callable) == (PyObject *)&PyTuple_Type) {
ADD_OP(_NOP, 0, 0);
}
sym_set_const(callable, (PyObject *)&PyTuple_Type);
else {
sym_set_const(callable, (PyObject *)&PyTuple_Type);
}
}
op(_GUARD_CALLABLE_STR_1, (callable, unused, unused -- callable, unused, unused)) {
if (sym_get_const(ctx, callable) == (PyObject *)&PyUnicode_Type) {
ADD_OP(_NOP, 0, 0);
}
sym_set_const(callable, (PyObject *)&PyUnicode_Type);
else {
sym_set_const(callable, (PyObject *)&PyUnicode_Type);
}
}
op(_CALL_LEN, (callable, null, arg -- res, a, c)) {
@ -1484,7 +1560,9 @@ dummy_func(void) {
if (sym_get_const(ctx, callable) == len) {
ADD_OP(_NOP, 0, 0);
}
sym_set_const(callable, len);
else {
sym_set_const(callable, len);
}
}
op(_GUARD_CALLABLE_ISINSTANCE, (callable, unused, unused, unused -- callable, unused, unused, unused)) {
@ -1492,7 +1570,9 @@ dummy_func(void) {
if (sym_get_const(ctx, callable) == isinstance) {
ADD_OP(_NOP, 0, 0);
}
sym_set_const(callable, isinstance);
else {
sym_set_const(callable, isinstance);
}
}
op(_GUARD_CALLABLE_LIST_APPEND, (callable, unused, unused -- callable, unused, unused)) {
@ -1500,7 +1580,9 @@ dummy_func(void) {
if (sym_get_const(ctx, callable) == list_append) {
ADD_OP(_NOP, 0, 0);
}
sym_set_const(callable, list_append);
else {
sym_set_const(callable, list_append);
}
}
op(_BINARY_SLICE, (container, start, stop -- res)) {