Skip to content

GH-113710: Fix optimization of globals using _CHECK_FUNCTION #116460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4120,8 +4120,9 @@ dummy_func(
null = NULL;
}

tier2 op(_CHECK_FUNCTION, (func/4 -- )) {
DEOPT_IF(frame->f_funcobj != func);
tier2 op(_CHECK_FUNCTION, (func_version/2 -- )) {
assert(PyFunction_Check(frame->f_funcobj));
DEOPT_IF(((PyFunctionObject *)frame->f_funcobj)->func_version != func_version);
}

/* Internal -- for testing executors */
Expand Down
5 changes: 3 additions & 2 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
return 1;
}
PyObject *globals = frame->f_globals;
assert(PyFunction_Check(((PyFunctionObject *)frame->f_funcobj)));
assert(((PyFunctionObject *)frame->f_funcobj)->func_builtins == builtins);
assert(((PyFunctionObject *)frame->f_funcobj)->func_globals == globals);
PyFunctionObject *function = (PyFunctionObject *)frame->f_funcobj;
assert(PyFunction_Check(function));
assert(function->func_builtins == builtins);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: these are read-only attributes so this precondition should hold.

assert(function->func_globals == globals);
uint32_t function_version = _PyFunction_GetVersionForCurrentState(function);
/* In order to treat globals as constants, we need to
* know that the globals dict is the one we expected, and
* that it hasn't changed
Expand Down Expand Up @@ -181,7 +183,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
}
else {
buffer[pc].opcode = _CHECK_FUNCTION;
buffer[pc].operand = (uintptr_t)builtins;
buffer[pc].operand = function_version;
function_checked |= 1;
}
break;
Expand All @@ -203,7 +205,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
}
else {
buffer[pc].opcode = _CHECK_FUNCTION;
buffer[pc].operand = (uintptr_t)globals;
buffer[pc].operand = function_version;
function_checked |= 1;
}
break;
Expand All @@ -227,7 +229,8 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
return 1;
}
assert(PyFunction_Check(func));
if (prechecked_function_version == func->func_version) {
function_version = func->func_version;
if (prechecked_function_version == function_version) {
function_checked |= 1;
}
prechecked_function_version = 0;
Expand All @@ -245,6 +248,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
function_checked >>= 1;
PyFunctionObject *func = (PyFunctionObject *)buffer[pc].operand;
assert(PyFunction_Check(func));
function_version = func->func_version;
globals = func->func_globals;
builtins = func->func_builtins;
break;
Expand Down