Skip to content

Commit

Permalink
pythongh-105481: add HAS_JUMP flag to opcode metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Jun 14, 2023
1 parent 7199584 commit 54aad59
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 164 deletions.
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_utils.h
Expand Up @@ -26,11 +26,11 @@ extern "C" {
(opcode) == SETUP_CLEANUP)

#define HAS_TARGET(opcode) \
(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode))
(OPCODE_HAS_JUMP(opcode) || IS_BLOCK_PUSH_OPCODE(opcode))

/* opcodes that must be last in the basicblock */
#define IS_TERMINATOR_OPCODE(opcode) \
(IS_JUMP_OPCODE(opcode) || IS_SCOPE_EXIT_OPCODE(opcode))
(OPCODE_HAS_JUMP(opcode) || IS_SCOPE_EXIT_OPCODE(opcode))

/* opcodes which are not emitted in codegen stage, only by the assembler */
#define IS_ASSEMBLER_OPCODE(opcode) \
Expand Down
34 changes: 19 additions & 15 deletions Python/bytecodes.c
Expand Up @@ -428,7 +428,7 @@ dummy_func(
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
ERROR_IF(*target_local == NULL, error);
// The STORE_FAST is already done.
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_OP + 1);
SKIP_OVER(INLINE_CACHE_ENTRIES_BINARY_OP + 1);
}

macro(BINARY_OP_INPLACE_ADD_UNICODE) =
Expand Down Expand Up @@ -555,7 +555,7 @@ dummy_func(
STACK_SHRINK(2);
new_frame->localsplus[0] = container;
new_frame->localsplus[1] = sub;
JUMPBY(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
SKIP_OVER(INLINE_CACHE_ENTRIES_BINARY_SUBSCR);
frame->return_offset = 0;
DISPATCH_INLINED(new_frame);
}
Expand Down Expand Up @@ -877,7 +877,7 @@ dummy_func(
gen->gi_frame_state = FRAME_EXECUTING;
gen->gi_exc_state.previous_item = tstate->exc_info;
tstate->exc_info = &gen->gi_exc_state;
JUMPBY(INLINE_CACHE_ENTRIES_SEND);
SKIP_OVER(INLINE_CACHE_ENTRIES_SEND);
DISPATCH_INLINED(gen_frame);
}
if (Py_IsNone(v) && PyIter_Check(receiver)) {
Expand Down Expand Up @@ -916,7 +916,7 @@ dummy_func(
gen->gi_frame_state = FRAME_EXECUTING;
gen->gi_exc_state.previous_item = tstate->exc_info;
tstate->exc_info = &gen->gi_exc_state;
JUMPBY(INLINE_CACHE_ENTRIES_SEND);
SKIP_OVER(INLINE_CACHE_ENTRIES_SEND);
DISPATCH_INLINED(gen_frame);
}

Expand Down Expand Up @@ -1903,7 +1903,7 @@ dummy_func(
int shrink_stack = !(oparg & 1);
STACK_SHRINK(shrink_stack);
new_frame->localsplus[0] = owner;
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR);
SKIP_OVER(INLINE_CACHE_ENTRIES_LOAD_ATTR);
frame->return_offset = 0;
DISPATCH_INLINED(new_frame);
}
Expand Down Expand Up @@ -1931,7 +1931,7 @@ dummy_func(
STACK_SHRINK(shrink_stack);
new_frame->localsplus[0] = owner;
new_frame->localsplus[1] = Py_NewRef(name);
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR);
SKIP_OVER(INLINE_CACHE_ENTRIES_LOAD_ATTR);
frame->return_offset = 0;
DISPATCH_INLINED(new_frame);
}
Expand Down Expand Up @@ -2354,8 +2354,9 @@ dummy_func(
next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == INSTRUMENTED_END_FOR);
Py_DECREF(iter);
STACK_SHRINK(1);
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
/* Jump forward oparg, then skip following END_FOR instruction */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
JUMPBY(oparg + 1);
DISPATCH();
}
// Common case: no jump, leave it to the code generator
Expand Down Expand Up @@ -2404,8 +2405,9 @@ dummy_func(
}
Py_DECREF(iter);
STACK_SHRINK(1);
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
/* Jump forward oparg, then skip following END_FOR instruction */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
JUMPBY(oparg + 1);
DISPATCH();
end_for_iter_list:
// Common case: no jump, leave it to the code generator
Expand All @@ -2426,8 +2428,9 @@ dummy_func(
}
Py_DECREF(iter);
STACK_SHRINK(1);
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
/* Jump forward oparg, then skip following END_FOR instruction */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
JUMPBY(oparg + 1);
DISPATCH();
end_for_iter_tuple:
// Common case: no jump, leave it to the code generator
Expand All @@ -2440,8 +2443,9 @@ dummy_func(
if (r->len <= 0) {
STACK_SHRINK(1);
Py_DECREF(r);
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
// Jump over END_FOR instruction.
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
JUMPBY(oparg + 1);
DISPATCH();
}
long value = r->start;
Expand All @@ -2465,7 +2469,7 @@ dummy_func(
gen->gi_frame_state = FRAME_EXECUTING;
gen->gi_exc_state.previous_item = tstate->exc_info;
tstate->exc_info = &gen->gi_exc_state;
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER);
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
assert(next_instr[oparg].op.code == END_FOR ||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR);
DISPATCH_INLINED(gen_frame);
Expand Down Expand Up @@ -2736,7 +2740,7 @@ dummy_func(
if (new_frame == NULL) {
goto error;
}
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
frame->return_offset = 0;
DISPATCH_INLINED(new_frame);
}
Expand Down Expand Up @@ -2810,7 +2814,7 @@ dummy_func(
}
// Manipulate stack directly since we leave using DISPATCH_INLINED().
STACK_SHRINK(oparg + 2);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
frame->return_offset = 0;
DISPATCH_INLINED(new_frame);
}
Expand Down Expand Up @@ -2848,7 +2852,7 @@ dummy_func(
}
// Manipulate stack and cache directly since we leave using DISPATCH_INLINED().
STACK_SHRINK(oparg + 2);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL);
frame->return_offset = 0;
DISPATCH_INLINED(new_frame);
}
Expand Down Expand Up @@ -3091,7 +3095,7 @@ dummy_func(
Py_DECREF(method);
STACK_SHRINK(3);
// CALL + POP_TOP
JUMPBY(INLINE_CACHE_ENTRIES_CALL + 1);
SKIP_OVER(INLINE_CACHE_ENTRIES_CALL + 1);
assert(next_instr[-1].op.code == POP_TOP);
DISPATCH();
}
Expand Down
6 changes: 6 additions & 0 deletions Python/ceval_macros.h
Expand Up @@ -145,7 +145,13 @@ GETITEM(PyObject *v, Py_ssize_t i) {
oparg = word.op.arg; \
} while (0)
#define JUMPTO(x) (next_instr = _PyCode_CODE(_PyFrame_GetCode(frame)) + (x))

/* JUMPBY makes the generator identify the instruction as a jump. SKIP_OVER is for
* advancing to the next instruction, taking into account cache entries and skipped
* instructions into account.
*/
#define JUMPBY(x) (next_instr += (x))
#define SKIP_OVER(x) (next_instr += (x))

/* OpCode prediction macros
Some opcodes tend to come in pairs thus making it possible to
Expand Down
3 changes: 2 additions & 1 deletion Python/compile.c
Expand Up @@ -250,6 +250,7 @@ instr_sequence_addop(instr_sequence *seq, int opcode, int oparg, location loc)
{
assert(!HAS_ARG(opcode) == !OPCODE_HAS_ARG(opcode));
assert(!HAS_CONST(opcode) == !OPCODE_HAS_CONST(opcode));
assert(!OPCODE_HAS_JUMP(opcode) == !OPCODE_HAS_JUMP(opcode));
assert(0 <= opcode && opcode <= MAX_OPCODE);
assert(IS_PSEUDO_OPCODE(opcode) == IS_PSEUDO_INSTR(opcode));
assert(IS_WITHIN_OPCODE_RANGE(opcode));
Expand Down Expand Up @@ -1114,7 +1115,7 @@ codegen_addop_j(instr_sequence *seq, location loc,
int opcode, jump_target_label target)
{
assert(IS_LABEL(target));
assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
assert(OPCODE_HAS_JUMP(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
assert(!IS_ASSEMBLER_OPCODE(opcode));
return instr_sequence_addop(seq, opcode, target.id, loc);
}
Expand Down
3 changes: 2 additions & 1 deletion Python/flowgraph.c
Expand Up @@ -45,7 +45,8 @@ is_block_push(cfg_instr *i)
static inline int
is_jump(cfg_instr *i)
{
return IS_JUMP_OPCODE(i->i_opcode);
assert(!OPCODE_HAS_JUMP(i->i_opcode) == !IS_JUMP_OPCODE(i->i_opcode));
return OPCODE_HAS_JUMP(i->i_opcode);
}

/* One arg*/
Expand Down

0 comments on commit 54aad59

Please sign in to comment.