Skip to content

GH-112354: _GUARD_IS_TRUE_POP side-exits to target the next instruction, not themselves.#114078

Merged
markshannon merged 3 commits into
python:mainfrom
faster-cpython:exit-to-target
Jan 15, 2024
Merged

GH-112354: _GUARD_IS_TRUE_POP side-exits to target the next instruction, not themselves.#114078
markshannon merged 3 commits into
python:mainfrom
faster-cpython:exit-to-target

Conversation

@markshannon

@markshannon markshannon commented Jan 15, 2024

Copy link
Copy Markdown
Member

In tier 2, JUMP_IF_TRUE and friends are converted to guards. Currently if they fail, they resume execution in tier 1 at the same instruction. Apart from being inefficient, this doesn't guarantee forward progress if the jump is the first instruction, which will happen when we optimize side exits.

This PR changes the target of _GUARD_IS_TRUE_POP, etc, to be the successor instruction, not the original JUMP_IF_TRUE/FALSE.

Comment thread Python/optimizer.c
ADD_TO_TRACE(uopcode, max_length, 0, INSTR_IP(next_instr, code));
goto top;
}
ADD_TO_TRACE(uopcode, max_length, 0, INSTR_IP(target_instr, code));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't follow - why is the target different depending on whether jump_likely or not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because if we expect to jump then the next instruction in the trace is the target, so if the guard fails we go to the following instruction.
Conversely, if we expect not to jump then the next instruction in the trace is the next instruction, so if the guard fails we go to the target.

    POP_JUMP_IF_TRUE L2
L1 ...

Becomes:

If we expect TOS to be True:

_GUARD_IS_TRUE_POP L1
trace continues from L2

If we expect TOS to be False:

_GUARD_IS_FALSE_POP L2
trace continues from L1

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PS. There's still an unused STORE_SP() definition in ceval_macros.h.

kulikjak pushed a commit to kulikjak/cpython that referenced this pull request Jan 22, 2024
@markshannon
markshannon deleted the exit-to-target branch February 1, 2024 19:50
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants