Skip to content

Commit 8a7edda

Browse files
authored
gh-149217: Avoid adding dependencies on immutable, immortal classes in the JIT (GH149256)
1 parent 00c4a94 commit 8a7edda

3 files changed

Lines changed: 24 additions & 40 deletions

File tree

Python/optimizer_analysis.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ type_watcher_callback(PyTypeObject* type)
156156
return 0;
157157
}
158158

159+
static void
160+
watch_type(PyTypeObject *type, _PyBloomFilter *filter)
161+
{
162+
if (_Py_IsImmortal(type) && (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) {
163+
return;
164+
}
165+
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
166+
_Py_BloomFilter_Add(filter, type);
167+
}
168+
159169
static PyObject *
160170
convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj)
161171
{
@@ -367,8 +377,7 @@ optimize_dict_known_hash(
367377
// for user-defined objects which don't override tp_hash
368378
Py_hash_t hash = PyObject_Hash(sub);
369379
ADD_OP(opcode, 0, hash);
370-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)Py_TYPE(sub));
371-
_Py_BloomFilter_Add(dependencies, Py_TYPE(sub));
380+
watch_type(Py_TYPE(sub), dependencies);
372381
}
373382
}
374383

@@ -401,8 +410,7 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction
401410
ADD_OP(suffix, 2, 0);
402411
}
403412
if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
404-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
405-
_Py_BloomFilter_Add(dependencies, type);
413+
watch_type(type, dependencies);
406414
}
407415
return sym_new_const(ctx, lookup);
408416
}
@@ -473,10 +481,8 @@ lookup_super_attr(JitOptContext *ctx, _PyBloomFilter *dependencies,
473481
}
474482
// if obj_type is immutable, then all its superclasses are immutable
475483
if ((obj_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
476-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)su_type);
477-
_Py_BloomFilter_Add(dependencies, su_type);
478-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)obj_type);
479-
_Py_BloomFilter_Add(dependencies, obj_type);
484+
watch_type(su_type, dependencies);
485+
watch_type(obj_type, dependencies);
480486
}
481487
return sym_new_const_steal(ctx, lookup);
482488
}

Python/optimizer_bytecodes.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ dummy_func(void) {
149149
// Promote the probable type version to a known one.
150150
sym_set_type(owner, probable_type);
151151
sym_set_type_version(owner, type_version);
152-
if ((probable_type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
153-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
154-
_Py_BloomFilter_Add(dependencies, probable_type);
155-
}
152+
watch_type(probable_type, dependencies);
156153
}
157154
else {
158155
ctx->contradiction = true;
@@ -238,10 +235,7 @@ dummy_func(void) {
238235
}
239236
else {
240237
sym_set_const(owner, type);
241-
if ((((PyTypeObject *)type)->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
242-
PyType_Watch(TYPE_WATCHER_ID, type);
243-
_Py_BloomFilter_Add(dependencies, type);
244-
}
238+
watch_type((PyTypeObject *)type, dependencies);
245239
}
246240
}
247241
}
@@ -258,8 +252,7 @@ dummy_func(void) {
258252
probable_type->tp_version_tag == type_version) {
259253
sym_set_type(owner, probable_type);
260254
sym_set_type_version(owner, type_version);
261-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
262-
_Py_BloomFilter_Add(dependencies, probable_type);
255+
watch_type(probable_type, dependencies);
263256
}
264257
else {
265258
ctx->contradiction = true;
@@ -1326,8 +1319,7 @@ dummy_func(void) {
13261319
assert(init != NULL);
13271320
assert(PyFunction_Check(init));
13281321
callable = sym_new_const(ctx, init);
1329-
PyType_Watch(TYPE_WATCHER_ID, callable_o);
1330-
_Py_BloomFilter_Add(dependencies, callable_o);;
1322+
watch_type((PyTypeObject *)callable_o, dependencies);
13311323
}
13321324
else {
13331325
callable = sym_new_not_null(ctx);
@@ -2033,10 +2025,7 @@ dummy_func(void) {
20332025
0, (uintptr_t)descr);
20342026
ADD_OP(_SWAP, 3, 0);
20352027
optimize_pop_top(ctx, this_instr, method_and_self[0]);
2036-
if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
2037-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
2038-
_Py_BloomFilter_Add(dependencies, type);
2039-
}
2028+
watch_type(type, dependencies);
20402029
method_and_self[0] = sym_new_const(ctx, descr);
20412030
optimized = true;
20422031
}

Python/optimizer_cases.c.h

Lines changed: 5 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)