Skip to content

Commit b07d5f0

Browse files
authored
Allow lowered opts to be deleted in lowering trasnform (#18669)
This only works for simple initialization ops and ops where the return value is ignored. This can be used for dummy init ops that are used to give hints to data flow analysis about lifetimes of values, when initialization is done via a pointer argument (e.g. `init_my_struct(&reg)` in C).
1 parent 03cf35c commit b07d5f0

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

mypyc/lower/registry.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
from __future__ import annotations
22

3-
from typing import Callable, Final
3+
from typing import Callable, Final, Optional, TypeVar
44

55
from mypyc.ir.ops import Value
66
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
77

88
LowerFunc = Callable[[LowLevelIRBuilder, list[Value], int], Value]
9+
LowerFuncOpt = Callable[[LowLevelIRBuilder, list[Value], int], Optional[Value]]
910

11+
lowering_registry: Final[dict[str, LowerFuncOpt]] = {}
1012

11-
lowering_registry: Final[dict[str, LowerFunc]] = {}
13+
LF = TypeVar("LF", LowerFunc, LowerFuncOpt)
1214

1315

14-
def lower_primitive_op(name: str) -> Callable[[LowerFunc], LowerFunc]:
16+
def lower_primitive_op(name: str) -> Callable[[LF], LF]:
1517
"""Register a handler that generates low-level IR for a primitive op."""
1618

17-
def wrapper(f: LowerFunc) -> LowerFunc:
19+
def wrapper(f: LF) -> LF:
1820
assert name not in lowering_registry
1921
lowering_registry[name] = f
2022
return f

mypyc/transform/ir_transform.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ def visit_unreachable(self, op: Unreachable) -> None:
119119
self.add(op)
120120

121121
def visit_assign(self, op: Assign) -> Value | None:
122+
if op.src in self.op_map and self.op_map[op.src] is None:
123+
# Special case: allow removing register initialization assignments
124+
return None
122125
return self.add(op)
123126

124127
def visit_assign_multi(self, op: AssignMulti) -> Value | None:

mypyc/transform/lower.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package.
1010
"""
1111

12+
from __future__ import annotations
13+
1214
from mypyc.ir.func_ir import FuncIR
1315
from mypyc.ir.ops import PrimitiveOp, Value
1416
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
@@ -25,7 +27,7 @@ def lower_ir(ir: FuncIR, options: CompilerOptions) -> None:
2527

2628

2729
class LoweringVisitor(IRTransform):
28-
def visit_primitive_op(self, op: PrimitiveOp) -> Value:
30+
def visit_primitive_op(self, op: PrimitiveOp) -> Value | None:
2931
# The lowering implementation functions of various primitive ops are stored
3032
# in a registry, which is populated using function decorators. The name
3133
# of op (such as "int_eq") is used as the key.

0 commit comments

Comments
 (0)