Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[next]: Flatten as_fieldop tuple arguments #1873

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
6 changes: 4 additions & 2 deletions src/gt4py/next/iterator/ir_utils/ir_makers.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def as_fieldop(expr: itir.Expr | str, domain: Optional[itir.Expr] = None) -> Cal
>>> str(as_fieldop(lambda_("it1", "it2")(plus(deref("it1"), deref("it2"))))("field1", "field2"))
'(⇑(λ(it1, it2) → ·it1 + ·it2))(field1, field2)'
"""
from gt4py.next.iterator.ir_utils import domain_utils
from gt4py.next.iterator.ir_utils import common_pattern_matcher as cpm, domain_utils

result = call(
call("as_fieldop")(
Expand All @@ -462,7 +462,9 @@ def as_fieldop(expr: itir.Expr | str, domain: Optional[itir.Expr] = None) -> Cal

def _populate_domain_annex_wrapper(*args, **kwargs):
node = result(*args, **kwargs)
if domain:
# note: if the domain is not a direct construction, e.g. because it is only a reference
# to a domain defined in a let, don't populate the annex
if domain and cpm.is_call_to(domain, ("cartesian_domain", "unstructured_domain")):
node.annex.domain = domain_utils.SymbolicDomain.from_expr(domain)
return node

Expand Down
78 changes: 77 additions & 1 deletion src/gt4py/next/iterator/ir_utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from gt4py import eve
from gt4py.eve import utils as eve_utils
from gt4py.next.iterator import ir as itir
from gt4py.next.iterator.ir_utils import ir_makers as im
from gt4py.next.iterator.ir_utils import common_pattern_matcher as cpm, ir_makers as im


@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -71,3 +71,79 @@ def is_equal(a: itir.Expr, b: itir.Expr):
return a == b or (
CannonicalizeBoundSymbolNames.apply(a) == CannonicalizeBoundSymbolNames.apply(b)
)


def canonicalize_as_fieldop(expr: itir.FunCall) -> itir.FunCall:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has only been moved without changes.

"""
Canonicalize applied `as_fieldop`s.

In case the stencil argument is a `deref` wrap it into a lambda such that we have a unified
format to work with (e.g. each parameter has a name without the need to special case).
"""
assert cpm.is_applied_as_fieldop(expr)

stencil = expr.fun.args[0] # type: ignore[attr-defined]
domain = expr.fun.args[1] if len(expr.fun.args) > 1 else None # type: ignore[attr-defined]
if cpm.is_ref_to(stencil, "deref"):
stencil = im.lambda_("arg")(im.deref("arg"))
new_expr = im.as_fieldop(stencil, domain)(*expr.args)

return new_expr

return expr


def _remove_let_alias(let_expr: itir.FunCall):
assert cpm.is_let(let_expr)
is_aliased_let = True
for param, arg in zip(let_expr.fun.params, let_expr.args, strict=True): # type: ignore[attr-defined] # ensured by cpm.is_let
is_aliased_let &= cpm.is_ref_to(arg, param.id)
if is_aliased_let:
return let_expr.fun.expr # type: ignore[attr-defined] # ensured by cpm.is_let
return let_expr


def unwrap_scan(stencil: itir.Lambda | itir.FunCall):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has been moved, the only addition is the _remove_let_alias call.

"""
If given a scan, extract stencil part of its scan pass and a back-transformation into a scan.

If a regular stencil is given the stencil is left as-is and the back-transformation is the
identity function. This function allows treating a scan stencil like a regular stencil during
a transformation avoiding the complexity introduced by the different IR format.

>>> scan = im.call("scan")(
... im.lambda_("state", "arg")(im.plus("state", im.deref("arg"))), True, 0.0
... )
>>> stencil, back_trafo = unwrap_scan(scan)
>>> str(stencil)
'λ(arg) → state + ·arg'
>>> str(back_trafo(stencil))
'scan(λ(state, arg) → state + ·arg, True, 0.0)'

In case a regular stencil is given it is returned as-is:

>>> deref_stencil = im.lambda_("it")(im.deref("it"))
>>> stencil, back_trafo = unwrap_scan(deref_stencil)
>>> assert stencil == deref_stencil
"""
if cpm.is_call_to(stencil, "scan"):
scan_pass, direction, init = stencil.args
assert isinstance(scan_pass, itir.Lambda)
# remove scan pass state to be used by caller
state_param = scan_pass.params[0]
stencil_like = im.lambda_(*scan_pass.params[1:])(scan_pass.expr)

def restore_scan(transformed_stencil_like: itir.Lambda):
new_scan_pass = im.lambda_(state_param, *transformed_stencil_like.params)(
_remove_let_alias(
im.call(transformed_stencil_like)(
*(param.id for param in transformed_stencil_like.params)
)
)
)
return im.call("scan")(new_scan_pass, direction, init)

return stencil_like, restore_scan

assert isinstance(stencil, itir.Lambda)
return stencil, lambda s: s
Loading