|
| 1 | +"""Insert spills for values that are live across yields.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from mypyc.analysis.dataflow import AnalysisResult, analyze_live_regs, get_cfg |
| 6 | +from mypyc.common import TEMP_ATTR_NAME |
| 7 | +from mypyc.ir.class_ir import ClassIR |
| 8 | +from mypyc.ir.func_ir import FuncIR |
| 9 | +from mypyc.ir.ops import ( |
| 10 | + BasicBlock, |
| 11 | + Branch, |
| 12 | + DecRef, |
| 13 | + GetAttr, |
| 14 | + IncRef, |
| 15 | + LoadErrorValue, |
| 16 | + Register, |
| 17 | + SetAttr, |
| 18 | + Value, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def insert_spills(ir: FuncIR, env: ClassIR) -> None: |
| 23 | + cfg = get_cfg(ir.blocks, use_yields=True) |
| 24 | + live = analyze_live_regs(ir.blocks, cfg) |
| 25 | + entry_live = live.before[ir.blocks[0], 0] |
| 26 | + |
| 27 | + # from mypyc.ir.pprint import format_func |
| 28 | + |
| 29 | + # print('\n'.join(format_func(ir))) |
| 30 | + |
| 31 | + entry_live = {op for op in entry_live if not (isinstance(op, Register) and op.is_arg)} |
| 32 | + # XXX: Actually for now, no Registers at all -- we keep the manual spills |
| 33 | + entry_live = {op for op in entry_live if not isinstance(op, Register)} |
| 34 | + |
| 35 | + ir.blocks = spill_regs(ir.blocks, env, entry_live, live) |
| 36 | + # print("\n".join(format_func(ir))) |
| 37 | + # print("\n\n\n=========") |
| 38 | + |
| 39 | + |
| 40 | +def spill_regs( |
| 41 | + blocks: list[BasicBlock], env: ClassIR, to_spill: set[Value], live: AnalysisResult[Value] |
| 42 | +) -> list[BasicBlock]: |
| 43 | + for op in blocks[0].ops: |
| 44 | + if isinstance(op, GetAttr) and op.attr == "__mypyc_env__": |
| 45 | + env_reg = op |
| 46 | + break |
| 47 | + else: |
| 48 | + raise AssertionError("could not find __mypyc_env__") |
| 49 | + |
| 50 | + spill_locs = {} |
| 51 | + for i, val in enumerate(to_spill): |
| 52 | + name = f"{TEMP_ATTR_NAME}2_{i}" |
| 53 | + env.attributes[name] = val.type |
| 54 | + spill_locs[val] = name |
| 55 | + |
| 56 | + for block in blocks: |
| 57 | + ops = block.ops |
| 58 | + block.ops = [] |
| 59 | + |
| 60 | + for i, op in enumerate(ops): |
| 61 | + to_decref = [] |
| 62 | + |
| 63 | + if isinstance(op, IncRef) and op.src in spill_locs: |
| 64 | + raise AssertionError("not sure what to do with an incref of a spill...") |
| 65 | + if isinstance(op, DecRef) and op.src in spill_locs: |
| 66 | + # When we decref a spilled value, we turn that into |
| 67 | + # NULLing out the attribute, but only if the spilled |
| 68 | + # value is not live *when we include yields in the |
| 69 | + # CFG*. (The original decrefs are computed without that.) |
| 70 | + # |
| 71 | + # We also skip a decref is the env register is not |
| 72 | + # live. That should only happen when an exception is |
| 73 | + # being raised, so everything should be handled there. |
| 74 | + if op.src not in live.after[block, i] and env_reg in live.after[block, i]: |
| 75 | + # Skip the DecRef but null out the spilled location |
| 76 | + null = LoadErrorValue(op.src.type) |
| 77 | + block.ops.extend([null, SetAttr(env_reg, spill_locs[op.src], null, op.line)]) |
| 78 | + continue |
| 79 | + |
| 80 | + if ( |
| 81 | + any(src in spill_locs for src in op.sources()) |
| 82 | + # N.B: IS_ERROR should be before a spill happens |
| 83 | + # XXX: but could we have a regular branch? |
| 84 | + and not (isinstance(op, Branch) and op.op == Branch.IS_ERROR) |
| 85 | + ): |
| 86 | + new_sources: list[Value] = [] |
| 87 | + for src in op.sources(): |
| 88 | + if src in spill_locs: |
| 89 | + read = GetAttr(env_reg, spill_locs[src], op.line) |
| 90 | + block.ops.append(read) |
| 91 | + new_sources.append(read) |
| 92 | + if src.type.is_refcounted: |
| 93 | + to_decref.append(read) |
| 94 | + else: |
| 95 | + new_sources.append(src) |
| 96 | + |
| 97 | + op.set_sources(new_sources) |
| 98 | + |
| 99 | + block.ops.append(op) |
| 100 | + |
| 101 | + for dec in to_decref: |
| 102 | + block.ops.append(DecRef(dec)) |
| 103 | + |
| 104 | + if op in spill_locs: |
| 105 | + # XXX: could we set uninit? |
| 106 | + block.ops.append(SetAttr(env_reg, spill_locs[op], op, op.line)) |
| 107 | + |
| 108 | + return blocks |
0 commit comments