Skip to content

Commit 0f86e23

Browse files
authored
[flang] Avoid optimizing min and max if not valid type (#134972)
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
1 parent 5307040 commit 0f86e23

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,15 @@ class ReductionConversion : public mlir::OpRewritePattern<Op> {
988988
op, "Currently minloc/maxloc is not handled");
989989
} else if constexpr (std::is_same_v<Op, hlfir::MaxvalOp> ||
990990
std::is_same_v<Op, hlfir::MinvalOp>) {
991+
mlir::Type ty = op.getType();
992+
if (!(mlir::isa<mlir::FloatType>(ty) ||
993+
mlir::isa<mlir::IntegerType>(ty))) {
994+
return rewriter.notifyMatchFailure(
995+
op, "Type is not supported for Maxval or Minval yet");
996+
}
997+
991998
bool isMax = std::is_same_v<Op, hlfir::MaxvalOp>;
992-
init = makeMinMaxInitValGenerator(isMax)(builder, loc, op.getType());
999+
init = makeMinMaxInitValGenerator(isMax)(builder, loc, ty);
9931000
genBodyFn = [inlineSource, isMax](
9941001
fir::FirOpBuilder builder, mlir::Location loc,
9951002
mlir::Value reduction,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Test to assure we don't hit assertion when passing characters to maxval
2+
// RUN: fir-opt %s -opt-bufferization | FileCheck %s
3+
4+
5+
// This simplified `fir` is derived from this test program:
6+
// program FlangOptimizerBug
7+
// character(len=*), parameter :: values(*) = &
8+
// [ "To be ","or not " &
9+
// , "to ","be. " &
10+
// , "that ","is " &
11+
// , "the ","question"]
12+
// integer :: me, ni, i
13+
// character(len=len(values)) :: my_val, expected
14+
//
15+
// me = 1
16+
// ni = 8
17+
//
18+
// my_val = values(mod(me-1, size(values))+1)
19+
// expected = maxval([(values(mod(i-1,size(values))+1), i = 1, ni)])
20+
//
21+
// print *, my_val, expected
22+
// end program
23+
24+
func.func @_QQmain() {
25+
%c8 = arith.constant 8 : index
26+
%1 = fir.alloca !fir.char<1, 8>
27+
%24 = fir.shape %c8 : (index) -> !fir.shape<1>
28+
%25 = hlfir.elemental %24 typeparams %c8 unordered : (!fir.shape<1>, index) -> !hlfir.expr<?x!fir.char<1, 8>> {
29+
^bb0(%arg0: index):
30+
%dummy = fir.string_lit "A"(8) : !fir.char<1, 8>
31+
hlfir.yield_element %dummy : !fir.char<1, 8>
32+
}
33+
%26 = hlfir.maxval %25 {fastmath = #arith.fastmath<contract>} : (!hlfir.expr<?x!fir.char<1, 8>>) -> !hlfir.expr<!fir.char<1, 8>>
34+
hlfir.assign %26 to %1 : !hlfir.expr<!fir.char<1, 8>>, !fir.ref<!fir.char<1, 8>> // Assign to %1 directly
35+
hlfir.destroy %26 : !hlfir.expr<!fir.char<1, 8>>
36+
hlfir.destroy %25 : !hlfir.expr<?x!fir.char<1, 8>>
37+
return
38+
}
39+
40+
// CHECK-LABEL: func.func @_QQmain() {
41+
// CHECK-NEXT: %c8 = arith.constant 8 : index
42+
// CHECK-NEXT: %[[V0:.*]] = fir.alloca !fir.char<1,8>
43+
// CHECK-NEXT: %[[V1:.*]] = fir.shape %c8 : (index) -> !fir.shape<1>
44+
// CHECK-NEXT: %[[V2:.*]] = hlfir.elemental %1 typeparams %c8 unordered : (!fir.shape<1>, index) -> !hlfir.expr<?x!fir.char<1,8>> {
45+
// CHECK-NEXT: ^bb0(%arg0: index):
46+
// CHECK-NEXT: %[[V4:.*]] = fir.string_lit "A"(8) : !fir.char<1,8>
47+
// CHECK-NEXT: hlfir.yield_element %[[V4]] : !fir.char<1,8>
48+
// CHECK-NEXT: }
49+
// CHECK-NEXT: %[[V3:.*]] = hlfir.maxval %[[V2]] {fastmath = #arith.fastmath<contract>} : (!hlfir.expr<?x!fir.char<1,8>>) -> !hlfir.expr<!fir.char<1,8>>
50+
// CHECK-NEXT: hlfir.assign %[[V3]] to %[[V0]] : !hlfir.expr<!fir.char<1,8>>, !fir.ref<!fir.char<1,8>>
51+
// CHECK-NEXT: hlfir.destroy %[[V3]] : !hlfir.expr<!fir.char<1,8>>
52+
// CHECK-NEXT: hlfir.destroy %[[V2]] : !hlfir.expr<?x!fir.char<1,8>>
53+
// CHECK-NEXT: return
54+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)