Skip to content

Commit 4cff320

Browse files
authored
[flang] lower LBOUND for assumed-rank arrays (#94995)
1 parent e9a3623 commit 4cff320

File tree

4 files changed

+92
-17
lines changed

4 files changed

+92
-17
lines changed

flang/include/flang/Optimizer/Builder/Runtime/Inquiry.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ class FirOpBuilder;
2020

2121
namespace fir::runtime {
2222

23-
/// Generate call to general `LboundDim` runtime routine. Calls to LBOUND
24-
/// without a DIM argument get transformed into descriptor inquiries so they're
25-
/// not handled in the runtime.
23+
/// Generate call to `LboundDim` runtime routine.
2624
mlir::Value genLboundDim(fir::FirOpBuilder &builder, mlir::Location loc,
2725
mlir::Value array, mlir::Value dim);
2826

27+
/// Generate call to Lbound` runtime routine.
28+
void genLbound(fir::FirOpBuilder &builder, mlir::Location loc,
29+
mlir::Value resultAddr, mlir::Value arrayt, mlir::Value kind);
30+
2931
/// Generate call to general `Ubound` runtime routine. Calls to UBOUND
3032
/// with a DIM argument get transformed into an expression equivalent to
3133
/// SIZE() + LBOUND() - 1, so they don't have an intrinsic in the runtime.

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6362,16 +6362,17 @@ IntrinsicLibrary::genLbound(mlir::Type resultType,
63626362
llvm::ArrayRef<fir::ExtendedValue> args) {
63636363
assert(args.size() == 2 || args.size() == 3);
63646364
const fir::ExtendedValue &array = args[0];
6365-
if (const auto *boxValue = array.getBoxOf<fir::BoxValue>())
6366-
if (boxValue->hasAssumedRank())
6367-
TODO(loc, "intrinsic: lbound with assumed rank argument");
6365+
// Semantics builds signatures for LBOUND calls as either
6366+
// LBOUND(array, dim, [kind]) or LBOUND(array, [kind]).
6367+
const bool dimIsAbsent = args.size() == 2 || isStaticallyAbsent(args, 1);
6368+
if (array.hasAssumedRank() && dimIsAbsent)
6369+
return genAssumedRankBoundInquiry(builder, loc, resultType, args,
6370+
/*kindPos=*/1, fir::runtime::genLbound);
63686371

63696372
mlir::Type indexType = builder.getIndexType();
63706373

6371-
// Semantics builds signatures for LBOUND calls as either
6372-
// LBOUND(array, dim, [kind]) or LBOUND(array, [kind]).
6373-
if (args.size() == 2 || isStaticallyAbsent(args, 1)) {
6374-
// DIM is absent.
6374+
if (dimIsAbsent) {
6375+
// DIM is absent and the rank of array is a compile time constant.
63756376
mlir::Type lbType = fir::unwrapSequenceType(resultType);
63766377
unsigned rank = array.rank();
63776378
mlir::Type lbArrayType = fir::SequenceType::get(
@@ -6396,13 +6397,16 @@ IntrinsicLibrary::genLbound(mlir::Type resultType,
63966397
// DIM is present.
63976398
mlir::Value dim = fir::getBase(args[1]);
63986399

6399-
// If it is a compile time constant, skip the runtime call.
6400-
if (std::optional<std::int64_t> cstDim = fir::getIntIfConstant(dim)) {
6401-
mlir::Value one = builder.createIntegerConstant(loc, resultType, 1);
6402-
mlir::Value zero = builder.createIntegerConstant(loc, indexType, 0);
6403-
mlir::Value lb = computeLBOUND(builder, loc, array, *cstDim - 1, zero, one);
6404-
return builder.createConvert(loc, resultType, lb);
6405-
}
6400+
// If it is a compile time constant and the rank is known, skip the runtime
6401+
// call.
6402+
if (!array.hasAssumedRank())
6403+
if (std::optional<std::int64_t> cstDim = fir::getIntIfConstant(dim)) {
6404+
mlir::Value one = builder.createIntegerConstant(loc, resultType, 1);
6405+
mlir::Value zero = builder.createIntegerConstant(loc, indexType, 0);
6406+
mlir::Value lb =
6407+
computeLBOUND(builder, loc, array, *cstDim - 1, zero, one);
6408+
return builder.createConvert(loc, resultType, lb);
6409+
}
64066410

64076411
fir::ExtendedValue box = createBoxForRuntimeBoundInquiry(loc, builder, array);
64086412
return builder.createConvert(

flang/lib/Optimizer/Builder/Runtime/Inquiry.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ mlir::Value fir::runtime::genLboundDim(fir::FirOpBuilder &builder,
2929
return builder.create<fir::CallOp>(loc, lboundFunc, args).getResult(0);
3030
}
3131

32+
void fir::runtime::genLbound(fir::FirOpBuilder &builder, mlir::Location loc,
33+
mlir::Value resultAddr, mlir::Value array,
34+
mlir::Value kind) {
35+
mlir::func::FuncOp func =
36+
fir::runtime::getRuntimeFunc<mkRTKey(Lbound)>(loc, builder);
37+
auto fTy = func.getFunctionType();
38+
auto sourceFile = fir::factory::locationToFilename(builder, loc);
39+
auto sourceLine =
40+
fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
41+
auto args = fir::runtime::createArguments(
42+
builder, loc, fTy, resultAddr, array, kind, sourceFile, sourceLine);
43+
builder.create<fir::CallOp>(loc, func, args).getResult(0);
44+
}
45+
3246
/// Generate call to `Ubound` runtime routine. Calls to UBOUND with a DIM
3347
/// argument get transformed into an expression equivalent to
3448
/// SIZE() + LBOUND() - 1, so they don't have an intrinsic in the runtime.

flang/test/Lower/HLFIR/assumed-rank-inquiries-3.f90

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,58 @@ subroutine test_shape_2(x)
5454
! CHECK: %[[VAL_13:.*]] = fir.box_rank %[[VAL_4]] : (!fir.box<!fir.ptr<!fir.array<*:f32>>>) -> index
5555
! CHECK: %[[VAL_14:.*]] = fir.shape %[[VAL_13]] : (index) -> !fir.shape<1>
5656
! CHECK: %[[VAL_15:.*]]:2 = hlfir.declare %[[VAL_12]](%[[VAL_14]]) {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref<!fir.array<?xi32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.array<?xi32>>)
57+
58+
59+
subroutine test_lbound(x)
60+
real :: x(..)
61+
call takes_integer_array(lbound(x))
62+
end subroutine
63+
! CHECK-LABEL: func.func @_QPtest_lbound(
64+
! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.array<15xi32>
65+
! CHECK: %[[VAL_4:.*]] = arith.constant 4 : i32
66+
! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<15xi32>>) -> !fir.llvm_ptr<i8>
67+
! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_3:.*]] : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
68+
! CHECK: %[[VAL_10:.*]] = fir.call @_FortranALbound(%[[VAL_7]], %[[VAL_8]], %[[VAL_4]], %{{.*}}, %{{.*}})
69+
! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<15xi32>>) -> !fir.ref<!fir.array<?xi32>>
70+
! CHECK: %[[VAL_12:.*]] = fir.box_rank %[[VAL_3]] : (!fir.box<!fir.array<*:f32>>) -> index
71+
! CHECK: %[[VAL_13:.*]] = fir.shape %[[VAL_12]] : (index) -> !fir.shape<1>
72+
! CHECK: %[[VAL_14:.*]]:2 = hlfir.declare %[[VAL_11]](%[[VAL_13]]) {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref<!fir.array<?xi32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.array<?xi32>>)
73+
! CHECK: %[[VAL_15:.*]] = arith.constant false
74+
! CHECK: %[[VAL_16:.*]] = hlfir.as_expr %[[VAL_14]]#0 move %[[VAL_15]] : (!fir.box<!fir.array<?xi32>>, i1) -> !hlfir.expr<?xi32>
75+
! CHECK: %[[VAL_17:.*]]:3 = hlfir.associate %[[VAL_16]](%[[VAL_13]]) {adapt.valuebyref} : (!hlfir.expr<?xi32>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.array<?xi32>>, i1)
76+
! CHECK: fir.call @_QPtakes_integer_array(%[[VAL_17]]#1) fastmath<contract> : (!fir.ref<!fir.array<?xi32>>) -> ()
77+
! CHECK: hlfir.end_associate %[[VAL_17]]#1, %[[VAL_17]]#2 : !fir.ref<!fir.array<?xi32>>, i1
78+
! CHECK: hlfir.destroy %[[VAL_16]] : !hlfir.expr<?xi32>
79+
! CHECK: return
80+
! CHECK: }
81+
82+
subroutine test_lbound_kind(x)
83+
real :: x(..)
84+
call takes_integer8_array(lbound(x, kind=8))
85+
end subroutine
86+
! CHECK-LABEL: func.func @_QPtest_lbound_kind(
87+
! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.array<15xi64>
88+
! CHECK: %[[VAL_4:.*]] = arith.constant 8 : i32
89+
! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<15xi64>>) -> !fir.llvm_ptr<i8>
90+
! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_3:.*]] : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
91+
! CHECK: %[[VAL_10:.*]] = fir.call @_FortranALbound(%[[VAL_7]], %[[VAL_8]], %[[VAL_4]], %{{.*}}, %{{.*}})
92+
! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<15xi64>>) -> !fir.ref<!fir.array<?xi64>>
93+
! CHECK: %[[VAL_12:.*]] = fir.box_rank %[[VAL_3]] : (!fir.box<!fir.array<*:f32>>) -> index
94+
! CHECK: %[[VAL_13:.*]] = fir.shape %[[VAL_12]] : (index) -> !fir.shape<1>
95+
! CHECK: %[[VAL_14:.*]]:2 = hlfir.declare %[[VAL_11]](%[[VAL_13]]) {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref<!fir.array<?xi64>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi64>>, !fir.ref<!fir.array<?xi64>>)
96+
97+
subroutine test_lbound_2(x)
98+
real, pointer :: x(..)
99+
call takes_integer_array(lbound(x))
100+
end subroutine
101+
! CHECK-LABEL: func.func @_QPtest_lbound_2(
102+
! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.array<15xi32>
103+
! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3:.*]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<*:f32>>>>
104+
! CHECK: %[[VAL_5:.*]] = arith.constant 4 : i32
105+
! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<15xi32>>) -> !fir.llvm_ptr<i8>
106+
! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_4]] : (!fir.box<!fir.ptr<!fir.array<*:f32>>>) -> !fir.box<none>
107+
! CHECK: %[[VAL_11:.*]] = fir.call @_FortranALbound(%[[VAL_8]], %[[VAL_9]], %[[VAL_5]], %{{.*}}, %{{.*}})
108+
! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<15xi32>>) -> !fir.ref<!fir.array<?xi32>>
109+
! CHECK: %[[VAL_13:.*]] = fir.box_rank %[[VAL_4]] : (!fir.box<!fir.ptr<!fir.array<*:f32>>>) -> index
110+
! CHECK: %[[VAL_14:.*]] = fir.shape %[[VAL_13]] : (index) -> !fir.shape<1>
111+
! CHECK: %[[VAL_15:.*]]:2 = hlfir.declare %[[VAL_12]](%[[VAL_14]]) {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref<!fir.array<?xi32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.array<?xi32>>)

0 commit comments

Comments
 (0)