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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 54 additions & 0 deletions flang/test/HLFIR/minval-maxval-issue-134308.fir
Original file line number Diff line number Diff line change
@@ -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: }