Skip to content

Commit 23e1ff7

Browse files
committed
Add CHECK_PERIOIDIC after every call, then skip it for Python call specializations
1 parent 60bf701 commit 23e1ff7

File tree

9 files changed

+53
-62
lines changed

9 files changed

+53
-62
lines changed

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_uop_metadata.h

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

Lib/test/test_compiler_assemble.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def inner():
106106
('SET_FUNCTION_ATTRIBUTE', 8, 2),
107107
('PUSH_NULL', None, 1),
108108
('CALL', 0, 2), # (lambda: x)()
109+
('CHECK_PERIODIC', 0, 2),
109110
('LOAD_CONST', 2, 2), # 2
110111
('BINARY_OP', 6, 2), # %
111112
('RETURN_VALUE', None, 2)

Python/bytecodes.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,13 @@ dummy_func(
25452545
pushed_frame->localsplus[0] = owner;
25462546
DEAD(owner);
25472547
new_frame = PyStackRef_Wrap(pushed_frame);
2548+
/* Can't use _SAVE_RETURN_OFFSET as there is no follwoing CHECK_PERIODIC to skip */
2549+
#if TIER_ONE
2550+
frame->return_offset = (uint16_t)(next_instr - this_instr);
2551+
#endif
2552+
#if TIER_TWO
2553+
frame->return_offset = oparg;
2554+
#endif
25482555
}
25492556

25502557
macro(LOAD_ATTR_PROPERTY) =
@@ -2553,7 +2560,6 @@ dummy_func(
25532560
_GUARD_TYPE_VERSION +
25542561
unused/2 +
25552562
_LOAD_ATTR_PROPERTY_FRAME +
2556-
_SAVE_RETURN_OFFSET +
25572563
_PUSH_FRAME;
25582564

25592565
inst(LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, (unused/1, type_version/2, func_version/2, getattribute/4, owner -- unused)) {
@@ -4352,6 +4358,7 @@ dummy_func(
43524358
macro(CALL_LIST_APPEND) =
43534359
unused/1 +
43544360
unused/2 +
4361+
CHECK_PERIODIC + // Do this first to avoid deopting in the middle of the qinstruction
43554362
_GUARD_CALLABLE_LIST_APPEND +
43564363
_GUARD_NOS_NOT_NULL +
43574364
_GUARD_NOS_LIST +
@@ -5203,8 +5210,10 @@ dummy_func(
52035210
}
52045211

52055212
op(_SAVE_RETURN_OFFSET, (--)) {
5213+
// Skips following CHEKC_PERIODIC
52065214
#if TIER_ONE
5207-
frame->return_offset = (uint16_t)(next_instr - this_instr);
5215+
assert(next_instr->op.code == CHECK_PERIODIC);
5216+
frame->return_offset = (uint16_t)(next_instr - this_instr)+1;
52085217
#endif
52095218
#if TIER_TWO
52105219
frame->return_offset = oparg;

Python/codegen.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ codegen_call_exit_with_nones(compiler *c, location loc)
463463
ADDOP_LOAD_CONST(c, loc, Py_None);
464464
ADDOP_LOAD_CONST(c, loc, Py_None);
465465
ADDOP_I(c, loc, CALL, 3);
466+
ADDOP(c, loc, CHECK_PERIODIC);
466467
return SUCCESS;
467468
}
468469

@@ -1504,6 +1505,7 @@ codegen_function(compiler *c, stmt_ty s, int is_async)
15041505
ADDOP(c, loc, PUSH_NULL);
15051506
ADDOP_I(c, loc, CALL, 0);
15061507
}
1508+
ADDOP(c, loc, CHECK_PERIODIC);
15071509
}
15081510

15091511
RETURN_IF_ERROR(codegen_apply_decorators(c, decos));
@@ -1777,6 +1779,8 @@ codegen_typealias(compiler *c, stmt_ty s)
17771779
RETURN_IF_ERROR(ret);
17781780
ADDOP(c, loc, PUSH_NULL);
17791781
ADDOP_I(c, loc, CALL, 0);
1782+
ADDOP(c, loc, CHECK_PERIODIC);
1783+
17801784
}
17811785
RETURN_IF_ERROR(codegen_nameop(c, loc, name, Store));
17821786
return SUCCESS;
@@ -2965,6 +2969,7 @@ codegen_assert(compiler *c, stmt_ty s)
29652969
if (s->v.Assert.msg) {
29662970
VISIT(c, expr, s->v.Assert.msg);
29672971
ADDOP_I(c, LOC(s), CALL, 0);
2972+
ADDOP(c, LOC(s), CHECK_PERIODIC);
29682973
}
29692974
ADDOP_I(c, LOC(s->v.Assert.test), RAISE_VARARGS, 1);
29702975

@@ -4135,6 +4140,7 @@ codegen_joined_str(compiler *c, expr_ty e)
41354140
ADDOP_I(c, loc, LIST_APPEND, 1);
41364141
}
41374142
ADDOP_I(c, loc, CALL, 1);
4143+
ADDOP(c, loc, CHECK_PERIODIC);
41384144
}
41394145
else {
41404146
VISIT_SEQ(c, expr, e->v.JoinedStr.values);
@@ -4871,6 +4877,7 @@ codegen_comprehension(compiler *c, expr_ty e, int type,
48714877

48724878
VISIT(c, expr, outermost->iter);
48734879
ADDOP_I(c, loc, CALL, 0);
4880+
ADDOP(c, loc, CHECK_PERIODIC);
48744881

48754882
if (is_async_comprehension && type != COMP_GENEXP) {
48764883
ADDOP_I(c, loc, GET_AWAITABLE, 0);
@@ -5012,6 +5019,7 @@ codegen_async_with_inner(compiler *c, stmt_ty s, int pos)
50125019
ADDOP_I(c, loc, SWAP, 3);
50135020
ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___AENTER__);
50145021
ADDOP_I(c, loc, CALL, 0);
5022+
ADDOP(c, loc, CHECK_PERIODIC);
50155023
ADDOP_I(c, loc, GET_AWAITABLE, 1);
50165024
ADDOP_LOAD_CONST(c, loc, Py_None);
50175025
ADD_YIELD_FROM(c, loc, 1);
@@ -5121,6 +5129,7 @@ codegen_with_inner(compiler *c, stmt_ty s, int pos)
51215129
ADDOP_I(c, loc, SWAP, 3);
51225130
ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___ENTER__);
51235131
ADDOP_I(c, loc, CALL, 0);
5132+
ADDOP(c, loc, CHECK_PERIODIC);
51245133
ADDOP_JUMP(c, loc, SETUP_WITH, final);
51255134

51265135
/* SETUP_WITH pushes a finally block. */

Python/executor_cases.c.h

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

Python/generated_cases.c.h

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

Python/optimizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ translate_bytecode_to_trace(
769769
oparg = orig_oparg & 0xF;
770770
break;
771771
case OPARG_SAVE_RETURN_OFFSET: // op=_SAVE_RETURN_OFFSET; oparg=return_offset
772-
oparg = offset;
772+
oparg = offset + 1;
773773
assert(uop == _SAVE_RETURN_OFFSET);
774774
break;
775775
case OPARG_REPLACED:

Python/optimizer_cases.c.h

Lines changed: 1 addition & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)