Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,15 @@ def visit_mapping_pattern(self, o: MappingPattern) -> None:
super().visit_mapping_pattern(o)


def accept_loop_state(
def accept_unaliased(
builder: IRBuilder, expr: Expression, reassigned: AbstractSet[SymbolNode]
) -> Value:
"""Evaluate the object to iterate over, or the end of a range.
"""Evaluate an expression whose value is read again later.

Specialized loops read this value again on every iteration. If it's the register
of a local variable that the loop may assign to, copy it to a new register, since
the assignment must not affect the iteration (as in "for x in a: a = []").
If the value is the register of a local variable in "reassigned", copy it to a new
register, so that assigning to the variable doesn't change the value read later.
Examples are the iterable of a specialized loop ("for x in a: a = []") and the
subject of a match statement ("match x: case [x, y]: ...").
"""
value = builder.accept(expr)
if isinstance(value, Register):
Expand Down Expand Up @@ -814,7 +815,7 @@ def make_for_loop_generator(
If "nested" is True, this is a nested iterator such as "e" in "enumerate(e)".

"reassigned" contains the local variables that the loop may assign to
(see accept_loop_state).
(see accept_unaliased).
"""

# Do an async loop if needed. async is always generic
Expand All @@ -829,7 +830,7 @@ def make_for_loop_generator(
rtyp = builder.node_type(expr)
if is_indexed_sequence_rprimitive(rtyp) or isinstance(rtyp, RVec):
# Special case "for x in <seq>" for concrete sequence types.
expr_reg = accept_loop_state(builder, expr, reassigned)
expr_reg = accept_unaliased(builder, expr, reassigned)
target_type = builder.get_sequence_type(expr)

for_list = ForSequence(builder, index, body_block, loop_exit, line, nested)
Expand All @@ -838,7 +839,7 @@ def make_for_loop_generator(

if is_dict_rprimitive(rtyp):
# Special case "for k in <dict>".
expr_reg = accept_loop_state(builder, expr, reassigned)
expr_reg = accept_unaliased(builder, expr, reassigned)
target_type = builder.get_dict_key_type(expr)

for_dict = ForDictionaryKeys(builder, index, body_block, loop_exit, line, nested)
Expand All @@ -860,10 +861,10 @@ def make_for_loop_generator(
# direction of comparison to do.
if len(expr.args) == 1:
start_reg: Value = Integer(0)
end_reg = accept_loop_state(builder, expr.args[0], reassigned)
end_reg = accept_unaliased(builder, expr.args[0], reassigned)
else:
start_reg = builder.accept(expr.args[0])
end_reg = accept_loop_state(builder, expr.args[1], reassigned)
end_reg = accept_unaliased(builder, expr.args[1], reassigned)
if len(expr.args) == 3:
step = builder.extract_int(expr.args[2])
assert step is not None
Expand Down Expand Up @@ -909,7 +910,7 @@ def make_for_loop_generator(
and is_indexed_sequence_rprimitive(builder.node_type(expr.args[0]))
):
# Special case "for x in reversed(<list>)".
expr_reg = accept_loop_state(builder, expr.args[0], reassigned)
expr_reg = accept_unaliased(builder, expr.args[0], reassigned)
target_type = builder.get_sequence_type(expr)

for_list = ForSequence(builder, index, body_block, loop_exit, line, nested)
Expand All @@ -919,7 +920,7 @@ def make_for_loop_generator(
# Special cases for dictionary iterator methods, like dict.items().
rtype = builder.node_type(expr.callee.expr)
if is_dict_rprimitive(rtype) and expr.callee.name in ("keys", "values", "items"):
expr_reg = accept_loop_state(builder, expr.callee.expr, reassigned)
expr_reg = accept_unaliased(builder, expr.callee.expr, reassigned)
for_dict_type: type[ForGenerator] | None = None
if expr.callee.name == "keys":
target_type = builder.get_dict_key_type(expr.callee.expr)
Expand Down
42 changes: 13 additions & 29 deletions mypyc/irbuild/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from mypyc.ir.ops import BasicBlock, Value
from mypyc.ir.rtypes import object_rprimitive
from mypyc.irbuild.builder import IRBuilder
from mypyc.irbuild.for_helpers import AssignmentCollector, accept_unaliased
from mypyc.primitives.dict_ops import (
dict_copy,
dict_del_item,
Expand Down Expand Up @@ -58,8 +59,6 @@ class MatchVisitor(TraverserVisitor):
subject: Value
match: MatchStmt

as_pattern: AsPattern | None = None

def __init__(self, builder: IRBuilder, match_node: MatchStmt) -> None:
self.builder = builder

Expand All @@ -68,7 +67,13 @@ def __init__(self, builder: IRBuilder, match_node: MatchStmt) -> None:
self.final_block = BasicBlock()

self.match = match_node
self.subject = builder.accept(match_node.subject)

assignments = AssignmentCollector()
for pattern, guard in zip(match_node.patterns, match_node.guards):
pattern.accept(assignments)
if guard is not None:
guard.accept(assignments)
self.subject = accept_unaliased(builder, match_node.subject, assignments.symbols)

def build_match_body(self, index: int) -> None:
self.builder.activate_block(self.code_block)
Expand Down Expand Up @@ -103,8 +108,6 @@ def visit_value_pattern(self, pattern: ValuePattern) -> None:

cond = self.builder.binary_op(self.subject, value, "==", pattern.expr.line)

self.bind_as_pattern(value)

self.builder.add_bool_branch(cond, self.code_block, self.next_block)

def visit_or_pattern(self, pattern: OrPattern) -> None:
Expand All @@ -115,11 +118,7 @@ def visit_or_pattern(self, pattern: OrPattern) -> None:
self.code_block = BasicBlock()
self.next_block = BasicBlock()

# Hack to ensure the as pattern is bound to each pattern in the
# "or" pattern, but not every subpattern
backup = self.as_pattern
p.accept(self)
self.as_pattern = backup

self.builder.activate_block(self.code_block)
self.builder.goto(code_block)
Expand All @@ -144,8 +143,6 @@ def visit_class_pattern(self, pattern: ClassPattern) -> None:

self.builder.add_bool_branch(cond, self.code_block, self.next_block)

self.bind_as_pattern(self.subject, new_block=True)

if pattern.positionals:
if pattern.class_ref.fullname in MATCHABLE_BUILTINS:
self.builder.activate_block(self.code_block)
Expand Down Expand Up @@ -182,12 +179,13 @@ def visit_class_pattern(self, pattern: ClassPattern) -> None:

def visit_as_pattern(self, pattern: AsPattern) -> None:
if pattern.pattern:
old_pattern = self.as_pattern
self.as_pattern = pattern
pattern.pattern.accept(self)
self.as_pattern = old_pattern

elif pattern.name:
# Bind the name to the subject only once the subpattern has matched.
self.builder.activate_block(self.code_block)
self.code_block = BasicBlock()

if pattern.name:
target = self.builder.get_assignment_target(pattern.name)

self.builder.assign(target, self.subject, pattern.line)
Expand Down Expand Up @@ -306,20 +304,6 @@ def visit_sequence_pattern(self, seq_pattern: SequencePattern) -> None:

self.builder.goto(self.code_block)

def bind_as_pattern(self, value: Value, new_block: bool = False) -> None:
if self.as_pattern and self.as_pattern.pattern and self.as_pattern.name:
if new_block:
self.builder.activate_block(self.code_block)
self.code_block = BasicBlock()

target = self.builder.get_assignment_target(self.as_pattern.name)
self.builder.assign(target, value, self.as_pattern.pattern.line)

self.as_pattern = None

if new_block:
self.builder.goto(self.code_block)

@contextmanager
def enter_subpattern(self, subject: Value) -> Generator[None]:
old_subject = self.subject
Expand Down
116 changes: 57 additions & 59 deletions mypyc/test-data/irbuild-match.test
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,21 @@ def f():
r7, r8 :: object
L0:
r0 = int_eq 246, 246
if r0 goto L1 else goto L3 :: bool
L1:
r1 = object 123
x = r1
if r0 goto L1 else goto L2 :: bool
L1:
L2:
r2 = builtins :: module
r3 = 'print'
r4 = CPyObject_GetAttr(r2, r3)
r5 = [x]
r6 = load_address r5
r7 = PyObject_Vectorcall(r4, r6, 1, 0)
keep_alive x
goto L3
L2:
goto L4
L3:
L4:
r8 = box(None, 1)
return r8

Expand All @@ -539,44 +540,41 @@ def f():
print(x)
[out]
def f():
r0 :: bit
r1, x :: object
r2 :: bit
r3, r4 :: object
r5 :: str
r6 :: object
r7 :: object[1]
r8 :: object_ptr
r9, r10 :: object
r0, r1 :: bit
r2, x, r3 :: object
r4 :: str
r5 :: object
r6 :: object[1]
r7 :: object_ptr
r8, r9 :: object
L0:
r0 = int_eq 2, 2
r1 = object 1
x = r1
if r0 goto L1 else goto L2 :: bool
L1:
goto L5
L2:
r2 = int_eq 2, 4
r3 = object 2
x = r3
if r2 goto L3 else goto L4 :: bool
r1 = int_eq 2, 4
if r1 goto L3 else goto L4 :: bool
L3:
goto L5
L4:
goto L6
L5:
r4 = builtins :: module
r5 = 'print'
r6 = CPyObject_GetAttr(r4, r5)
r7 = [x]
r8 = load_address r7
r9 = PyObject_Vectorcall(r6, r8, 1, 0)
keep_alive x
goto L7
L5:
r2 = object 1
x = r2
L6:
r3 = builtins :: module
r4 = 'print'
r5 = CPyObject_GetAttr(r3, r4)
r6 = [x]
r7 = load_address r6
r8 = PyObject_Vectorcall(r5, r7, 1, 0)
keep_alive x
goto L8
L7:
r10 = box(None, 1)
return r10
L8:
r9 = box(None, 1)
return r9

[case testMatchAsPatternOnClassPattern_python3_10]
def f():
Expand Down Expand Up @@ -897,17 +895,17 @@ def f(x):
r1 :: i32
r2 :: bit
r3 :: bool
r4, y :: __main__.C
r5 :: str
r6, r7, r8 :: object
r9 :: i32
r10 :: bit
r11 :: bool
r12 :: str
r13, r14, r15 :: object
r16 :: i32
r17 :: bit
r18 :: bool
r4 :: str
r5, r6, r7 :: object
r8 :: i32
r9 :: bit
r10 :: bool
r11 :: str
r12, r13, r14 :: object
r15 :: i32
r16 :: bit
r17 :: bool
r18, y :: __main__.C
r19 :: str
r20 :: object
r21 :: str
Expand All @@ -922,26 +920,26 @@ L0:
r3 = truncate r1: i32 to builtins.bool
if r3 goto L1 else goto L5 :: bool
L1:
r4 = cast(__main__.C, x)
y = r4
r4 = 'a'
r5 = CPyObject_GetAttr(x, r4)
r6 = object 1
r7 = PyObject_RichCompare(r5, r6, 2)
r8 = PyObject_IsTrue(r7)
r9 = r8 >= 0 :: signed
r10 = truncate r8: i32 to builtins.bool
if r10 goto L2 else goto L5 :: bool
L2:
r5 = 'a'
r6 = CPyObject_GetAttr(x, r5)
r7 = object 1
r8 = PyObject_RichCompare(r6, r7, 2)
r9 = PyObject_IsTrue(r8)
r10 = r9 >= 0 :: signed
r11 = truncate r9: i32 to builtins.bool
if r11 goto L3 else goto L5 :: bool
r11 = 'b'
r12 = CPyObject_GetAttr(x, r11)
r13 = object 2
r14 = PyObject_RichCompare(r12, r13, 2)
r15 = PyObject_IsTrue(r14)
r16 = r15 >= 0 :: signed
r17 = truncate r15: i32 to builtins.bool
if r17 goto L3 else goto L5 :: bool
L3:
r12 = 'b'
r13 = CPyObject_GetAttr(x, r12)
r14 = object 2
r15 = PyObject_RichCompare(r13, r14, 2)
r16 = PyObject_IsTrue(r15)
r17 = r16 >= 0 :: signed
r18 = truncate r16: i32 to builtins.bool
if r18 goto L4 else goto L5 :: bool
r18 = cast(__main__.C, x)
y = r18
L4:
r19 = 'matched'
r20 = builtins :: module
Expand Down
Loading
Loading