diff --git a/mypyc/analysis/dataflow.py b/mypyc/analysis/dataflow.py index 0657261e7a8f..db62ef1700fa 100644 --- a/mypyc/analysis/dataflow.py +++ b/mypyc/analysis/dataflow.py @@ -17,6 +17,7 @@ Cast, ComparisonOp, ControlOp, + DecRef, Extend, Float, FloatComparisonOp, @@ -25,6 +26,7 @@ GetAttr, GetElementPtr, Goto, + IncRef, InitStatic, Integer, IntOp, @@ -77,12 +79,11 @@ def __str__(self) -> str: return f"exits: {exits}\nsucc: {self.succ}\npred: {self.pred}" -def get_cfg(blocks: list[BasicBlock]) -> CFG: +def get_cfg(blocks: list[BasicBlock], *, use_yields: bool = False) -> CFG: """Calculate basic block control-flow graph. - The result is a dictionary like this: - - basic block index -> (successors blocks, predecesssor blocks) + If use_yields is set, then we treat returns inserted by yields as gotos + instead of exits. """ succ_map = {} pred_map: dict[BasicBlock, list[BasicBlock]] = {} @@ -92,7 +93,10 @@ def get_cfg(blocks: list[BasicBlock]) -> CFG: isinstance(op, ControlOp) for op in block.ops[:-1] ), "Control-flow ops must be at the end of blocks" - succ = list(block.terminator.targets()) + if use_yields and isinstance(block.terminator, Return) and block.terminator.yield_target: + succ = [block.terminator.yield_target] + else: + succ = list(block.terminator.targets()) if not succ: exits.add(block) @@ -474,6 +478,12 @@ def visit_assign_multi(self, op: AssignMulti) -> GenAndKill[Value]: def visit_set_mem(self, op: SetMem) -> GenAndKill[Value]: return non_trivial_sources(op), set() + def visit_inc_ref(self, op: IncRef) -> GenAndKill[Value]: + return set(), set() + + def visit_dec_ref(self, op: DecRef) -> GenAndKill[Value]: + return set(), set() + def analyze_live_regs(blocks: list[BasicBlock], cfg: CFG) -> AnalysisResult[Value]: """Calculate live registers at each CFG location. diff --git a/mypyc/codegen/emitmodule.py b/mypyc/codegen/emitmodule.py index 713fa5c51fa1..b8a19ac1d669 100644 --- a/mypyc/codegen/emitmodule.py +++ b/mypyc/codegen/emitmodule.py @@ -61,6 +61,7 @@ from mypyc.transform.flag_elimination import do_flag_elimination from mypyc.transform.lower import lower_ir from mypyc.transform.refcount import insert_ref_count_opcodes +from mypyc.transform.spill import insert_spills from mypyc.transform.uninit import insert_uninit_checks # All of the modules being compiled are divided into "groups". A group @@ -228,6 +229,12 @@ def compile_scc_to_ir( if errors.num_errors > 0: return modules + env_user_functions = {} + for module in modules.values(): + for cls in module.classes: + if cls.env_user_function: + env_user_functions[cls.env_user_function] = cls + for module in modules.values(): for fn in module.functions: # Insert uninit checks. @@ -236,6 +243,10 @@ def compile_scc_to_ir( insert_exception_handling(fn) # Insert refcount handling. insert_ref_count_opcodes(fn) + + if fn in env_user_functions: + insert_spills(fn, env_user_functions[fn]) + # Switch to lower abstraction level IR. lower_ir(fn, compiler_options) # Perform optimizations. diff --git a/mypyc/ir/class_ir.py b/mypyc/ir/class_ir.py index 94181e115145..d18f15f667c8 100644 --- a/mypyc/ir/class_ir.py +++ b/mypyc/ir/class_ir.py @@ -196,6 +196,9 @@ def __init__( # value of an attribute is the same as the error value. self.bitmap_attrs: list[str] = [] + # If this is a generator environment class, what is the actual method for it + self.env_user_function: FuncIR | None = None + def __repr__(self) -> str: return ( "ClassIR(" @@ -394,6 +397,7 @@ def serialize(self) -> JsonDict: "_always_initialized_attrs": sorted(self._always_initialized_attrs), "_sometimes_initialized_attrs": sorted(self._sometimes_initialized_attrs), "init_self_leak": self.init_self_leak, + "env_user_function": self.env_user_function.id if self.env_user_function else None, } @classmethod @@ -446,6 +450,9 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> ClassIR: ir._always_initialized_attrs = set(data["_always_initialized_attrs"]) ir._sometimes_initialized_attrs = set(data["_sometimes_initialized_attrs"]) ir.init_self_leak = data["init_self_leak"] + ir.env_user_function = ( + ctx.functions[data["env_user_function"]] if data["env_user_function"] else None + ) return ir diff --git a/mypyc/ir/ops.py b/mypyc/ir/ops.py index 0323d31d0605..eec9c34a965e 100644 --- a/mypyc/ir/ops.py +++ b/mypyc/ir/ops.py @@ -236,6 +236,10 @@ def can_raise(self) -> bool: def sources(self) -> list[Value]: """All the values the op may read.""" + @abstractmethod + def set_sources(self, new: list[Value]) -> None: + """Rewrite the sources of an op""" + def stolen(self) -> list[Value]: """Return arguments that have a reference count stolen by this op""" return [] @@ -272,6 +276,9 @@ def __init__(self, dest: Register, src: Value, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def stolen(self) -> list[Value]: return [self.src] @@ -302,6 +309,9 @@ def __init__(self, dest: Register, src: list[Value], line: int = -1) -> None: def sources(self) -> list[Value]: return self.src.copy() + def set_sources(self, new: list[Value]) -> None: + self.src = new[:] + def stolen(self) -> list[Value]: return [] @@ -343,6 +353,9 @@ def __repr__(self) -> str: def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_goto(self) @@ -403,6 +416,9 @@ def set_target(self, i: int, new: BasicBlock) -> None: def sources(self) -> list[Value]: return [self.value] + def set_sources(self, new: list[Value]) -> None: + (self.value,) = new + def invert(self) -> None: self.negated = not self.negated @@ -415,13 +431,23 @@ class Return(ControlOp): error_kind = ERR_NEVER - def __init__(self, value: Value, line: int = -1) -> None: + def __init__( + self, value: Value, line: int = -1, *, yield_target: BasicBlock | None = None + ) -> None: super().__init__(line) self.value = value + # If this return is created by a yield, keep track of the next + # basic block. This doesn't affect the code we generate but + # can feed into analysis that need to understand the + # *original* CFG. + self.yield_target = yield_target def sources(self) -> list[Value]: return [self.value] + def set_sources(self, new: list[Value]) -> None: + (self.value,) = new + def stolen(self) -> list[Value]: return [self.value] @@ -453,6 +479,9 @@ def __init__(self, line: int = -1) -> None: def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_unreachable(self) @@ -495,6 +524,9 @@ def __init__(self, src: Value, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_inc_ref(self) @@ -520,6 +552,9 @@ def __repr__(self) -> str: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_dec_ref(self) @@ -545,6 +580,9 @@ def __init__(self, fn: FuncDecl, args: Sequence[Value], line: int) -> None: def sources(self) -> list[Value]: return list(self.args.copy()) + def set_sources(self, new: list[Value]) -> None: + self.args = new[:] + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_call(self) @@ -573,6 +611,9 @@ def __init__(self, obj: Value, method: str, args: list[Value], line: int = -1) - def sources(self) -> list[Value]: return self.args.copy() + [self.obj] + def set_sources(self, new: list[Value]) -> None: + *self.args, self.obj = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_method_call(self) @@ -651,6 +692,9 @@ def __init__(self, args: list[Value], desc: PrimitiveDescription, line: int = -1 def sources(self) -> list[Value]: return self.args + def set_sources(self, new: list[Value]) -> None: + self.args = new[:] + def stolen(self) -> list[Value]: steals = self.desc.steals if isinstance(steals, list): @@ -686,6 +730,9 @@ def __init__( def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_load_error_value(self) @@ -718,6 +765,9 @@ def __init__(self, value: LiteralValue, rtype: RType) -> None: def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_load_literal(self) @@ -742,6 +792,9 @@ def __init__(self, obj: Value, attr: str, line: int, *, borrow: bool = False) -> def sources(self) -> list[Value]: return [self.obj] + def set_sources(self, new: list[Value]) -> None: + (self.obj,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_get_attr(self) @@ -774,6 +827,9 @@ def mark_as_initializer(self) -> None: def sources(self) -> list[Value]: return [self.obj, self.src] + def set_sources(self, new: list[Value]) -> None: + self.obj, self.src = new + def stolen(self) -> list[Value]: return [self.src] @@ -827,6 +883,9 @@ def __init__( def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_load_static(self) @@ -856,6 +915,9 @@ def __init__( def sources(self) -> list[Value]: return [self.value] + def set_sources(self, new: list[Value]) -> None: + (self.value,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_init_static(self) @@ -885,6 +947,9 @@ def sources(self) -> list[Value]: def stolen(self) -> list[Value]: return self.items.copy() + def set_sources(self, new: list[Value]) -> None: + self.items = new[:] + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_tuple_set(self) @@ -906,6 +971,9 @@ def __init__(self, src: Value, index: int, line: int = -1, *, borrow: bool = Fal def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_tuple_get(self) @@ -929,6 +997,9 @@ def __init__(self, src: Value, typ: RType, line: int, *, borrow: bool = False) - def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def stolen(self) -> list[Value]: if self.is_borrowed: return [] @@ -962,6 +1033,9 @@ def __init__(self, src: Value, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def stolen(self) -> list[Value]: return [self.src] @@ -988,6 +1062,9 @@ def __init__(self, src: Value, typ: RType, line: int) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_unbox(self) @@ -1020,6 +1097,9 @@ def __init__(self, class_name: str, value: str | Value | None, line: int) -> Non def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_raise_standard_error(self) @@ -1066,7 +1146,10 @@ def __init__( assert error_kind == ERR_NEVER def sources(self) -> list[Value]: - return self.args + return self.args[:] + + def set_sources(self, new: list[Value]) -> None: + self.args = new[:] def stolen(self) -> list[Value]: if isinstance(self.steals, list): @@ -1099,6 +1182,9 @@ def __init__(self, src: Value, dst_type: RType, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def stolen(self) -> list[Value]: return [] @@ -1130,6 +1216,9 @@ def __init__(self, src: Value, dst_type: RType, signed: bool, line: int = -1) -> def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def stolen(self) -> list[Value]: return [] @@ -1157,6 +1246,9 @@ def __init__(self, type: RType, identifier: str, line: int = -1, ann: object = N def sources(self) -> list[Value]: return [] + def set_sources(self, new: list[Value]) -> None: + assert not new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_load_global(self) @@ -1213,6 +1305,9 @@ def __init__(self, type: RType, lhs: Value, rhs: Value, op: int, line: int = -1) def sources(self) -> list[Value]: return [self.lhs, self.rhs] + def set_sources(self, new: list[Value]) -> None: + self.lhs, self.rhs = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_int_op(self) @@ -1276,6 +1371,9 @@ def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.lhs, self.rhs] + def set_sources(self, new: list[Value]) -> None: + self.lhs, self.rhs = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_comparison_op(self) @@ -1309,6 +1407,9 @@ def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.lhs, self.rhs] + def set_sources(self, new: list[Value]) -> None: + (self.lhs, self.rhs) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_float_op(self) @@ -1331,6 +1432,9 @@ def __init__(self, src: Value, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_float_neg(self) @@ -1359,6 +1463,9 @@ def __init__(self, lhs: Value, rhs: Value, op: int, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.lhs, self.rhs] + def set_sources(self, new: list[Value]) -> None: + (self.lhs, self.rhs) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_float_comparison_op(self) @@ -1390,6 +1497,9 @@ def __init__(self, type: RType, src: Value, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_load_mem(self) @@ -1415,6 +1525,9 @@ def __init__(self, type: RType, dest: Value, src: Value, line: int = -1) -> None def sources(self) -> list[Value]: return [self.src, self.dest] + def set_sources(self, new: list[Value]) -> None: + self.src, self.dest = new + def stolen(self) -> list[Value]: return [self.src] @@ -1441,6 +1554,9 @@ def __init__(self, src: Value, src_type: RType, field: str, line: int = -1) -> N def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_get_element_ptr(self) @@ -1469,6 +1585,12 @@ def sources(self) -> list[Value]: else: return [] + def set_sources(self, new: list[Value]) -> None: + if new: + assert isinstance(new[0], Register) + assert len(new) == 1 + self.src = new[0] + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_load_address(self) @@ -1513,6 +1635,9 @@ def stolen(self) -> list[Value]: return self.src.copy() return [] + def set_sources(self, new: list[Value]) -> None: + self.src = new[:] + def accept(self, visitor: OpVisitor[T]) -> T: return visitor.visit_keep_alive(self) @@ -1553,6 +1678,9 @@ def __init__(self, src: Value, line: int = -1) -> None: def sources(self) -> list[Value]: return [self.src] + def set_sources(self, new: list[Value]) -> None: + (self.src,) = new + def stolen(self) -> list[Value]: return [] diff --git a/mypyc/irbuild/function.py b/mypyc/irbuild/function.py index dd996985e43d..b5902892758e 100644 --- a/mypyc/irbuild/function.py +++ b/mypyc/irbuild/function.py @@ -270,6 +270,7 @@ def c() -> None: # Re-enter the FuncItem and visit the body of the function this time. builder.enter(fn_info) setup_env_for_generator_class(builder) + load_outer_envs(builder, builder.fn_info.generator_class) top_level = builder.top_level_fn_info() if ( diff --git a/mypyc/irbuild/generator.py b/mypyc/irbuild/generator.py index 92f9abff467c..bc61c4493d55 100644 --- a/mypyc/irbuild/generator.py +++ b/mypyc/irbuild/generator.py @@ -181,6 +181,8 @@ def add_helper_to_generator_class( ) fn_info.generator_class.ir.methods["__mypyc_generator_helper__"] = helper_fn_ir builder.functions.append(helper_fn_ir) + fn_info.env_class.env_user_function = helper_fn_ir + return helper_fn_decl diff --git a/mypyc/irbuild/statement.py b/mypyc/irbuild/statement.py index f5b65bedbbca..b109d925558b 100644 --- a/mypyc/irbuild/statement.py +++ b/mypyc/irbuild/statement.py @@ -905,7 +905,7 @@ def emit_yield(builder: IRBuilder, val: Value, line: int) -> Value: next_label = len(cls.continuation_blocks) cls.continuation_blocks.append(next_block) builder.assign(cls.next_label_target, Integer(next_label), line) - builder.add(Return(retval)) + builder.add(Return(retval, yield_target=next_block)) builder.activate_block(next_block) add_raise_exception_blocks_to_generator_class(builder, line) diff --git a/mypyc/test-data/run-async.test b/mypyc/test-data/run-async.test index 8488632e6574..89d661900de0 100644 --- a/mypyc/test-data/run-async.test +++ b/mypyc/test-data/run-async.test @@ -1,6 +1,6 @@ # async test cases (compile and run) -[case testAsync] +[case testRunAsyncBasics] import asyncio async def h() -> int: @@ -11,19 +11,110 @@ async def g() -> int: return await h() async def f() -> int: - return await g() + return await g() + 2 + +async def f2() -> int: + x = 0 + for i in range(2): + x += i + await f() + await g() + return x + +def test_1() -> None: + result = asyncio.run(f()) + assert result == 3 + +def test_2() -> None: + result = asyncio.run(f2()) + assert result == 9 [file asyncio/__init__.pyi] async def sleep(t: float) -> None: ... +# eh, we could use the real type but it doesn't seem important +def run(x: object) -> object: ... [typing fixtures/typing-full.pyi] -[file driver.py] -from native import f +[case testRunAsyncAwaitInVariousPositions] +from typing import cast, Any + import asyncio -result = asyncio.run(f()) -assert result == 1 +async def one() -> int: + await asyncio.sleep(0.0) + return int() + 1 + +async def true() -> bool: + return bool(int() + await one()) + +async def branch_await() -> int: + if bool(int() + 1) == await true(): + return 3 + return 2 + +async def branch_await_not() -> int: + if bool(int() + 1) == (not await true()): + return 3 + return 2 + +def test_branch() -> None: + assert asyncio.run(branch_await()) == 3 + assert asyncio.run(branch_await_not()) == 2 + +async def assign_multi() -> int: + _, x = int(), await one() + return x + 1 + +def test_assign_multi() -> None: + assert asyncio.run(assign_multi()) == 2 + +class C: + def __init__(self, s: str) -> None: + self.s = s + + def concat(self, s: str) -> str: + return self.s + s + +async def concat(s: str, t: str) -> str: + await one() + return s + t + +def concat2(x: str, y: str) -> str: + return x + y + +async def call1(s: str) -> str: + return concat2(str(int()), await concat(s, "a")) + +async def call2(s: str) -> str: + return await concat(str(int()), await concat(s, "b")) + +def test_call() -> None: + assert asyncio.run(call1("foo")) == "0fooa" + assert asyncio.run(call2("foo")) == "0foob" + +async def method_call(s: str) -> str: + return C("<").concat(await concat(s, ">")) + +def test_method_call() -> None: + assert asyncio.run(method_call("foo")) == "" + +class D: + def __init__(self, a: str, b: str) -> None: + self.a = a + self.b = b + +async def construct(s: str) -> str: + c = D(await concat(s, "!"), await concat(s, "?")) + return c.a + c.b + +def test_construct() -> None: + assert asyncio.run(construct("foo")) == "foo!foo?" + +[file asyncio/__init__.pyi] +async def sleep(t: float) -> None: ... +# eh, we could use the real type but it doesn't seem important +def run(x: object) -> object: ... + +[typing fixtures/typing-full.pyi] [case testAsyncWith] from testutil import async_val @@ -68,7 +159,6 @@ yields, val = run_generator(async_return()) assert yields == ('foo',) assert val == 'test', val - [case testAsyncFor] from typing import AsyncIterable, List, Set, Dict diff --git a/mypyc/test-data/run-generators.test b/mypyc/test-data/run-generators.test index 7e9804c49582..2e55ded76f74 100644 --- a/mypyc/test-data/run-generators.test +++ b/mypyc/test-data/run-generators.test @@ -680,3 +680,20 @@ def test_basic() -> None: with context: assert context.x == 1 assert context.x == 0 + + +[case testYieldSpill] +from typing import Generator +from testutil import run_generator + +def f() -> int: + return 1 + +def yield_spill() -> Generator[str, int, int]: + return f() + (yield "foo") + +def test_basic() -> None: + x = run_generator(yield_spill(), [2]) + yields, val = x + assert yields == ('foo',) + assert val == 3, val diff --git a/mypyc/transform/spill.py b/mypyc/transform/spill.py new file mode 100644 index 000000000000..331f1d3c1536 --- /dev/null +++ b/mypyc/transform/spill.py @@ -0,0 +1,102 @@ +"""Insert spills for values that are live across yields.""" + +from __future__ import annotations + +from mypyc.analysis.dataflow import AnalysisResult, analyze_live_regs, get_cfg +from mypyc.common import TEMP_ATTR_NAME +from mypyc.ir.class_ir import ClassIR +from mypyc.ir.func_ir import FuncIR +from mypyc.ir.ops import ( + BasicBlock, + Branch, + DecRef, + GetAttr, + IncRef, + LoadErrorValue, + Register, + SetAttr, + Value, +) + + +def insert_spills(ir: FuncIR, env: ClassIR) -> None: + cfg = get_cfg(ir.blocks, use_yields=True) + live = analyze_live_regs(ir.blocks, cfg) + entry_live = live.before[ir.blocks[0], 0] + + entry_live = {op for op in entry_live if not (isinstance(op, Register) and op.is_arg)} + # TODO: Actually for now, no Registers at all -- we keep the manual spills + entry_live = {op for op in entry_live if not isinstance(op, Register)} + + ir.blocks = spill_regs(ir.blocks, env, entry_live, live) + + +def spill_regs( + blocks: list[BasicBlock], env: ClassIR, to_spill: set[Value], live: AnalysisResult[Value] +) -> list[BasicBlock]: + for op in blocks[0].ops: + if isinstance(op, GetAttr) and op.attr == "__mypyc_env__": + env_reg = op + break + else: + raise AssertionError("could not find __mypyc_env__") + + spill_locs = {} + for i, val in enumerate(to_spill): + name = f"{TEMP_ATTR_NAME}2_{i}" + env.attributes[name] = val.type + spill_locs[val] = name + + for block in blocks: + ops = block.ops + block.ops = [] + + for i, op in enumerate(ops): + to_decref = [] + + if isinstance(op, IncRef) and op.src in spill_locs: + raise AssertionError("not sure what to do with an incref of a spill...") + if isinstance(op, DecRef) and op.src in spill_locs: + # When we decref a spilled value, we turn that into + # NULLing out the attribute, but only if the spilled + # value is not live *when we include yields in the + # CFG*. (The original decrefs are computed without that.) + # + # We also skip a decref is the env register is not + # live. That should only happen when an exception is + # being raised, so everything should be handled there. + if op.src not in live.after[block, i] and env_reg in live.after[block, i]: + # Skip the DecRef but null out the spilled location + null = LoadErrorValue(op.src.type) + block.ops.extend([null, SetAttr(env_reg, spill_locs[op.src], null, op.line)]) + continue + + if ( + any(src in spill_locs for src in op.sources()) + # N.B: IS_ERROR should be before a spill happens + # XXX: but could we have a regular branch? + and not (isinstance(op, Branch) and op.op == Branch.IS_ERROR) + ): + new_sources: list[Value] = [] + for src in op.sources(): + if src in spill_locs: + read = GetAttr(env_reg, spill_locs[src], op.line) + block.ops.append(read) + new_sources.append(read) + if src.type.is_refcounted: + to_decref.append(read) + else: + new_sources.append(src) + + op.set_sources(new_sources) + + block.ops.append(op) + + for dec in to_decref: + block.ops.append(DecRef(dec)) + + if op in spill_locs: + # XXX: could we set uninit? + block.ops.append(SetAttr(env_reg, spill_locs[op], op, op.line)) + + return blocks diff --git a/mypyc/transform/uninit.py b/mypyc/transform/uninit.py index 6bf71ac4a8bc..45b403588f8e 100644 --- a/mypyc/transform/uninit.py +++ b/mypyc/transform/uninit.py @@ -69,14 +69,19 @@ def split_blocks_at_uninits( and not (isinstance(op, Branch) and op.op == Branch.IS_ERROR) and not isinstance(op, LoadAddress) ): - new_block, error_block = BasicBlock(), BasicBlock() - new_block.error_handler = error_block.error_handler = cur_block.error_handler - new_blocks += [error_block, new_block] - if src not in init_registers_set: init_registers.append(src) init_registers_set.add(src) + # XXX: if src.name is empty, it should be a + # temp... and it should be OK?? + if not src.name: + continue + + new_block, error_block = BasicBlock(), BasicBlock() + new_block.error_handler = error_block.error_handler = cur_block.error_handler + new_blocks += [error_block, new_block] + if not src.type.error_overlap: cur_block.ops.append( Branch(