Skip to content

Commit 281c045

Browse files
DinoVfacebook-github-bot
authored andcommitted
Use NEXT_LOCATION for popping subject case
Summary: There's a new position added which is `NEXT_LOCATION` - when this is used the instruction will later be populated with the following instructions location. Reviewed By: martindemello Differential Revision: D80820421 fbshipit-source-id: fff6775095bec4452bb225390d3a2ab545a4aecb
1 parent 6c0ea75 commit 281c045

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

cinderx/PythonLib/cinderx/compiler/pyassem.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def __repr__(self) -> str:
125125

126126

127127
NO_LOCATION = SrcLocation(-1, -1, -1, -1)
128+
NEXT_LOCATION = SrcLocation(-2, -2, -2, -2)
128129

129130

130131
class Instruction:
@@ -1080,7 +1081,7 @@ def propagate_line_numbers(self) -> None:
10801081
continue
10811082
prev_loc = NO_LOCATION
10821083
for instr in block.insts:
1083-
if instr.lineno < 0:
1084+
if instr.lineno == NO_LOCATION.lineno:
10841085
instr.loc = prev_loc
10851086
else:
10861087
prev_loc = instr.loc
@@ -1090,7 +1091,7 @@ def propagate_line_numbers(self) -> None:
10901091
if next.num_predecessors == 1:
10911092
assert next.insts
10921093
next_instr = next.insts[0]
1093-
if next_instr.lineno < 0:
1094+
if next_instr.lineno == -1:
10941095
next_instr.loc = prev_loc
10951096
last_instr = block.insts[-1]
10961097
if (
@@ -1103,7 +1104,7 @@ def propagate_line_numbers(self) -> None:
11031104
if target.num_predecessors == 1:
11041105
assert target.insts
11051106
next_instr = target.insts[0]
1106-
if next_instr.lineno < 0:
1107+
if next_instr.lineno == NO_LOCATION.lineno:
11071108
next_instr.loc = prev_loc
11081109

11091110
def guarantee_lineno_for_exits(self) -> None:
@@ -2061,6 +2062,16 @@ def make_line_table(self) -> bytes:
20612062

20622063
loc = NO_LOCATION
20632064
size = 0
2065+
prev = None
2066+
for t in reversed(self.insts):
2067+
if t.loc is NEXT_LOCATION:
2068+
if t.is_jump(self.opcode) or t.opname in SCOPE_EXIT_OPCODES:
2069+
t.loc = NO_LOCATION
2070+
else:
2071+
assert prev is not None
2072+
t.loc = prev.loc
2073+
prev = t
2074+
20642075
for t in self.insts:
20652076
if t.loc != loc:
20662077
lpostab.emit_location(loc, size)

cinderx/PythonLib/cinderx/compiler/pycodegen.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
FVC_STR,
5656
FVS_HAVE_SPEC,
5757
Instruction,
58+
NEXT_LOCATION,
5859
NO_LOCATION,
5960
PyFlowGraph,
6061
PyFlowGraph310,
@@ -1827,6 +1828,10 @@ def emit_match_jump_to_end(self, end: Block) -> None:
18271828
def emit_match_jump_to_fail_pop_unconditional(self, pc: PatternContext) -> None:
18281829
raise NotImplementedError()
18291830

1831+
def emit_pop_subject(self, case: ast.match_case) -> None:
1832+
self.set_pos(case.pattern)
1833+
self.emit("POP_TOP")
1834+
18301835
def visitMatch(self, node: ast.Match) -> None:
18311836
"""See compiler_match_inner in compile.c"""
18321837
pc = self.pattern_context()
@@ -1863,8 +1868,7 @@ def visitMatch(self, node: ast.Match) -> None:
18631868
self.compileJumpIf(guard, pc.fail_pop[0], False)
18641869
# Success! Pop the subject off, we're done with it:
18651870
if not is_last_non_default_case:
1866-
self.set_pos(case.pattern)
1867-
self.emit("POP_TOP")
1871+
self.emit_pop_subject(case)
18681872
self.visit_list(case.body)
18691873
self.emit_match_jump_to_end(end)
18701874
# If the pattern fails to match, we want the line number of the
@@ -6418,6 +6422,10 @@ def emit_init_class_attrs(
64186422
gen.emit("LOAD_CONST", first_lineno)
64196423
gen.storeName("__firstlineno__")
64206424

6425+
def emit_pop_subject(self, case: ast.match_case) -> None:
6426+
# Use the next location to give better locations for branch events
6427+
self.graph.emit_with_loc("POP_TOP", 0, NEXT_LOCATION)
6428+
64216429
def maybe_add_static_attribute_to_class(self, node: ast.Attribute) -> None:
64226430
attr_value = node.value
64236431
if (

0 commit comments

Comments
 (0)