| 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 |
entry_live = {op for op in entry_live if not (isinstance(op, Register) and op.is_arg)} |
| 28 |
# TODO: Actually for now, no Registers at all -- we keep the manual spills |
| 29 |
entry_live = {op for op in entry_live if not isinstance(op, Register)} |
| 30 |
|
| 31 |
ir.blocks = spill_regs(ir.blocks, env, entry_live, live, ir.arg_regs[0]) |
| 32 |
|
| 33 |
|
| 34 |
def spill_regs( |
| 35 |
blocks: list[BasicBlock], |
| 36 |
env: ClassIR, |
| 37 |
to_spill: set[Value], |
| 38 |
live: AnalysisResult[Value], |
| 39 |
self_reg: Register, |
| 40 |
) -> list[BasicBlock]: |
| 41 |
env_reg: Value |
| 42 |
for op in blocks[0].ops: |
| 43 |
if isinstance(op, GetAttr) and op.attr == "__mypyc_env__": |
| 44 |
env_reg = op |
| 45 |
break |
| 46 |
else: |
| 47 |
# Environment has been merged into generator object |
| 48 |
env_reg = self_reg |
| 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 |
if val.type.error_overlap: |
| 55 |
# We can safely treat as always initialized, since the type has no pointers. |
| 56 |
# This way we also don't need to manage the defined attribute bitfield. |
| 57 |
env._always_initialized_attrs.add(name) |
| 58 |
spill_locs[val] = name |
| 59 |
|
| 60 |
for block in blocks: |
| 61 |
ops = block.ops |
| 62 |
block.ops = [] |
| 63 |
|
| 64 |
for i, op in enumerate(ops): |
| 65 |
to_decref = [] |
| 66 |
|
| 67 |
if isinstance(op, IncRef) and op.src in spill_locs: |
| 68 |
raise AssertionError("not sure what to do with an incref of a spill...") |
| 69 |
if isinstance(op, DecRef) and op.src in spill_locs: |
| 70 |
# When we decref a spilled value, we turn that into |
| 71 |
# NULLing out the attribute, but only if the spilled |
| 72 |
# value is not live *when we include yields in the |
| 73 |
# CFG*. (The original decrefs are computed without that.) |
| 74 |
# |
| 75 |
# We also skip a decref is the env register is not |
| 76 |
# live. That should only happen when an exception is |
| 77 |
# being raised, so everything should be handled there. |
| 78 |
if op.src not in live.after[block, i] and env_reg in live.after[block, i]: |
| 79 |
# Skip the DecRef but null out the spilled location |
| 80 |
null = LoadErrorValue(op.src.type) |
| 81 |
block.ops.extend([null, SetAttr(env_reg, spill_locs[op.src], null, op.line)]) |
| 82 |
continue |
| 83 |
|
| 84 |
if ( |
| 85 |
any(src in spill_locs for src in op.sources()) |
| 86 |
# N.B: IS_ERROR should be before a spill happens |
| 87 |
# XXX: but could we have a regular branch? |
| 88 |
and not (isinstance(op, Branch) and op.op == Branch.IS_ERROR) |
| 89 |
): |
| 90 |
new_sources: list[Value] = [] |
| 91 |
stolen = op.stolen() |
| 92 |
for src in op.sources(): |
| 93 |
if src in spill_locs: |
| 94 |
read = GetAttr(env_reg, spill_locs[src], op.line) |
| 95 |
block.ops.append(read) |
| 96 |
new_sources.append(read) |
| 97 |
if src.type.is_refcounted and src not in stolen: |
| 98 |
to_decref.append(read) |
| 99 |
else: |
| 100 |
new_sources.append(src) |
| 101 |
|
| 102 |
op.set_sources(new_sources) |
| 103 |
|
| 104 |
block.ops.append(op) |
| 105 |
|
| 106 |
for dec in to_decref: |
| 107 |
block.ops.append(DecRef(dec)) |
| 108 |
|
| 109 |
if op in spill_locs: |
| 110 |
# XXX: could we set uninit? |
| 111 |
block.ops.append(SetAttr(env_reg, spill_locs[op], op, op.line)) |
| 112 |
|
| 113 |
return blocks |