diff --git a/helion/_compiler/ast_read_writes.py b/helion/_compiler/ast_read_writes.py index 12ae38b68..58d4e0683 100644 --- a/helion/_compiler/ast_read_writes.py +++ b/helion/_compiler/ast_read_writes.py @@ -5,8 +5,10 @@ import typing from typing import TYPE_CHECKING from typing import TypeVar +from typing import overload from .ast_extension import ExtendedAST +from .ast_scoped_rename import rename as _scoped_rename if TYPE_CHECKING: from collections.abc import Callable @@ -215,26 +217,21 @@ def from_ast(node: ast.AST) -> ReadWrites: return visitor.rw -def ast_rename(node: _A, renames: dict[str, str]) -> _A: - """ - Rename variables in an Abstract Syntax Tree (AST) node, in-place. +@overload +def ast_rename(node: ast.expr, renames: dict[str, str]) -> ast.expr: ... + + +@overload +def ast_rename(node: _A, renames: dict[str, str]) -> _A: ... - This function traverses the given AST node and renames variables - based on the provided mapping of old names to new names. - Args: - node: The root AST node to rename variables in. - renames: A dictionary mapping old variable names to new variable names. +def ast_rename(node: ast.AST, renames: dict[str, str]) -> ast.AST: + """Coalesce outer compiler names, preserving lambda and comprehension locals. - Returns: - The modified AST node with variables renamed. + Statements and modules are updated in place. Use the return value for a + bare lambda: avoiding a collision with a parameter may wrap the callback. """ - # Every ``Name`` is reached the same way a visitor would reach it; the - # flat walk only saves the per-node dispatch of ``NodeVisitor``. - for child in ast.walk(node): - if isinstance(child, ast.Name): - child.id = renames.get(child.id, child.id) - return node + return _scoped_rename(node, renames) if renames else node class _PreserveRequiredSuites(ast.NodeTransformer): diff --git a/helion/_compiler/ast_scoped_rename.py b/helion/_compiler/ast_scoped_rename.py new file mode 100644 index 000000000..2ccbca116 --- /dev/null +++ b/helion/_compiler/ast_scoped_rename.py @@ -0,0 +1,339 @@ +"""Preserve the local names of Python expressions during compiler variable coalescing.""" + +from __future__ import annotations + +import ast +from collections import defaultdict +import dataclasses +from dataclasses import dataclass +from typing import TYPE_CHECKING +from typing import TypeVar +from typing import cast + +if TYPE_CHECKING: + from collections.abc import Iterator + +_Node = TypeVar("_Node", bound=ast.AST) + +_Comprehension = ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp +EXPRESSION_SCOPES = ( + ast.Lambda, + ast.ListComp, + ast.SetComp, + ast.DictComp, + ast.GeneratorExp, +) + + +def _lambda_locals(fn: ast.Lambda) -> set[str]: + names = {a.arg for a in ast.iter_child_nodes(fn.args) if isinstance(a, ast.arg)} + # Walrus assignments in comprehensions belong to the containing function. + # A nested lambda owns its body, but its defaults execute in this scope. + pending: list[ast.AST] = [fn.body] + while pending: + node = pending.pop() + if isinstance(node, ast.NamedExpr) and isinstance(node.target, ast.Name): + names.add(node.target.id) + if isinstance(node, ast.Lambda): + pending.append(node.args) + else: + pending.extend(ast.iter_child_nodes(node)) + return names + + +def _target_names(target: ast.expr) -> set[str]: + names: set[str] = set() + pending = [target] + while pending: + item = pending.pop() + if isinstance(item, ast.Name): + names.add(item.id) + elif isinstance(item, ast.Starred): + pending.append(item.value) + elif isinstance(item, (ast.Tuple, ast.List)): + pending.extend(item.elts) + return names + + +def _lambda(at: ast.AST, args: list[str], body: ast.expr) -> ast.Lambda: + return ast.copy_location( + ast.Lambda( + args=ast.arguments( + posonlyargs=[], + args=[ast.copy_location(ast.arg(name), at) for name in args], + kwonlyargs=[], + kw_defaults=[], + defaults=[], + ), + body=body, + ), + at, + ) + + +@dataclass +class _Scope: + names: set[str] + is_lambda: bool + refs: dict[str, list[ast.Name]] = dataclasses.field( + default_factory=lambda: defaultdict(list) + ) + aliases: dict[str, str] = dataclasses.field(default_factory=dict) + + +def _shell(node: _Node) -> _Node: + result = cast("_Node", ast.AST.__new__(type(node))) + vars(result).update(vars(node)) + return result + + +def _copy_occurrence(root: _Node) -> _Node: + # Templates splice the same object at multiple positions. Detach every + # occurrence *inside this expression*; deepcopy preserves that sharing. + # Keep ExtendedAST locations, types and other compiler annotations shallow. + result = _shell(root) + work: list[tuple[ast.AST, ast.AST]] = [(root, result)] + while work: + node, owned = work.pop() + for field, value in ast.iter_fields(node): + if isinstance(value, ast.AST): + child = _shell(value) + setattr(owned, field, child) + work.append((value, child)) + elif isinstance(value, list): + items = list(value) # Never modify FX's immutable input list. + for i, item in enumerate(items): + if isinstance(item, ast.AST): + child = _shell(item) + items[i] = child + work.append((item, child)) + setattr( + owned, field, items if type(value) is list else type(value)(items) + ) + return result + + +@dataclass(frozen=True) +class _Slot: + parent: ast.AST + field: str + index: int | None = None + + def put(self, node: ast.AST) -> None: + if self.index is None: + setattr(self.parent, self.field, node) + else: + values = getattr(self.parent, self.field) + if values[self.index] is node: + return + if type(values) is list: + values[self.index] = node + else: + items = list(values) + items[self.index] = node + setattr(self.parent, self.field, type(values)(items)) + + +def _children(node: ast.AST) -> Iterator[tuple[ast.AST, _Slot]]: + for field, value in ast.iter_fields(node): + if isinstance(value, ast.AST): + yield value, _Slot(node, field) + elif isinstance(value, list): + for i, item in enumerate(value): + if isinstance(item, ast.AST): + yield item, _Slot(node, field, i) + + +@dataclass +class _Finish: + node: ast.Lambda | _Comprehension + scope: _Scope + + +class _ScopedRename: + def __init__(self, root: ast.AST, renames: dict[str, str]) -> None: + self.root = root + self.renames = renames + self.used: set[str] | None = None + self.sequence = 0 + + def fresh(self) -> str: + if self.used is None: + self.used = set(self.renames) | set(self.renames.values()) + for node in ast.walk(self.root): + if isinstance(node, ast.Name): + self.used.add(node.id) + elif isinstance(node, ast.arg): + self.used.add(node.arg) + while True: + name = f"_helion_scoped_{self.sequence}" + self.sequence += 1 + if name not in self.used: + self.used.add(name) + return name + + def name(self, node: ast.Name, scopes: tuple[_Scope, ...]) -> ast.expr: + for scope in reversed(scopes): + if node.id in scope.names: + if not scope.is_lambda: + scope.refs[node.id].append(node) + return node + target = self.renames.get(node.id, node.id) + if target != node.id: + for scope in scopes: + if target in scope.names: + if target not in scope.aliases: + scope.aliases[target] = self.fresh() + if scope.is_lambda: + # Keep the actual callback's positional and keyword ABI. + # Resolve the outer name at invocation, not at creation. + get = ast.copy_location( + ast.Name(id=scope.aliases[target], ctx=ast.Load()), node + ) + return ast.copy_location(ast.Call(get, [], []), node) + node.id = target + return node + + def wrap(self, node: ast.Lambda, scope: _Scope) -> ast.expr: + if not scope.aliases: + return node + params = list(scope.aliases.values()) + values: list[ast.expr] = [ + _lambda( + node, [], ast.copy_location(ast.Name(id=name, ctx=ast.Load()), node) + ) + for name in scope.aliases + ] + # Defaults execute once, in their original scope, including walrus writes. + for key in ("defaults", "kw_defaults"): + defaults = getattr(node.args, key) + updated = list(defaults) + for index, default in enumerate(defaults): + if default is not None: + name = self.fresh() + params.append(name) + values.append(default) + updated[index] = ast.copy_location( + ast.Name(id=name, ctx=ast.Load()), default + ) + setattr(node.args, key, type(defaults)(updated)) + return ast.copy_location( + ast.Call(_lambda(node, params, node), values, []), node + ) + + def rewrite(self, original: ast.expr) -> ast.expr: + if not any( + isinstance(n, ast.Name) and self.renames.get(n.id, n.id) != n.id + for n in ast.walk(original) + ): + return original + root = _copy_occurrence(original) + holder = ast.Expression(body=root) + work: list[tuple[ast.AST | _Finish, _Slot, tuple[_Scope, ...]]] = [ + (root, _Slot(holder, "body"), ()) + ] + while work: + node, slot, scopes = work.pop() + if isinstance(node, _Finish): + if isinstance(node.node, ast.Lambda): + slot.put(self.wrap(node.node, node.scope)) + else: + for name, fresh in node.scope.aliases.items(): + for ref in node.scope.refs[name]: + ref.id = fresh + elif isinstance(node, ast.Name): + slot.put(self.name(node, scopes)) + elif isinstance(node, ast.Lambda): + scope = _Scope(_lambda_locals(node), is_lambda=True) + work.extend( + ( + (_Finish(node, scope), slot, scopes), + (node.body, _Slot(node, "body"), (*scopes, scope)), + (node.args, _Slot(node, "args"), scopes), + ) + ) + elif isinstance(node, _Comprehension): + first = node.generators[0] + scope = _Scope( + { + name + for gen in node.generators + for name in _target_names(gen.target) + }, + is_lambda=False, + ) + inner = (*scopes, scope) + work.append((_Finish(node, scope), slot, scopes)) + for child, child_slot in _children(node): + if isinstance(child, ast.comprehension): + for value, elem in _children(child): + context = ( + scopes + if child is first and elem.field == "iter" + else inner + ) + work.append((value, elem, context)) + else: + work.append((child, child_slot, inner)) + else: + work.extend( + (child, child_slot, scopes) for child, child_slot in _children(node) + ) + return holder.body + + +def rename(root: ast.AST, renames: dict[str, str]) -> ast.AST: + if isinstance(root, EXPRESSION_SCOPES): + rewritten = _ScopedRename(root, renames).rewrite(root) + if type(rewritten) is type(root) and rewritten is not root: + for key, value in ast.iter_fields(rewritten): + setattr(root, key, value) + return root + return rewritten + # Inspect and detach scoped occurrences before applying any flat writes: + # the template builder may share those exact nodes with the outer body. + scoped = None + writes: dict[ast.Name, str] = {} + replacements: dict[ast.AST, ast.expr] = {} + parents: set[ast.AST] = set() + pending = [root] + while pending: + node = pending.pop() + if isinstance(node, ast.Name): + if (target := renames.get(node.id, node.id)) != node.id: + writes[node] = target + else: + for child in ast.iter_child_nodes(node): + if isinstance(child, EXPRESSION_SCOPES): + if scoped is None: + scoped = _ScopedRename(root, renames) + if child not in replacements: + replacements[child] = scoped.rewrite(child) + if replacements[child] is not child: + parents.add(node) + else: + pending.append(child) + for parent in parents: + for field, value in ast.iter_fields(parent): + if isinstance(value, ast.AST) and value in replacements: + setattr(parent, field, replacements[value]) + elif isinstance(value, list) and any( + isinstance(item, ast.AST) and replacements.get(item, item) is not item + for item in value + ): + # Rebuild just this field; its owner may be an immutable FX list. + setattr( + parent, + field, + type(value)( + [ + replacements.get(item, item) + if isinstance(item, ast.AST) + else item + for item in value + ] + ), + ) + for name, target in writes.items(): + name.id = target + return root diff --git a/test/test_ast_rename.py b/test/test_ast_rename.py new file mode 100644 index 000000000..993150b86 --- /dev/null +++ b/test/test_ast_rename.py @@ -0,0 +1,211 @@ +"""Free compiler names may be coalesced without capturing Python expression locals.""" + +from __future__ import annotations + +import ast + +import pytest +from torch.fx.immutable_collections import immutable_list + +from helion._compiler.ast_extension import expr_from_string +from helion._compiler.ast_read_writes import ast_rename + +pytestmark = pytest.mark.cpu_only + + +def _run(source: str, renames: dict[str, str]) -> object: + tree = ast.parse(source) + assert ast_rename(tree, renames) is tree + scope = {} + exec(compile(ast.fix_missing_locations(tree), "", "exec"), scope) + return scope["result"] + + +def test_closed_lambda_ignores_an_outer_rename() -> None: + source = "carry = 7\nf = lambda a, b: a * b\nresult = f(a=2, b=3)" + assert _run(source, {"a": "carry"}) == 6 + tree = ast.parse(source) + before = ast.dump(tree) + ast_rename(tree, {"a": "carry", "unused": "b"}) + assert ast.dump(tree) == before + + +def test_free_name_is_read_at_call_time_without_capturing_a_keyword() -> None: + source = """ +x = 7 +f = lambda a, /, b=2, *, c=3: x * b + c +x = 11 +result = f(1, b=4, c=5) +""" + assert _run(source, {"x": "b"}) == 49 + + +def test_nested_defaults_see_their_defining_scope() -> None: + source = """ +x = 5 +f = lambda a=x: (lambda b=a: (a, b, x)) +g = f() +x = 11 +result = g(), f(a=3)(b=4) +""" + assert _run(source, {"x": "a"}) == ((5, 5, 11), (3, 4, 11)) + + +def test_comprehension_uses_outer_first_iterable_and_inner_targets() -> None: + source = """ +x = [2, 4] +result = [(a, b, x[0]) for a in x for b in (a+i for i in range(a)) if b%2==0] +""" + assert _run(source, {"x": "a", "i": "junk"}) == [(2, 2, 2), (4, 4, 2), (4, 6, 2)] + + +def test_later_generator_target_is_local_even_before_it_is_assigned() -> None: + source = """ +later = 1 +result = [item for item in [2] if later for later in [3]] +""" + with pytest.raises(UnboundLocalError): + _run(source, {"later": "outside"}) + + +def test_plain_kernel_rename_preserves_signature_and_ast_objects() -> None: + fn = ast.parse( + "def f(input):\n temporary = input + 2\n return temporary" + ).body[0] + assert isinstance(fn, ast.FunctionDef) + value = fn.body[-1] + assert ast_rename(fn, {"temporary": "iv", "iv": "next"}) is fn + assert fn.body[-1] is value + assert ast.unparse(fn) == "def f(input):\n iv = input + 2\n return iv" + + +def test_factory_defaults_run_once_in_defining_scope_and_keep_all_keywords() -> None: + source = """ +events = [] +def once(x): + events.append(x) + return x +free = 4 +f = lambda a=(record := once(6)), *rest, b=once(7), **kw: (free, a, rest, b, kw) +free = 9 +result = f(a=2, b=3, _helion_scoped_0=11), f(1, 5), record, events +""" + assert _run(source, {"free": "a"}) == ( + (9, 2, (), 3, {"_helion_scoped_0": 11}), + (9, 1, (5,), 7, {}), + 6, + [6, 7], + ) + + +def test_rename_inside_two_comprehensions_and_lambda_keeps_outer_capture() -> None: + source = """ +x = 7 +callbacks = {a: [lambda a: x * a for a in (2,)] for a in (3, 5)} +x = 11 +result = {key: callback(a=2) for key, (callback,) in callbacks.items()} +""" + assert _run(source, {"x": "a"}) == {3: 22, 5: 22} + + +def test_walrus_inside_comprehension_binds_enclosing_lambda() -> None: + source = """ +x = 7 +f = lambda foo: ([captured := i for i in (foo,)], captured + x) +result = f(2) +""" + assert _run(source, {"captured": "other", "x": "captured"}) == ([2], 9) + + +def test_outlined_helper_owner_coalesces_formals_before_renaming_the_body() -> None: + # DeviceFunction.codegen_helper_functions rewrites the argument declarations + # independently, then passes the statement module to ast_rename. + (fn,) = ast.parse( + "def f(source):\n return (lambda source: source)(3) + source" + ).body + assert isinstance(fn, ast.FunctionDef) + renames = {"source": "coalesced"} + fn.args.args[0].arg = renames["source"] + module = ast.Module(body=fn.body, type_ignores=[]) + ast_rename(module, renames) + assert ( + ast.unparse(fn) + == "def f(coalesced):\n return (lambda source: source)(3) + coalesced" + ) + assert _run(ast.unparse(fn) + "\nresult = f(7)", {}) == 10 + + +def test_template_placeholders_remain_independent_across_lambda_scopes() -> None: + for text, renames, outer, expected in [ + ("a", {"a": "carry"}, {"carry": 7}, (7, 6)), + ("x + 1", {"x": "a"}, {"a": 7}, (8, 24)), + ]: + operand = expr_from_string(text) + tree = expr_from_string("({v}, (lambda a,b: {v} * b)(2,3))", v=operand) + assert isinstance(tree, ast.Tuple) and tree.elts[0] is operand + changed = ast.Expression(ast_rename(tree, renames)) + assert ( + eval( + compile(ast.fix_missing_locations(changed), "