-
Notifications
You must be signed in to change notification settings - Fork 49
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
feature[next]: Improve CollapseTuple pass #1350
Merged
tehrengruber
merged 31 commits into
GridTools:main
from
tehrengruber:improve_collapse_tuple
Feb 13, 2024
Merged
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
fe49d60
Improve CollapseTuple pass
tehrengruber cd7b6e3
Add comments
tehrengruber e49eae0
Bugfixes for scan
tehrengruber 4e750b4
Introduce `_is_equal_value_heuristics` to avoid double visit
tehrengruber 29560a3
Merge origin/main
tehrengruber e6c9f44
Fix tests
tehrengruber 5a27524
Fix tests
tehrengruber 367a05f
Merge remote-tracking branch 'origin/main' into improve_collapse_tuple
tehrengruber 0437fce
Move iterator utils to dedicated module (in preparation for other PR …
tehrengruber 36039a4
Fix format
tehrengruber ea42a16
Merge branch 'refactor_ir_utils' into improve_collapse_tuple
tehrengruber 8772906
Fix tests
tehrengruber ace2dc0
Fix tests
tehrengruber 2621f89
Merge branch 'refactor_ir_utils' into improve_collapse_tuple
tehrengruber 8b7a6d7
Fix tests
tehrengruber b5fd847
Merge remote-tracking branch 'origin/main' into improve_collapse_tuple
tehrengruber 6113312
Fix tests
tehrengruber 6680075
Merge origin/main
tehrengruber 3367c28
Small fix
tehrengruber c22cd35
Merge origin/main
tehrengruber 7c15360
Merge remote-tracking branch 'origin/main' into improve_collapse_tuple
tehrengruber 3a2a007
Cleanup
tehrengruber d915af5
Cleanup
tehrengruber fcdb8ae
Cleanup
tehrengruber 8d4f93e
Merge remote-tracking branch 'origin/main' into improve_collapse_tuple
tehrengruber 796f3a7
Revert debug changes to caching
tehrengruber bdc9221
Cleanup
tehrengruber 9902d63
Address reviewer comments
tehrengruber 650a934
Fix broken CI
tehrengruber 00bbf2b
Merge remote-tracking branch 'origin/main' into improve_collapse_tuple
tehrengruber e9f6fb1
Address reviewer comments
tehrengruber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,70 @@ | ||
# 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_provable_equal(a: itir.Expr, b: itir.Expr): | ||
""" | ||
Return true if, bot not only if, two expression (with equal scope) have the same value. | ||
|
||
>>> testee1 = im.lambda_("a")(im.plus("a", "b")) | ||
>>> testee2 = im.lambda_("c")(im.plus("c", "b")) | ||
>>> assert is_provable_equal(testee1, testee2) | ||
""" | ||
tehrengruber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return a == b or ( | ||
CannonicalizeBoundSymbolNames.apply(a) == CannonicalizeBoundSymbolNames.apply(b) | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the "bot not only if", even after bot->but
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a sufficient, but not a necessary condition. I think the formulation is correct as
Return true if and only if
certainly is, but I was already unhappy with it when I wrote it. I take any suggestion. I also renamed the function tois_equal
, I vaguely remember we had a discussion in person about it.