Skip to content

Commit

Permalink
GH-109214: _SET_IP before _PUSH_FRAME (but not _POP_FRAME) (GH-111001)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandtbucher authored Oct 24, 2023
1 parent c0ea67d commit e5168ff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove unnecessary instruction pointer updates before returning from frames.
2 changes: 0 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,6 @@ dummy_func(
}

macro(RETURN_VALUE) =
_SAVE_CURRENT_IP + // Sets frame->prev_instr
_POP_FRAME;

inst(INSTRUMENTED_RETURN_VALUE, (retval --)) {
Expand All @@ -827,7 +826,6 @@ dummy_func(

macro(RETURN_CONST) =
LOAD_CONST +
_SAVE_CURRENT_IP + // Sets frame->prev_instr
_POP_FRAME;

inst(INSTRUMENTED_RETURN_CONST, (--)) {
Expand Down
51 changes: 19 additions & 32 deletions Python/generated_cases.c.h

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

14 changes: 6 additions & 8 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,9 @@ translate_bytecode_to_trace(
case OPARG_BOTTOM: // Second half of super-instr
oparg = orig_oparg & 0xF;
break;
case OPARG_SET_IP: // op==_SET_IP; oparg=next instr
case OPARG_SET_IP: // uop=_SET_IP; oparg=next_instr-1
// The number of caches is smuggled in via offset:
assert(offset == _PyOpcode_Caches[_PyOpcode_Deopt[opcode]]);
oparg = INSTR_IP(instr + offset, code);
uop = _SET_IP;
break;
Expand Down Expand Up @@ -850,11 +852,7 @@ remove_unneeded_uops(_PyUOpInstruction *trace, int trace_length)
bool need_ip = true;
for (int pc = 0; pc < trace_length; pc++) {
int opcode = trace[pc].opcode;
if (opcode == _SAVE_CURRENT_IP) {
// Special case: never remove preceding _SET_IP
last_set_ip = -1;
}
else if (opcode == _SET_IP) {
if (opcode == _SET_IP) {
if (!need_ip && last_set_ip >= 0) {
trace[last_set_ip].opcode = NOP;
}
Expand All @@ -866,8 +864,8 @@ remove_unneeded_uops(_PyUOpInstruction *trace, int trace_length)
break;
}
else {
// If opcode has ERROR or DEOPT, set need_up to true
if (_PyOpcode_opcode_metadata[opcode].flags & (HAS_ERROR_FLAG | HAS_DEOPT_FLAG)) {
// If opcode has ERROR or DEOPT, set need_ip to true
if (_PyOpcode_opcode_metadata[opcode].flags & (HAS_ERROR_FLAG | HAS_DEOPT_FLAG) || opcode == _PUSH_FRAME) {
need_ip = true;
}
}
Expand Down

0 comments on commit e5168ff

Please sign in to comment.