Skip to content

Commit 99bd446

Browse files
fold A.classmethod as well using constant promotion
1 parent ecc3d6b commit 99bd446

File tree

3 files changed

+93
-39
lines changed

3 files changed

+93
-39
lines changed

Python/optimizer_bytecodes.c

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,8 @@ dummy_func(void) {
883883
op(_LOAD_ATTR, (owner -- attr, self_or_null[oparg&1])) {
884884
PyObject *value = sym_get_probable_value(owner);
885885
PyTypeObject *type = NULL;
886-
if (value != NULL && PyType_Check(value)) {
886+
bool is_class = PyType_Check(value);
887+
if (value != NULL && is_class) {
887888
type = (PyTypeObject *)value;
888889
}
889890
else {
@@ -905,26 +906,42 @@ dummy_func(void) {
905906
callable = _PyStaticMethod_GetFunc(descr);
906907
}
907908
assert(callable);
909+
if (is_class && class_method) {
910+
PyObject *meth = PyMethod_New(callable, value);
911+
if (meth == NULL || sym_promote_to_constant_pool(ctx, meth) != 0) {
912+
ctx->done = true;
913+
ctx->out_of_space = true;
914+
break;
915+
}
916+
callable = meth;
917+
}
908918
bool immortal = _Py_IsImmortal(callable) || (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE);
909-
if (PyType_Check(value)) {
919+
if (is_class && class_method) {
910920
ADD_OP(_CHECK_ATTR_CLASS, 0, type->tp_version_tag);
921+
ADD_OP(_POP_TOP, 0, 0);
922+
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE, 0, (uintptr_t)callable);
923+
ADD_OP(_PUSH_NULL, 0, 0);
924+
self_or_null[0] = sym_new_null(ctx);
925+
attr = sym_new_const(ctx, callable);
926+
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
927+
_Py_BloomFilter_Add(dependencies, (PyTypeObject *)type);
911928
}
912929
else {
913930
ADD_OP(_GUARD_TYPE_VERSION, 0, type->tp_version_tag);
931+
ADD_OP(_POP_TOP, 0, 0);
932+
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE, 0, (uintptr_t)callable);
933+
if (class_method) {
934+
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)type);
935+
self_or_null[0] = sym_new_const(ctx, (PyObject *)type);
936+
}
937+
else {
938+
ADD_OP(_PUSH_NULL, 0, 0);
939+
self_or_null[0] = sym_new_null(ctx);
940+
}
941+
attr = sym_new_const(ctx, callable);
942+
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
943+
_Py_BloomFilter_Add(dependencies, (PyTypeObject *)type);
914944
}
915-
ADD_OP(_POP_TOP, 0, 0);
916-
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE, 0, (uintptr_t)callable);
917-
if (class_method) {
918-
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)type);
919-
self_or_null[0] = sym_new_const(ctx, (PyObject *)type);
920-
}
921-
else {
922-
ADD_OP(_PUSH_NULL, 0, 0);
923-
self_or_null[0] = sym_new_null(ctx);
924-
}
925-
attr = sym_new_const(ctx, callable);
926-
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
927-
_Py_BloomFilter_Add(dependencies, (PyTypeObject *)type);
928945
}
929946
else {
930947
attr = sym_new_not_null(ctx);
@@ -1040,8 +1057,15 @@ dummy_func(void) {
10401057
}
10411058

10421059
op(_INIT_CALL_BOUND_METHOD_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
1043-
callable = sym_new_not_null(ctx);
1044-
self_or_null = sym_new_not_null(ctx);
1060+
PyObject *meth = sym_get_const(ctx, callable);
1061+
if (meth != NULL) {
1062+
callable = sym_new_const(ctx, ((PyMethodObject *)meth)->im_func);
1063+
self_or_null = sym_new_const(ctx, ((PyMethodObject *)meth)->im_self);
1064+
}
1065+
else {
1066+
callable = sym_new_not_null(ctx);
1067+
self_or_null = sym_new_not_null(ctx);
1068+
}
10451069
}
10461070

10471071
op(_CHECK_FUNCTION_VERSION, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {

Python/optimizer_cases.c.h

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

Python/optimizer_symbols.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,9 @@ _Py_uop_sym_set_recorded_type(JitOptContext *ctx, JitOptRef ref, PyTypeObject *t
14211421
if (type == NULL) {
14221422
return;
14231423
}
1424-
assert(PyType_Check((PyObject *)type));
1424+
if (!PyType_Check((PyObject *)type)) {
1425+
return;
1426+
}
14251427
JitOptSymbol *sym = PyJitRef_Unwrap(ref);
14261428
JitSymType tag = sym->tag;
14271429
switch(tag) {

0 commit comments

Comments
 (0)