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

core: Implement constant folding for AffineExpr #1178

Merged
merged 2 commits into from Jun 25, 2023

Conversation

Groverkss
Copy link
Collaborator

No description provided.

@Groverkss Groverkss marked this pull request as ready for review June 22, 2023 13:57
@codecov
Copy link

codecov bot commented Jun 22, 2023

Codecov Report

Patch coverage: 87.93% and project coverage change: +0.03 🎉

Comparison is base (6fca6fe) 88.72% compared to head (be58dff) 88.75%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1178      +/-   ##
==========================================
+ Coverage   88.72%   88.75%   +0.03%     
==========================================
  Files         161      161              
  Lines       22398    22455      +57     
  Branches     3385     3409      +24     
==========================================
+ Hits        19873    19931      +58     
+ Misses       1983     1981       -2     
- Partials      542      543       +1     
Impacted Files Coverage Δ
xdsl/ir/affine/affine_expr.py 91.44% <87.71%> (-1.64%) ⬇️
xdsl/parser/affine_parser.py 90.27% <100.00%> (+11.11%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@webmiche webmiche left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Collaborator

@math-fehr math-fehr left a comment

Choose a reason for hiding this comment

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

Nice! Thanks!


# Keep the constant on the right side.
if self._impl.kind == _AffineExprKind.Constant:
self, other = other, self
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's so cursed ;) (But that's fine)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Any better way to swap things?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No that's the best!
The only curse thing in my opinion is swapping self, but that's fine!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe something like other.__add__(self)?

Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

I don't really understand, it seems like the output is more complex than the source. Should we not aim to at least make it as simple as the input, and maybe simpler?

tests/filecheck/parser-printer/affine_map.mlir Outdated Show resolved Hide resolved
@@ -159,9 +159,42 @@ def eval(self, dims: list[int], symbols: list[int]) -> int:

raise ValueError("Unreachable")

def _try_fold_constant(
Copy link
Member

Choose a reason for hiding this comment

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

Before you merge this, why not make the signature of this function

Suggested change
def _try_fold_constant(
def _try_fold_constant(self) -> AffineExpr:

Where you return self if you could not merge the constant? Where the implementation folds the constant in the lhs, rhs, and then matches on the kind of the operation to see how they could be combined?

Copy link
Member

Choose a reason for hiding this comment

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

Also it seems like swapping around the order of arguments might be confusing, feels like that's a separate thing from constant folding to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also it seems like swapping around the order of arguments might be confusing, feels like that's a separate thing from constant folding to me.

It's canonicalizing the input for simplifications. Seems pretty standard to me?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Before you merge this, why not make the signature of this function

Where you return self if you could not merge the constant? Where the implementation folds the constant in the lhs, rhs, and then matches on the kind of the operation to see how they could be combined?

I like the current implementation. It allows me to fold things like (expr + constant) + constant too.

@Groverkss
Copy link
Collaborator Author

I don't really understand, it seems like the output is more complex than the source. Should we not aim to at least make it as simple as the input, and maybe simpler?

This will be done as more simplifications are added. This patch just adds some of them.

@Groverkss
Copy link
Collaborator Author

I added some more simplifications for now, but I don't think I'll add any more simplifications for now.

@webmiche webmiche added the core xDSL core (ir, textual format, ...) label Jun 25, 2023
@webmiche webmiche merged commit 39fd147 into xdslproject:main Jun 25, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core xDSL core (ir, textual format, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants