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

refactor[next]: Refactor ITIR CSE pass #1261

Merged
merged 13 commits into from
Aug 10, 2023

Conversation

tehrengruber
Copy link
Contributor

@tehrengruber tehrengruber commented May 30, 2023

This PR introduces a new function extract_subexpression that given an ITIR expression extracts all subexprs and returns a new expr with the subexprs replaced by new symbols. The function is extracted from the CSE pass and generalized to be used in the temporary extraction pass.

>>> import gt4py.next.iterator.ir_makers as im
>>> from gt4py.eve.utils import UIDGenerator
>>> expr = im.plus(im.plus("x", "y"), im.plus(im.plus("x", "y"), "z"))
>>> predicate = lambda subexpr, num_occurences: num_occurences > 1
>>> new_expr, extracted_subexprs, _ = extract_subexpression(
...                                     expr, predicate, UIDGenerator(prefix="_subexpr"))
>>> print(new_expr)
_subexpr_1 + (_subexpr_1 + z)
>>> for sym, subexpr in extracted_subexprs.items():
...    print(f"`{sym}`: `{subexpr}`")
`_subexpr_1`: `x + y`

No review required right now, will be reviewed in conjunction with the Temporary pass.

Introduced new function `extract_subexpression` that given an expression extract all subexprs and return a new expr with the subexprs replaced.
@havogt havogt self-assigned this Jun 21, 2023
@havogt havogt self-requested a review June 23, 2023 09:18
@havogt havogt removed their assignment Jun 23, 2023
Copy link
Contributor

@havogt havogt left a comment

Choose a reason for hiding this comment

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

Looks good, but please add some tests for the extract_subexpression.

@egparedes
Copy link
Contributor

@tehrengruber what's the current status of this PR? Is it still relevant?

Copy link
Contributor

@havogt havogt left a comment

Choose a reason for hiding this comment

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

Why this complicated test instead of simpler isolated tests for the extract_subexpression function?

@tehrengruber tehrengruber requested a review from havogt July 27, 2023 10:42
Copy link
Contributor

@havogt havogt left a comment

Choose a reason for hiding this comment

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

Just a question...

if not isinstance(node, SymbolTableTrait) and not _is_collectable_expr(node):
return super().visit(node, **kwargs)
return super().visit(node, depth=depth + 1, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

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

does this make sense? we are visiting the same node, but call the visit of the super class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree this is not nice. Except for the visit_SymRef case super().visit will dispatch to generic_visit and visit the children where depth + 1 is correct. I'm open for other solutions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to call generic_visit directly?

Copy link
Contributor

Choose a reason for hiding this comment

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

overload generic_visit and increase depth there.

else:
super().visit(
node,
depth=depth + 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

same question as above

@tehrengruber tehrengruber requested a review from havogt August 7, 2023 11:22

def visit(self, node: ir.Node, **kwargs) -> None: # type: ignore[override] # supertype accepts any node, but we want to be more specific here.
if not isinstance(node, SymbolTableTrait) and not _is_collectable_expr(node):
return super().visit(node, **kwargs)

depth = kwargs["depth"]
Copy link
Contributor

Choose a reason for hiding this comment

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

why not pop to avoid the funny pattern in 143?

Copy link
Contributor

Choose a reason for hiding this comment

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

(and move into the condition for if_)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The depth is used also in line 187 so I can not move it into the if_ part. I could also pop it which would make line 143 nicer, but then I would need to manually forward the depth in the other parts. I equally dislike both, pick one if you like to :-)

@tehrengruber tehrengruber merged commit 472ae15 into GridTools:main Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants