Skip to content

[flang] Avoid optimizing min and max if not valid type #134972

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

Merged
merged 1 commit into from
Apr 15, 2025

Conversation

Saldivarcher
Copy link
Contributor

In makeMinMaxInitValGenerator it explicitly checks for only FloatType and IntegerType, so we shouldn't match if we don't have either of those types.

Fix for #134308

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Apr 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 9, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Miguel Saldivar (Saldivarcher)

Changes

In makeMinMaxInitValGenerator it explicitly checks for only FloatType and IntegerType, so we shouldn't match if we don't have either of those types.

Fix for #134308


Full diff: https://github.com/llvm/llvm-project/pull/134972.diff

1 Files Affected:

  • (modified) flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp (+8-1)
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
index 96a3622f4afee..cb3894965f9aa 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
@@ -988,8 +988,15 @@ class ReductionConversion : public mlir::OpRewritePattern<Op> {
           op, "Currently minloc/maxloc is not handled");
     } else if constexpr (std::is_same_v<Op, hlfir::MaxvalOp> ||
                          std::is_same_v<Op, hlfir::MinvalOp>) {
+      auto ty = op.getType();
+      if (!(mlir::isa<mlir::FloatType>(ty) ||
+            mlir::isa<mlir::IntegerType>(ty))) {
+        return rewriter.notifyMatchFailure(
+            op, "Type is not supported for Maxval or Minval yet");
+      }
+
       bool isMax = std::is_same_v<Op, hlfir::MaxvalOp>;
-      init = makeMinMaxInitValGenerator(isMax)(builder, loc, op.getType());
+      init = makeMinMaxInitValGenerator(isMax)(builder, loc, ty);
       genBodyFn = [inlineSource, isMax](
                       fir::FirOpBuilder builder, mlir::Location loc,
                       mlir::Value reduction,

@Saldivarcher
Copy link
Contributor Author

Saldivarcher commented Apr 9, 2025

Note, I need to add a test. First time working here, so still learning!

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this. The change looks good to me once you have added a test.

I would test this by checking that hlfir.minval etc are not modified by the pass for a few types that aren't integers or floating point. See flang/test/HLFIR/minval-elemental.fir for an example test for this pass.

In `makeMinMaxInitValGenerator` it explicitly checks for only
`FloatType` and `IntegerType`, so we shouldn't match if we don't have
either of those types.
@Saldivarcher Saldivarcher force-pushed the user/saldivar/134308 branch 2 times, most recently from 1707b84 to 9cbacb6 Compare April 10, 2025 18:41
@Saldivarcher
Copy link
Contributor Author

Thank you for fixing this. The change looks good to me once you have added a test.

I would test this by checking that hlfir.minval etc are not modified by the pass for a few types that aren't integers or floating point. See flang/test/HLFIR/minval-elemental.fir for an example test for this pass.

Since maxval and minval only take arrays of integer, real, and character, wouldn't this new test and the old test cover everything?

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the update. Feel free to merge without another round of review.

nit: Please could you also test minval in the same way.

@Saldivarcher
Copy link
Contributor Author

@tblah I actually don't have commit access, can you merge for me please. Thanks again for reviewing this PR btw 👍

@tblah tblah merged commit 0f86e23 into llvm:main Apr 15, 2025
11 checks passed
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
In `makeMinMaxInitValGenerator` it explicitly checks for only
`FloatType` and `IntegerType`, so we shouldn't match if we don't have
either of those types.

Fix for llvm#134308
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants