8000 gh-107557: Remove unnecessary SAVE_IP instructions by gvanrossum · Pull Request #108583 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-107557: Remove unnecessary SAVE_IP instructions #108583

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

Merged
merged 12 commits into from
Aug 29, 2023
Merged
Prev Previous commit
Next Next commit
Factor squeeze_stubs() out of translate_bytecode_to_trace()
  • Loading branch information
gvanrossum committed Aug 28, 2023
commit 51eaa2fb54c5ebfdd696fab9510d4ceca06945b4
62 changes: 40 additions & 22 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,32 @@ static PyTypeObject UOpExecutor_Type = {
.tp_as_sequence = &uop_as_sequence,
};

static int
squeeze_stubs(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
squeeze_stubs(
shift_stubs(

(the name implied to me that something happens to the size of the stubs).

Copy link
Member Author

Choose a reason for hiding this comment

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

How about move_stubs() (echoing that it uses memmove()).

_PyUOpInstruction *trace,
int trace_length,
int stubs_start,
int stubs_end
)
{
memmove(trace + trace_length,
trace + stubs_start,
(stubs_end - stubs_start) * sizeof(_PyUOpInstruction));
// Patch up the jump targets
for (int i = 0; i < trace_length; i++) {
if (trace[i].opcode == _POP_JUMP_IF_FALSE ||
trace[i].opcode == _POP_JUMP_IF_TRUE)
{
int target = trace[i].oparg;
if (target >= stubs_start) {
target += trace_length - stubs_start;
trace[i].oparg = target;
}
}
}
return trace_length + stubs_end - stubs_start;
}

#define TRACE_STACK_SIZE 5

static int
Expand Down Expand Up @@ -736,30 +762,22 @@ translate_bytecode_to_trace(
code->co_firstlineno,
2 * INSTR_IP(initial_instr, code),
trace_length);
if (max_length < buffer_size && trace_length < max_length) {
// Move the stubs back to be immediately after the main trace
// (which ends at trace_length)
DPRINTF(2,
"Moving %d stub uops back by %d\n",
buffer_size - max_length,
max_length - trace_length);
memmove(trace + trace_length,
trace + max_length,
(buffer_size - max_length) * sizeof(_PyUOpInstruction));
// Patch up the jump targets
for (int i = 0; i < trace_length; i++) {
if (trace[i].opcode == _POP_JUMP_IF_FALSE ||
trace[i].opcode == _POP_JUMP_IF_TRUE)
{
int target = trace[i].oparg;
if (target >= max_length) {
target += trace_length - max_length;
trace[i].oparg = target;
}
}
if (max_length < buffer_size) {
// There are stubs
if (trace_length < max_length) {
// There's a gap before the stubs
// Move the stubs back to be immediately after the main trace
// (which ends at trace_length)
DPRINTF(2,
"Moving %d stub uops back by %d\n",
buffer_size - max_length,
max_length - trace_length);
trace_length = squeeze_stubs(trace, trace_length, max_length, buffer_size);
}
else {
trace_length += buffer_size - max_length;
}
}
trace_length += buffer_size - max_length;
return trace_length;
}
else {
Expand Down
0