Skip to content
Open
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
26 changes: 26 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4288,9 +4288,35 @@ def g():
PYTHON_JIT="1", PYTHON_JIT_STRESS="1")
self.assertEqual(result[0].rc, 0, result)

def test_func_version_watched_and_invalidated(self):
def testfunc(n):
for i in range(n):
# Only works on functions promoted to constants
global_identity_code_will_be_modified(i)

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_PUSH_FRAME", uops)
# Both should be not present, as this is a call
# to a simple function with a known function version.
self.assertNotIn("_CHECK_FUNCTION_VERSION_INLINE", uops)
self.assertNotIn("_CHECK_FUNCTION_VERSION", uops)

global_identity_code_will_be_modified.__code__ = (lambda a:a).__code__
ex = get_first_executor(testfunc)
# Invalidated and removed.
self.assertIsNone(ex)


def global_identity(x):
return x

def global_identity_code_will_be_modified(x):
return x

class TestObject:
def test(self, *args, **kwargs):
return args[0]
Expand Down
4 changes: 4 additions & 0 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_object_deferred.h" // _PyObject_SetDeferredRefcount()
#include "pycore_optimizer.h"
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_stats.h"
Expand Down Expand Up @@ -63,6 +64,9 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
case PyFunction_EVENT_MODIFY_DEFAULTS:
case PyFunction_EVENT_MODIFY_KWDEFAULTS:
case PyFunction_EVENT_MODIFY_QUALNAME:
#if _Py_TIER2
_Py_Executors_InvalidateDependency(interp, func, 1);
#endif
RARE_EVENT_INTERP_INC(interp, func_modification);
break;
default:
Expand Down
9 changes: 9 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,15 @@ dummy_func(void) {
}

op(_CHECK_FUNCTION_VERSION, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
PyObject *func = sym_get_probable_value(callable);
if (func == NULL || !PyFunction_Check(func)) {
ctx->contradiction = true;
ctx->done = true;
break;
}
// This could pass due to a global promoted const.
// So we need to add it to the dependencies on both branches.
_Py_BloomFilter_Add(dependencies, func);
if (sym_get_func_version(callable) == func_version) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
Expand Down
7 changes: 7 additions & 0 deletions Python/optimizer_cases.c.h

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

Loading