-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature[next]: Improve CollapseTuple pass (#1350)
Significantly improves the collapse tuple pass preparing wider support for `if` statements.
- Loading branch information
1 parent
1d305e1
commit 2970575
Showing
8 changed files
with
513 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# GT4Py - GridTools Framework | ||
# | ||
# Copyright (c) 2014-2023, ETH Zurich | ||
# All rights reserved. | ||
# | ||
# This file is part of the GT4Py project and the GridTools framework. | ||
# GT4Py is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU General Public License as published by the | ||
# Free Software Foundation, either version 3 of the License, or any later | ||
# version. See the LICENSE.txt file at the top-level directory of this | ||
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>. | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import dataclasses | ||
from collections import ChainMap | ||
|
||
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 | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class CannonicalizeBoundSymbolNames(eve.NodeTranslator): | ||
""" | ||
Given an iterator expression cannonicalize all bound symbol names. | ||
If two such expression are in the same scope and equal so are their values. | ||
>>> testee1 = im.lambda_("a")(im.plus("a", "b")) | ||
>>> cannonicalized_testee1 = CannonicalizeBoundSymbolNames.apply(testee1) | ||
>>> str(cannonicalized_testee1) | ||
'λ(_csym_1) → _csym_1 + b' | ||
>>> testee2 = im.lambda_("c")(im.plus("c", "b")) | ||
>>> cannonicalized_testee2 = CannonicalizeBoundSymbolNames.apply(testee2) | ||
>>> assert cannonicalized_testee1 == cannonicalized_testee2 | ||
""" | ||
|
||
_uids: eve_utils.UIDGenerator = dataclasses.field( | ||
init=False, repr=False, default_factory=lambda: eve_utils.UIDGenerator(prefix="_csym") | ||
) | ||
|
||
@classmethod | ||
def apply(cls, node: itir.Expr): | ||
return cls().visit(node, sym_map=ChainMap({})) | ||
|
||
def visit_Lambda(self, node: itir.Lambda, *, sym_map: ChainMap): | ||
sym_map = sym_map.new_child() | ||
for param in node.params: | ||
sym_map[str(param.id)] = self._uids.sequential_id() | ||
|
||
return im.lambda_(*sym_map.values())(self.visit(node.expr, sym_map=sym_map)) | ||
|
||
def visit_SymRef(self, node: itir.SymRef, *, sym_map: dict[str, str]): | ||
return im.ref(sym_map[node.id]) if node.id in sym_map else node | ||
|
||
|
||
def is_equal(a: itir.Expr, b: itir.Expr): | ||
""" | ||
Return true if two expressions have provably equal values. | ||
Be aware that this function might return false even though the two expression have the same | ||
value. | ||
>>> testee1 = im.lambda_("a")(im.plus("a", "b")) | ||
>>> testee2 = im.lambda_("c")(im.plus("c", "b")) | ||
>>> assert is_equal(testee1, testee2) | ||
>>> testee1 = im.lambda_("a")(im.plus("a", "b")) | ||
>>> testee2 = im.lambda_("c")(im.plus("c", "d")) | ||
>>> assert not is_equal(testee1, testee2) | ||
""" | ||
# TODO(tehrengruber): Extend this function cover more cases than just those with equal | ||
# structure, e.g., by also canonicalization of the structure. | ||
return a == b or ( | ||
CannonicalizeBoundSymbolNames.apply(a) == CannonicalizeBoundSymbolNames.apply(b) | ||
) |
Oops, something went wrong.