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
2 changes: 0 additions & 2 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ extern PyCodeObject *_Py_uop_sym_get_probable_func_code(JitOptRef sym);
extern PyObject *_Py_uop_sym_get_probable_value(JitOptRef sym);
extern PyTypeObject *_Py_uop_sym_get_probable_type(JitOptRef sym);
extern JitOptRef *_Py_uop_sym_set_stack_depth(JitOptContext *ctx, int stack_depth, JitOptRef *current_sp);
extern uint32_t _Py_uop_sym_get_func_version(JitOptRef ref);
bool _Py_uop_sym_set_func_version(JitOptContext *ctx, JitOptRef ref, uint32_t version);

extern void _Py_uop_abstractcontext_init(JitOptContext *ctx, _PyBloomFilter *dependencies);
extern void _Py_uop_abstractcontext_fini(JitOptContext *ctx);
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_optimizer_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ typedef enum _JitSymType {
JIT_SYM_NON_NULL_TAG = 3,
JIT_SYM_BOTTOM_TAG = 4,
JIT_SYM_TYPE_VERSION_TAG = 5,
JIT_SYM_FUNC_VERSION_TAG = 6,
JIT_SYM_KNOWN_CLASS_TAG = 7,
JIT_SYM_KNOWN_VALUE_TAG = 8,
JIT_SYM_TUPLE_TAG = 9,
Expand Down
31 changes: 24 additions & 7 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,9 +1622,8 @@ def testfunc(n):
self.assertEqual(uops.count("_PUSH_FRAME"), 2)
# Type version propagation: one guard covers both method lookups
self.assertEqual(uops.count("_GUARD_TYPE_VERSION"), 1)
# Function checks eliminated (type info resolves the callable)
self.assertNotIn("_CHECK_FUNCTION_VERSION", uops)
self.assertNotIn("_CHECK_FUNCTION_EXACT_ARGS", uops)
# Function checks cannot be eliminated for safety reasons.
self.assertIn("_CHECK_FUNCTION_VERSION", uops)

def test_method_chain_guard_elimination(self):
"""
Expand Down Expand Up @@ -1669,10 +1668,7 @@ def testfunc(n):
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)
self.assertIn("_CHECK_FUNCTION_VERSION", uops)
# Removed guard
self.assertNotIn("_CHECK_FUNCTION_EXACT_ARGS", uops)

Expand Down Expand Up @@ -5178,6 +5174,24 @@ def g():
PYTHON_JIT="1", PYTHON_JIT_STRESS="1")
self.assertEqual(result[0].rc, 0, result)

def test_func_version_guarded_on_change(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)
self.assertIn("_CHECK_FUNCTION_VERSION", uops)

global_identity_code_will_be_modified.__code__ = (lambda a: 0xdeadead).__code__
# JItted code should've deopted.
self.assertEqual(global_identity_code_will_be_modified(None), 0xdeadead)

def test_call_super(self):
class A:
def method1(self):
Expand Down Expand Up @@ -5224,6 +5238,9 @@ def testfunc(n):
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
1 change: 1 addition & 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" // _Py_Executors_InvalidateDependency()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_stats.h"
Expand Down
2 changes: 0 additions & 2 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
#define sym_get_probable_func_code _Py_uop_sym_get_probable_func_code
#define sym_get_probable_value _Py_uop_sym_get_probable_value
#define sym_set_stack_depth(DEPTH, SP) _Py_uop_sym_set_stack_depth(ctx, DEPTH, SP)
#define sym_get_func_version _Py_uop_sym_get_func_version
#define sym_set_func_version _Py_uop_sym_set_func_version

/* Comparison oparg masks */
#define COMPARE_LT_MASK 2
Expand Down
22 changes: 15 additions & 7 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,12 +1001,18 @@ dummy_func(void) {
}

op(_CHECK_FUNCTION_VERSION, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
if (sym_get_func_version(callable) == func_version) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
else {
sym_set_func_version(ctx, callable, func_version);
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);
// We do not need to add func to the bloom filter, as we never remove
// this guard. Note: we generally do not want to add functions to our dependencies,
// as we want to avoid having to invalidate all executors on every function
// deallocation, which is a common procedure (e.g. lambdas).
}

op(_CHECK_METHOD_VERSION, (func_version/2, callable, null, unused[oparg] -- callable, null, unused[oparg])) {
Expand Down Expand Up @@ -2229,7 +2235,8 @@ dummy_func(void) {
if (co->co_version == version) {
_Py_BloomFilter_Add(dependencies, co);
// Functions derive their version from code objects.
if (sym_get_func_version(ctx->frame->callable) == version) {
PyFunctionObject *func = (PyFunctionObject *)sym_get_const(ctx, ctx->frame->callable);
if (func != NULL && func->func_version == version) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
}
Expand Down Expand Up @@ -2262,7 +2269,8 @@ dummy_func(void) {
op(_GUARD_IP__PUSH_FRAME, (ip/4 --)) {
(void)ip;
stack_pointer = sym_set_stack_depth((int)this_instr->operand1, stack_pointer);
if (sym_get_func_version(ctx->frame->callable) != 0 &&
PyFunctionObject *func = (PyFunctionObject *)sym_get_const(ctx, ctx->frame->callable);
if (func != NULL && func->func_version != 0 &&
// We can remove this guard for simple function call targets.
(((PyCodeObject *)ctx->frame->func->func_code)->co_flags &
(CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) == 0) {
Expand Down
17 changes: 10 additions & 7 deletions Python/optimizer_cases.c.h

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

Loading
Loading