From fca52e82185a9eb49da47372ac92b7a4107b0fc9 Mon Sep 17 00:00:00 2001 From: Jean Perier Date: Mon, 25 Apr 2022 09:20:07 +0200 Subject: [PATCH] [flang] fix LBOUND lowering with KIND and no DIM arguments The lowering code was mistakenly assuming that the second argument in the signature provided by semantics is the DIM argument. This caused calls with a KIND argument but no DIM to be lowered as if the KIND argument was DIM. Differential Revision: https://reviews.llvm.org/D124243 --- flang/lib/Lower/IntrinsicCall.cpp | 7 +++-- flang/test/Lower/Intrinsics/lbound.f90 | 40 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp index 957a75972147..702e0cf849c2 100644 --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -3363,7 +3363,7 @@ static mlir::Value computeLBOUND(fir::FirOpBuilder &builder, mlir::Location loc, fir::ExtendedValue IntrinsicLibrary::genLbound(mlir::Type resultType, llvm::ArrayRef args) { - assert(args.size() > 0); + assert(args.size() == 2 || args.size() == 3); const fir::ExtendedValue &array = args[0]; if (const auto *boxValue = array.getBoxOf()) if (boxValue->hasAssumedRank()) @@ -3372,7 +3372,10 @@ IntrinsicLibrary::genLbound(mlir::Type resultType, //===----------------------------------------------------------------------===// mlir::Type indexType = builder.getIndexType(); - if (isStaticallyAbsent(args, 1)) { + // Semantics builds signatures for LBOUND calls as either + // LBOUND(array, dim, [kind]) or LBOUND(array, [kind]). + if (args.size() == 2 || isStaticallyAbsent(args, 1)) { + // DIM is absent. mlir::Type lbType = fir::unwrapSequenceType(resultType); unsigned rank = array.rank(); mlir::Type lbArrayType = fir::SequenceType::get( diff --git a/flang/test/Lower/Intrinsics/lbound.f90 b/flang/test/Lower/Intrinsics/lbound.f90 index bc2bfa9d9723..2a84d760c89f 100644 --- a/flang/test/Lower/Intrinsics/lbound.f90 +++ b/flang/test/Lower/Intrinsics/lbound.f90 @@ -52,3 +52,43 @@ subroutine lbound_test_3(a, dim, res) ! CHECK: fir.store %[[VAL_8]] to %arg2 : !fir.ref res = lbound(a, dim, 8) end subroutine + +! CHECK-LABEL: func @_QPlbound_test_4( +! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref> {fir.bindc_name = "a"}, +! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref {fir.bindc_name = "dim"}, +! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref {fir.bindc_name = "l1"}, +! CHECK-SAME: %[[VAL_3:.*]]: !fir.ref {fir.bindc_name = "u1"}, +! CHECK-SAME: %[[VAL_4:.*]]: !fir.ref {fir.bindc_name = "l2"}, +! CHECK-SAME: %[[VAL_5:.*]]: !fir.ref {fir.bindc_name = "u2"}) { +subroutine lbound_test_4(a, dim, l1, u1, l2, u2) + integer(8):: dim, l1, u1, l2, u2 +! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.array<2xi32> +! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_2]] : !fir.ref +! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i64) -> index +! CHECK: %[[VAL_17:.*]] = fir.load %[[VAL_4]] : !fir.ref +! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_17]] : (i64) -> index + real, dimension(l1:u1, l2:u2) :: a +! BeginExternalListOutput +! CHECK: %[[VAL_32:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_33:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_34:.*]] = arith.cmpi eq, %[[VAL_16:.*]], %[[VAL_33]] : index +! CHECK: %[[VAL_35:.*]] = fir.convert %[[VAL_32]] : (i32) -> index +! CHECK: %[[VAL_36:.*]] = arith.select %[[VAL_34]], %[[VAL_35]], %[[VAL_8]] : index +! CHECK: %[[VAL_37:.*]] = fir.convert %[[VAL_36]] : (index) -> i32 +! CHECK: %[[VAL_38:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_39:.*]] = fir.coordinate_of %[[VAL_6]], %[[VAL_38]] : (!fir.ref>, index) -> !fir.ref +! CHECK: fir.store %[[VAL_37]] to %[[VAL_39]] : !fir.ref +! CHECK: %[[VAL_40:.*]] = arith.cmpi eq, %[[VAL_26:.*]], %[[VAL_33]] : index +! CHECK: %[[VAL_41:.*]] = fir.convert %[[VAL_32]] : (i32) -> index +! CHECK: %[[VAL_42:.*]] = arith.select %[[VAL_40]], %[[VAL_41]], %[[VAL_18]] : index +! CHECK: %[[VAL_43:.*]] = fir.convert %[[VAL_42]] : (index) -> i32 +! CHECK: %[[VAL_44:.*]] = arith.constant 1 : index +! CHECK: %[[VAL_45:.*]] = fir.coordinate_of %[[VAL_6]], %[[VAL_44]] : (!fir.ref>, index) -> !fir.ref +! CHECK: fir.store %[[VAL_43]] to %[[VAL_45]] : !fir.ref +! CHECK: %[[VAL_46:.*]] = arith.constant 2 : index +! CHECK: %[[VAL_47:.*]] = fir.shape %[[VAL_46]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_48:.*]] = fir.embox %[[VAL_6]](%[[VAL_47]]) : (!fir.ref>, !fir.shape<1>) -> !fir.box> +! CHECK: %[[VAL_49:.*]] = fir.convert %[[VAL_48]] : (!fir.box>) -> !fir.box +! CHECK: fir.call @_FortranAioOutputDescriptor(%{{.*}}, %[[VAL_49]]) : (!fir.ref, !fir.box) -> i1 + print *, lbound(a, kind=4) +end subroutine