Skip to content

Conversation

@Hardcode84
Copy link
Contributor

  • WaterComposeAffineArithPass: merges arith ops into affine.apply
  • WaterReorderAffinePass: tries to reorder expressions for better hoistability using upstream util.
  • WaterReorderAffineExprsPass: tries to reorder experssions for better CSE'bility
  • WaterReorderAffineExprsByRangePass: tries to reorder expressions to reduce intermediate results integer range.

Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
Signed-off-by: Ivan Butygin <[email protected]>
@Hardcode84 Hardcode84 requested a review from ftynse January 2, 2026 16:58
Comment on lines +180 to +185
Example:
%0 = affine.apply affine_map<(d0)[s0] -> (d0 + s0)>(%x)[%y]
%1 = arith.addi %0, %c4 : index

Transforms to:
%1 = affine.apply affine_map<(d0)[s0, s1] -> (d0 + s0 + s1)>(%x)[%y, %c4]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we have the operand folding upstream, maybe we can also add a canonicalization for result folding (can't use fold because we can't have arith depend on affine).

Comment on lines +196 to +198
This pass reorders the operands of affine.apply operations to improve
hoistability and enable better loop-invariant code motion. Operands
that are more likely to be loop-invariant are moved to symbol positions.
Copy link
Contributor

Choose a reason for hiding this comment

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

We have canonicalizeAffineApplyAndOperands (or something like that) upstream that should be moving operands to symbols when applicable. There is extra semantics that says symbols should not be (affine) loop induction variables, so we can't just arbitrarily make things symbols.

Comment on lines +214 to +218
The pass works in two phases:
1. Collects all affine.apply operations and builds hash statistics for
all sub-expressions encountered.
2. For each commutative operation, tries all permutations and selects
the ordering that maximizes hash hits (sum of occurrence counts).
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 be simpler to just convert affine to arith, run CSE, and then recover the affine form back by "raising"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not just doing the CSE, it's checking all permutations of commutative ops to match it better with any previous exprs encountered. Which is still doable on arith level, of course.

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.

2 participants