gh-131798: optimize through keyword and bound method calls in the JIT (GH-148466)

Co-authored-by: Ken Jin <kenjin4096@gmail.com>
This commit is contained in:
Kumar Aditya 2026-04-13 18:44:48 +05:30 committed by GitHub
parent 10d275fdf8
commit 88e378cc1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 1174 additions and 1001 deletions

View file

@ -1022,6 +1022,18 @@ dummy_func(void) {
_Py_BloomFilter_Add(dependencies, func);
}
op(_CHECK_FUNCTION_VERSION_KW, (func_version/2, callable, unused, unused[oparg], unused -- callable, unused, unused[oparg], unused)) {
PyObject *func = sym_get_probable_value(callable);
if (func == NULL || !PyFunction_Check(func) || ((PyFunctionObject *)func)->func_version != func_version) {
ctx->contradiction = true;
ctx->done = true;
break;
}
// Guarded on this, so it can be promoted.
sym_set_const(callable, func);
_Py_BloomFilter_Add(dependencies, func);
}
op(_CHECK_METHOD_VERSION, (func_version/2, callable, null, unused[oparg] -- callable, null, unused[oparg])) {
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyMethod_Type)) {
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
@ -1045,6 +1057,29 @@ dummy_func(void) {
sym_set_type(callable, &PyMethod_Type);
}
op(_CHECK_METHOD_VERSION_KW, (func_version/2, callable, null, unused[oparg], unused -- callable, null, unused[oparg], unused)) {
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyMethod_Type)) {
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
assert(PyMethod_Check(method));
ADD_OP(_CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
uop_buffer_last(&ctx->out_buffer)->operand1 = (uintptr_t)method->im_func;
}
else {
// Guarding on the bound method, safe to promote.
PyObject *bound_method = sym_get_probable_value(callable);
if (bound_method != NULL && Py_TYPE(bound_method) == &PyMethod_Type) {
PyMethodObject *method = (PyMethodObject *)bound_method;
PyObject *func = method->im_func;
if (PyFunction_Check(func) &&
((PyFunctionObject *)func)->func_version == func_version) {
_Py_BloomFilter_Add(dependencies, func);
sym_set_const(callable, bound_method);
}
}
}
sym_set_type(callable, &PyMethod_Type);
}
op(_CHECK_FUNCTION_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
assert(sym_matches_type(callable, &PyFunction_Type));
if (sym_is_const(ctx, callable)) {
@ -1092,6 +1127,18 @@ dummy_func(void) {
}
}
op(_EXPAND_METHOD_KW, (callable, self_or_null, unused[oparg], unused -- callable, self_or_null, unused[oparg], unused)) {
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyMethod_Type)) {
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
callable = sym_new_const(ctx, method->im_func);
self_or_null = sym_new_const(ctx, method->im_self);
}
else {
callable = sym_new_not_null(ctx);
self_or_null = sym_new_not_null(ctx);
}
}
op(_MAYBE_EXPAND_METHOD, (callable, self_or_null, args[oparg] -- callable, self_or_null, args[oparg])) {
(void)args;
callable = sym_new_not_null(ctx);
@ -2261,6 +2308,10 @@ dummy_func(void) {
sym_set_recorded_value(func, (PyObject *)this_instr->operand0);
}
op(_RECORD_CALLABLE_KW, (func, self, args[oparg], kwnames -- func, self, args[oparg], kwnames)) {
sym_set_recorded_value(func, (PyObject *)this_instr->operand0);
}
op(_RECORD_BOUND_METHOD, (callable, self, args[oparg] -- callable, self, args[oparg])) {
sym_set_recorded_value(callable, (PyObject *)this_instr->operand0);
}