diff --git a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp --- a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp +++ b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp @@ -583,6 +583,8 @@ bool dimAndMaskAbsent = isZero(dim) && isOperandAbsent(mask); unsigned rank = getDimCount(args[0]); + // Rank is set to 0 for assumed shape arrays, don't simplify + // in these cases if (!(dimAndMaskAbsent && rank > 0)) return; @@ -619,11 +621,13 @@ mlir::Operation::operand_range args = call.getArgs(); const mlir::Value &dim = args[3]; + unsigned rank = getDimCount(args[0]); - if (!isZero(dim)) + // Rank is set to 0 for assumed shape arrays, don't simplify + // in these cases + if (!(isZero(dim) && rank > 0)) return; - unsigned rank = getDimCount(args[0]); mlir::SymbolRefAttr callee = call.getCalleeAttr(); fir::FirOpBuilder builder{getSimplificationBuilder(call, kindMap)}; diff --git a/flang/test/Transforms/simplifyintrinsics.fir b/flang/test/Transforms/simplifyintrinsics.fir --- a/flang/test/Transforms/simplifyintrinsics.fir +++ b/flang/test/Transforms/simplifyintrinsics.fir @@ -1214,3 +1214,28 @@ // CHECK-NOT fir.call @_FortranACountDim_simplified({{.*}}) // CHECK: %[[RES:.*]] = fir.call @_FortranACountDim({{.*}}) fastmath : (!fir.ref>, !fir.box, i32, i32, !fir.ref, i32) -> none // CHECK-NOT fir.call @_FortranACountDim_simplified({{.*}}) + +// ----- +// Ensure count isn't simplified for unknown dimension arrays + +func.func @_QPmc(%arg0: !fir.box>> {fir.bindc_name = "m"}) -> i32 { + %0 = fir.alloca i32 {bindc_name = "mc", uniq_name = "_QFmcEmc"} + %c0 = arith.constant 0 : index + %1 = fir.address_of(@_QQcl.95529933117f47914fc21e220c1a0896) : !fir.ref> + %c3_i32 = arith.constant 3 : i32 + %2 = fir.convert %arg0 : (!fir.box>>) -> !fir.box + %3 = fir.convert %1 : (!fir.ref>) -> !fir.ref + %4 = fir.convert %c0 : (index) -> i32 + %5 = fir.call @_FortranACount(%2, %3, %c3_i32, %4) fastmath : (!fir.box, !fir.ref, i32, i32) -> i64 + %6 = fir.convert %5 : (i64) -> i32 + fir.store %6 to %0 : !fir.ref + %7 = fir.load %0 : !fir.ref + return %7 : i32 +} +func.func private @_FortranACount(!fir.box, !fir.ref, i32, i32) -> i64 attributes {fir.runtime} + +// CHECK-LABEL: func.func @_QPmc( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.box>> {fir.bindc_name = "m"}) -> i32 { +// CHECK-NOT fir.call @_FortranACount_simplified({{.*}}) +// CHECK: %[[RES:.*]] = fir.call @_FortranACount({{.*}}) fastmath : (!fir.box, !fir.ref, i32, i32) -> i64 +// CHECK-NOT fir.call @_FortranACount_simplified({{.*}})