[flang-commits] [flang] [flang] Avoid optimizing min and max if not valid type (PR #134972)
Miguel Saldivar via flang-commits
flang-commits at lists.llvm.org
Thu Apr 10 11:42:00 PDT 2025
https://github.com/Saldivarcher updated https://github.com/llvm/llvm-project/pull/134972
>From 9cbacb6bdab48bb15effb19a317ee63fa04e30c4 Mon Sep 17 00:00:00 2001
From: Miguel Saldivar <miguel.saldivar at hpe.com>
Date: Wed, 9 Apr 2025 01:22:09 -0500
Subject: [PATCH] [flang] Avoid optimizing min and max if not valid type
In `makeMinMaxInitValGenerator` it explicitly checks for only
`FloatType` and `IntegerType`, so we shouldn't match if we don't have
either of those types.
---
.../Transforms/OptimizedBufferization.cpp | 9 +++-
.../test/HLFIR/minval-maxval-issue-134308.fir | 54 +++++++++++++++++++
2 files changed, 62 insertions(+), 1 deletion(-)
create mode 100644 flang/test/HLFIR/minval-maxval-issue-134308.fir
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
index 96a3622f4afee..c489450384a35 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>) {
+ mlir::Type 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,
diff --git a/flang/test/HLFIR/minval-maxval-issue-134308.fir b/flang/test/HLFIR/minval-maxval-issue-134308.fir
new file mode 100644
index 0000000000000..9108a70be0fe7
--- /dev/null
+++ b/flang/test/HLFIR/minval-maxval-issue-134308.fir
@@ -0,0 +1,54 @@
+// Test to assure we don't hit assertion when passing characters to maxval
+// RUN: fir-opt %s -opt-bufferization | FileCheck %s
+
+
+// This simplified `fir` is derived from this test program:
+// program FlangOptimizerBug
+// character(len=*), parameter :: values(*) = &
+// [ "To be ","or not " &
+// , "to ","be. " &
+// , "that ","is " &
+// , "the ","question"]
+// integer :: me, ni, i
+// character(len=len(values)) :: my_val, expected
+//
+// me = 1
+// ni = 8
+//
+// my_val = values(mod(me-1, size(values))+1)
+// expected = maxval([(values(mod(i-1,size(values))+1), i = 1, ni)])
+//
+// print *, my_val, expected
+// end program
+
+func.func @_QQmain() {
+ %c8 = arith.constant 8 : index
+ %1 = fir.alloca !fir.char<1, 8>
+ %24 = fir.shape %c8 : (index) -> !fir.shape<1>
+ %25 = hlfir.elemental %24 typeparams %c8 unordered : (!fir.shape<1>, index) -> !hlfir.expr<?x!fir.char<1, 8>> {
+ ^bb0(%arg0: index):
+ %dummy = fir.string_lit "A"(8) : !fir.char<1, 8>
+ hlfir.yield_element %dummy : !fir.char<1, 8>
+ }
+ %26 = hlfir.maxval %25 {fastmath = #arith.fastmath<contract>} : (!hlfir.expr<?x!fir.char<1, 8>>) -> !hlfir.expr<!fir.char<1, 8>>
+ hlfir.assign %26 to %1 : !hlfir.expr<!fir.char<1, 8>>, !fir.ref<!fir.char<1, 8>> // Assign to %1 directly
+ hlfir.destroy %26 : !hlfir.expr<!fir.char<1, 8>>
+ hlfir.destroy %25 : !hlfir.expr<?x!fir.char<1, 8>>
+ return
+}
+
+// CHECK-LABEL: func.func @_QQmain() {
+// CHECK-NEXT: %c8 = arith.constant 8 : index
+// CHECK-NEXT: %[[V0:.*]] = fir.alloca !fir.char<1,8>
+// CHECK-NEXT: %[[V1:.*]] = fir.shape %c8 : (index) -> !fir.shape<1>
+// CHECK-NEXT: %[[V2:.*]] = hlfir.elemental %1 typeparams %c8 unordered : (!fir.shape<1>, index) -> !hlfir.expr<?x!fir.char<1,8>> {
+// CHECK-NEXT: ^bb0(%arg0: index):
+// CHECK-NEXT: %[[V4:.*]] = fir.string_lit "A"(8) : !fir.char<1,8>
+// CHECK-NEXT: hlfir.yield_element %[[V4]] : !fir.char<1,8>
+// CHECK-NEXT: }
+// CHECK-NEXT: %[[V3:.*]] = hlfir.maxval %[[V2]] {fastmath = #arith.fastmath<contract>} : (!hlfir.expr<?x!fir.char<1,8>>) -> !hlfir.expr<!fir.char<1,8>>
+// CHECK-NEXT: hlfir.assign %[[V3]] to %[[V0]] : !hlfir.expr<!fir.char<1,8>>, !fir.ref<!fir.char<1,8>>
+// CHECK-NEXT: hlfir.destroy %[[V3]] : !hlfir.expr<!fir.char<1,8>>
+// CHECK-NEXT: hlfir.destroy %[[V2]] : !hlfir.expr<?x!fir.char<1,8>>
+// CHECK-NEXT: return
+// CHECK-NEXT: }
More information about the flang-commits
mailing list