summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2024-03-08 09:47:41 (GMT)
committerGitHub <noreply@github.com>2024-03-08 09:47:41 (GMT)
commit0003285c8d78f0b463f2acc164655456fcfc3206 (patch)
tree8cb52887f94c39d3099c312188220592242a0e54 /Python
parent0b647141d587065c5b82bd658485adca8823a943 (diff)
downloadcpython-0003285c8d78f0b463f2acc164655456fcfc3206.zip
cpython-0003285c8d78f0b463f2acc164655456fcfc3206.tar.gz
cpython-0003285c8d78f0b463f2acc164655456fcfc3206.tar.bz2
GH-113710: Fix optimization of globals using `_CHECK_FUNCTION` (GH-116460)
Diffstat (limited to 'Python')
-rw-r--r--Python/bytecodes.c5
-rw-r--r--Python/executor_cases.c.h5
-rw-r--r--Python/optimizer_analysis.c16
3 files changed, 16 insertions, 10 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 0397d96..bf9d2a3 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -4096,8 +4096,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 */
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index 26ac159..ccd8fb3 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -3829,8 +3829,9 @@
}
case _CHECK_FUNCTION: {
- PyObject *func = (PyObject *)CURRENT_OPERAND();
- if (frame->f_funcobj != func) goto deoptimize;
+ uint32_t func_version = (uint32_t)CURRENT_OPERAND();
+ assert(PyFunction_Check(frame->f_funcobj));
+ if (((PyFunctionObject *)frame->f_funcobj)->func_version != func_version) goto deoptimize;
break;
}
diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c
index 0f25527..51ec14b 100644
--- a/Python/optimizer_analysis.c
+++ b/Python/optimizer_analysis.c
@@ -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);
+ 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
@@ -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;
@@ -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;
@@ -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;
@@ -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;