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

transformations: (canonicalize) Arith const reassociation #3364

Merged
merged 8 commits into from
Oct 31, 2024

Conversation

n-io
Copy link
Collaborator

@n-io n-io commented Oct 30, 2024

This adds the second of two canonicalisation patterns for FloatingPointLikeBinaryOperations on arith.constants.

  • Ops with two constant operands are folded by canonicalisation, regardless of math flags (see transformations: Canonicalizations for arith on float constants #3362 )
  • Chained ops of the form (const1 * var) * const2 are rewritten as (const1 * const2) * var iff the fastmath<reassoc> or fastmath<fast> flags are set, and if the ops are multiplications or additions. Note, this is not currently implemented in upstream mlir.

See here to check how it works in upstream mlir.

Note, these canonicalisations currently do not support container types.

@n-io n-io added the transformations Changes or adds a transformatio label Oct 30, 2024
@n-io n-io self-assigned this Oct 30, 2024
@n-io n-io requested a review from emmau678 October 30, 2024 18:35
@n-io n-io changed the base branch from main to nicolai/arith-const-canonicalize October 30, 2024 18:37
Copy link

codecov bot commented Oct 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.10%. Comparing base (a5628c9) to head (5bd9c00).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3364   +/-   ##
=======================================
  Coverage   90.10%   90.10%           
=======================================
  Files         449      449           
  Lines       56666    56686   +20     
  Branches     5440     5443    +3     
=======================================
+ Hits        51060    51079   +19     
  Misses       4168     4168           
- Partials     1438     1439    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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! I just had a qustion about the flags to add in the new created operation, but otherwise everything is good! Thanks!

// CHECK-NEXT: %2 = arith.mulf %0, %a : f32
// CHECK-NEXT: %3 = arith.mulf %2, %b : f32
// CHECK-NEXT: %4 = arith.constant 2.129352e+01 : f32
// CHECK-NEXT: %5 = arith.mulf %4, %0 fastmath<fast> : f32
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the resulting flag the or of both operations?
I am out of my depth here, is it what MLIR does?

Copy link
Collaborator Author

@n-io n-io Oct 31, 2024

Choose a reason for hiding this comment

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

Yes, since we're essentially folding two ops into one, I wasn't entirely sure how to set the flags on the new op. The union set of both ops seemed like a cleaner option as compared to picking either one at random. I am not entirely sure myself as to how mlir would resolve such a case.

@n-io n-io requested a review from alexarice October 31, 2024 11:34

return

// CHECK-LABEL: @test_const_var_const
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would also prefer to see the checks appear above the tests here, but feel free to ignore

Base automatically changed from nicolai/arith-const-canonicalize to main October 31, 2024 12:03
@n-io n-io merged commit a0d3757 into main Oct 31, 2024
14 checks passed
@n-io n-io deleted the nicolai/arith-const-canonicalize-part2 branch October 31, 2024 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
transformations Changes or adds a transformatio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants