FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-123044: Give the `POP_TOP` after a case test a location in the body, not the pattern. by markshannon · Pull Request #130627 · python/cpython · GitHub

Repository navigation

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

Merged
markshannon merged 10 commits into
python:mainfrom
faster-cpython:location-pop-top-after-case
Mar 10, 2025
Merged

markshannon merged 10 commits into
python:mainfrom
faster-cpython:location-pop-top-after-case

Conversation

markshannon commented Feb 27, 2025 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

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
Comment thread 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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Include/internal/pycore_symtable.h Outdated
.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};

iritkatriel Mar 1, 2025 •
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Python/assemble.c
if (same_location(instr[-1].i_loc, NEXT_LOCATION)) {
instr[-1].i_loc = instr->i_loc;
}
}

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

markshannon requested a review from iritkatriel March 6, 2025 10:25

Copy link
Copy Markdown
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 closed this Mar 7, 2025
markshannon reopened this Mar 7, 2025
markshannon force-pushed the location-pop-top-after-case branch from 859aa5a to a077433 Compare March 10, 2025 14:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL