[Python-checkins] Normalize jumps in compiler. All forward jumps to use JUMP_FORWARD. (GH-28755)
markshannon
webhook-mailer at python.org
Wed Oct 6 08:05:52 EDT 2021
https://github.com/python/cpython/commit/f6eafe18c004c55082de40d20cad084ef9dd3db7
commit: f6eafe18c004c55082de40d20cad084ef9dd3db7
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2021-10-06T13:05:45+01:00
summary:
Normalize jumps in compiler. All forward jumps to use JUMP_FORWARD. (GH-28755)
files:
M Lib/test/test_dis.py
M Python/ceval.c
M Python/compile.c
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index 0899db66d8241..4755e3f3a1724 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -1046,7 +1046,7 @@ def _prepare_test_cases():
Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False, positions=None),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=5, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False, positions=None),
@@ -1071,7 +1071,7 @@ def _prepare_test_cases():
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False, positions=None),
- Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False, positions=None),
+ Instruction(opname='JUMP_FORWARD', opcode=110, arg=6, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True, positions=None),
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True, positions=None),
diff --git a/Python/ceval.c b/Python/ceval.c
index 2dbc291897c03..a3a173dfb7013 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4051,19 +4051,18 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
TARGET(JUMP_ABSOLUTE) {
PREDICTED(JUMP_ABSOLUTE);
- if (oparg < INSTR_OFFSET()) {
- /* Increment the warmup counter and quicken if warm enough
- * _Py_Quicken is idempotent so we don't worry about overflow */
- if (!PyCodeObject_IsWarmedUp(co)) {
- PyCodeObject_IncrementWarmup(co);
- if (PyCodeObject_IsWarmedUp(co)) {
- if (_Py_Quicken(co)) {
- goto error;
- }
- int nexti = INSTR_OFFSET();
- first_instr = co->co_firstinstr;
- next_instr = first_instr + nexti;
+ assert(oparg < INSTR_OFFSET());
+ /* Increment the warmup counter and quicken if warm enough
+ * _Py_Quicken is idempotent so we don't worry about overflow */
+ if (!PyCodeObject_IsWarmedUp(co)) {
+ PyCodeObject_IncrementWarmup(co);
+ if (PyCodeObject_IsWarmedUp(co)) {
+ if (_Py_Quicken(co)) {
+ goto error;
}
+ int nexti = INSTR_OFFSET();
+ first_instr = co->co_firstinstr;
+ next_instr = first_instr + nexti;
}
}
JUMPTO(oparg);
@@ -4072,6 +4071,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
}
TARGET(JUMP_ABSOLUTE_QUICK) {
+ assert(oparg < INSTR_OFFSET());
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
DISPATCH();
diff --git a/Python/compile.c b/Python/compile.c
index 694da29b771d0..2d82d6a1e5a91 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -7220,6 +7220,26 @@ assemble_emit(struct assembler *a, struct instr *i)
return 1;
}
+static void
+normalize_jumps(struct assembler *a)
+{
+ for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
+ b->b_visited = 0;
+ }
+ for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
+ b->b_visited = 1;
+ if (b->b_iused == 0) {
+ continue;
+ }
+ struct instr *last = &b->b_instr[b->b_iused-1];
+ if (last->i_opcode == JUMP_ABSOLUTE &&
+ last->i_target->b_visited == 0
+ ) {
+ last->i_opcode = JUMP_FORWARD;
+ }
+ }
+}
+
static void
assemble_jump_offsets(struct assembler *a, struct compiler *c)
{
@@ -7897,6 +7917,9 @@ assemble(struct compiler *c, int addNone)
clean_basic_block(b);
}
+ /* Order of basic blocks must have been determined by now */
+ normalize_jumps(&a);
+
/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(&a, c);
More information about the Python-checkins
mailing list