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 @@ -217,7 +217,7 @@ auto loop = builder.create(loc, zeroIdx, loopCount, step, unorderedOrInitialLoopCond, /*finalCountValue=*/false, init); - init = loop.getRegionIterArgs()[0]; + init = loop.getRegionIterArgs()[resultIndex]; indices.push_back(loop.getInductionVar()); // Set insertion point to the loop body so that the next loop // is inserted inside the current one. @@ -732,17 +732,14 @@ return; mlir::Value inputBox = findBoxDef(args[0]); - LLVM_DEBUG(llvm::dbgs() << "Boxdef was: " << inputBox << '\n'); mlir::Type elementType = hlfir::getFortranElementType(inputBox.getType()); mlir::SymbolRefAttr callee = call.getCalleeAttr(); fir::FirOpBuilder builder{getSimplificationBuilder(call, kindMap)}; - LLVM_DEBUG(llvm::dbgs() << "In DIM0 simplify" << '\n'); // Treating logicals as integers makes things a lot easier fir::LogicalType logicalType = {elementType.dyn_cast()}; - LLVM_DEBUG(llvm::dbgs() << "Done logical cast, got: " << logicalType << '\n'); fir::KindTy kind = logicalType.getFKind(); mlir::Type intElementType = mlir::IntegerType::get(builder.getContext(), kind * 8); @@ -753,8 +750,6 @@ mlir::Twine{kind} + "x" + mlir::Twine{rank}) .str(); - LLVM_DEBUG(llvm::dbgs() << "end of DIM0" << '\n'); - simplifyReductionBody(call, kindMap, genBodyFunc, builder, funcName, intElementType); } @@ -918,12 +913,10 @@ return; } if (funcName.startswith(RTNAME_STRING(Count))) { - LLVM_DEBUG(llvm::dbgs() << "Count" << '\n'); simplifyLogicalDim0Reduction(call, kindMap, genRuntimeCountBody); return; } if (funcName.startswith(RTNAME_STRING(Any))) { - LLVM_DEBUG(llvm::dbgs() << "Any" << '\n'); simplifyLogicalDim1Reduction(call, kindMap, genRuntimeAnyBody); return; } 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 @@ -1423,6 +1423,65 @@ // CHECK: fir.call @_FortranAAny({{.*}}) fastmath : (!fir.box, !fir.ref, i32, i32) -> i1 // CHECK-NOT fir.call @_FortranAAnyLogical4x1_simplified({{.*}}) +// ----- +// Check that multi-rank Any cases are properly simplified + +func.func @_QPtestAny_2D(%arg0: !fir.ref>> {fir.bindc_name = "a"}) -> !fir.logical<4> { + %c10 = arith.constant 10 : index + %c0 = arith.constant 0 : index + %0 = fir.alloca !fir.logical<4> {bindc_name = "testAny_2D", uniq_name = "_QFtestAny_2DEtestAny_2D"} + %1 = fir.shape %c10, %c0 : (index, index) -> !fir.shape<2> + %2 = fir.embox %arg0(%1) : (!fir.ref>>, !fir.shape<2>) -> !fir.box>> + %c1 = arith.constant 1 : index + %3 = fir.address_of(@_QQcl.04ab56883945fd2c21a3b6d132f0bb37) : !fir.ref> + %c3_i32 = arith.constant 3 : i32 + %4 = fir.convert %2 : (!fir.box>>) -> !fir.box + %5 = fir.convert %3 : (!fir.ref>) -> !fir.ref + %6 = fir.convert %c1 : (index) -> i32 + %7 = fir.call @_FortranAAny(%4, %5, %c3_i32, %6) fastmath : (!fir.box, !fir.ref, i32, i32) -> i1 + %8 = fir.convert %7 : (i1) -> !fir.logical<4> + fir.store %8 to %0 : !fir.ref> + %9 = fir.load %0 : !fir.ref> + return %9 : !fir.logical<4> + } + +// CHECK-LABEL: func.func @_QPtestAny_2D( +// CHECK-SAME: %[[A:.*]]: !fir.ref>> {fir.bindc_name = "a"}) -> !fir.logical<4> { +// CHECK: %[[SIZE10:.*]] = arith.constant 10 : index +// CHECK: %[[SIZE_0:.*]] = arith.constant 0 : index +// CHECK: %[[SHAPE:.*]] = fir.shape %[[SIZE10]], %[[SIZE_0]] : (index, index) -> !fir.shape<2> +// CHECK: %[[A_BOX_LOGICAL:.*]] = fir.embox %[[VAL_0]](%[[SHAPE]]) : (!fir.ref>>, !fir.shape<2>) -> !fir.box>> +// CHECK: %[[A_BOX_NONE:.*]] = fir.convert %[[A_BOX_LOGICAL]] : (!fir.box>>) -> !fir.box +// CHECK: %[[RES:.*]] = fir.call @_FortranAAnyLogical4x2_simplified(%[[A_BOX_NONE]]) fastmath : (!fir.box) -> i1 + +// CHECK-LABEL: func.func private @_FortranAAnyLogical4x2_simplified( +// CHECK-SAME: %[[ARR:.*]]: !fir.box) -> i1 attributes {llvm.linkage = #llvm.linkage} { +// CHECK: %[[TRUE:.*]] = arith.constant true +// CHECK: %[[CINDEX_0:.*]] = arith.constant 0 : index +// CHECK: %[[A_BOX_I32:.*]] = fir.convert %[[VAL_0]] : (!fir.box) -> !fir.box> +// CHECK: %[[FALSE:.*]] = arith.constant false +// CHECK: %[[CINDEX_1:.*]] = arith.constant 1 : index +// CHECK: %[[DIMINDEX_0:.*]] = arith.constant 0 : index +// CHECK: %[[DIMS:.*]]:3 = fir.box_dims %[[A_BOX_I32]], %[[DIMINDEX_0]] : (!fir.box>, index) -> (index, index, index) +// CHECK: %[[EXTENT:.*]] = arith.subi %[[DIMS]]#1, %[[CINDEX_1]] : index +// CHECK: %[[DIMINDEX_1:.*]] = arith.constant 1 : index +// CHECK: %[[DIMS_1:.*]]:3 = fir.box_dims %[[A_BOX_I32]], %[[DIMINDEX_1]] : (!fir.box>, index) -> (index, index, index) +// CHECK: %[[EXTENT1:.*]] = arith.subi %[[DIMS_1]]#1, %[[CINDEX_1]] : index +// CHECK: %[[RES:.*]]:2 = fir.iterate_while (%[[ITER:.*]] = %[[CINDEX_0]] to %[[EXTENT1]] step %[[CINDEX_1]]) and (%[[OK:.*]] = %[[TRUE]]) iter_args(%[[INIT:.*]] = %[[FALSE]]) -> (i1) { +// CHECK: %[[INNER_RES:.*]]:2 = fir.iterate_while (%[[ITER_1:.*]] = %[[CINDEX_0]] to %[[EXTENT]] step %[[CINDEX_1]]) and (%[[OK1:.*]] = %[[TRUE]]) iter_args(%[[INNER_INIT:.*]] = %[[INIT]]) -> (i1) { +// CHECK: %[[ITEM:.*]] = fir.coordinate_of %[[A_BOX_I32]], %[[ITER_1]], %[[ITER]] : (!fir.box>, index, index) -> !fir.ref +// CHECK: %[[ITEMVAL:.*]] = fir.load %[[ITEM]] : !fir.ref +// CHECK: %[[I32_0:.*]] = arith.constant 0 : i32 +// CHECK: %[[NEXT_ANY:.*]] = arith.cmpi ne, %[[ITEMVAL]], %[[I32_0]] : i32 +// CHECK: %[[I1_1:.*]] = arith.constant true +// CHECK: %[[CONTINUE:.*]] = arith.xori %[[NEXT_ANY]], %[[I1_1]] : i1 +// CHECK: fir.result %[[CONTINUE]], %[[NEXT_ANY]] : i1, i1 +// CHECK: } +// CHECK: fir.result %[[RETURN_VALS:.*]]#0, %[[RETURN_VALS]]#1 : i1, i1 +// CHECK: } +// CHECK: return %[[RETURN_VAL:.*]]#1 : i1 +// CHECK: } + // ----- // Ensure All is simplified in correct usage @@ -1477,7 +1536,6 @@ // ----- // Ensure All is correctly simplified for different kind logical - func.func @_QPtestAll_NoDimArgLogical1(%arg0: !fir.ref>> {fir.bindc_name = "a"}) -> !fir.logical<1> { %c10 = arith.constant 10 : index %0 = fir.alloca !fir.logical<1> {bindc_name = "testAll_NoDimArgLogical1", uniq_name = "_QFtestAll_NoDimArgLogical1EtestAll_NoDimArgLogical1"}