Skip to content

Commit 8806c74

Browse files
Fixups
1 parent 9dd70a0 commit 8806c74

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,6 +5174,24 @@ def g():
51745174
PYTHON_JIT="1", PYTHON_JIT_STRESS="1")
51755175
self.assertEqual(result[0].rc, 0, result)
51765176

5177+
def test_func_version_guarded_on_change(self):
5178+
def testfunc(n):
5179+
for i in range(n):
5180+
# Only works on functions promoted to constants
5181+
global_identity_code_will_be_modified(i)
5182+
5183+
testfunc(TIER2_THRESHOLD)
5184+
5185+
ex = get_first_executor(testfunc)
5186+
self.assertIsNotNone(ex)
5187+
uops = get_opnames(ex)
5188+
self.assertIn("_PUSH_FRAME", uops)
5189+
self.assertIn("_CHECK_FUNCTION_VERSION", uops)
5190+
5191+
global_identity_code_will_be_modified.__code__ = (lambda a: 0xdeadead).__code__
5192+
# JItted code should've deopted.
5193+
self.assertEqual(global_identity_code_will_be_modified(None), 0xdeadead)
5194+
51775195
def test_call_super(self):
51785196
class A:
51795197
def method1(self):

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,8 @@ dummy_func(void) {
22352235
if (co->co_version == version) {
22362236
_Py_BloomFilter_Add(dependencies, co);
22372237
// Functions derive their version from code objects.
2238-
if (sym_get_func_version(ctx->frame->callable) == version) {
2238+
PyFunctionObject *func = (PyFunctionObject *)sym_get_const(ctx, ctx->frame->callable);
2239+
if (func != NULL && func->func_version == version) {
22392240
REPLACE_OP(this_instr, _NOP, 0, 0);
22402241
}
22412242
}
@@ -2268,7 +2269,8 @@ dummy_func(void) {
22682269
op(_GUARD_IP__PUSH_FRAME, (ip/4 --)) {
22692270
(void)ip;
22702271
stack_pointer = sym_set_stack_depth((int)this_instr->operand1, stack_pointer);
2271-
if (sym_get_func_version(ctx->frame->callable) != 0 &&
2272+
PyFunctionObject *func = (PyFunctionObject *)sym_get_const(ctx, ctx->frame->callable);
2273+
if (func != NULL && func->func_version != 0 &&
22722274
// We can remove this guard for simple function call targets.
22732275
(((PyCodeObject *)ctx->frame->func->func_code)->co_flags &
22742276
(CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) == 0) {

Python/optimizer_cases.c.h

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)