Skip to content

GH-123044: Give the POP_TOP after a case test a location in the body, not the pattern. #130627

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 10 commits into from
Mar 10, 2025

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Feb 27, 2025

@markshannon markshannon changed the title GH-123044: GIve the POP_TOP after a case test a location in the body, not the pattern. GH-123044: Give the POP_TOP after a case test a location in the body, not the pattern. Feb 27, 2025
Python/codegen.c Outdated
ADDOP(c, LOC(m->pattern), POP_TOP);
/* Use the body location to give better locations for branch events */
assert(asdl_seq_LEN(m->body) > 0);
ADDOP(c, LOC(asdl_seq_GET(m->body, 0)), POP_TOP);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be the location of the last instruction in the block? Line tracing might look like we executed that line more than once if we "return" to it after running another line.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a NEXT_LOCATION pseudo location so that the POP_TOP gets the location of whatever follows it.

@@ -58,6 +58,7 @@ typedef struct {
.end_col_offset = (n)->end_col_offset }

static const _Py_SourceLocation NO_LOCATION = {-1, -1, -1, -1};
static const _Py_SourceLocation NEXT_LOCATION = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
Copy link
Member

@iritkatriel iritkatriel Mar 1, 2025

Choose a reason for hiding this comment

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

This is a good idea.

We probably need to change remove_redundant_nops to treat this as no location. Maybe it should be (-2, -2, -2, -2) and then just compare to 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are some checks for NO_LOCATION that compare to 0, but it isn't clear to me which should support NEXT_LOCATION and which shouldn't, so I'm a bit reluctant to do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

With {2, -2, -2, -2}, basicblock_remove_redundant_nops and propagate_line_numbers needed changing.
basicblock_remove_redundant_nops needs to treat NEXT_LOCATION like NO_LOCATION
propagate_line_numbers needs to treat NEXT_LOCATION not like NO_LOCATION

if (same_location(instr[-1].i_loc, NEXT_LOCATION)) {
instr[-1].i_loc = instr->i_loc;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this transformation should be in propagate_line_numbers in flowgraph.c. Otherwise the last instruction may remain without location, when it could have received a location from an earlier instruction in its block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wouldn't it be too early in propagate_line_numbers?
If the NEXT_LOCATION instruction is at the end of the block where would it get the location information from?

Copy link
Member

Choose a reason for hiding this comment

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

From the next block (fall through or jump). If there is more than one successor, then we have a problem anyway, right, so what do we do?

Copy link
Member Author

Choose a reason for hiding this comment

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

We should only use NEXT_LOCATION if instruction is not the last instruction in the block. I'll add an assert for that.

@markshannon markshannon requested a review from iritkatriel March 6, 2025 10:25
@markshannon
Copy link
Member Author

The fuzzer found an example where the case body consists solely of a local variable annotation:

    ...
    case _:
        l: int

Since local variable annotations are removed from the compiler, there is no following instruction in the case body.
In this, somewhat weird, case we have to accept that the location for the POP_TOP is going to be meaningless, and a terminator may end up with a NEXT_LOCATION. In that specific case, we have to just convert it to NO_LOCATION.

…tion to use. Assert NEXT_LOCATION never gets emitted
@markshannon markshannon closed this Mar 7, 2025
@markshannon markshannon reopened this Mar 7, 2025
@markshannon markshannon force-pushed the location-pop-top-after-case branch from 859aa5a to a077433 Compare March 10, 2025 14:02
@markshannon markshannon merged commit be046ee into python:main Mar 10, 2025
46 checks passed
seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants