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

Add rewrite pattern into AggresiveSimplification that turns compare+select into min/max #2244

Merged
merged 2 commits into from
Apr 22, 2024

Conversation

mvpant
Copy link
Contributor

@mvpant mvpant commented Apr 19, 2024

This patch rewrites a comparison and selection combination that utilizes the same operands into a min/max operation:

func.func @test(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> (tensor<2xi32>, tensor<2xi32>) {
  %0 = stablehlo.compare GE, %arg0, %arg1, SIGNED : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1>
  %1 = stablehlo.compare LE, %arg0, %arg1, SIGNED : (tensor<2xi32>, tensor<2xi32>) -> tensor<2xi1>
  %s0 = stablehlo.select %0, %arg0, %arg1 : (tensor<2xi1>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32>
  %s1 = stablehlo.select %1, %arg0, %arg1 : (tensor<2xi1>, tensor<2xi32>, tensor<2xi32>) -> tensor<2xi32>
  return %s0, %s1 : tensor<2xi32>, tensor<2xi32>
}

transformed into:

func.func @test(%arg0: tensor<2xi32>, %arg1: tensor<2xi32>) -> (tensor<2xi32>, tensor<2xi32>) {
  %0 = stablehlo.maximum %arg0, %arg1 : tensor<2xi32>
  %1 = stablehlo.minimum %arg0, %arg1 : tensor<2xi32>
  return %0, %1 : tensor<2xi32>, tensor<2xi32>
}

@mvpant
Copy link
Contributor Author

mvpant commented Apr 19, 2024

I am primarily interested in implementing this simplification within the reduce body. Should I restrict the scope of the pattern?

@mlevesquedion mlevesquedion self-requested a review April 19, 2024 19:51
Copy link
Contributor

@mlevesquedion mlevesquedion left a comment

Choose a reason for hiding this comment

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

Looks great! No need to restrict to reduce, I think this is good to have everywhere.

Just a few nits, PTAL.

stablehlo/transforms/StablehloAggressiveSimplification.cpp Outdated Show resolved Hide resolved
stablehlo/transforms/StablehloAggressiveSimplification.cpp Outdated Show resolved Hide resolved
stablehlo/tests/stablehlo_aggressive_simplification.mlir Outdated Show resolved Hide resolved
@mlevesquedion mlevesquedion merged commit 1455e52 into openxla:main Apr 22, 2024
10 checks passed
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