Skip to content
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

GH-109214: _SET_IP before _PUSH_FRAME (but not _POP_FRAME) #111001

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -679,7 +679,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]]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is always it, why do we need to smuggle it in via offset? Also it's beginning to look like the offset should just be made one larger in the code generator, so we can drop the ugly + 1 on line 654 above.

oparg = INSTR_IP(instr + offset, code);
uop = _SET_IP;
break;
Expand Down Expand Up @@ -827,11 +829,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 @@ -843,8 +841,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
Loading