Skip to content

GH-132532: Make CHECK_PERIODIC an instruction, not just a uop. #135772

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

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
Add CHECK_PERIOIDIC after every call, then skip it for Python call sp…
…ecializations
  • Loading branch information
markshannon committed Jun 20, 2025
commit 23e1ff7a6f2b4fabcc30ccf1d272f461efddd276
5 changes: 2 additions & 3 deletions Include/internal/pycore_opcode_metadata.h

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

4 changes: 0 additions & 4 deletions Include/internal/pycore_uop_metadata.h

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

1 change: 1 addition & 0 deletions Lib/test/test_compiler_assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def inner():
('SET_FUNCTION_ATTRIBUTE', 8, 2),
('PUSH_NULL', None, 1),
('CALL', 0, 2), # (lambda: x)()
('CHECK_PERIODIC', 0, 2),
('LOAD_CONST', 2, 2), # 2
('BINARY_OP', 6, 2), # %
('RETURN_VALUE', None, 2)
Expand Down
13 changes: 11 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,13 @@ dummy_func(
pushed_frame->localsplus[0] = owner;
DEAD(owner);
new_frame = PyStackRef_Wrap(pushed_frame);
/* Can't use _SAVE_RETURN_OFFSET as there is no follwoing CHECK_PERIODIC to skip */
#if TIER_ONE
frame->return_offset = (uint16_t)(next_instr - this_instr);
#endif
#if TIER_TWO
frame->return_offset = oparg;
#endif
}

macro(LOAD_ATTR_PROPERTY) =
Expand All @@ -2553,7 +2560,6 @@ dummy_func(
_GUARD_TYPE_VERSION +
unused/2 +
_LOAD_ATTR_PROPERTY_FRAME +
_SAVE_RETURN_OFFSET +
_PUSH_FRAME;

inst(LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, (unused/1, type_version/2, func_version/2, getattribute/4, owner -- unused)) {
Expand Down Expand Up @@ -4352,6 +4358,7 @@ dummy_func(
macro(CALL_LIST_APPEND) =
unused/1 +
unused/2 +
CHECK_PERIODIC + // Do this first to avoid deopting in the middle of the qinstruction
_GUARD_CALLABLE_LIST_APPEND +
_GUARD_NOS_NOT_NULL +
_GUARD_NOS_LIST +
Expand Down Expand Up @@ -5203,8 +5210,10 @@ dummy_func(
}

op(_SAVE_RETURN_OFFSET, (--)) {
// Skips following CHEKC_PERIODIC
#if TIER_ONE
frame->return_offset = (uint16_t)(next_instr - this_instr);
assert(next_instr->op.code == CHECK_PERIODIC);
frame->return_offset = (uint16_t)(next_instr - this_instr)+1;
#endif
#if TIER_TWO
frame->return_offset = oparg;
Expand Down
9 changes: 9 additions & 0 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ codegen_call_exit_with_nones(compiler *c, location loc)
ADDOP_LOAD_CONST(c, loc, Py_None);
ADDOP_LOAD_CONST(c, loc, Py_None);
ADDOP_I(c, loc, CALL, 3);
ADDOP(c, loc, CHECK_PERIODIC);
return SUCCESS;
}

Expand Down Expand Up @@ -1504,6 +1505,7 @@ codegen_function(compiler *c, stmt_ty s, int is_async)
ADDOP(c, loc, PUSH_NULL);
ADDOP_I(c, loc, CALL, 0);
}
ADDOP(c, loc, CHECK_PERIODIC);
}

RETURN_IF_ERROR(codegen_apply_decorators(c, decos));
Expand Down Expand Up @@ -1777,6 +1779,8 @@ codegen_typealias(compiler *c, stmt_ty s)
RETURN_IF_ERROR(ret);
ADDOP(c, loc, PUSH_NULL);
ADDOP_I(c, loc, CALL, 0);
ADDOP(c, loc, CHECK_PERIODIC);

}
RETURN_IF_ERROR(codegen_nameop(c, loc, name, Store));
return SUCCESS;
Expand Down Expand Up @@ -2965,6 +2969,7 @@ codegen_assert(compiler *c, stmt_ty s)
if (s->v.Assert.msg) {
VISIT(c, expr, s->v.Assert.msg);
ADDOP_I(c, LOC(s), CALL, 0);
ADDOP(c, LOC(s), CHECK_PERIODIC);
}
ADDOP_I(c, LOC(s->v.Assert.test), RAISE_VARARGS, 1);

Expand Down Expand Up @@ -4135,6 +4140,7 @@ codegen_joined_str(compiler *c, expr_ty e)
ADDOP_I(c, loc, LIST_APPEND, 1);
}
ADDOP_I(c, loc, CALL, 1);
ADDOP(c, loc, CHECK_PERIODIC);
}
else {
VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Expand Down Expand Up @@ -4871,6 +4877,7 @@ codegen_comprehension(compiler *c, expr_ty e, int type,

VISIT(c, expr, outermost->iter);
ADDOP_I(c, loc, CALL, 0);
ADDOP(c, loc, CHECK_PERIODIC);

if (is_async_comprehension && type != COMP_GENEXP) {
ADDOP_I(c, loc, GET_AWAITABLE, 0);
Expand Down Expand Up @@ -5012,6 +5019,7 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos)
ADDOP_I(c, loc, SWAP, 3);
ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___AENTER__);
ADDOP_I(c, loc, CALL, 0);
ADDOP(c, loc, CHECK_PERIODIC);
ADDOP_I(c, loc, GET_AWAITABLE, 1);
ADDOP_LOAD_CONST(c, loc, Py_None);
ADD_YIELD_FROM(c, loc, 1);
Expand Down Expand Up @@ -5121,6 +5129,7 @@ codegen_with_inner(compiler *c, stmt_ty s, int pos)
ADDOP_I(c, loc, SWAP, 3);
ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___ENTER__);
ADDOP_I(c, loc, CALL, 0);
ADDOP(c, loc, CHECK_PERIODIC);
ADDOP_JUMP(c, loc, SETUP_WITH, final);

/* SETUP_WITH pushes a finally block. */
Expand Down
37 changes: 3 additions & 34 deletions Python/executor_cases.c.h

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

34 changes: 25 additions & 9 deletions Python/generated_cases.c.h

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

2 changes: 1 addition & 1 deletion Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ translate_bytecode_to_trace(
oparg = orig_oparg & 0xF;
break;
case OPARG_SAVE_RETURN_OFFSET: // op=_SAVE_RETURN_OFFSET; oparg=return_offset
oparg = offset;
oparg = offset + 1;
assert(uop == _SAVE_RETURN_OFFSET);
break;
case OPARG_REPLACED:
Expand Down
10 changes: 1 addition & 9 deletions Python/optimizer_cases.c.h

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy