Skip to content

Commit

Permalink
Allow lowered opts to be deleted in lowering trasnform (#18669)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
JukkaL authored Feb 13, 2025
1 parent 03cf35c commit b07d5f0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
10 changes: 6 additions & 4 deletions mypyc/lower/registry.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from __future__ import annotations

from typing import Callable, Final
from typing import Callable, Final, Optional, TypeVar

from mypyc.ir.ops import Value
from mypyc.irbuild.ll_builder import LowLevelIRBuilder

LowerFunc = Callable[[LowLevelIRBuilder, list[Value], int], Value]
LowerFuncOpt = Callable[[LowLevelIRBuilder, list[Value], int], Optional[Value]]

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

lowering_registry: Final[dict[str, LowerFunc]] = {}
LF = TypeVar("LF", LowerFunc, LowerFuncOpt)


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

def wrapper(f: LowerFunc) -> LowerFunc:
def wrapper(f: LF) -> LF:
assert name not in lowering_registry
lowering_registry[name] = f
return f
Expand Down
3 changes: 3 additions & 0 deletions mypyc/transform/ir_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def visit_unreachable(self, op: Unreachable) -> None:
self.add(op)

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

def visit_assign_multi(self, op: AssignMulti) -> Value | None:
Expand Down
4 changes: 3 additions & 1 deletion mypyc/transform/lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package.
"""

from __future__ import annotations

from mypyc.ir.func_ir import FuncIR
from mypyc.ir.ops import PrimitiveOp, Value
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
Expand All @@ -25,7 +27,7 @@ def lower_ir(ir: FuncIR, options: CompilerOptions) -> None:


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

0 comments on commit b07d5f0

Please sign in to comment.