Coverage Report

Created: 2025-11-09 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/specialize.c
Line
Count
Source
1
#include "Python.h"
2
3
#include "opcode.h"
4
5
#include "pycore_code.h"
6
#include "pycore_critical_section.h"
7
#include "pycore_descrobject.h"   // _PyMethodWrapper_Type
8
#include "pycore_dict.h"          // DICT_KEYS_UNICODE
9
#include "pycore_function.h"      // _PyFunction_GetVersionForCurrentState()
10
#include "pycore_interpframe.h"   // FRAME_SPECIALS_SIZE
11
#include "pycore_list.h"          // _PyListIterObject
12
#include "pycore_long.h"          // _PyLong_IsNonNegativeCompact()
13
#include "pycore_moduleobject.h"
14
#include "pycore_object.h"
15
#include "pycore_opcode_metadata.h" // _PyOpcode_Caches
16
#include "pycore_uop_metadata.h"    // _PyOpcode_uop_name
17
#include "pycore_uop_ids.h"       // MAX_UOP_ID
18
#include "pycore_opcode_utils.h"  // RESUME_AT_FUNC_START
19
#include "pycore_pylifecycle.h"   // _PyOS_URandomNonblock()
20
#include "pycore_runtime.h"       // _Py_ID()
21
#include "pycore_unicodeobject.h" // _PyUnicodeASCIIIter_Type
22
23
#include <stdlib.h> // rand()
24
25
/* For guidance on adding or extending families of instructions see
26
 * InternalDocs/interpreter.md `Specialization` section.
27
 */
28
29
#if Py_STATS
30
#define SPECIALIZATION_FAIL(opcode, kind) \
31
do { \
32
    PyStats *s = _PyStats_GET(); \
33
    if (s) { \
34
        int _kind = (kind); \
35
        assert(_kind < SPECIALIZATION_FAILURE_KINDS); \
36
        s->opcode_stats[opcode].specialization.failure_kinds[_kind]++; \
37
    } \
38
} while (0)
39
#else
40
321k
#  define SPECIALIZATION_FAIL(opcode, kind) ((void)0)
41
#endif  // Py_STATS
42
43
// Initialize warmup counters and optimize instructions. This cannot fail.
44
void
45
_PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters)
46
33.2k
{
47
33.2k
    #if ENABLE_SPECIALIZATION_FT
48
33.2k
    _Py_BackoffCounter jump_counter, adaptive_counter;
49
33.2k
    if (enable_counters) {
50
33.2k
        jump_counter = initial_jump_backoff_counter();
51
33.2k
        adaptive_counter = adaptive_counter_warmup();
52
33.2k
    }
53
0
    else {
54
0
        jump_counter = initial_unreachable_backoff_counter();
55
0
        adaptive_counter = initial_unreachable_backoff_counter();
56
0
    }
57
33.2k
    int opcode = 0;
58
33.2k
    int oparg = 0;
59
    /* The last code unit cannot have a cache, so we don't need to check it */
60
1.59M
    for (Py_ssize_t i = 0; i < size-1; i++) {
61
1.55M
        opcode = instructions[i].op.code;
62
1.55M
        int caches = _PyOpcode_Caches[opcode];
63
1.55M
        oparg = (oparg << 8) | instructions[i].op.arg;
64
1.55M
        if (caches) {
65
            // The initial value depends on the opcode
66
449k
            switch (opcode) {
67
14.8k
                case JUMP_BACKWARD:
68
14.8k
                    instructions[i + 1].counter = jump_counter;
69
14.8k
                    break;
70
37.2k
                case POP_JUMP_IF_FALSE:
71
50.5k
                case POP_JUMP_IF_TRUE:
72
53.5k
                case POP_JUMP_IF_NONE:
73
57.4k
                case POP_JUMP_IF_NOT_NONE:
74
57.4k
                    instructions[i + 1].cache = 0x5555;  // Alternating 0, 1 bits
75
57.4k
                    break;
76
377k
                default:
77
377k
                    instructions[i + 1].counter = adaptive_counter;
78
377k
                    break;
79
449k
            }
80
449k
            i += caches;
81
449k
        }
82
1.55M
        if (opcode != EXTENDED_ARG) {
83
1.53M
            oparg = 0;
84
1.53M
        }
85
1.55M
    }
86
33.2k
    #endif /* ENABLE_SPECIALIZATION_FT */
87
33.2k
}
88
89
526k
#define SIMPLE_FUNCTION 0
90
91
/* Common */
92
93
#define SPEC_FAIL_OTHER 0
94
#define SPEC_FAIL_NO_DICT 1
95
#define SPEC_FAIL_OVERRIDDEN 2
96
#define SPEC_FAIL_OUT_OF_VERSIONS 3
97
#define SPEC_FAIL_OUT_OF_RANGE 4
98
#define SPEC_FAIL_EXPECTED_ERROR 5
99
#define SPEC_FAIL_WRONG_NUMBER_ARGUMENTS 6
100
876
#define SPEC_FAIL_CODE_COMPLEX_PARAMETERS 7
101
18.8k
#define SPEC_FAIL_CODE_NOT_OPTIMIZED 8
102
103
104
#define SPEC_FAIL_LOAD_GLOBAL_NON_DICT 17
105
#define SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT 18
106
107
/* Super */
108
109
#define SPEC_FAIL_SUPER_BAD_CLASS 9
110
#define SPEC_FAIL_SUPER_SHADOWED 10
111
112
/* Attributes */
113
114
#define SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR 9
115
#define SPEC_FAIL_ATTR_NON_OVERRIDING_DESCRIPTOR 10
116
#define SPEC_FAIL_ATTR_NOT_DESCRIPTOR 11
117
#define SPEC_FAIL_ATTR_METHOD 12
118
#define SPEC_FAIL_ATTR_MUTABLE_CLASS 13
119
#define SPEC_FAIL_ATTR_PROPERTY 14
120
#define SPEC_FAIL_ATTR_NON_OBJECT_SLOT 15
121
#define SPEC_FAIL_ATTR_READ_ONLY 16
122
#define SPEC_FAIL_ATTR_AUDITED_SLOT 17
123
#define SPEC_FAIL_ATTR_NOT_MANAGED_DICT 18
124
#define SPEC_FAIL_ATTR_NON_STRING 19
125
#define SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND 20
126
#define SPEC_FAIL_ATTR_SHADOWED 21
127
#define SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD 22
128
#define SPEC_FAIL_ATTR_CLASS_METHOD_OBJ 23
129
#define SPEC_FAIL_ATTR_OBJECT_SLOT 24
130
131
#define SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE 26
132
#define SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE 27
133
#define SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION 28
134
#define SPEC_FAIL_ATTR_NOT_IN_KEYS 29
135
#define SPEC_FAIL_ATTR_NOT_IN_DICT 30
136
#define SPEC_FAIL_ATTR_CLASS_ATTR_SIMPLE 31
137
#define SPEC_FAIL_ATTR_CLASS_ATTR_DESCRIPTOR 32
138
#define SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD_OBJ 33
139
#define SPEC_FAIL_ATTR_METACLASS_OVERRIDDEN 34
140
#define SPEC_FAIL_ATTR_SPLIT_DICT 35
141
#define SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED 36
142
143
/* Binary subscr and store subscr */
144
145
#define SPEC_FAIL_SUBSCR_ARRAY_INT 9
146
#define SPEC_FAIL_SUBSCR_ARRAY_SLICE 10
147
#define SPEC_FAIL_SUBSCR_LIST_SLICE 11
148
#define SPEC_FAIL_SUBSCR_BUFFER_INT 12
149
#define SPEC_FAIL_SUBSCR_BUFFER_SLICE 13
150
151
/* Store subscr */
152
#define SPEC_FAIL_SUBSCR_BYTEARRAY_INT 18
153
#define SPEC_FAIL_SUBSCR_BYTEARRAY_SLICE 19
154
#define SPEC_FAIL_SUBSCR_PY_SIMPLE 20
155
#define SPEC_FAIL_SUBSCR_PY_OTHER 21
156
#define SPEC_FAIL_SUBSCR_DICT_SUBCLASS_NO_OVERRIDE 22
157
#define SPEC_FAIL_SUBSCR_NOT_HEAP_TYPE 23
158
159
/* Binary op */
160
161
#define SPEC_FAIL_BINARY_OP_ADD_DIFFERENT_TYPES          9
162
#define SPEC_FAIL_BINARY_OP_ADD_OTHER                   10
163
#define SPEC_FAIL_BINARY_OP_AND_DIFFERENT_TYPES         11
164
#define SPEC_FAIL_BINARY_OP_AND_INT                     12
165
#define SPEC_FAIL_BINARY_OP_AND_OTHER                   13
166
#define SPEC_FAIL_BINARY_OP_FLOOR_DIVIDE                14
167
#define SPEC_FAIL_BINARY_OP_LSHIFT                      15
168
#define SPEC_FAIL_BINARY_OP_MATRIX_MULTIPLY             16
169
#define SPEC_FAIL_BINARY_OP_MULTIPLY_DIFFERENT_TYPES    17
170
#define SPEC_FAIL_BINARY_OP_MULTIPLY_OTHER              18
171
#define SPEC_FAIL_BINARY_OP_OR                          19
172
#define SPEC_FAIL_BINARY_OP_POWER                       20
173
#define SPEC_FAIL_BINARY_OP_REMAINDER                   21
174
#define SPEC_FAIL_BINARY_OP_RSHIFT                      22
175
#define SPEC_FAIL_BINARY_OP_SUBTRACT_DIFFERENT_TYPES    23
176
#define SPEC_FAIL_BINARY_OP_SUBTRACT_OTHER              24
177
#define SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_DIFFERENT_TYPES 25
178
#define SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_FLOAT           26
179
#define SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_OTHER           27
180
#define SPEC_FAIL_BINARY_OP_XOR                         28
181
#define SPEC_FAIL_BINARY_OP_OR_INT                      29
182
#define SPEC_FAIL_BINARY_OP_OR_DIFFERENT_TYPES          30
183
#define SPEC_FAIL_BINARY_OP_XOR_INT                     31
184
#define SPEC_FAIL_BINARY_OP_XOR_DIFFERENT_TYPES         32
185
#define SPEC_FAIL_BINARY_OP_SUBSCR                      33
186
#define SPEC_FAIL_BINARY_OP_SUBSCR_LIST_SLICE           34
187
#define SPEC_FAIL_BINARY_OP_SUBSCR_TUPLE_SLICE          35
188
#define SPEC_FAIL_BINARY_OP_SUBSCR_STRING_SLICE         36
189
#define SPEC_FAIL_BINARY_OP_SUBSCR_NOT_HEAP_TYPE        37
190
#define SPEC_FAIL_BINARY_OP_SUBSCR_OTHER_SLICE          38
191
#define SPEC_FAIL_BINARY_OP_SUBSCR_MAPPINGPROXY         39
192
#define SPEC_FAIL_BINARY_OP_SUBSCR_RE_MATCH             40
193
#define SPEC_FAIL_BINARY_OP_SUBSCR_ARRAY                41
194
#define SPEC_FAIL_BINARY_OP_SUBSCR_DEQUE                42
195
#define SPEC_FAIL_BINARY_OP_SUBSCR_ENUMDICT             43
196
#define SPEC_FAIL_BINARY_OP_SUBSCR_STACKSUMMARY         44
197
#define SPEC_FAIL_BINARY_OP_SUBSCR_DEFAULTDICT          45
198
#define SPEC_FAIL_BINARY_OP_SUBSCR_COUNTER              46
199
#define SPEC_FAIL_BINARY_OP_SUBSCR_ORDEREDDICT          47
200
#define SPEC_FAIL_BINARY_OP_SUBSCR_BYTES                48
201
#define SPEC_FAIL_BINARY_OP_SUBSCR_STRUCTTIME           49
202
#define SPEC_FAIL_BINARY_OP_SUBSCR_RANGE                50
203
204
/* Calls */
205
206
#define SPEC_FAIL_CALL_INSTANCE_METHOD 11
207
#define SPEC_FAIL_CALL_CMETHOD 12
208
#define SPEC_FAIL_CALL_CFUNC_VARARGS 13
209
#define SPEC_FAIL_CALL_CFUNC_VARARGS_KEYWORDS 14
210
#define SPEC_FAIL_CALL_CFUNC_NOARGS 15
211
#define SPEC_FAIL_CALL_CFUNC_METHOD_FASTCALL_KEYWORDS 16
212
#define SPEC_FAIL_CALL_METH_DESCR_VARARGS 17
213
#define SPEC_FAIL_CALL_METH_DESCR_VARARGS_KEYWORDS 18
214
#define SPEC_FAIL_CALL_METH_DESCR_METHOD_FASTCALL_KEYWORDS 19
215
#define SPEC_FAIL_CALL_BAD_CALL_FLAGS 20
216
#define SPEC_FAIL_CALL_INIT_NOT_PYTHON 21
217
#define SPEC_FAIL_CALL_PEP_523 22
218
#define SPEC_FAIL_CALL_BOUND_METHOD 23
219
#define SPEC_FAIL_CALL_VECTORCALL 24
220
#define SPEC_FAIL_CALL_CLASS_MUTABLE 26
221
#define SPEC_FAIL_CALL_METHOD_WRAPPER 28
222
#define SPEC_FAIL_CALL_OPERATOR_WRAPPER 29
223
#define SPEC_FAIL_CALL_INIT_NOT_SIMPLE 30
224
#define SPEC_FAIL_CALL_METACLASS 31
225
#define SPEC_FAIL_CALL_INIT_NOT_INLINE_VALUES 32
226
227
/* COMPARE_OP */
228
#define SPEC_FAIL_COMPARE_OP_DIFFERENT_TYPES 12
229
#define SPEC_FAIL_COMPARE_OP_STRING 13
230
#define SPEC_FAIL_COMPARE_OP_BIG_INT 14
231
#define SPEC_FAIL_COMPARE_OP_BYTES 15
232
#define SPEC_FAIL_COMPARE_OP_TUPLE 16
233
#define SPEC_FAIL_COMPARE_OP_LIST 17
234
#define SPEC_FAIL_COMPARE_OP_SET 18
235
#define SPEC_FAIL_COMPARE_OP_BOOL 19
236
#define SPEC_FAIL_COMPARE_OP_BASEOBJECT 20
237
#define SPEC_FAIL_COMPARE_OP_FLOAT_LONG 21
238
#define SPEC_FAIL_COMPARE_OP_LONG_FLOAT 22
239
240
/* FOR_ITER and SEND */
241
#define SPEC_FAIL_ITER_GENERATOR 10
242
#define SPEC_FAIL_ITER_COROUTINE 11
243
#define SPEC_FAIL_ITER_ASYNC_GENERATOR 12
244
#define SPEC_FAIL_ITER_LIST 13
245
#define SPEC_FAIL_ITER_TUPLE 14
246
#define SPEC_FAIL_ITER_SET 15
247
#define SPEC_FAIL_ITER_STRING 16
248
#define SPEC_FAIL_ITER_BYTES 17
249
#define SPEC_FAIL_ITER_RANGE 18
250
#define SPEC_FAIL_ITER_ITERTOOLS 19
251
#define SPEC_FAIL_ITER_DICT_KEYS 20
252
#define SPEC_FAIL_ITER_DICT_ITEMS 21
253
#define SPEC_FAIL_ITER_DICT_VALUES 22
254
#define SPEC_FAIL_ITER_ENUMERATE 23
255
#define SPEC_FAIL_ITER_MAP 24
256
#define SPEC_FAIL_ITER_ZIP 25
257
#define SPEC_FAIL_ITER_SEQ_ITER 26
258
#define SPEC_FAIL_ITER_REVERSED_LIST 27
259
#define SPEC_FAIL_ITER_CALLABLE 28
260
#define SPEC_FAIL_ITER_ASCII_STRING 29
261
#define SPEC_FAIL_ITER_ASYNC_GENERATOR_SEND 30
262
#define SPEC_FAIL_ITER_SELF 31
263
264
// UNPACK_SEQUENCE
265
266
#define SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR 9
267
#define SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE 10
268
269
// TO_BOOL
270
#define SPEC_FAIL_TO_BOOL_BYTEARRAY    9
271
#define SPEC_FAIL_TO_BOOL_BYTES       10
272
#define SPEC_FAIL_TO_BOOL_DICT        11
273
#define SPEC_FAIL_TO_BOOL_FLOAT       12
274
19.4k
#define SPEC_FAIL_TO_BOOL_MAPPING     13
275
#define SPEC_FAIL_TO_BOOL_MEMORY_VIEW 14
276
26
#define SPEC_FAIL_TO_BOOL_NUMBER      15
277
10.7k
#define SPEC_FAIL_TO_BOOL_SEQUENCE    16
278
#define SPEC_FAIL_TO_BOOL_SET         17
279
#define SPEC_FAIL_TO_BOOL_TUPLE       18
280
281
// CONTAINS_OP
282
#define SPEC_FAIL_CONTAINS_OP_STR        9
283
#define SPEC_FAIL_CONTAINS_OP_TUPLE      10
284
#define SPEC_FAIL_CONTAINS_OP_LIST       11
285
#define SPEC_FAIL_CONTAINS_OP_USER_CLASS 12
286
287
static inline int
288
set_opcode(_Py_CODEUNIT *instr, uint8_t opcode)
289
4.39M
{
290
#ifdef Py_GIL_DISABLED
291
    uint8_t old_op = _Py_atomic_load_uint8_relaxed(&instr->op.code);
292
    if (old_op >= MIN_INSTRUMENTED_OPCODE) {
293
        /* Lost race with instrumentation */
294
        return 0;
295
    }
296
    if (!_Py_atomic_compare_exchange_uint8(&instr->op.code, &old_op, opcode)) {
297
        /* Lost race with instrumentation */
298
        assert(old_op >= MIN_INSTRUMENTED_OPCODE);
299
        return 0;
300
    }
301
    return 1;
302
#else
303
4.39M
    instr->op.code = opcode;
304
4.39M
    return 1;
305
4.39M
#endif
306
4.39M
}
307
308
static inline void
309
set_counter(_Py_BackoffCounter *counter, _Py_BackoffCounter value)
310
4.50M
{
311
4.50M
    FT_ATOMIC_STORE_UINT16_RELAXED(counter->value_and_backoff,
312
4.50M
                                   value.value_and_backoff);
313
4.50M
}
314
315
static inline _Py_BackoffCounter
316
load_counter(_Py_BackoffCounter *counter)
317
321k
{
318
321k
    _Py_BackoffCounter result = {
319
321k
        .value_and_backoff =
320
321k
            FT_ATOMIC_LOAD_UINT16_RELAXED(counter->value_and_backoff)};
321
321k
    return result;
322
321k
}
323
324
static inline void
325
specialize(_Py_CODEUNIT *instr, uint8_t specialized_opcode)
326
4.07M
{
327
4.07M
    assert(!PyErr_Occurred());
328
4.07M
    if (!set_opcode(instr, specialized_opcode)) {
329
0
        STAT_INC(_PyOpcode_Deopt[specialized_opcode], failure);
330
0
        SPECIALIZATION_FAIL(_PyOpcode_Deopt[specialized_opcode],
331
0
                            SPEC_FAIL_OTHER);
332
0
        return;
333
0
    }
334
4.07M
    STAT_INC(_PyOpcode_Deopt[specialized_opcode], success);
335
4.07M
    set_counter((_Py_BackoffCounter *)instr + 1, adaptive_counter_cooldown());
336
4.07M
}
337
338
static inline void
339
unspecialize(_Py_CODEUNIT *instr)
340
321k
{
341
321k
    assert(!PyErr_Occurred());
342
321k
    uint8_t opcode = FT_ATOMIC_LOAD_UINT8_RELAXED(instr->op.code);
343
321k
    uint8_t generic_opcode = _PyOpcode_Deopt[opcode];
344
321k
    STAT_INC(generic_opcode, failure);
345
321k
    if (!set_opcode(instr, generic_opcode)) {
346
0
        SPECIALIZATION_FAIL(generic_opcode, SPEC_FAIL_OTHER);
347
0
        return;
348
0
    }
349
321k
    _Py_BackoffCounter *counter = (_Py_BackoffCounter *)instr + 1;
350
321k
    _Py_BackoffCounter cur = load_counter(counter);
351
321k
    set_counter(counter, adaptive_counter_backoff(cur));
352
321k
}
353
354
static int function_kind(PyCodeObject *code);
355
static bool function_check_args(PyObject *o, int expected_argcount, int opcode);
356
static uint32_t function_get_version(PyObject *o, int opcode);
357
358
static int
359
specialize_module_load_attr_lock_held(PyDictObject *dict, _Py_CODEUNIT *instr, PyObject *name)
360
3.51k
{
361
3.51k
    _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
362
3.51k
    if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
363
0
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NON_STRING);
364
0
        return -1;
365
0
    }
366
3.51k
    Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__));
367
3.51k
    assert(index != DKIX_ERROR);
368
3.51k
    if (index != DKIX_EMPTY) {
369
807
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND);
370
807
        return -1;
371
807
    }
372
2.70k
    index = _PyDict_LookupIndex(dict, name);
373
2.70k
    assert (index != DKIX_ERROR);
374
2.70k
    if (index != (uint16_t)index) {
375
214
        SPECIALIZATION_FAIL(LOAD_ATTR,
376
214
                            index == DKIX_EMPTY ?
377
214
                            SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND :
378
214
                            SPEC_FAIL_OUT_OF_RANGE);
379
214
        return -1;
380
214
    }
381
2.49k
    uint32_t keys_version = _PyDict_GetKeysVersionForCurrentState(
382
2.49k
            _PyInterpreterState_GET(), dict);
383
2.49k
    if (keys_version == 0) {
384
0
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
385
0
        return -1;
386
0
    }
387
2.49k
    write_u32(cache->version, keys_version);
388
2.49k
    cache->index = (uint16_t)index;
389
2.49k
    specialize(instr, LOAD_ATTR_MODULE);
390
2.49k
    return 0;
391
2.49k
}
392
393
static int
394
specialize_module_load_attr(
395
    PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
396
3.51k
{
397
3.51k
    PyModuleObject *m = (PyModuleObject *)owner;
398
3.51k
    assert((Py_TYPE(owner)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
399
3.51k
    PyDictObject *dict = (PyDictObject *)m->md_dict;
400
3.51k
    if (dict == NULL) {
401
0
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);
402
0
        return -1;
403
0
    }
404
3.51k
    int result;
405
3.51k
    Py_BEGIN_CRITICAL_SECTION(dict);
406
3.51k
    result = specialize_module_load_attr_lock_held(dict, instr, name);
407
3.51k
    Py_END_CRITICAL_SECTION();
408
3.51k
    return result;
409
3.51k
}
410
411
/* Attribute specialization */
412
413
Py_NO_INLINE void
414
157
_Py_Specialize_LoadSuperAttr(_PyStackRef global_super_st, _PyStackRef cls_st, _Py_CODEUNIT *instr, int load_method) {
415
157
    PyObject *global_super = PyStackRef_AsPyObjectBorrow(global_super_st);
416
157
    PyObject *cls = PyStackRef_AsPyObjectBorrow(cls_st);
417
418
157
    assert(ENABLE_SPECIALIZATION_FT);
419
157
    assert(_PyOpcode_Caches[LOAD_SUPER_ATTR] == INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR);
420
157
    if (global_super != (PyObject *)&PySuper_Type) {
421
0
        SPECIALIZATION_FAIL(LOAD_SUPER_ATTR, SPEC_FAIL_SUPER_SHADOWED);
422
0
        goto fail;
423
0
    }
424
157
    if (!PyType_Check(cls)) {
425
0
        SPECIALIZATION_FAIL(LOAD_SUPER_ATTR, SPEC_FAIL_SUPER_BAD_CLASS);
426
0
        goto fail;
427
0
    }
428
157
    uint8_t load_code = load_method ? LOAD_SUPER_ATTR_METHOD : LOAD_SUPER_ATTR_ATTR;
429
157
    specialize(instr, load_code);
430
157
    return;
431
0
fail:
432
0
    unspecialize(instr);
433
0
}
434
435
typedef enum {
436
    OVERRIDING, /* Is an overriding descriptor, and will remain so. */
437
    METHOD, /* Attribute has Py_TPFLAGS_METHOD_DESCRIPTOR set */
438
    PROPERTY, /* Is a property */
439
    OBJECT_SLOT, /* Is an object slot descriptor */
440
    OTHER_SLOT, /* Is a slot descriptor of another type */
441
    NON_OVERRIDING, /* Is another non-overriding descriptor, and is an instance of an immutable class*/
442
    BUILTIN_CLASSMETHOD, /* Builtin methods with METH_CLASS */
443
    PYTHON_CLASSMETHOD, /* Python classmethod(func) object */
444
    NON_DESCRIPTOR, /* Is not a descriptor, and is an instance of an immutable class */
445
    MUTABLE,   /* Instance of a mutable class; might, or might not, be a descriptor */
446
    ABSENT, /* Attribute is not present on the class */
447
    DUNDER_CLASS, /* __class__ attribute */
448
    GETSET_OVERRIDDEN, /* __getattribute__ or __setattr__ has been overridden */
449
    GETATTRIBUTE_IS_PYTHON_FUNCTION  /* Descriptor requires calling a Python __getattribute__ */
450
} DescriptorClassification;
451
452
453
static DescriptorClassification
454
classify_descriptor(PyObject *descriptor, bool has_getattr)
455
1.04M
{
456
1.04M
    if (descriptor == NULL) {
457
543k
        return ABSENT;
458
543k
    }
459
500k
    PyTypeObject *desc_cls = Py_TYPE(descriptor);
460
500k
    if (!(desc_cls->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) {
461
655
        return MUTABLE;
462
655
    }
463
499k
    if (desc_cls->tp_descr_set) {
464
248k
        if (desc_cls == &PyMemberDescr_Type) {
465
661
            PyMemberDescrObject *member = (PyMemberDescrObject *)descriptor;
466
661
            struct PyMemberDef *dmem = member->d_member;
467
661
            if (dmem->type == Py_T_OBJECT_EX || dmem->type == _Py_T_OBJECT) {
468
559
                return OBJECT_SLOT;
469
559
            }
470
102
            return OTHER_SLOT;
471
661
        }
472
247k
        if (desc_cls == &PyProperty_Type) {
473
            /* We can't detect at runtime whether an attribute exists
474
               with property. So that means we may have to call
475
               __getattr__. */
476
244k
            return has_getattr ? GETSET_OVERRIDDEN : PROPERTY;
477
244k
        }
478
3.26k
        return OVERRIDING;
479
247k
    }
480
251k
    if (desc_cls->tp_descr_get) {
481
145k
        if (desc_cls->tp_flags & Py_TPFLAGS_METHOD_DESCRIPTOR) {
482
143k
            return METHOD;
483
143k
        }
484
1.59k
        if (Py_IS_TYPE(descriptor, &PyClassMethodDescr_Type)) {
485
162
            return BUILTIN_CLASSMETHOD;
486
162
        }
487
1.43k
        if (Py_IS_TYPE(descriptor, &PyClassMethod_Type)) {
488
759
            return PYTHON_CLASSMETHOD;
489
759
        }
490
675
        return NON_OVERRIDING;
491
1.43k
    }
492
106k
    return NON_DESCRIPTOR;
493
251k
}
494
495
static bool
496
descriptor_is_class(PyObject *descriptor, PyObject *name)
497
1.10M
{
498
1.10M
    return ((PyUnicode_CompareWithASCIIString(name, "__class__") == 0) &&
499
66.4k
            (descriptor == _PyType_Lookup(&PyBaseObject_Type, name)));
500
1.10M
}
501
502
static DescriptorClassification
503
872k
analyze_descriptor_load(PyTypeObject *type, PyObject *name, PyObject **descr, unsigned int *tp_version) {
504
872k
    bool has_getattr = false;
505
872k
    bool have_ga_version = false;
506
872k
    unsigned int ga_version;
507
872k
    getattrofunc getattro_slot = type->tp_getattro;
508
872k
    if (getattro_slot == PyObject_GenericGetAttr) {
509
        /* Normal attribute lookup; */
510
872k
        has_getattr = false;
511
872k
    }
512
158
    else if (getattro_slot == _Py_slot_tp_getattr_hook ||
513
158
        getattro_slot == _Py_slot_tp_getattro) {
514
        /* One or both of __getattribute__ or __getattr__ may have been
515
         overridden See typeobject.c for why these functions are special. */
516
0
        PyObject *getattribute = _PyType_LookupRefAndVersion(type,
517
0
                &_Py_ID(__getattribute__), &ga_version);
518
0
        have_ga_version = true;
519
0
        PyInterpreterState *interp = _PyInterpreterState_GET();
520
0
        bool has_custom_getattribute = getattribute != NULL &&
521
0
            getattribute != interp->callable_cache.object__getattribute__;
522
0
        PyObject *getattr = _PyType_Lookup(type, &_Py_ID(__getattr__));
523
0
        has_getattr = getattr != NULL;
524
0
        if (has_custom_getattribute) {
525
0
            if (!has_getattr &&
526
0
                Py_IS_TYPE(getattribute, &PyFunction_Type)) {
527
0
                *descr = getattribute;
528
0
                *tp_version = ga_version;
529
0
                return GETATTRIBUTE_IS_PYTHON_FUNCTION;
530
0
            }
531
            /* Potentially both __getattr__ and __getattribute__ are set.
532
               Too complicated */
533
0
            Py_DECREF(getattribute);
534
0
            *descr = NULL;
535
0
            *tp_version = ga_version;
536
0
            return GETSET_OVERRIDDEN;
537
0
        }
538
        /* Potentially has __getattr__ but no custom __getattribute__.
539
           Fall through to usual descriptor analysis.
540
           Usual attribute lookup should only be allowed at runtime
541
           if we can guarantee that there is no way an exception can be
542
           raised. This means some specializations, e.g. specializing
543
           for property() isn't safe.
544
        */
545
0
        Py_XDECREF(getattribute);
546
0
    }
547
158
    else {
548
158
        *descr = NULL;
549
158
        *tp_version = FT_ATOMIC_LOAD_UINT_RELAXED(type->tp_version_tag);
550
158
        return GETSET_OVERRIDDEN;
551
158
    }
552
872k
    unsigned int descr_version;
553
872k
    PyObject *descriptor = _PyType_LookupRefAndVersion(type, name, &descr_version);
554
872k
    *descr = descriptor;
555
872k
    *tp_version = have_ga_version ? ga_version : descr_version;
556
872k
    if (descriptor_is_class(descriptor, name)) {
557
66.4k
        return DUNDER_CLASS;
558
66.4k
    }
559
805k
    return classify_descriptor(descriptor, has_getattr);
560
872k
}
561
562
static DescriptorClassification
563
analyze_descriptor_store(PyTypeObject *type, PyObject *name, PyObject **descr, unsigned int *tp_version)
564
233k
{
565
233k
    if (type->tp_setattro != PyObject_GenericSetAttr) {
566
165
        *descr = NULL;
567
165
        return GETSET_OVERRIDDEN;
568
165
    }
569
232k
    PyObject *descriptor = _PyType_LookupRefAndVersion(type, name, tp_version);
570
232k
    *descr = descriptor;
571
232k
    if (descriptor_is_class(descriptor, name)) {
572
0
        return DUNDER_CLASS;
573
0
    }
574
232k
    return classify_descriptor(descriptor, false);
575
232k
}
576
577
static int
578
specialize_dict_access_inline(
579
    PyObject *owner, _Py_CODEUNIT *instr, PyTypeObject *type,
580
    PyObject *name, unsigned int tp_version,
581
    int base_op, int values_op)
582
425k
{
583
425k
    _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
584
425k
    PyDictKeysObject *keys = ((PyHeapTypeObject *)type)->ht_cached_keys;
585
425k
    assert(PyUnicode_CheckExact(name));
586
425k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(owner);
587
425k
    Py_ssize_t index = _PyDictKeys_StringLookupSplit(keys, name);
588
425k
    assert (index != DKIX_ERROR);
589
425k
    if (index == DKIX_EMPTY) {
590
524
        SPECIALIZATION_FAIL(base_op, SPEC_FAIL_ATTR_NOT_IN_KEYS);
591
524
        return 0;
592
524
    }
593
425k
    assert(index >= 0);
594
425k
    assert(_PyObject_InlineValues(owner)->valid);
595
425k
    char *value_addr = (char *)&_PyObject_InlineValues(owner)->values[index];
596
425k
    Py_ssize_t offset = value_addr - (char *)owner;
597
425k
    if (offset != (uint16_t)offset) {
598
0
        SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE);
599
0
        return 0;
600
0
    }
601
425k
    cache->index = (uint16_t)offset;
602
425k
    write_u32(cache->version, tp_version);
603
425k
    specialize(instr, values_op);
604
425k
    return 1;
605
425k
}
606
607
static int
608
specialize_dict_access_hint(
609
    PyDictObject *dict, _Py_CODEUNIT *instr, PyTypeObject *type,
610
    PyObject *name, unsigned int tp_version,
611
    int base_op, int hint_op)
612
17.5k
{
613
17.5k
    _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
614
615
17.5k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dict);
616
#ifdef Py_GIL_DISABLED
617
    _PyDict_EnsureSharedOnRead(dict);
618
#endif
619
620
    // We found an instance with a __dict__.
621
17.5k
    if (_PyDict_HasSplitTable(dict)) {
622
395
        SPECIALIZATION_FAIL(base_op, SPEC_FAIL_ATTR_SPLIT_DICT);
623
395
        return 0;
624
395
    }
625
17.1k
    Py_ssize_t index = _PyDict_LookupIndex(dict, name);
626
17.1k
    if (index != (uint16_t)index) {
627
5.61k
        SPECIALIZATION_FAIL(base_op,
628
5.61k
                            index == DKIX_EMPTY ?
629
5.61k
                            SPEC_FAIL_ATTR_NOT_IN_DICT :
630
5.61k
                            SPEC_FAIL_OUT_OF_RANGE);
631
5.61k
        return 0;
632
5.61k
    }
633
11.5k
    cache->index = (uint16_t)index;
634
11.5k
    write_u32(cache->version, tp_version);
635
11.5k
    specialize(instr, hint_op);
636
11.5k
    return 1;
637
17.1k
}
638
639
640
static int
641
specialize_dict_access(
642
    PyObject *owner, _Py_CODEUNIT *instr, PyTypeObject *type,
643
    DescriptorClassification kind, PyObject *name, unsigned int tp_version,
644
    int base_op, int values_op, int hint_op)
645
446k
{
646
446k
    assert(kind == NON_OVERRIDING || kind == NON_DESCRIPTOR || kind == ABSENT ||
647
446k
        kind == BUILTIN_CLASSMETHOD || kind == PYTHON_CLASSMETHOD ||
648
446k
        kind == METHOD);
649
    // No descriptor, or non overriding.
650
446k
    if ((type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
651
2.91k
        SPECIALIZATION_FAIL(base_op, SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
652
2.91k
        return 0;
653
2.91k
    }
654
443k
    if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES &&
655
442k
        FT_ATOMIC_LOAD_UINT8(_PyObject_InlineValues(owner)->valid) &&
656
425k
        !(base_op == STORE_ATTR && _PyObject_GetManagedDict(owner) != NULL))
657
425k
    {
658
425k
        int res;
659
425k
        Py_BEGIN_CRITICAL_SECTION(owner);
660
425k
        PyDictObject *dict = _PyObject_GetManagedDict(owner);
661
425k
        if (dict == NULL) {
662
            // managed dict, not materialized, inline values valid
663
425k
            res = specialize_dict_access_inline(owner, instr, type, name,
664
425k
                                                tp_version, base_op, values_op);
665
425k
        }
666
0
        else {
667
            // lost race and dict was created, fail specialization
668
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OTHER);
669
0
            res = 0;
670
0
        }
671
425k
        Py_END_CRITICAL_SECTION();
672
425k
        return res;
673
425k
    }
674
17.7k
    else {
675
17.7k
        PyDictObject *dict = _PyObject_GetManagedDict(owner);
676
17.7k
        if (dict == NULL || !PyDict_CheckExact(dict)) {
677
195
            SPECIALIZATION_FAIL(base_op, SPEC_FAIL_NO_DICT);
678
195
            return 0;
679
195
        }
680
17.5k
        int res;
681
17.5k
        Py_BEGIN_CRITICAL_SECTION(dict);
682
        // materialized managed dict
683
17.5k
        res = specialize_dict_access_hint(dict, instr, type, name,
684
17.5k
                                          tp_version, base_op, hint_op);
685
17.5k
        Py_END_CRITICAL_SECTION();
686
17.5k
        return res;
687
17.7k
    }
688
443k
}
689
690
static int
691
specialize_attr_loadclassattr(PyObject *owner, _Py_CODEUNIT *instr,
692
                              PyObject *name, PyObject *descr,
693
                              unsigned int tp_version,
694
                              DescriptorClassification kind, bool is_method,
695
                              uint32_t shared_keys_version);
696
static int specialize_class_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject* name);
697
698
/* Returns true if instances of obj's class are
699
 * likely to have `name` in their __dict__.
700
 * For objects with inline values, we check in the shared keys.
701
 * For other objects, we check their actual dictionary.
702
 */
703
static bool
704
instance_has_key(PyObject *obj, PyObject *name, uint32_t *shared_keys_version)
705
869k
{
706
869k
    PyTypeObject *cls = Py_TYPE(obj);
707
869k
    if ((cls->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
708
112k
        return false;
709
112k
    }
710
757k
    if (cls->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
711
664k
        PyDictKeysObject *keys = ((PyHeapTypeObject *)cls)->ht_cached_keys;
712
664k
        Py_ssize_t index =
713
664k
            _PyDictKeys_StringLookupAndVersion(keys, name, shared_keys_version);
714
664k
        return index >= 0;
715
664k
    }
716
92.5k
    PyDictObject *dict = _PyObject_GetManagedDict(obj);
717
92.5k
    if (dict == NULL || !PyDict_CheckExact(dict)) {
718
0
        return false;
719
0
    }
720
92.5k
    bool result;
721
92.5k
    Py_BEGIN_CRITICAL_SECTION(dict);
722
92.5k
    if (dict->ma_values) {
723
92.5k
        result = false;
724
92.5k
    }
725
0
    else {
726
0
        result = (_PyDict_LookupIndex(dict, name) >= 0);
727
0
    }
728
92.5k
    Py_END_CRITICAL_SECTION();
729
92.5k
    return result;
730
92.5k
}
731
732
static int
733
do_specialize_instance_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject* name,
734
                                 bool shadow, uint32_t shared_keys_version,
735
                                 DescriptorClassification kind, PyObject *descr, unsigned int tp_version)
736
869k
{
737
869k
    _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
738
869k
    PyTypeObject *type = Py_TYPE(owner);
739
869k
    if (tp_version == 0) {
740
7.36k
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
741
7.36k
        return -1;
742
7.36k
    }
743
862k
    uint8_t oparg = FT_ATOMIC_LOAD_UINT8_RELAXED(instr->op.arg);
744
862k
    switch(kind) {
745
170
        case OVERRIDING:
746
170
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR);
747
170
            return -1;
748
139k
        case METHOD:
749
139k
        {
750
139k
            if (shadow) {
751
72
                goto try_instance;
752
72
            }
753
139k
            if (oparg & 1) {
754
71.7k
                if (specialize_attr_loadclassattr(owner, instr, name, descr,
755
71.7k
                                                  tp_version, kind, true,
756
71.7k
                                                  shared_keys_version)) {
757
70.6k
                    return 0;
758
70.6k
                }
759
1.05k
                else {
760
1.05k
                    return -1;
761
1.05k
                }
762
71.7k
            }
763
67.9k
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
764
67.9k
            return -1;
765
139k
        }
766
244k
        case PROPERTY:
767
244k
        {
768
244k
            _PyLoadMethodCache *lm_cache = (_PyLoadMethodCache *)(instr + 1);
769
244k
            assert(Py_TYPE(descr) == &PyProperty_Type);
770
244k
            PyObject *fget = ((_PyPropertyObject *)descr)->prop_get;
771
244k
            if (fget == NULL) {
772
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
773
0
                return -1;
774
0
            }
775
244k
            if (!Py_IS_TYPE(fget, &PyFunction_Type)) {
776
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION);
777
0
                return -1;
778
0
            }
779
244k
            if (!function_check_args(fget, 1, LOAD_ATTR)) {
780
0
                return -1;
781
0
            }
782
244k
            if (oparg & 1) {
783
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
784
0
                return -1;
785
0
            }
786
            /* Don't specialize if PEP 523 is active */
787
244k
            if (_PyInterpreterState_GET()->eval_frame) {
788
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
789
0
                return -1;
790
0
            }
791
            #ifdef Py_GIL_DISABLED
792
            if (!_PyObject_HasDeferredRefcount(fget)) {
793
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED);
794
                return -1;
795
            }
796
            #endif
797
244k
            assert(tp_version != 0);
798
244k
            write_u32(lm_cache->type_version, tp_version);
799
            /* borrowed */
800
244k
            write_ptr(lm_cache->descr, fget);
801
244k
            specialize(instr, LOAD_ATTR_PROPERTY);
802
244k
            return 0;
803
244k
        }
804
385
        case OBJECT_SLOT:
805
385
        {
806
385
            PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
807
385
            struct PyMemberDef *dmem = member->d_member;
808
385
            Py_ssize_t offset = dmem->offset;
809
385
            if (!PyObject_TypeCheck(owner, member->d_common.d_type)) {
810
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
811
0
                return -1;
812
0
            }
813
385
            if (dmem->flags & Py_AUDIT_READ) {
814
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_AUDITED_SLOT);
815
0
                return -1;
816
0
            }
817
385
            if (offset != (uint16_t)offset) {
818
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_RANGE);
819
0
                return -1;
820
0
            }
821
385
            assert(dmem->type == Py_T_OBJECT_EX || dmem->type == _Py_T_OBJECT);
822
385
            assert(offset > 0);
823
385
            cache->index = (uint16_t)offset;
824
385
            write_u32(cache->version, tp_version);
825
385
            specialize(instr, LOAD_ATTR_SLOT);
826
385
            return 0;
827
385
        }
828
66.4k
        case DUNDER_CLASS:
829
66.4k
        {
830
66.4k
            Py_ssize_t offset = offsetof(PyObject, ob_type);
831
66.4k
            assert(offset == (uint16_t)offset);
832
66.4k
            cache->index = (uint16_t)offset;
833
66.4k
            write_u32(cache->version, tp_version);
834
66.4k
            specialize(instr, LOAD_ATTR_SLOT);
835
66.4k
            return 0;
836
385
        }
837
102
        case OTHER_SLOT:
838
102
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NON_OBJECT_SLOT);
839
102
            return -1;
840
335
        case MUTABLE:
841
335
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_MUTABLE_CLASS);
842
335
            return -1;
843
2
        case GETSET_OVERRIDDEN:
844
2
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OVERRIDDEN);
845
2
            return -1;
846
0
        case GETATTRIBUTE_IS_PYTHON_FUNCTION:
847
0
        {
848
0
            assert(Py_IS_TYPE(descr, &PyFunction_Type));
849
0
            _PyLoadMethodCache *lm_cache = (_PyLoadMethodCache *)(instr + 1);
850
0
            if (!function_check_args(descr, 2, LOAD_ATTR)) {
851
0
                return -1;
852
0
            }
853
0
            if (oparg & 1) {
854
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
855
0
                return -1;
856
0
            }
857
0
            uint32_t version = function_get_version(descr, LOAD_ATTR);
858
0
            if (version == 0) {
859
0
                return -1;
860
0
            }
861
            /* Don't specialize if PEP 523 is active */
862
0
            if (_PyInterpreterState_GET()->eval_frame) {
863
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
864
0
                return -1;
865
0
            }
866
            #ifdef Py_GIL_DISABLED
867
            if (!_PyObject_HasDeferredRefcount(descr)) {
868
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED);
869
                return -1;
870
            }
871
            #endif
872
0
            write_u32(lm_cache->keys_version, version);
873
            /* borrowed */
874
0
            write_ptr(lm_cache->descr, descr);
875
0
            write_u32(lm_cache->type_version, tp_version);
876
0
            specialize(instr, LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN);
877
0
            return 0;
878
0
        }
879
0
        case BUILTIN_CLASSMETHOD:
880
126
        case PYTHON_CLASSMETHOD:
881
238
        case NON_OVERRIDING:
882
238
            if (shadow) {
883
0
                goto try_instance;
884
0
            }
885
238
            return -1;
886
103k
        case NON_DESCRIPTOR:
887
103k
            if (shadow) {
888
17.3k
                goto try_instance;
889
17.3k
            }
890
85.8k
            if ((oparg & 1) == 0) {
891
85.8k
                if (specialize_attr_loadclassattr(owner, instr, name, descr,
892
85.8k
                                                  tp_version, kind, false,
893
85.8k
                                                  shared_keys_version)) {
894
85.1k
                    return 0;
895
85.1k
                }
896
85.8k
            }
897
648
            return -1;
898
307k
        case ABSENT:
899
307k
            if (shadow) {
900
198k
                goto try_instance;
901
198k
            }
902
108k
            set_counter((_Py_BackoffCounter*)instr + 1, adaptive_counter_cooldown());
903
108k
            return 0;
904
862k
    }
905
862k
    Py_UNREACHABLE();
906
215k
try_instance:
907
215k
    if (specialize_dict_access(owner, instr, type, kind, name, tp_version,
908
215k
                               LOAD_ATTR, LOAD_ATTR_INSTANCE_VALUE, LOAD_ATTR_WITH_HINT))
909
210k
    {
910
210k
        return 0;
911
210k
    }
912
5.59k
    return -1;
913
215k
}
914
915
static int
916
specialize_instance_load_attr(PyObject* owner, _Py_CODEUNIT* instr, PyObject* name)
917
869k
{
918
    // 0 is not a valid version
919
869k
    uint32_t shared_keys_version = 0;
920
869k
    bool shadow = instance_has_key(owner, name, &shared_keys_version);
921
869k
    PyObject *descr = NULL;
922
869k
    unsigned int tp_version = 0;
923
869k
    PyTypeObject *type = Py_TYPE(owner);
924
869k
    DescriptorClassification kind = analyze_descriptor_load(type, name, &descr, &tp_version);
925
869k
    int result = do_specialize_instance_load_attr(owner, instr, name, shadow, shared_keys_version, kind, descr, tp_version);
926
869k
    Py_XDECREF(descr);
927
869k
    return result;
928
869k
}
929
930
Py_NO_INLINE void
931
_Py_Specialize_LoadAttr(_PyStackRef owner_st, _Py_CODEUNIT *instr, PyObject *name)
932
878k
{
933
878k
    PyObject *owner = PyStackRef_AsPyObjectBorrow(owner_st);
934
935
878k
    assert(ENABLE_SPECIALIZATION_FT);
936
878k
    assert(_PyOpcode_Caches[LOAD_ATTR] == INLINE_CACHE_ENTRIES_LOAD_ATTR);
937
878k
    PyTypeObject *type = Py_TYPE(owner);
938
878k
    bool fail;
939
878k
    if (!_PyType_IsReady(type)) {
940
        // We *might* not really need this check, but we inherited it from
941
        // PyObject_GenericGetAttr and friends... and this way we still do the
942
        // right thing if someone forgets to call PyType_Ready(type):
943
0
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OTHER);
944
0
        fail = true;
945
0
    }
946
878k
    else if (Py_TYPE(owner)->tp_getattro == PyModule_Type.tp_getattro) {
947
3.51k
        fail = specialize_module_load_attr(owner, instr, name);
948
3.51k
    }
949
875k
    else if (PyType_Check(owner)) {
950
5.69k
        fail = specialize_class_load_attr(owner, instr, name);
951
5.69k
    }
952
869k
    else {
953
869k
        fail = specialize_instance_load_attr(owner, instr, name);
954
869k
    }
955
956
878k
    if (fail) {
957
89.2k
        unspecialize(instr);
958
89.2k
    }
959
878k
}
960
961
Py_NO_INLINE void
962
_Py_Specialize_StoreAttr(_PyStackRef owner_st, _Py_CODEUNIT *instr, PyObject *name)
963
233k
{
964
233k
    PyObject *owner = PyStackRef_AsPyObjectBorrow(owner_st);
965
966
233k
    assert(ENABLE_SPECIALIZATION_FT);
967
233k
    assert(_PyOpcode_Caches[STORE_ATTR] == INLINE_CACHE_ENTRIES_STORE_ATTR);
968
233k
    PyObject *descr = NULL;
969
233k
    _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
970
233k
    PyTypeObject *type = Py_TYPE(owner);
971
233k
    if (!_PyType_IsReady(type)) {
972
        // We *might* not really need this check, but we inherited it from
973
        // PyObject_GenericSetAttr and friends... and this way we still do the
974
        // right thing if someone forgets to call PyType_Ready(type):
975
0
        SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OTHER);
976
0
        goto fail;
977
0
    }
978
233k
    if (PyModule_CheckExact(owner)) {
979
415
        SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OVERRIDDEN);
980
415
        goto fail;
981
415
    }
982
233k
    unsigned int tp_version = 0;
983
233k
    DescriptorClassification kind = analyze_descriptor_store(type, name, &descr, &tp_version);
984
233k
    if (tp_version == 0) {
985
225
        goto fail;
986
225
    }
987
233k
    assert(descr != NULL || kind == ABSENT || kind == GETSET_OVERRIDDEN);
988
232k
    switch(kind) {
989
155
        case OVERRIDING:
990
155
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR);
991
155
            goto fail;
992
12
        case METHOD:
993
12
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_METHOD);
994
12
            goto fail;
995
16
        case PROPERTY:
996
16
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_PROPERTY);
997
16
            goto fail;
998
174
        case OBJECT_SLOT:
999
174
        {
1000
174
            PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
1001
174
            struct PyMemberDef *dmem = member->d_member;
1002
174
            Py_ssize_t offset = dmem->offset;
1003
174
            if (!PyObject_TypeCheck(owner, member->d_common.d_type)) {
1004
0
                SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_EXPECTED_ERROR);
1005
0
                goto fail;
1006
0
            }
1007
174
            if (dmem->flags & Py_READONLY) {
1008
0
                SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_READ_ONLY);
1009
0
                goto fail;
1010
0
            }
1011
174
            if (offset != (uint16_t)offset) {
1012
0
                SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OUT_OF_RANGE);
1013
0
                goto fail;
1014
0
            }
1015
174
            assert(dmem->type == Py_T_OBJECT_EX || dmem->type == _Py_T_OBJECT);
1016
174
            assert(offset > 0);
1017
174
            cache->index = (uint16_t)offset;
1018
174
            write_u32(cache->version, tp_version);
1019
174
            specialize(instr, STORE_ATTR_SLOT);
1020
174
            goto success;
1021
174
        }
1022
0
        case DUNDER_CLASS:
1023
0
        case OTHER_SLOT:
1024
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_NON_OBJECT_SLOT);
1025
0
            goto fail;
1026
0
        case MUTABLE:
1027
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_MUTABLE_CLASS);
1028
0
            goto fail;
1029
0
        case GETATTRIBUTE_IS_PYTHON_FUNCTION:
1030
0
        case GETSET_OVERRIDDEN:
1031
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OVERRIDDEN);
1032
0
            goto fail;
1033
0
        case BUILTIN_CLASSMETHOD:
1034
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD_OBJ);
1035
0
            goto fail;
1036
0
        case PYTHON_CLASSMETHOD:
1037
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_CLASS_METHOD_OBJ);
1038
0
            goto fail;
1039
0
        case NON_OVERRIDING:
1040
0
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_CLASS_ATTR_DESCRIPTOR);
1041
0
            goto fail;
1042
2.21k
        case NON_DESCRIPTOR:
1043
2.21k
            SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_CLASS_ATTR_SIMPLE);
1044
2.21k
            goto fail;
1045
230k
        case ABSENT:
1046
230k
            if (specialize_dict_access(owner, instr, type, kind, name, tp_version,
1047
230k
                                       STORE_ATTR, STORE_ATTR_INSTANCE_VALUE,
1048
230k
                                       STORE_ATTR_WITH_HINT)) {
1049
226k
                goto success;
1050
226k
            }
1051
232k
    }
1052
7.09k
fail:
1053
7.09k
    Py_XDECREF(descr);
1054
7.09k
    unspecialize(instr);
1055
7.09k
    return;
1056
226k
success:
1057
226k
    Py_XDECREF(descr);
1058
226k
    return;
1059
232k
}
1060
1061
#ifdef Py_STATS
1062
static int
1063
load_attr_fail_kind(DescriptorClassification kind)
1064
{
1065
    switch (kind) {
1066
        case OVERRIDING:
1067
            return SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR;
1068
        case METHOD:
1069
            return SPEC_FAIL_ATTR_METHOD;
1070
        case PROPERTY:
1071
            return SPEC_FAIL_ATTR_PROPERTY;
1072
        case OBJECT_SLOT:
1073
            return SPEC_FAIL_ATTR_OBJECT_SLOT;
1074
        case OTHER_SLOT:
1075
            return SPEC_FAIL_ATTR_NON_OBJECT_SLOT;
1076
        case DUNDER_CLASS:
1077
            return SPEC_FAIL_OTHER;
1078
        case MUTABLE:
1079
            return SPEC_FAIL_ATTR_MUTABLE_CLASS;
1080
        case GETSET_OVERRIDDEN:
1081
        case GETATTRIBUTE_IS_PYTHON_FUNCTION:
1082
            return SPEC_FAIL_OVERRIDDEN;
1083
        case BUILTIN_CLASSMETHOD:
1084
            return SPEC_FAIL_ATTR_BUILTIN_CLASS_METHOD;
1085
        case PYTHON_CLASSMETHOD:
1086
            return SPEC_FAIL_ATTR_CLASS_METHOD_OBJ;
1087
        case NON_OVERRIDING:
1088
            return SPEC_FAIL_ATTR_NON_OVERRIDING_DESCRIPTOR;
1089
        case NON_DESCRIPTOR:
1090
            return SPEC_FAIL_ATTR_NOT_DESCRIPTOR;
1091
        case ABSENT:
1092
            return SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE;
1093
    }
1094
    Py_UNREACHABLE();
1095
}
1096
#endif   // Py_STATS
1097
1098
static int
1099
specialize_class_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
1100
                             PyObject *name)
1101
5.69k
{
1102
5.69k
    assert(PyType_Check(owner));
1103
5.69k
    PyTypeObject *cls = (PyTypeObject *)owner;
1104
5.69k
    _PyLoadMethodCache *cache = (_PyLoadMethodCache *)(instr + 1);
1105
5.69k
    if (Py_TYPE(cls)->tp_getattro != _Py_type_getattro) {
1106
0
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METACLASS_OVERRIDDEN);
1107
0
        return -1;
1108
0
    }
1109
5.69k
    unsigned int meta_version = 0;
1110
5.69k
    PyObject *metadescriptor = _PyType_LookupRefAndVersion(Py_TYPE(cls), name, &meta_version);
1111
5.69k
    DescriptorClassification metakind = classify_descriptor(metadescriptor, false);
1112
5.69k
    Py_XDECREF(metadescriptor);
1113
5.69k
    switch (metakind) {
1114
289
        case METHOD:
1115
392
        case NON_DESCRIPTOR:
1116
513
        case NON_OVERRIDING:
1117
513
        case BUILTIN_CLASSMETHOD:
1118
513
        case PYTHON_CLASSMETHOD:
1119
2.74k
        case ABSENT:
1120
2.74k
            break;
1121
2.94k
        default:
1122
2.94k
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE);
1123
2.94k
            return -1;
1124
5.69k
    }
1125
2.74k
    PyObject *descr = NULL;
1126
2.74k
    DescriptorClassification kind = 0;
1127
2.74k
    unsigned int tp_version = 0;
1128
2.74k
    kind = analyze_descriptor_load(cls, name, &descr, &tp_version);
1129
2.74k
    if (tp_version == 0) {
1130
36
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
1131
36
        Py_XDECREF(descr);
1132
36
        return -1;
1133
36
    }
1134
2.74k
    bool metaclass_check = false;
1135
2.71k
    if ((Py_TYPE(cls)->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
1136
1.02k
        metaclass_check = true;
1137
1.02k
        if (meta_version == 0) {
1138
0
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
1139
0
            Py_XDECREF(descr);
1140
0
            return -1;
1141
0
        }
1142
1.02k
    }
1143
2.71k
    switch (kind) {
1144
258
        case METHOD:
1145
899
        case NON_DESCRIPTOR:
1146
            #ifdef Py_GIL_DISABLED
1147
            if (!_PyObject_HasDeferredRefcount(descr)) {
1148
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED);
1149
                Py_XDECREF(descr);
1150
                return -1;
1151
            }
1152
            #endif
1153
899
            write_u32(cache->type_version, tp_version);
1154
899
            write_ptr(cache->descr, descr);
1155
899
            if (metaclass_check) {
1156
459
                write_u32(cache->keys_version, meta_version);
1157
459
                specialize(instr, LOAD_ATTR_CLASS_WITH_METACLASS_CHECK);
1158
459
            }
1159
440
            else {
1160
440
                specialize(instr, LOAD_ATTR_CLASS);
1161
440
            }
1162
899
            Py_XDECREF(descr);
1163
899
            return 0;
1164
#ifdef Py_STATS
1165
        case ABSENT:
1166
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_EXPECTED_ERROR);
1167
            Py_XDECREF(descr);
1168
            return -1;
1169
#endif
1170
1.81k
        default:
1171
1.81k
            SPECIALIZATION_FAIL(LOAD_ATTR, load_attr_fail_kind(kind));
1172
1.81k
            Py_XDECREF(descr);
1173
1.81k
            return -1;
1174
2.71k
    }
1175
2.71k
}
1176
1177
// Please collect stats carefully before and after modifying. A subtle change
1178
// can cause a significant drop in cache hits. A possible test is
1179
// python.exe -m test_typing test_re test_dis test_zlib.
1180
static int
1181
specialize_attr_loadclassattr(PyObject *owner, _Py_CODEUNIT *instr,
1182
                              PyObject *name, PyObject *descr,
1183
                              unsigned int tp_version,
1184
                              DescriptorClassification kind, bool is_method,
1185
                              uint32_t shared_keys_version)
1186
157k
{
1187
157k
    _PyLoadMethodCache *cache = (_PyLoadMethodCache *)(instr + 1);
1188
157k
    PyTypeObject *owner_cls = Py_TYPE(owner);
1189
1190
157k
    assert(descr != NULL);
1191
157k
    assert((is_method && kind == METHOD) || (!is_method && kind == NON_DESCRIPTOR));
1192
1193
    #ifdef Py_GIL_DISABLED
1194
    if (!_PyObject_HasDeferredRefcount(descr)) {
1195
        SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_DESCR_NOT_DEFERRED);
1196
        return 0;
1197
    }
1198
    #endif
1199
1200
157k
    unsigned long tp_flags = PyType_GetFlags(owner_cls);
1201
157k
    if (tp_flags & Py_TPFLAGS_INLINE_VALUES) {
1202
133k
        #ifndef Py_GIL_DISABLED
1203
133k
        assert(_PyDictKeys_StringLookup(
1204
133k
                   ((PyHeapTypeObject *)owner_cls)->ht_cached_keys, name) < 0);
1205
133k
        #endif
1206
133k
        if (shared_keys_version == 0) {
1207
0
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
1208
0
            return 0;
1209
0
        }
1210
133k
        write_u32(cache->keys_version, shared_keys_version);
1211
133k
        specialize(instr, is_method ? LOAD_ATTR_METHOD_WITH_VALUES : LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES);
1212
133k
    }
1213
24.4k
    else {
1214
24.4k
        Py_ssize_t dictoffset;
1215
24.4k
        if (tp_flags & Py_TPFLAGS_MANAGED_DICT) {
1216
1.04k
            dictoffset = MANAGED_DICT_OFFSET;
1217
1.04k
        }
1218
23.4k
        else {
1219
23.4k
            dictoffset = owner_cls->tp_dictoffset;
1220
23.4k
            if (dictoffset < 0 || dictoffset > INT16_MAX + MANAGED_DICT_OFFSET) {
1221
0
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_RANGE);
1222
0
                return 0;
1223
0
            }
1224
23.4k
        }
1225
24.4k
        if (dictoffset == 0) {
1226
22.7k
            specialize(instr, is_method ? LOAD_ATTR_METHOD_NO_DICT : LOAD_ATTR_NONDESCRIPTOR_NO_DICT);
1227
22.7k
        }
1228
1.74k
        else if (is_method) {
1229
1.09k
            PyObject **addr = (PyObject **)((char *)owner + dictoffset);
1230
1.09k
            PyObject *dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*addr);
1231
1.09k
            if (dict) {
1232
1.05k
                SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
1233
1.05k
                return 0;
1234
1.05k
            }
1235
            /* Cache entries must be unsigned values, so we offset the
1236
             * dictoffset by MANAGED_DICT_OFFSET.
1237
             * We do the reverse offset in LOAD_ATTR_METHOD_LAZY_DICT */
1238
42
            dictoffset -= MANAGED_DICT_OFFSET;
1239
42
            assert(((uint16_t)dictoffset) == dictoffset);
1240
42
            cache->dict_offset = (uint16_t)dictoffset;
1241
42
            specialize(instr, LOAD_ATTR_METHOD_LAZY_DICT);
1242
42
        }
1243
648
        else {
1244
648
            SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_CLASS_ATTR_SIMPLE);
1245
648
            return 0;
1246
648
        }
1247
24.4k
    }
1248
    /* `descr` is borrowed. This is safe for methods (even inherited ones from
1249
    *  super classes!) as long as tp_version_tag is validated for two main reasons:
1250
    *
1251
    *  1. The class will always hold a reference to the method so it will
1252
    *  usually not be GC-ed. Should it be deleted in Python, e.g.
1253
    *  `del obj.meth`, tp_version_tag will be invalidated, because of reason 2.
1254
    *
1255
    *  2. The pre-existing type method cache (MCACHE) uses the same principles
1256
    *  of caching a borrowed descriptor. The MCACHE infrastructure does all the
1257
    *  heavy lifting for us. E.g. it invalidates tp_version_tag on any MRO
1258
    *  modification, on any type object change along said MRO, etc. (see
1259
    *  PyType_Modified usages in typeobject.c). The MCACHE has been
1260
    *  working since Python 2.6 and it's battle-tested.
1261
    */
1262
155k
    write_u32(cache->type_version, tp_version);
1263
155k
    write_ptr(cache->descr, descr);
1264
155k
    return 1;
1265
157k
}
1266
1267
1268
static void
1269
specialize_load_global_lock_held(
1270
    PyObject *globals, PyObject *builtins,
1271
    _Py_CODEUNIT *instr, PyObject *name)
1272
14.2k
{
1273
14.2k
    assert(ENABLE_SPECIALIZATION_FT);
1274
14.2k
    assert(_PyOpcode_Caches[LOAD_GLOBAL] == INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
1275
    /* Use inline cache */
1276
14.2k
    _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1);
1277
14.2k
    assert(PyUnicode_CheckExact(name));
1278
14.2k
    if (!PyDict_CheckExact(globals)) {
1279
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
1280
0
        goto fail;
1281
0
    }
1282
14.2k
    PyDictKeysObject * globals_keys = ((PyDictObject *)globals)->ma_keys;
1283
14.2k
    if (!DK_IS_UNICODE(globals_keys)) {
1284
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
1285
0
        goto fail;
1286
0
    }
1287
14.2k
    Py_ssize_t index = _PyDictKeys_StringLookup(globals_keys, name);
1288
14.2k
    if (index == DKIX_ERROR) {
1289
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
1290
0
        goto fail;
1291
0
    }
1292
14.2k
    PyInterpreterState *interp = _PyInterpreterState_GET();
1293
14.2k
    if (index != DKIX_EMPTY) {
1294
8.72k
        if (index != (uint16_t)index) {
1295
0
            SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
1296
0
            goto fail;
1297
0
        }
1298
8.72k
        uint32_t keys_version = _PyDict_GetKeysVersionForCurrentState(
1299
8.72k
                interp, (PyDictObject*) globals);
1300
8.72k
        if (keys_version == 0) {
1301
0
            SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
1302
0
            goto fail;
1303
0
        }
1304
8.72k
        if (keys_version != (uint16_t)keys_version) {
1305
0
            SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
1306
0
            goto fail;
1307
0
        }
1308
8.72k
        cache->index = (uint16_t)index;
1309
8.72k
        cache->module_keys_version = (uint16_t)keys_version;
1310
8.72k
        specialize(instr, LOAD_GLOBAL_MODULE);
1311
8.72k
        return;
1312
8.72k
    }
1313
5.53k
    if (!PyDict_CheckExact(builtins)) {
1314
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
1315
0
        goto fail;
1316
0
    }
1317
5.53k
    PyDictKeysObject * builtin_keys = ((PyDictObject *)builtins)->ma_keys;
1318
5.53k
    if (!DK_IS_UNICODE(builtin_keys)) {
1319
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
1320
0
        goto fail;
1321
0
    }
1322
5.53k
    index = _PyDictKeys_StringLookup(builtin_keys, name);
1323
5.53k
    if (index == DKIX_ERROR) {
1324
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
1325
0
        goto fail;
1326
0
    }
1327
5.53k
    if (index != (uint16_t)index) {
1328
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
1329
0
        goto fail;
1330
0
    }
1331
5.53k
    uint32_t globals_version = _PyDict_GetKeysVersionForCurrentState(
1332
5.53k
            interp, (PyDictObject*) globals);
1333
5.53k
    if (globals_version == 0) {
1334
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
1335
0
        goto fail;
1336
0
    }
1337
5.53k
    if (globals_version != (uint16_t)globals_version) {
1338
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
1339
0
        goto fail;
1340
0
    }
1341
5.53k
    uint32_t builtins_version = _PyDict_GetKeysVersionForCurrentState(
1342
5.53k
            interp, (PyDictObject*) builtins);
1343
5.53k
    if (builtins_version == 0) {
1344
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
1345
0
        goto fail;
1346
0
    }
1347
5.53k
    if (builtins_version > UINT16_MAX) {
1348
0
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
1349
0
        goto fail;
1350
0
    }
1351
5.53k
    cache->index = (uint16_t)index;
1352
5.53k
    cache->module_keys_version = (uint16_t)globals_version;
1353
5.53k
    cache->builtin_keys_version = (uint16_t)builtins_version;
1354
5.53k
    specialize(instr, LOAD_GLOBAL_BUILTIN);
1355
5.53k
    return;
1356
0
fail:
1357
0
    unspecialize(instr);
1358
0
}
1359
1360
Py_NO_INLINE void
1361
_Py_Specialize_LoadGlobal(
1362
    PyObject *globals, PyObject *builtins,
1363
    _Py_CODEUNIT *instr, PyObject *name)
1364
14.2k
{
1365
14.2k
    Py_BEGIN_CRITICAL_SECTION2(globals, builtins);
1366
14.2k
    specialize_load_global_lock_held(globals, builtins, instr, name);
1367
14.2k
    Py_END_CRITICAL_SECTION2();
1368
14.2k
}
1369
1370
static int
1371
263k
function_kind(PyCodeObject *code) {
1372
263k
    int flags = code->co_flags;
1373
263k
    if ((flags & (CO_VARKEYWORDS | CO_VARARGS)) || code->co_kwonlyargcount) {
1374
876
        return SPEC_FAIL_CODE_COMPLEX_PARAMETERS;
1375
876
    }
1376
262k
    if ((flags & CO_OPTIMIZED) == 0) {
1377
0
        return SPEC_FAIL_CODE_NOT_OPTIMIZED;
1378
0
    }
1379
262k
    return SIMPLE_FUNCTION;
1380
262k
}
1381
1382
/* Returning false indicates a failure. */
1383
static bool
1384
function_check_args(PyObject *o, int expected_argcount, int opcode)
1385
244k
{
1386
244k
    assert(Py_IS_TYPE(o, &PyFunction_Type));
1387
244k
    PyFunctionObject *func = (PyFunctionObject *)o;
1388
244k
    PyCodeObject *fcode = (PyCodeObject *)func->func_code;
1389
244k
    int kind = function_kind(fcode);
1390
244k
    if (kind != SIMPLE_FUNCTION) {
1391
0
        SPECIALIZATION_FAIL(opcode, kind);
1392
0
        return false;
1393
0
    }
1394
244k
    if (fcode->co_argcount != expected_argcount) {
1395
0
        SPECIALIZATION_FAIL(opcode, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
1396
0
        return false;
1397
0
    }
1398
244k
    return true;
1399
244k
}
1400
1401
/* Returning 0 indicates a failure. */
1402
static uint32_t
1403
function_get_version(PyObject *o, int opcode)
1404
0
{
1405
0
    assert(Py_IS_TYPE(o, &PyFunction_Type));
1406
0
    PyFunctionObject *func = (PyFunctionObject *)o;
1407
0
    uint32_t version = _PyFunction_GetVersionForCurrentState(func);
1408
0
    if (!_PyFunction_IsVersionValid(version)) {
1409
0
        SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS);
1410
0
        return 0;
1411
0
    }
1412
0
    return version;
1413
0
}
1414
1415
#ifdef Py_STATS
1416
static int
1417
store_subscr_fail_kind(PyObject *container, PyObject *sub)
1418
{
1419
    PyTypeObject *container_type = Py_TYPE(container);
1420
    PyMappingMethods *as_mapping = container_type->tp_as_mapping;
1421
    if (as_mapping && (as_mapping->mp_ass_subscript
1422
                       == PyDict_Type.tp_as_mapping->mp_ass_subscript)) {
1423
        return SPEC_FAIL_SUBSCR_DICT_SUBCLASS_NO_OVERRIDE;
1424
    }
1425
    if (PyObject_CheckBuffer(container)) {
1426
        if (PyLong_CheckExact(sub) && (!_PyLong_IsNonNegativeCompact((PyLongObject *)sub))) {
1427
            return SPEC_FAIL_OUT_OF_RANGE;
1428
        }
1429
        else if (strcmp(container_type->tp_name, "array.array") == 0) {
1430
            if (PyLong_CheckExact(sub)) {
1431
                return SPEC_FAIL_SUBSCR_ARRAY_INT;
1432
            }
1433
            else if (PySlice_Check(sub)) {
1434
                return SPEC_FAIL_SUBSCR_ARRAY_SLICE;
1435
            }
1436
            else {
1437
                return SPEC_FAIL_OTHER;
1438
            }
1439
        }
1440
        else if (PyByteArray_CheckExact(container)) {
1441
            if (PyLong_CheckExact(sub)) {
1442
                return SPEC_FAIL_SUBSCR_BYTEARRAY_INT;
1443
            }
1444
            else if (PySlice_Check(sub)) {
1445
                return SPEC_FAIL_SUBSCR_BYTEARRAY_SLICE;
1446
            }
1447
            else {
1448
                return SPEC_FAIL_OTHER;
1449
            }
1450
        }
1451
        else {
1452
            if (PyLong_CheckExact(sub)) {
1453
                return SPEC_FAIL_SUBSCR_BUFFER_INT;
1454
            }
1455
            else if (PySlice_Check(sub)) {
1456
                return SPEC_FAIL_SUBSCR_BUFFER_SLICE;
1457
            }
1458
            else {
1459
                return SPEC_FAIL_OTHER;
1460
            }
1461
        }
1462
        return SPEC_FAIL_OTHER;
1463
    }
1464
    PyObject *descriptor = _PyType_Lookup(container_type, &_Py_ID(__setitem__));
1465
    if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type) {
1466
        PyFunctionObject *func = (PyFunctionObject *)descriptor;
1467
        PyCodeObject *code = (PyCodeObject *)func->func_code;
1468
        int kind = function_kind(code);
1469
        if (kind == SIMPLE_FUNCTION) {
1470
            return SPEC_FAIL_SUBSCR_PY_SIMPLE;
1471
        }
1472
        else {
1473
            return SPEC_FAIL_SUBSCR_PY_OTHER;
1474
        }
1475
    }
1476
    return SPEC_FAIL_OTHER;
1477
}
1478
#endif
1479
1480
Py_NO_INLINE void
1481
_Py_Specialize_StoreSubscr(_PyStackRef container_st, _PyStackRef sub_st, _Py_CODEUNIT *instr)
1482
1.22k
{
1483
1.22k
    PyObject *container = PyStackRef_AsPyObjectBorrow(container_st);
1484
1.22k
    PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
1485
1486
1.22k
    assert(ENABLE_SPECIALIZATION_FT);
1487
1.22k
    PyTypeObject *container_type = Py_TYPE(container);
1488
1.22k
    if (container_type == &PyList_Type) {
1489
170
        if (PyLong_CheckExact(sub)) {
1490
168
            if (_PyLong_IsNonNegativeCompact((PyLongObject *)sub)
1491
112
                && ((PyLongObject *)sub)->long_value.ob_digit[0] < (size_t)PyList_GET_SIZE(container))
1492
112
            {
1493
112
                specialize(instr, STORE_SUBSCR_LIST_INT);
1494
112
                return;
1495
112
            }
1496
56
            else {
1497
56
                SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
1498
56
                unspecialize(instr);
1499
56
                return;
1500
56
            }
1501
168
        }
1502
2
        else if (PySlice_Check(sub)) {
1503
2
            SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_SUBSCR_LIST_SLICE);
1504
2
            unspecialize(instr);
1505
2
            return;
1506
2
        }
1507
0
        else {
1508
0
            SPECIALIZATION_FAIL(STORE_SUBSCR, SPEC_FAIL_OTHER);
1509
0
            unspecialize(instr);
1510
0
            return;
1511
0
        }
1512
170
    }
1513
1.05k
    if (container_type == &PyDict_Type) {
1514
745
        specialize(instr, STORE_SUBSCR_DICT);
1515
745
        return;
1516
745
    }
1517
311
    SPECIALIZATION_FAIL(STORE_SUBSCR, store_subscr_fail_kind(container, sub));
1518
311
    unspecialize(instr);
1519
311
}
1520
1521
/* Returns a strong reference. */
1522
static PyObject *
1523
get_init_for_simple_managed_python_class(PyTypeObject *tp, unsigned int *tp_version)
1524
485
{
1525
485
    assert(tp->tp_new == PyBaseObject_Type.tp_new);
1526
485
    if (tp->tp_alloc != PyType_GenericAlloc) {
1527
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OVERRIDDEN);
1528
0
        return NULL;
1529
0
    }
1530
485
    unsigned long tp_flags = PyType_GetFlags(tp);
1531
485
    if (!(tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1532
        /* Is this possible? */
1533
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_EXPECTED_ERROR);
1534
0
        return NULL;
1535
0
    }
1536
485
    PyObject *init = _PyType_LookupRefAndVersion(tp, &_Py_ID(__init__), tp_version);
1537
485
    if (init == NULL || !PyFunction_Check(init)) {
1538
258
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_INIT_NOT_PYTHON);
1539
258
        Py_XDECREF(init);
1540
258
        return NULL;
1541
258
    }
1542
227
    int kind = function_kind((PyCodeObject *)PyFunction_GET_CODE(init));
1543
227
    if (kind != SIMPLE_FUNCTION) {
1544
17
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_INIT_NOT_SIMPLE);
1545
17
        Py_DECREF(init);
1546
17
        return NULL;
1547
17
    }
1548
210
    return init;
1549
227
}
1550
1551
static int
1552
specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
1553
1.65k
{
1554
1.65k
    assert(PyType_Check(callable));
1555
1.65k
    PyTypeObject *tp = _PyType_CAST(callable);
1556
1.65k
    if (tp->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
1557
962
        int oparg = instr->op.arg;
1558
962
        if (nargs == 1 && oparg == 1) {
1559
614
            if (tp == &PyUnicode_Type) {
1560
74
                specialize(instr, CALL_STR_1);
1561
74
                return 0;
1562
74
            }
1563
540
            else if (tp == &PyType_Type) {
1564
119
                specialize(instr, CALL_TYPE_1);
1565
119
                return 0;
1566
119
            }
1567
421
            else if (tp == &PyTuple_Type) {
1568
48
                specialize(instr, CALL_TUPLE_1);
1569
48
                return 0;
1570
48
            }
1571
614
        }
1572
721
        if (tp->tp_vectorcall != NULL) {
1573
474
            specialize(instr, CALL_BUILTIN_CLASS);
1574
474
            return 0;
1575
474
        }
1576
247
        goto generic;
1577
721
    }
1578
689
    if (Py_TYPE(tp) != &PyType_Type) {
1579
21
        goto generic;
1580
21
    }
1581
668
    if (tp->tp_new == PyBaseObject_Type.tp_new) {
1582
485
        unsigned int tp_version = 0;
1583
485
        PyObject *init = get_init_for_simple_managed_python_class(tp, &tp_version);
1584
485
        if (!tp_version) {
1585
12
            SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OUT_OF_VERSIONS);
1586
12
            Py_XDECREF(init);
1587
12
            return -1;
1588
12
        }
1589
473
        if (init != NULL && _PyType_CacheInitForSpecialization(
1590
198
                                (PyHeapTypeObject *)tp, init, tp_version)) {
1591
198
            _PyCallCache *cache = (_PyCallCache *)(instr + 1);
1592
198
            write_u32(cache->func_version, tp_version);
1593
198
            specialize(instr, CALL_ALLOC_AND_ENTER_INIT);
1594
198
            Py_DECREF(init);
1595
198
            return 0;
1596
198
        }
1597
275
        Py_XDECREF(init);
1598
275
    }
1599
726
generic:
1600
726
    specialize(instr, CALL_NON_PY_GENERAL);
1601
726
    return 0;
1602
668
}
1603
1604
static int
1605
specialize_method_descriptor(PyMethodDescrObject *descr, _Py_CODEUNIT *instr,
1606
                             int nargs)
1607
524k
{
1608
524k
    switch (descr->d_method->ml_flags &
1609
524k
        (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O |
1610
524k
        METH_KEYWORDS | METH_METHOD)) {
1611
692
        case METH_NOARGS: {
1612
692
            if (nargs != 1) {
1613
0
                SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
1614
0
                return -1;
1615
0
            }
1616
692
            specialize(instr, CALL_METHOD_DESCRIPTOR_NOARGS);
1617
692
            return 0;
1618
692
        }
1619
432k
        case METH_O: {
1620
432k
            if (nargs != 2) {
1621
0
                SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
1622
0
                return -1;
1623
0
            }
1624
432k
            PyInterpreterState *interp = _PyInterpreterState_GET();
1625
432k
            PyObject *list_append = interp->callable_cache.list_append;
1626
432k
            _Py_CODEUNIT next = instr[INLINE_CACHE_ENTRIES_CALL + 1];
1627
432k
            bool pop = (next.op.code == POP_TOP);
1628
432k
            int oparg = instr->op.arg;
1629
432k
            if ((PyObject *)descr == list_append && oparg == 1 && pop) {
1630
425k
                specialize(instr, CALL_LIST_APPEND);
1631
425k
                return 0;
1632
425k
            }
1633
7.47k
            specialize(instr, CALL_METHOD_DESCRIPTOR_O);
1634
7.47k
            return 0;
1635
432k
        }
1636
1.73k
        case METH_FASTCALL: {
1637
1.73k
            specialize(instr, CALL_METHOD_DESCRIPTOR_FAST);
1638
1.73k
            return 0;
1639
432k
        }
1640
89.5k
        case METH_FASTCALL | METH_KEYWORDS: {
1641
89.5k
            specialize(instr, CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS);
1642
89.5k
            return 0;
1643
432k
        }
1644
524k
    }
1645
210
    specialize(instr, CALL_NON_PY_GENERAL);
1646
210
    return 0;
1647
524k
}
1648
1649
static int
1650
specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
1651
                   bool bound_method)
1652
18.6k
{
1653
18.6k
    _PyCallCache *cache = (_PyCallCache *)(instr + 1);
1654
18.6k
    PyCodeObject *code = (PyCodeObject *)func->func_code;
1655
18.6k
    int kind = function_kind(code);
1656
    /* Don't specialize if PEP 523 is active */
1657
18.6k
    if (_PyInterpreterState_GET()->eval_frame) {
1658
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523);
1659
0
        return -1;
1660
0
    }
1661
18.6k
    if (func->vectorcall != _PyFunction_Vectorcall) {
1662
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL);
1663
0
        return -1;
1664
0
    }
1665
18.6k
    int argcount = -1;
1666
18.6k
    if (kind == SPEC_FAIL_CODE_NOT_OPTIMIZED) {
1667
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CODE_NOT_OPTIMIZED);
1668
0
        return -1;
1669
0
    }
1670
18.6k
    if (kind == SIMPLE_FUNCTION) {
1671
17.9k
        argcount = code->co_argcount;
1672
17.9k
    }
1673
18.6k
    int version = _PyFunction_GetVersionForCurrentState(func);
1674
18.6k
    if (!_PyFunction_IsVersionValid(version)) {
1675
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OUT_OF_VERSIONS);
1676
0
        return -1;
1677
0
    }
1678
18.6k
    write_u32(cache->func_version, version);
1679
18.6k
    uint8_t opcode;
1680
18.6k
    if (argcount == nargs + bound_method) {
1681
17.7k
        opcode =
1682
17.7k
            bound_method ? CALL_BOUND_METHOD_EXACT_ARGS : CALL_PY_EXACT_ARGS;
1683
17.7k
    }
1684
861
    else {
1685
861
        opcode = bound_method ? CALL_BOUND_METHOD_GENERAL : CALL_PY_GENERAL;
1686
861
    }
1687
18.6k
    specialize(instr, opcode);
1688
18.6k
    return 0;
1689
18.6k
}
1690
1691
1692
static int
1693
specialize_py_call_kw(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
1694
                   bool bound_method)
1695
191
{
1696
191
    _PyCallCache *cache = (_PyCallCache *)(instr + 1);
1697
191
    PyCodeObject *code = (PyCodeObject *)func->func_code;
1698
191
    int kind = function_kind(code);
1699
    /* Don't specialize if PEP 523 is active */
1700
191
    if (_PyInterpreterState_GET()->eval_frame) {
1701
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_PEP_523);
1702
0
        return -1;
1703
0
    }
1704
191
    if (func->vectorcall != _PyFunction_Vectorcall) {
1705
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_VECTORCALL);
1706
0
        return -1;
1707
0
    }
1708
191
    if (kind == SPEC_FAIL_CODE_NOT_OPTIMIZED) {
1709
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CODE_NOT_OPTIMIZED);
1710
0
        return -1;
1711
0
    }
1712
191
    int version = _PyFunction_GetVersionForCurrentState(func);
1713
191
    if (!_PyFunction_IsVersionValid(version)) {
1714
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OUT_OF_VERSIONS);
1715
0
        return -1;
1716
0
    }
1717
191
    write_u32(cache->func_version, version);
1718
191
    specialize(instr, bound_method ? CALL_KW_BOUND_METHOD : CALL_KW_PY);
1719
191
    return 0;
1720
191
}
1721
1722
static int
1723
specialize_c_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
1724
1.41M
{
1725
1.41M
    if (PyCFunction_GET_FUNCTION(callable) == NULL) {
1726
0
        SPECIALIZATION_FAIL(CALL, SPEC_FAIL_OTHER);
1727
0
        return 1;
1728
0
    }
1729
1.41M
    switch (PyCFunction_GET_FLAGS(callable) &
1730
1.41M
        (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O |
1731
1.41M
        METH_KEYWORDS | METH_METHOD)) {
1732
1.40M
        case METH_O: {
1733
1.40M
            if (nargs != 1) {
1734
0
                SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
1735
0
                return 1;
1736
0
            }
1737
            /* len(o) */
1738
1.40M
            PyInterpreterState *interp = _PyInterpreterState_GET();
1739
1.40M
            if (callable == interp->callable_cache.len && instr->op.arg == 1) {
1740
817
                specialize(instr, CALL_LEN);
1741
817
                return 0;
1742
817
            }
1743
1.40M
            specialize(instr, CALL_BUILTIN_O);
1744
1.40M
            return 0;
1745
1.40M
        }
1746
1.90k
        case METH_FASTCALL: {
1747
1.90k
            if (nargs == 2) {
1748
                /* isinstance(o1, o2) */
1749
1.26k
                PyInterpreterState *interp = _PyInterpreterState_GET();
1750
1.26k
                if (callable == interp->callable_cache.isinstance && instr->op.arg == 2) {
1751
649
                    specialize(instr, CALL_ISINSTANCE);
1752
649
                    return 0;
1753
649
                }
1754
1.26k
            }
1755
1.26k
            specialize(instr, CALL_BUILTIN_FAST);
1756
1.26k
            return 0;
1757
1.90k
        }
1758
468
        case METH_FASTCALL | METH_KEYWORDS: {
1759
468
            specialize(instr, CALL_BUILTIN_FAST_WITH_KEYWORDS);
1760
468
            return 0;
1761
1.90k
        }
1762
302
        default:
1763
302
            specialize(instr, CALL_NON_PY_GENERAL);
1764
302
            return 0;
1765
1.41M
    }
1766
1.41M
}
1767
1768
Py_NO_INLINE void
1769
_Py_Specialize_Call(_PyStackRef callable_st, _Py_CODEUNIT *instr, int nargs)
1770
1.96M
{
1771
1.96M
    PyObject *callable = PyStackRef_AsPyObjectBorrow(callable_st);
1772
1773
1.96M
    assert(ENABLE_SPECIALIZATION_FT);
1774
1.96M
    assert(_PyOpcode_Caches[CALL] == INLINE_CACHE_ENTRIES_CALL);
1775
1.96M
    assert(_Py_OPCODE(*instr) != INSTRUMENTED_CALL);
1776
1.96M
    int fail;
1777
1.96M
    if (PyCFunction_CheckExact(callable)) {
1778
1.41M
        fail = specialize_c_call(callable, instr, nargs);
1779
1.41M
    }
1780
554k
    else if (PyFunction_Check(callable)) {
1781
16.1k
        fail = specialize_py_call((PyFunctionObject *)callable, instr, nargs, false);
1782
16.1k
    }
1783
538k
    else if (PyType_Check(callable)) {
1784
1.65k
        fail = specialize_class_call(callable, instr, nargs);
1785
1.65k
    }
1786
536k
    else if (Py_IS_TYPE(callable, &PyMethodDescr_Type)) {
1787
524k
        fail = specialize_method_descriptor((PyMethodDescrObject *)callable, instr, nargs);
1788
524k
    }
1789
11.5k
    else if (PyMethod_Check(callable)) {
1790
2.49k
        PyObject *func = ((PyMethodObject *)callable)->im_func;
1791
2.49k
        if (PyFunction_Check(func)) {
1792
2.49k
            fail = specialize_py_call((PyFunctionObject *)func, instr, nargs, true);
1793
2.49k
        }
1794
0
        else {
1795
0
            SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_BOUND_METHOD);
1796
0
            fail = -1;
1797
0
        }
1798
2.49k
    }
1799
9.06k
    else {
1800
9.06k
        specialize(instr, CALL_NON_PY_GENERAL);
1801
9.06k
        fail = 0;
1802
9.06k
    }
1803
1.96M
    if (fail) {
1804
12
        unspecialize(instr);
1805
12
    }
1806
1.96M
}
1807
1808
Py_NO_INLINE void
1809
_Py_Specialize_CallKw(_PyStackRef callable_st, _Py_CODEUNIT *instr, int nargs)
1810
515
{
1811
515
    PyObject *callable = PyStackRef_AsPyObjectBorrow(callable_st);
1812
1813
515
    assert(ENABLE_SPECIALIZATION_FT);
1814
515
    assert(_PyOpcode_Caches[CALL_KW] == INLINE_CACHE_ENTRIES_CALL_KW);
1815
515
    assert(_Py_OPCODE(*instr) != INSTRUMENTED_CALL_KW);
1816
515
    int fail;
1817
515
    if (PyFunction_Check(callable)) {
1818
189
        fail = specialize_py_call_kw((PyFunctionObject *)callable, instr, nargs, false);
1819
189
    }
1820
326
    else if (PyMethod_Check(callable)) {
1821
2
        PyObject *func = ((PyMethodObject *)callable)->im_func;
1822
2
        if (PyFunction_Check(func)) {
1823
2
            fail = specialize_py_call_kw((PyFunctionObject *)func, instr, nargs, true);
1824
2
        }
1825
0
        else {
1826
0
            SPECIALIZATION_FAIL(CALL_KW, SPEC_FAIL_CALL_BOUND_METHOD);
1827
0
            fail = -1;
1828
0
        }
1829
2
    }
1830
324
    else {
1831
324
        specialize(instr, CALL_KW_NON_PY);
1832
324
        fail = 0;
1833
324
    }
1834
515
    if (fail) {
1835
0
        unspecialize(instr);
1836
0
    }
1837
515
}
1838
1839
#ifdef Py_STATS
1840
static int
1841
binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs)
1842
{
1843
    switch (oparg) {
1844
        case NB_ADD:
1845
        case NB_INPLACE_ADD:
1846
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1847
                return SPEC_FAIL_BINARY_OP_ADD_DIFFERENT_TYPES;
1848
            }
1849
            return SPEC_FAIL_BINARY_OP_ADD_OTHER;
1850
        case NB_AND:
1851
        case NB_INPLACE_AND:
1852
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1853
                return SPEC_FAIL_BINARY_OP_AND_DIFFERENT_TYPES;
1854
            }
1855
            if (PyLong_CheckExact(lhs)) {
1856
                return SPEC_FAIL_BINARY_OP_AND_INT;
1857
            }
1858
            return SPEC_FAIL_BINARY_OP_AND_OTHER;
1859
        case NB_FLOOR_DIVIDE:
1860
        case NB_INPLACE_FLOOR_DIVIDE:
1861
            return SPEC_FAIL_BINARY_OP_FLOOR_DIVIDE;
1862
        case NB_LSHIFT:
1863
        case NB_INPLACE_LSHIFT:
1864
            return SPEC_FAIL_BINARY_OP_LSHIFT;
1865
        case NB_MATRIX_MULTIPLY:
1866
        case NB_INPLACE_MATRIX_MULTIPLY:
1867
            return SPEC_FAIL_BINARY_OP_MATRIX_MULTIPLY;
1868
        case NB_MULTIPLY:
1869
        case NB_INPLACE_MULTIPLY:
1870
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1871
                return SPEC_FAIL_BINARY_OP_MULTIPLY_DIFFERENT_TYPES;
1872
            }
1873
            return SPEC_FAIL_BINARY_OP_MULTIPLY_OTHER;
1874
        case NB_OR:
1875
        case NB_INPLACE_OR:
1876
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1877
                return SPEC_FAIL_BINARY_OP_OR_DIFFERENT_TYPES;
1878
            }
1879
            if (PyLong_CheckExact(lhs)) {
1880
                return SPEC_FAIL_BINARY_OP_OR_INT;
1881
            }
1882
            return SPEC_FAIL_BINARY_OP_OR;
1883
        case NB_POWER:
1884
        case NB_INPLACE_POWER:
1885
            return SPEC_FAIL_BINARY_OP_POWER;
1886
        case NB_REMAINDER:
1887
        case NB_INPLACE_REMAINDER:
1888
            return SPEC_FAIL_BINARY_OP_REMAINDER;
1889
        case NB_RSHIFT:
1890
        case NB_INPLACE_RSHIFT:
1891
            return SPEC_FAIL_BINARY_OP_RSHIFT;
1892
        case NB_SUBTRACT:
1893
        case NB_INPLACE_SUBTRACT:
1894
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1895
                return SPEC_FAIL_BINARY_OP_SUBTRACT_DIFFERENT_TYPES;
1896
            }
1897
            return SPEC_FAIL_BINARY_OP_SUBTRACT_OTHER;
1898
        case NB_TRUE_DIVIDE:
1899
        case NB_INPLACE_TRUE_DIVIDE:
1900
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1901
                return SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_DIFFERENT_TYPES;
1902
            }
1903
            if (PyFloat_CheckExact(lhs)) {
1904
                return SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_FLOAT;
1905
            }
1906
            return SPEC_FAIL_BINARY_OP_TRUE_DIVIDE_OTHER;
1907
        case NB_XOR:
1908
        case NB_INPLACE_XOR:
1909
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
1910
                return SPEC_FAIL_BINARY_OP_XOR_DIFFERENT_TYPES;
1911
            }
1912
            if (PyLong_CheckExact(lhs)) {
1913
                return SPEC_FAIL_BINARY_OP_XOR_INT;
1914
            }
1915
            return SPEC_FAIL_BINARY_OP_XOR;
1916
        case NB_SUBSCR:
1917
            if (PyList_CheckExact(lhs)) {
1918
                if (PyLong_CheckExact(rhs) && !_PyLong_IsNonNegativeCompact((PyLongObject *)rhs)) {
1919
                    return SPEC_FAIL_OUT_OF_RANGE;
1920
                }
1921
                if (PySlice_Check(rhs)) {
1922
                    return SPEC_FAIL_BINARY_OP_SUBSCR_LIST_SLICE;
1923
                }
1924
            }
1925
            if (PyTuple_CheckExact(lhs)) {
1926
                if (PyLong_CheckExact(rhs) && !_PyLong_IsNonNegativeCompact((PyLongObject *)rhs)) {
1927
                    return SPEC_FAIL_OUT_OF_RANGE;
1928
                }
1929
                if (PySlice_Check(rhs)) {
1930
                    return SPEC_FAIL_BINARY_OP_SUBSCR_TUPLE_SLICE;
1931
                }
1932
            }
1933
            if (PyUnicode_CheckExact(lhs)) {
1934
                if (PyLong_CheckExact(rhs) && !_PyLong_IsNonNegativeCompact((PyLongObject *)rhs)) {
1935
                    return SPEC_FAIL_OUT_OF_RANGE;
1936
                }
1937
                if (PySlice_Check(rhs)) {
1938
                    return SPEC_FAIL_BINARY_OP_SUBSCR_STRING_SLICE;
1939
                }
1940
            }
1941
            unsigned int tp_version;
1942
            PyTypeObject *container_type = Py_TYPE(lhs);
1943
            PyObject *descriptor = _PyType_LookupRefAndVersion(container_type, &_Py_ID(__getitem__), &tp_version);
1944
            if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type) {
1945
                if (!(container_type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1946
                    Py_DECREF(descriptor);
1947
                    return SPEC_FAIL_BINARY_OP_SUBSCR_NOT_HEAP_TYPE;
1948
                }
1949
                PyFunctionObject *func = (PyFunctionObject *)descriptor;
1950
                PyCodeObject *fcode = (PyCodeObject *)func->func_code;
1951
                int kind = function_kind(fcode);
1952
                if (kind != SIMPLE_FUNCTION) {
1953
                    Py_DECREF(descriptor);
1954
                    return kind;
1955
                }
1956
                if (fcode->co_argcount != 2) {
1957
                    Py_DECREF(descriptor);
1958
                    return SPEC_FAIL_WRONG_NUMBER_ARGUMENTS;
1959
                }
1960
1961
                if (_PyInterpreterState_GET()->eval_frame) {
1962
                    /* Don't specialize if PEP 523 is active */
1963
                    Py_DECREF(descriptor);
1964
                    return SPEC_FAIL_OTHER;
1965
                }
1966
            }
1967
            Py_XDECREF(descriptor);
1968
1969
            if (PyObject_TypeCheck(lhs, &PyDictProxy_Type)) {
1970
                return SPEC_FAIL_BINARY_OP_SUBSCR_MAPPINGPROXY;
1971
            }
1972
1973
            if (PyObject_TypeCheck(lhs, &PyBytes_Type)) {
1974
                return SPEC_FAIL_BINARY_OP_SUBSCR_BYTES;
1975
            }
1976
1977
            if (PyObject_TypeCheck(lhs, &PyRange_Type)) {
1978
                return SPEC_FAIL_BINARY_OP_SUBSCR_RANGE;
1979
            }
1980
1981
            if (strcmp(container_type->tp_name, "array.array") == 0) {
1982
                return SPEC_FAIL_BINARY_OP_SUBSCR_ARRAY;
1983
            }
1984
1985
            if (strcmp(container_type->tp_name, "re.Match") == 0) {
1986
                return SPEC_FAIL_BINARY_OP_SUBSCR_RE_MATCH;
1987
            }
1988
1989
            if (strcmp(container_type->tp_name, "collections.deque") == 0) {
1990
                return SPEC_FAIL_BINARY_OP_SUBSCR_DEQUE;
1991
            }
1992
1993
            if (strcmp(_PyType_Name(container_type), "EnumDict") == 0) {
1994
                return SPEC_FAIL_BINARY_OP_SUBSCR_ENUMDICT;
1995
            }
1996
1997
            if (strcmp(container_type->tp_name, "StackSummary") == 0) {
1998
                return SPEC_FAIL_BINARY_OP_SUBSCR_STACKSUMMARY;
1999
            }
2000
2001
            if (strcmp(container_type->tp_name, "collections.defaultdict") == 0) {
2002
                return SPEC_FAIL_BINARY_OP_SUBSCR_DEFAULTDICT;
2003
            }
2004
2005
            if (strcmp(container_type->tp_name, "Counter") == 0) {
2006
                return SPEC_FAIL_BINARY_OP_SUBSCR_COUNTER;
2007
            }
2008
2009
            if (strcmp(container_type->tp_name, "collections.OrderedDict") == 0) {
2010
                return SPEC_FAIL_BINARY_OP_SUBSCR_ORDEREDDICT;
2011
            }
2012
2013
            if (strcmp(container_type->tp_name, "time.struct_time") == 0) {
2014
                return SPEC_FAIL_BINARY_OP_SUBSCR_STRUCTTIME;
2015
            }
2016
2017
            if (PySlice_Check(rhs)) {
2018
                return SPEC_FAIL_BINARY_OP_SUBSCR_OTHER_SLICE;
2019
            }
2020
            return SPEC_FAIL_BINARY_OP_SUBSCR;
2021
    }
2022
    Py_UNREACHABLE();
2023
}
2024
#endif
2025
2026
/** Binary Op Specialization Extensions */
2027
2028
/* long-long */
2029
2030
static inline int
2031
is_compactlong(PyObject *v)
2032
1.94M
{
2033
1.94M
    return PyLong_CheckExact(v) &&
2034
1.94M
           _PyLong_IsCompact((PyLongObject *)v);
2035
1.94M
}
2036
2037
static int
2038
compactlongs_guard(PyObject *lhs, PyObject *rhs)
2039
972k
{
2040
972k
    return (is_compactlong(lhs) && is_compactlong(rhs));
2041
972k
}
2042
2043
#define BITWISE_LONGS_ACTION(NAME, OP) \
2044
    static PyObject * \
2045
    (NAME)(PyObject *lhs, PyObject *rhs) \
2046
971k
    { \
2047
971k
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2048
971k
        Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
2049
971k
        return PyLong_FromSsize_t(lhs_val OP rhs_val); \
2050
971k
    }
specialize.c:compactlongs_or
Line
Count
Source
2046
948k
    { \
2047
948k
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2048
948k
        Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
2049
948k
        return PyLong_FromSsize_t(lhs_val OP rhs_val); \
2050
948k
    }
specialize.c:compactlongs_and
Line
Count
Source
2046
22.3k
    { \
2047
22.3k
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2048
22.3k
        Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
2049
22.3k
        return PyLong_FromSsize_t(lhs_val OP rhs_val); \
2050
22.3k
    }
specialize.c:compactlongs_xor
Line
Count
Source
2046
22
    { \
2047
22
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2048
22
        Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
2049
22
        return PyLong_FromSsize_t(lhs_val OP rhs_val); \
2050
22
    }
2051
BITWISE_LONGS_ACTION(compactlongs_or, |)
2052
BITWISE_LONGS_ACTION(compactlongs_and, &)
2053
BITWISE_LONGS_ACTION(compactlongs_xor, ^)
2054
#undef BITWISE_LONGS_ACTION
2055
2056
/* float-long */
2057
2058
static inline int
2059
float_compactlong_guard(PyObject *lhs, PyObject *rhs)
2060
5.89M
{
2061
5.89M
    return (
2062
5.89M
        PyFloat_CheckExact(lhs) &&
2063
5.88M
        !isnan(PyFloat_AsDouble(lhs)) &&
2064
5.89M
        PyLong_CheckExact(rhs) &&
2065
5.88M
        _PyLong_IsCompact((PyLongObject *)rhs)
2066
5.89M
    );
2067
5.89M
}
2068
2069
static inline int
2070
nonzero_float_compactlong_guard(PyObject *lhs, PyObject *rhs)
2071
2.94M
{
2072
2.94M
    return (
2073
2.94M
        float_compactlong_guard(lhs, rhs) && !PyLong_IsZero(rhs)
2074
2.94M
    );
2075
2.94M
}
2076
2077
#define FLOAT_LONG_ACTION(NAME, OP) \
2078
    static PyObject * \
2079
    (NAME)(PyObject *lhs, PyObject *rhs) \
2080
5.88M
    { \
2081
5.88M
        double lhs_val = PyFloat_AsDouble(lhs); \
2082
5.88M
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2083
5.88M
        return PyFloat_FromDouble(lhs_val OP rhs_val); \
2084
5.88M
    }
Unexecuted instantiation: specialize.c:float_compactlong_add
Unexecuted instantiation: specialize.c:float_compactlong_subtract
specialize.c:float_compactlong_true_div
Line
Count
Source
2080
2.94M
    { \
2081
2.94M
        double lhs_val = PyFloat_AsDouble(lhs); \
2082
2.94M
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2083
2.94M
        return PyFloat_FromDouble(lhs_val OP rhs_val); \
2084
2.94M
    }
specialize.c:float_compactlong_multiply
Line
Count
Source
2080
2.94M
    { \
2081
2.94M
        double lhs_val = PyFloat_AsDouble(lhs); \
2082
2.94M
        Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \
2083
2.94M
        return PyFloat_FromDouble(lhs_val OP rhs_val); \
2084
2.94M
    }
2085
FLOAT_LONG_ACTION(float_compactlong_add, +)
2086
FLOAT_LONG_ACTION(float_compactlong_subtract, -)
2087
FLOAT_LONG_ACTION(float_compactlong_multiply, *)
2088
FLOAT_LONG_ACTION(float_compactlong_true_div, /)
2089
#undef FLOAT_LONG_ACTION
2090
2091
/*  long-float */
2092
2093
static inline int
2094
compactlong_float_guard(PyObject *lhs, PyObject *rhs)
2095
1.83k
{
2096
1.83k
    return (
2097
1.83k
        PyLong_CheckExact(lhs) &&
2098
1.04k
        _PyLong_IsCompact((PyLongObject *)lhs) &&
2099
1.83k
        PyFloat_CheckExact(rhs) &&
2100
0
        !isnan(PyFloat_AsDouble(rhs))
2101
1.83k
    );
2102
1.83k
}
2103
2104
static inline int
2105
nonzero_compactlong_float_guard(PyObject *lhs, PyObject *rhs)
2106
22
{
2107
22
    return (
2108
22
        compactlong_float_guard(lhs, rhs) && PyFloat_AsDouble(rhs) != 0.0
2109
22
    );
2110
22
}
2111
2112
#define LONG_FLOAT_ACTION(NAME, OP) \
2113
    static PyObject * \
2114
    (NAME)(PyObject *lhs, PyObject *rhs) \
2115
0
    { \
2116
0
        double rhs_val = PyFloat_AsDouble(rhs); \
2117
0
        Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \
2118
0
        return PyFloat_FromDouble(lhs_val OP rhs_val); \
2119
0
    }
Unexecuted instantiation: specialize.c:compactlong_float_add
Unexecuted instantiation: specialize.c:compactlong_float_subtract
Unexecuted instantiation: specialize.c:compactlong_float_true_div
Unexecuted instantiation: specialize.c:compactlong_float_multiply
2120
LONG_FLOAT_ACTION(compactlong_float_add, +)
2121
LONG_FLOAT_ACTION(compactlong_float_subtract, -)
2122
LONG_FLOAT_ACTION(compactlong_float_multiply, *)
2123
LONG_FLOAT_ACTION(compactlong_float_true_div, /)
2124
#undef LONG_FLOAT_ACTION
2125
2126
static _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = {
2127
    /* long-long arithmetic */
2128
    {NB_OR, compactlongs_guard, compactlongs_or},
2129
    {NB_AND, compactlongs_guard, compactlongs_and},
2130
    {NB_XOR, compactlongs_guard, compactlongs_xor},
2131
    {NB_INPLACE_OR, compactlongs_guard, compactlongs_or},
2132
    {NB_INPLACE_AND, compactlongs_guard, compactlongs_and},
2133
    {NB_INPLACE_XOR, compactlongs_guard, compactlongs_xor},
2134
2135
    /* float-long arithemetic */
2136
    {NB_ADD, float_compactlong_guard, float_compactlong_add},
2137
    {NB_SUBTRACT, float_compactlong_guard, float_compactlong_subtract},
2138
    {NB_TRUE_DIVIDE, nonzero_float_compactlong_guard, float_compactlong_true_div},
2139
    {NB_MULTIPLY, float_compactlong_guard, float_compactlong_multiply},
2140
2141
    /* float-float arithmetic */
2142
    {NB_ADD, compactlong_float_guard, compactlong_float_add},
2143
    {NB_SUBTRACT, compactlong_float_guard, compactlong_float_subtract},
2144
    {NB_TRUE_DIVIDE, nonzero_compactlong_float_guard, compactlong_float_true_div},
2145
    {NB_MULTIPLY, compactlong_float_guard, compactlong_float_multiply},
2146
};
2147
2148
static int
2149
binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
2150
                                  _PyBinaryOpSpecializationDescr **descr)
2151
27.9k
{
2152
27.9k
    size_t n = sizeof(binaryop_extend_descrs)/sizeof(_PyBinaryOpSpecializationDescr);
2153
413k
    for (size_t i = 0; i < n; i++) {
2154
385k
        _PyBinaryOpSpecializationDescr *d = &binaryop_extend_descrs[i];
2155
385k
        if (d->oparg == oparg && d->guard(lhs, rhs)) {
2156
511
            *descr = d;
2157
511
            return 1;
2158
511
        }
2159
385k
    }
2160
27.4k
    return 0;
2161
27.9k
}
2162
2163
Py_NO_INLINE void
2164
_Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *instr,
2165
                        int oparg, _PyStackRef *locals)
2166
1.03M
{
2167
1.03M
    PyObject *lhs = PyStackRef_AsPyObjectBorrow(lhs_st);
2168
1.03M
    PyObject *rhs = PyStackRef_AsPyObjectBorrow(rhs_st);
2169
1.03M
    assert(ENABLE_SPECIALIZATION_FT);
2170
1.03M
    assert(_PyOpcode_Caches[BINARY_OP] == INLINE_CACHE_ENTRIES_BINARY_OP);
2171
2172
1.03M
    _PyBinaryOpCache *cache = (_PyBinaryOpCache *)(instr + 1);
2173
1.03M
    if (instr->op.code == BINARY_OP_EXTEND) {
2174
0
        write_ptr(cache->external_cache, NULL);
2175
0
    }
2176
2177
1.03M
    switch (oparg) {
2178
1.09k
        case NB_ADD:
2179
1.93k
        case NB_INPLACE_ADD:
2180
1.93k
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
2181
26
                break;
2182
26
            }
2183
1.91k
            if (PyUnicode_CheckExact(lhs)) {
2184
301
                _Py_CODEUNIT next = instr[INLINE_CACHE_ENTRIES_BINARY_OP + 1];
2185
301
                bool to_store = (next.op.code == STORE_FAST);
2186
301
                if (to_store && PyStackRef_AsPyObjectBorrow(locals[next.op.arg]) == lhs) {
2187
65
                    specialize(instr, BINARY_OP_INPLACE_ADD_UNICODE);
2188
65
                    return;
2189
65
                }
2190
236
                specialize(instr, BINARY_OP_ADD_UNICODE);
2191
236
                return;
2192
301
            }
2193
1.61k
            if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) {
2194
475
                specialize(instr, BINARY_OP_ADD_INT);
2195
475
                return;
2196
475
            }
2197
1.13k
            if (PyFloat_CheckExact(lhs)) {
2198
0
                specialize(instr, BINARY_OP_ADD_FLOAT);
2199
0
                return;
2200
0
            }
2201
1.13k
            break;
2202
1.47k
        case NB_MULTIPLY:
2203
1.47k
        case NB_INPLACE_MULTIPLY:
2204
1.47k
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
2205
274
                break;
2206
274
            }
2207
1.20k
            if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) {
2208
234
                specialize(instr, BINARY_OP_MULTIPLY_INT);
2209
234
                return;
2210
234
            }
2211
969
            if (PyFloat_CheckExact(lhs)) {
2212
0
                specialize(instr, BINARY_OP_MULTIPLY_FLOAT);
2213
0
                return;
2214
0
            }
2215
969
            break;
2216
969
        case NB_SUBTRACT:
2217
322
        case NB_INPLACE_SUBTRACT:
2218
322
            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
2219
0
                break;
2220
0
            }
2221
322
            if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) {
2222
307
                specialize(instr, BINARY_OP_SUBTRACT_INT);
2223
307
                return;
2224
307
            }
2225
15
            if (PyFloat_CheckExact(lhs)) {
2226
0
                specialize(instr, BINARY_OP_SUBTRACT_FLOAT);
2227
0
                return;
2228
0
            }
2229
15
            break;
2230
1.02M
        case NB_SUBSCR:
2231
1.02M
            if (PyLong_CheckExact(rhs) && _PyLong_IsNonNegativeCompact((PyLongObject *)rhs)) {
2232
1.00M
                if (PyList_CheckExact(lhs)) {
2233
4.44k
                    specialize(instr, BINARY_OP_SUBSCR_LIST_INT);
2234
4.44k
                    return;
2235
4.44k
                }
2236
1.00M
                if (PyTuple_CheckExact(lhs)) {
2237
460
                    specialize(instr, BINARY_OP_SUBSCR_TUPLE_INT);
2238
460
                    return;
2239
460
                }
2240
1.00M
                if (PyUnicode_CheckExact(lhs)) {
2241
1.00M
                    specialize(instr, BINARY_OP_SUBSCR_STR_INT);
2242
1.00M
                    return;
2243
1.00M
                }
2244
1.00M
            }
2245
15.8k
            if (PyDict_CheckExact(lhs)) {
2246
297
                specialize(instr, BINARY_OP_SUBSCR_DICT);
2247
297
                return;
2248
297
            }
2249
15.5k
            if (PyList_CheckExact(lhs) && PySlice_Check(rhs)) {
2250
31
                specialize(instr, BINARY_OP_SUBSCR_LIST_SLICE);
2251
31
                return;
2252
31
            }
2253
15.5k
            unsigned int tp_version;
2254
15.5k
            PyTypeObject *container_type = Py_TYPE(lhs);
2255
15.5k
            PyObject *descriptor = _PyType_LookupRefAndVersion(container_type, &_Py_ID(__getitem__), &tp_version);
2256
15.5k
            if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type &&
2257
89
                container_type->tp_flags & Py_TPFLAGS_HEAPTYPE)
2258
89
            {
2259
89
                PyFunctionObject *func = (PyFunctionObject *)descriptor;
2260
89
                PyCodeObject *fcode = (PyCodeObject *)func->func_code;
2261
89
                int kind = function_kind(fcode);
2262
89
                PyHeapTypeObject *ht = (PyHeapTypeObject *)container_type;
2263
89
                if (kind == SIMPLE_FUNCTION &&
2264
89
                    fcode->co_argcount == 2 &&
2265
89
                    !_PyInterpreterState_GET()->eval_frame && /* Don't specialize if PEP 523 is active */
2266
89
                    _PyType_CacheGetItemForSpecialization(ht, descriptor, (uint32_t)tp_version))
2267
89
                {
2268
89
                    specialize(instr, BINARY_OP_SUBSCR_GETITEM);
2269
89
                    Py_DECREF(descriptor);
2270
89
                    return;
2271
89
                }
2272
89
            }
2273
15.4k
            Py_XDECREF(descriptor);
2274
15.4k
            break;
2275
1.03M
    }
2276
2277
27.9k
    _PyBinaryOpSpecializationDescr *descr;
2278
27.9k
    if (binary_op_extended_specialization(lhs, rhs, oparg, &descr)) {
2279
511
        specialize(instr, BINARY_OP_EXTEND);
2280
511
        write_ptr(cache->external_cache, (void*)descr);
2281
511
        return;
2282
511
    }
2283
2284
27.4k
    SPECIALIZATION_FAIL(BINARY_OP, binary_op_fail_kind(oparg, lhs, rhs));
2285
27.4k
    unspecialize(instr);
2286
27.4k
    return;
2287
27.9k
}
2288
2289
2290
#ifdef Py_STATS
2291
static int
2292
compare_op_fail_kind(PyObject *lhs, PyObject *rhs)
2293
{
2294
    if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
2295
        if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) {
2296
            return SPEC_FAIL_COMPARE_OP_FLOAT_LONG;
2297
        }
2298
        if (PyLong_CheckExact(lhs) && PyFloat_CheckExact(rhs)) {
2299
            return SPEC_FAIL_COMPARE_OP_LONG_FLOAT;
2300
        }
2301
        return SPEC_FAIL_COMPARE_OP_DIFFERENT_TYPES;
2302
    }
2303
    if (PyBytes_CheckExact(lhs)) {
2304
        return SPEC_FAIL_COMPARE_OP_BYTES;
2305
    }
2306
    if (PyTuple_CheckExact(lhs)) {
2307
        return SPEC_FAIL_COMPARE_OP_TUPLE;
2308
    }
2309
    if (PyList_CheckExact(lhs)) {
2310
        return SPEC_FAIL_COMPARE_OP_LIST;
2311
    }
2312
    if (PySet_CheckExact(lhs) || PyFrozenSet_CheckExact(lhs)) {
2313
        return SPEC_FAIL_COMPARE_OP_SET;
2314
    }
2315
    if (PyBool_Check(lhs)) {
2316
        return SPEC_FAIL_COMPARE_OP_BOOL;
2317
    }
2318
    if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) {
2319
        return SPEC_FAIL_COMPARE_OP_BASEOBJECT;
2320
    }
2321
    return SPEC_FAIL_OTHER;
2322
}
2323
#endif   // Py_STATS
2324
2325
Py_NO_INLINE void
2326
_Py_Specialize_CompareOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *instr,
2327
                         int oparg)
2328
26.1k
{
2329
26.1k
    PyObject *lhs = PyStackRef_AsPyObjectBorrow(lhs_st);
2330
26.1k
    PyObject *rhs = PyStackRef_AsPyObjectBorrow(rhs_st);
2331
26.1k
    uint8_t specialized_op;
2332
2333
26.1k
    assert(ENABLE_SPECIALIZATION_FT);
2334
26.1k
    assert(_PyOpcode_Caches[COMPARE_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP);
2335
    // All of these specializations compute boolean values, so they're all valid
2336
    // regardless of the fifth-lowest oparg bit.
2337
26.1k
    if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
2338
11.2k
        SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
2339
11.2k
        goto failure;
2340
11.2k
    }
2341
14.8k
    if (PyFloat_CheckExact(lhs)) {
2342
18
        specialized_op = COMPARE_OP_FLOAT;
2343
18
        goto success;
2344
18
    }
2345
14.8k
    if (PyLong_CheckExact(lhs)) {
2346
8.72k
        if (_PyLong_IsCompact((PyLongObject *)lhs) && _PyLong_IsCompact((PyLongObject *)rhs)) {
2347
4.38k
            specialized_op = COMPARE_OP_INT;
2348
4.38k
            goto success;
2349
4.38k
        }
2350
4.34k
        else {
2351
4.34k
            SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_BIG_INT);
2352
4.34k
            goto failure;
2353
4.34k
        }
2354
8.72k
    }
2355
6.09k
    if (PyUnicode_CheckExact(lhs)) {
2356
5.67k
        int cmp = oparg >> 5;
2357
5.67k
        if (cmp != Py_EQ && cmp != Py_NE) {
2358
4.38k
            SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_STRING);
2359
4.38k
            goto failure;
2360
4.38k
        }
2361
1.29k
        else {
2362
1.29k
            specialized_op = COMPARE_OP_STR;
2363
1.29k
            goto success;
2364
1.29k
        }
2365
5.67k
    }
2366
418
    SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
2367
20.4k
failure:
2368
20.4k
    unspecialize(instr);
2369
20.4k
    return;
2370
5.69k
success:
2371
5.69k
    specialize(instr, specialized_op);
2372
5.69k
}
2373
2374
#ifdef Py_STATS
2375
static int
2376
unpack_sequence_fail_kind(PyObject *seq)
2377
{
2378
    if (PySequence_Check(seq)) {
2379
        return SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE;
2380
    }
2381
    if (PyIter_Check(seq)) {
2382
        return SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR;
2383
    }
2384
    return SPEC_FAIL_OTHER;
2385
}
2386
#endif   // Py_STATS
2387
2388
Py_NO_INLINE void
2389
_Py_Specialize_UnpackSequence(_PyStackRef seq_st, _Py_CODEUNIT *instr, int oparg)
2390
16.6k
{
2391
16.6k
    PyObject *seq = PyStackRef_AsPyObjectBorrow(seq_st);
2392
2393
16.6k
    assert(ENABLE_SPECIALIZATION_FT);
2394
16.6k
    assert(_PyOpcode_Caches[UNPACK_SEQUENCE] ==
2395
16.6k
           INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
2396
16.6k
    if (PyTuple_CheckExact(seq)) {
2397
1.15k
        if (PyTuple_GET_SIZE(seq) != oparg) {
2398
0
            SPECIALIZATION_FAIL(UNPACK_SEQUENCE, SPEC_FAIL_EXPECTED_ERROR);
2399
0
            unspecialize(instr);
2400
0
            return;
2401
0
        }
2402
1.15k
        if (PyTuple_GET_SIZE(seq) == 2) {
2403
562
            specialize(instr, UNPACK_SEQUENCE_TWO_TUPLE);
2404
562
            return;
2405
562
        }
2406
595
        specialize(instr, UNPACK_SEQUENCE_TUPLE);
2407
595
        return;
2408
1.15k
    }
2409
15.5k
    if (PyList_CheckExact(seq)) {
2410
14.8k
        if (PyList_GET_SIZE(seq) != oparg) {
2411
11.5k
            SPECIALIZATION_FAIL(UNPACK_SEQUENCE, SPEC_FAIL_EXPECTED_ERROR);
2412
11.5k
            unspecialize(instr);
2413
11.5k
            return;
2414
11.5k
        }
2415
3.26k
        specialize(instr, UNPACK_SEQUENCE_LIST);
2416
3.26k
        return;
2417
14.8k
    }
2418
722
    SPECIALIZATION_FAIL(UNPACK_SEQUENCE, unpack_sequence_fail_kind(seq));
2419
722
    unspecialize(instr);
2420
722
}
2421
2422
#ifdef Py_STATS
2423
int
2424
 _PySpecialization_ClassifyIterator(PyObject *iter)
2425
{
2426
    if (PyGen_CheckExact(iter)) {
2427
        return SPEC_FAIL_ITER_GENERATOR;
2428
    }
2429
    if (PyCoro_CheckExact(iter)) {
2430
        return SPEC_FAIL_ITER_COROUTINE;
2431
    }
2432
    if (PyAsyncGen_CheckExact(iter)) {
2433
        return SPEC_FAIL_ITER_ASYNC_GENERATOR;
2434
    }
2435
    if (PyAsyncGenASend_CheckExact(iter)) {
2436
        return SPEC_FAIL_ITER_ASYNC_GENERATOR_SEND;
2437
    }
2438
    PyTypeObject *t = Py_TYPE(iter);
2439
    if (t == &PyListIter_Type) {
2440
        return SPEC_FAIL_ITER_LIST;
2441
    }
2442
    if (t == &PyTupleIter_Type) {
2443
        return SPEC_FAIL_ITER_TUPLE;
2444
    }
2445
    if (t == &PyDictIterKey_Type) {
2446
        return SPEC_FAIL_ITER_DICT_KEYS;
2447
    }
2448
    if (t == &PyDictIterValue_Type) {
2449
        return SPEC_FAIL_ITER_DICT_VALUES;
2450
    }
2451
    if (t == &PyDictIterItem_Type) {
2452
        return SPEC_FAIL_ITER_DICT_ITEMS;
2453
    }
2454
    if (t == &PySetIter_Type) {
2455
        return SPEC_FAIL_ITER_SET;
2456
    }
2457
    if (t == &PyUnicodeIter_Type) {
2458
        return SPEC_FAIL_ITER_STRING;
2459
    }
2460
    if (t == &PyBytesIter_Type) {
2461
        return SPEC_FAIL_ITER_BYTES;
2462
    }
2463
    if (t == &PyRangeIter_Type) {
2464
        return SPEC_FAIL_ITER_RANGE;
2465
    }
2466
    if (t == &PyEnum_Type) {
2467
        return SPEC_FAIL_ITER_ENUMERATE;
2468
    }
2469
    if (t == &PyMap_Type) {
2470
        return SPEC_FAIL_ITER_MAP;
2471
    }
2472
    if (t == &PyZip_Type) {
2473
        return SPEC_FAIL_ITER_ZIP;
2474
    }
2475
    if (t == &PySeqIter_Type) {
2476
        return SPEC_FAIL_ITER_SEQ_ITER;
2477
    }
2478
    if (t == &PyListRevIter_Type) {
2479
        return SPEC_FAIL_ITER_REVERSED_LIST;
2480
    }
2481
    if (t == &_PyUnicodeASCIIIter_Type) {
2482
        return SPEC_FAIL_ITER_ASCII_STRING;
2483
    }
2484
    const char *name = t->tp_name;
2485
    if (strncmp(name, "itertools", 9) == 0) {
2486
        return SPEC_FAIL_ITER_ITERTOOLS;
2487
    }
2488
    if (strncmp(name, "callable_iterator", 17) == 0) {
2489
        return SPEC_FAIL_ITER_CALLABLE;
2490
    }
2491
    return SPEC_FAIL_OTHER;
2492
}
2493
#endif   // Py_STATS
2494
2495
Py_NO_INLINE void
2496
_Py_Specialize_ForIter(_PyStackRef iter, _PyStackRef null_or_index, _Py_CODEUNIT *instr, int oparg)
2497
106k
{
2498
106k
    assert(ENABLE_SPECIALIZATION_FT);
2499
106k
    assert(_PyOpcode_Caches[FOR_ITER] == INLINE_CACHE_ENTRIES_FOR_ITER);
2500
106k
    PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter);
2501
106k
    PyTypeObject *tp = Py_TYPE(iter_o);
2502
2503
106k
    if (PyStackRef_IsNull(null_or_index)) {
2504
#ifdef Py_GIL_DISABLED
2505
        // Only specialize for uniquely referenced iterators, so that we know
2506
        // they're only referenced by this one thread. This is more limiting
2507
        // than we need (even `it = iter(mylist); for item in it:` won't get
2508
        // specialized) but we don't have a way to check whether we're the only
2509
        // _thread_ who has access to the object.
2510
        if (!_PyObject_IsUniquelyReferenced(iter_o)) {
2511
            goto failure;
2512
        }
2513
#endif
2514
105k
        if (tp == &PyRangeIter_Type) {
2515
86
            specialize(instr, FOR_ITER_RANGE);
2516
86
            return;
2517
86
        }
2518
105k
        else if (tp == &PyGen_Type && oparg <= SHRT_MAX) {
2519
            // Generators are very much not thread-safe, so don't worry about
2520
            // the specialization not being thread-safe.
2521
567
            assert(instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == END_FOR  ||
2522
567
                instr[oparg + INLINE_CACHE_ENTRIES_FOR_ITER + 1].op.code == INSTRUMENTED_END_FOR
2523
567
            );
2524
            /* Don't specialize if PEP 523 is active */
2525
567
            if (_PyInterpreterState_GET()->eval_frame) {
2526
0
                goto failure;
2527
0
            }
2528
567
            specialize(instr, FOR_ITER_GEN);
2529
567
            return;
2530
567
        }
2531
105k
    }
2532
1.10k
    else {
2533
1.10k
        if (tp == &PyList_Type) {
2534
#ifdef Py_GIL_DISABLED
2535
            // Only specialize for lists owned by this thread or shared
2536
            if (!_Py_IsOwnedByCurrentThread(iter_o) && !_PyObject_GC_IS_SHARED(iter_o)) {
2537
                goto failure;
2538
            }
2539
#endif
2540
581
            specialize(instr, FOR_ITER_LIST);
2541
581
            return;
2542
581
        }
2543
520
        else if (tp == &PyTuple_Type) {
2544
520
            specialize(instr, FOR_ITER_TUPLE);
2545
520
            return;
2546
520
        }
2547
1.10k
    }
2548
104k
failure:
2549
104k
    SPECIALIZATION_FAIL(FOR_ITER,
2550
104k
                        _PySpecialization_ClassifyIterator(iter_o));
2551
104k
    unspecialize(instr);
2552
104k
}
2553
2554
Py_NO_INLINE void
2555
_Py_Specialize_Send(_PyStackRef receiver_st, _Py_CODEUNIT *instr)
2556
4.81k
{
2557
4.81k
    PyObject *receiver = PyStackRef_AsPyObjectBorrow(receiver_st);
2558
2559
4.81k
    assert(ENABLE_SPECIALIZATION_FT);
2560
4.81k
    assert(_PyOpcode_Caches[SEND] == INLINE_CACHE_ENTRIES_SEND);
2561
4.81k
    PyTypeObject *tp = Py_TYPE(receiver);
2562
4.81k
    if (tp == &PyGen_Type || tp == &PyCoro_Type) {
2563
        /* Don't specialize if PEP 523 is active */
2564
942
        if (_PyInterpreterState_GET()->eval_frame) {
2565
0
            SPECIALIZATION_FAIL(SEND, SPEC_FAIL_OTHER);
2566
0
            goto failure;
2567
0
        }
2568
942
        specialize(instr, SEND_GEN);
2569
942
        return;
2570
942
    }
2571
3.87k
    SPECIALIZATION_FAIL(SEND,
2572
3.87k
                        _PySpecialization_ClassifyIterator(receiver));
2573
3.87k
failure:
2574
3.87k
    unspecialize(instr);
2575
3.87k
}
2576
2577
#ifdef Py_STATS
2578
static int
2579
to_bool_fail_kind(PyObject *value)
2580
{
2581
    if (PyByteArray_CheckExact(value)) {
2582
        return SPEC_FAIL_TO_BOOL_BYTEARRAY;
2583
    }
2584
    if (PyBytes_CheckExact(value)) {
2585
        return SPEC_FAIL_TO_BOOL_BYTES;
2586
    }
2587
    if (PyDict_CheckExact(value)) {
2588
        return SPEC_FAIL_TO_BOOL_DICT;
2589
    }
2590
    if (PyFloat_CheckExact(value)) {
2591
        return SPEC_FAIL_TO_BOOL_FLOAT;
2592
    }
2593
    if (PyMemoryView_Check(value)) {
2594
        return SPEC_FAIL_TO_BOOL_MEMORY_VIEW;
2595
    }
2596
    if (PyAnySet_CheckExact(value)) {
2597
        return SPEC_FAIL_TO_BOOL_SET;
2598
    }
2599
    if (PyTuple_CheckExact(value)) {
2600
        return SPEC_FAIL_TO_BOOL_TUPLE;
2601
    }
2602
    return SPEC_FAIL_OTHER;
2603
}
2604
#endif  // Py_STATS
2605
2606
static int
2607
check_type_always_true(PyTypeObject *ty)
2608
86.7k
{
2609
86.7k
    PyNumberMethods *nb = ty->tp_as_number;
2610
86.7k
    if (nb && nb->nb_bool) {
2611
26
        return SPEC_FAIL_TO_BOOL_NUMBER;
2612
26
    }
2613
86.7k
    PyMappingMethods *mp = ty->tp_as_mapping;
2614
86.7k
    if (mp && mp->mp_length) {
2615
19.4k
        return SPEC_FAIL_TO_BOOL_MAPPING;
2616
19.4k
    }
2617
67.3k
    PySequenceMethods *sq = ty->tp_as_sequence;
2618
67.3k
    if (sq && sq->sq_length) {
2619
10.7k
      return SPEC_FAIL_TO_BOOL_SEQUENCE;
2620
10.7k
    }
2621
56.6k
    return 0;
2622
67.3k
}
2623
2624
Py_NO_INLINE void
2625
_Py_Specialize_ToBool(_PyStackRef value_o, _Py_CODEUNIT *instr)
2626
192k
{
2627
192k
    assert(ENABLE_SPECIALIZATION_FT);
2628
192k
    assert(_PyOpcode_Caches[TO_BOOL] == INLINE_CACHE_ENTRIES_TO_BOOL);
2629
192k
    _PyToBoolCache *cache = (_PyToBoolCache *)(instr + 1);
2630
192k
    PyObject *value = PyStackRef_AsPyObjectBorrow(value_o);
2631
192k
    uint8_t specialized_op;
2632
192k
    if (PyBool_Check(value)) {
2633
2.37k
        specialized_op = TO_BOOL_BOOL;
2634
2.37k
        goto success;
2635
2.37k
    }
2636
190k
    if (PyLong_CheckExact(value)) {
2637
2.80k
        specialized_op = TO_BOOL_INT;
2638
2.80k
        goto success;
2639
2.80k
    }
2640
187k
    if (PyList_CheckExact(value)) {
2641
217
        specialized_op = TO_BOOL_LIST;
2642
217
        goto success;
2643
217
    }
2644
186k
    if (Py_IsNone(value)) {
2645
75.8k
        specialized_op = TO_BOOL_NONE;
2646
75.8k
        goto success;
2647
75.8k
    }
2648
111k
    if (PyUnicode_CheckExact(value)) {
2649
22.9k
        specialized_op = TO_BOOL_STR;
2650
22.9k
        goto success;
2651
22.9k
    }
2652
88.2k
    if (PyType_HasFeature(Py_TYPE(value), Py_TPFLAGS_HEAPTYPE)) {
2653
86.7k
        unsigned int version = 0;
2654
86.7k
        int err = _PyType_Validate(Py_TYPE(value), check_type_always_true, &version);
2655
86.7k
        if (err < 0) {
2656
0
            SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_OUT_OF_VERSIONS);
2657
0
            goto failure;
2658
0
        }
2659
86.7k
        else if (err > 0) {
2660
30.1k
            SPECIALIZATION_FAIL(TO_BOOL, err);
2661
30.1k
            goto failure;
2662
30.1k
        }
2663
2664
86.7k
        assert(err == 0);
2665
56.6k
        assert(version);
2666
56.6k
        write_u32(cache->version, version);
2667
56.6k
        specialized_op = TO_BOOL_ALWAYS_TRUE;
2668
56.6k
        goto success;
2669
86.7k
    }
2670
2671
1.40k
    SPECIALIZATION_FAIL(TO_BOOL, to_bool_fail_kind(value));
2672
31.5k
failure:
2673
31.5k
    unspecialize(instr);
2674
31.5k
    return;
2675
160k
success:
2676
160k
    specialize(instr, specialized_op);
2677
160k
}
2678
2679
#ifdef Py_STATS
2680
static int
2681
containsop_fail_kind(PyObject *value) {
2682
    if (PyUnicode_CheckExact(value)) {
2683
        return SPEC_FAIL_CONTAINS_OP_STR;
2684
    }
2685
    if (PyList_CheckExact(value)) {
2686
        return SPEC_FAIL_CONTAINS_OP_LIST;
2687
    }
2688
    if (PyTuple_CheckExact(value)) {
2689
        return SPEC_FAIL_CONTAINS_OP_TUPLE;
2690
    }
2691
    if (PyType_Check(value)) {
2692
        return SPEC_FAIL_CONTAINS_OP_USER_CLASS;
2693
    }
2694
    return SPEC_FAIL_OTHER;
2695
}
2696
#endif
2697
2698
Py_NO_INLINE void
2699
_Py_Specialize_ContainsOp(_PyStackRef value_st, _Py_CODEUNIT *instr)
2700
25.2k
{
2701
25.2k
    PyObject *value = PyStackRef_AsPyObjectBorrow(value_st);
2702
2703
25.2k
    assert(ENABLE_SPECIALIZATION_FT);
2704
25.2k
    assert(_PyOpcode_Caches[CONTAINS_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP);
2705
25.2k
    if (PyDict_CheckExact(value)) {
2706
253
        specialize(instr, CONTAINS_OP_DICT);
2707
253
        return;
2708
253
    }
2709
24.9k
    if (PySet_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
2710
273
        specialize(instr, CONTAINS_OP_SET);
2711
273
        return;
2712
273
    }
2713
2714
24.7k
    SPECIALIZATION_FAIL(CONTAINS_OP, containsop_fail_kind(value));
2715
24.7k
    unspecialize(instr);
2716
24.7k
    return;
2717
24.9k
}
2718
2719
#ifdef Py_STATS
2720
void
2721
_Py_GatherStats_GetIter(_PyStackRef iterable)
2722
{
2723
    PyTypeObject *tp = PyStackRef_TYPE(iterable);
2724
    int kind = SPEC_FAIL_OTHER;
2725
    if (tp == &PyTuple_Type) {
2726
        kind = SPEC_FAIL_ITER_TUPLE;
2727
    }
2728
    else if (tp == &PyList_Type) {
2729
        kind = SPEC_FAIL_ITER_LIST;
2730
    }
2731
    else if (tp == &PyDict_Type) {
2732
        kind = SPEC_FAIL_ITER_DICT_KEYS;
2733
    }
2734
    else if (tp == &PySet_Type) {
2735
        kind = SPEC_FAIL_ITER_SET;
2736
    }
2737
    else if (tp == &PyBytes_Type) {
2738
        kind = SPEC_FAIL_ITER_BYTES;
2739
    }
2740
    else if (tp == &PyEnum_Type) {
2741
        kind = SPEC_FAIL_ITER_ENUMERATE;
2742
    }
2743
    else if (tp == &PyUnicode_Type) {
2744
        kind = SPEC_FAIL_ITER_STRING;
2745
    }
2746
    else if (tp == &PyGen_Type) {
2747
        kind = SPEC_FAIL_ITER_GENERATOR;
2748
    }
2749
    else if (tp == &PyCoro_Type) {
2750
        kind = SPEC_FAIL_ITER_COROUTINE;
2751
    }
2752
    else if (tp == &PyAsyncGen_Type) {
2753
        kind = SPEC_FAIL_ITER_ASYNC_GENERATOR;
2754
    }
2755
    else if (tp == &_PyAsyncGenASend_Type) {
2756
        kind = SPEC_FAIL_ITER_ASYNC_GENERATOR_SEND;
2757
    }
2758
    else if (tp->tp_iter == PyObject_SelfIter) {
2759
        kind = SPEC_FAIL_ITER_SELF;
2760
    }
2761
    SPECIALIZATION_FAIL(GET_ITER, kind);
2762
}
2763
#endif
2764
2765
2766
/* Code init cleanup.
2767
 * CALL_ALLOC_AND_ENTER_INIT will set up
2768
 * the frame to execute the EXIT_INIT_CHECK
2769
 * instruction.
2770
 * Ends with a RESUME so that it is not traced.
2771
 * This is used as a plain code object, not a function,
2772
 * so must not access globals or builtins.
2773
 * There are a few other constraints imposed on the code
2774
 * by the free-threaded build:
2775
 *
2776
 * 1. The RESUME instruction must not be executed. Otherwise we may attempt to
2777
 *    free the statically allocated TLBC array.
2778
 * 2. It must contain no specializable instructions. Specializing multiple
2779
 *    copies of the same bytecode is not thread-safe in free-threaded builds.
2780
 *
2781
 * This should be dynamically allocated if either of those restrictions need to
2782
 * be lifted.
2783
 */
2784
2785
#define NO_LOC_4 (128 | (PY_CODE_LOCATION_INFO_NONE << 3) | 3)
2786
2787
static const PyBytesObject no_location = {
2788
    PyVarObject_HEAD_INIT(&PyBytes_Type, 1)
2789
    .ob_sval = { NO_LOC_4 }
2790
};
2791
2792
#ifdef Py_GIL_DISABLED
2793
static _PyCodeArray init_cleanup_tlbc = {
2794
    .size = 1,
2795
    .entries = {(char*) &_Py_InitCleanup.co_code_adaptive},
2796
};
2797
#endif
2798
2799
const struct _PyCode8 _Py_InitCleanup = {
2800
    _PyVarObject_HEAD_INIT(&PyCode_Type, 3),
2801
    .co_consts = (PyObject *)&_Py_SINGLETON(tuple_empty),
2802
    .co_names = (PyObject *)&_Py_SINGLETON(tuple_empty),
2803
    .co_exceptiontable = (PyObject *)&_Py_SINGLETON(bytes_empty),
2804
    .co_flags = CO_OPTIMIZED | CO_NO_MONITORING_EVENTS,
2805
    .co_localsplusnames = (PyObject *)&_Py_SINGLETON(tuple_empty),
2806
    .co_localspluskinds = (PyObject *)&_Py_SINGLETON(bytes_empty),
2807
    .co_filename = &_Py_ID(__init__),
2808
    .co_name = &_Py_ID(__init__),
2809
    .co_qualname = &_Py_ID(__init__),
2810
    .co_linetable = (PyObject *)&no_location,
2811
    ._co_firsttraceable = 4,
2812
    .co_stacksize = 2,
2813
    .co_framesize = 2 + FRAME_SPECIALS_SIZE,
2814
#ifdef Py_GIL_DISABLED
2815
    .co_tlbc = &init_cleanup_tlbc,
2816
#endif
2817
    .co_code_adaptive = {
2818
        EXIT_INIT_CHECK, 0,
2819
        RETURN_VALUE, 0,
2820
        RESUME, RESUME_AT_FUNC_START,
2821
    }
2822
};