Skip to content
Merged
1 change: 1 addition & 0 deletions Include/internal/pycore_symtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


/* __future__ information */
typedef struct {
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,35 @@ async def foo():
('branch left', 'func', 12, 12)])


def test_match(self):

def func(v=1):
x = 0
for v in range(4):
match v:
case 1:
x += 1
case 2:
x += 2
case _:
x += 3
return x

self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
('branch left', 'func', 2, 2),
('branch right', 'func', 4, 6),
('branch right', 'func', 6, 8),
('branch left', 'func', 2, 2),
('branch left', 'func', 4, 5),
('branch left', 'func', 2, 2),
('branch right', 'func', 4, 6),
('branch left', 'func', 6, 7),
('branch left', 'func', 2, 2),
('branch right', 'func', 4, 6),
('branch right', 'func', 6, 8),
('branch right', 'func', 2, 10)])


class TestBranchConsistency(MonitoringTestBase, unittest.TestCase):

def check_branches(self, func, tool=TEST_TOOL, recorders=BRANCH_OFFSET_RECORDERS):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make sure that the location of branch targets in ``match`` cases is in the
body, not the pattern.
9 changes: 9 additions & 0 deletions Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ assemble_location_info(struct assembler *a, instr_sequence *instrs,
a->a_lineno = firstlineno;
location loc = NO_LOCATION;
int size = 0;
if (same_location(instrs->s_instrs[instrs->s_used-1].i_loc, NEXT_LOCATION)) {
instrs->s_instrs[instrs->s_used-1].i_loc = NO_LOCATION;
}
for (int i = instrs->s_used-1; i > 0; i--) {
instruction *instr = &instrs->s_instrs[i];
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.

for (int i = 0; i < instrs->s_used; i++) {
instruction *instr = &instrs->s_instrs[i];
if (!same_location(loc, instr->i_loc)) {
Expand Down
3 changes: 2 additions & 1 deletion Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -6121,7 +6121,8 @@ codegen_match_inner(compiler *c, stmt_ty s, pattern_context *pc)
}
// Success! Pop the subject off, we're done with it:
if (i != cases - has_default - 1) {
ADDOP(c, LOC(m->pattern), POP_TOP);
/* Use the next location to give better locations for branch events */
ADDOP(c, NEXT_LOCATION, POP_TOP);
}
VISIT_SEQ(c, stmt, m->body);
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
Expand Down
Loading