Skip to content

Commit 55bdb36

Browse files
authored
[flang] lower SIZE and SIZEOF for assumed-ranks (#94684)
1 parent eb33e46 commit 55bdb36

File tree

3 files changed

+128
-11
lines changed

3 files changed

+128
-11
lines changed

flang/include/flang/Optimizer/Builder/BoxValue.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &, const ExtendedValue &);
433433
/// substituted.
434434
ExtendedValue substBase(const ExtendedValue &exv, mlir::Value base);
435435

436-
/// Is the extended value `exv` an array?
436+
/// Is the extended value `exv` an array? Note that this returns true for
437+
/// assumed-ranks that could actually be scalars at runtime.
437438
bool isArray(const ExtendedValue &exv);
438439

439440
/// Get the type parameters for `exv`.
@@ -527,6 +528,15 @@ class ExtendedValue : public details::matcher<ExtendedValue> {
527528
[](const auto &box) -> bool { return false; });
528529
}
529530

531+
bool hasAssumedRank() const {
532+
return match(
533+
[](const fir::BoxValue &box) -> bool { return box.hasAssumedRank(); },
534+
[](const fir::MutableBoxValue &box) -> bool {
535+
return box.hasAssumedRank();
536+
},
537+
[](const auto &box) -> bool { return false; });
538+
}
539+
530540
/// LLVM style debugging of extended values
531541
LLVM_DUMP_METHOD void dump() const { llvm::errs() << *this << '\n'; }
532542

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -6121,9 +6121,6 @@ IntrinsicLibrary::genSize(mlir::Type resultType,
61216121
// Note that the value of the KIND argument is already reflected in the
61226122
// resultType
61236123
assert(args.size() == 3);
6124-
if (const auto *boxValue = args[0].getBoxOf<fir::BoxValue>())
6125-
if (boxValue->hasAssumedRank())
6126-
TODO(loc, "intrinsic: size with assumed rank argument");
61276124

61286125
// Get the ARRAY argument
61296126
mlir::Value array = builder.createBox(loc, args[0]);
@@ -6137,13 +6134,15 @@ IntrinsicLibrary::genSize(mlir::Type resultType,
61376134

61386135
// Get the DIM argument.
61396136
mlir::Value dim = fir::getBase(args[1]);
6140-
if (std::optional<std::int64_t> cstDim = fir::getIntIfConstant(dim)) {
6141-
// If it is a compile time constant, skip the runtime call.
6142-
return builder.createConvert(loc, resultType,
6143-
fir::factory::readExtent(builder, loc,
6144-
fir::BoxValue{array},
6145-
cstDim.value() - 1));
6146-
}
6137+
if (!args[0].hasAssumedRank())
6138+
if (std::optional<std::int64_t> cstDim = fir::getIntIfConstant(dim)) {
6139+
// If both DIM and the rank are compile time constants, skip the runtime
6140+
// call.
6141+
return builder.createConvert(
6142+
loc, resultType,
6143+
fir::factory::readExtent(builder, loc, fir::BoxValue{array},
6144+
cstDim.value() - 1));
6145+
}
61476146
if (!fir::isa_ref_type(dim.getType()))
61486147
return builder.createConvert(
61496148
loc, resultType, fir::runtime::genSizeDim(builder, loc, array, dim));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
! Test lowering of SIZE/SIZEOF inquiry intrinsics with assumed-ranks
2+
! arguments.
3+
! RUN: bbc -emit-hlfir -o - %s -allow-assumed-rank | FileCheck %s
4+
5+
6+
subroutine test_size_1(x)
7+
real :: x(..)
8+
call takes_integer(size(x))
9+
end subroutine
10+
11+
subroutine test_size_2(x)
12+
real :: x(..)
13+
call takes_integer(size(x, 2))
14+
end subroutine
15+
16+
subroutine test_size_3(x, d)
17+
real :: x(..)
18+
integer, optional :: d
19+
call takes_integer(size(x, d))
20+
end subroutine
21+
22+
subroutine test_size_4(x)
23+
real, allocatable :: x(..)
24+
call takes_integer(size(x))
25+
end subroutine
26+
27+
28+
! CHECK-LABEL: func.func @_QPtest_size_1(
29+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>> {fir.bindc_name = "x"}) {
30+
! CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope
31+
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {uniq_name = "_QFtest_size_1Ex"} : (!fir.box<!fir.array<*:f32>>, !fir.dscope) -> (!fir.box<!fir.array<*:f32>>, !fir.box<!fir.array<*:f32>>)
32+
! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_2]]#1 : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
33+
! CHECK: %[[VAL_7:.*]] = fir.call @_FortranASize(%[[VAL_5]]
34+
! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i64) -> i32
35+
! CHECK: %[[VAL_9:.*]]:3 = hlfir.associate %[[VAL_8]] {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
36+
! CHECK: fir.call @_QPtakes_integer(%[[VAL_9]]#1) fastmath<contract> : (!fir.ref<i32>) -> ()
37+
! CHECK: hlfir.end_associate %[[VAL_9]]#1, %[[VAL_9]]#2 : !fir.ref<i32>, i1
38+
! CHECK: return
39+
! CHECK: }
40+
41+
! CHECK-LABEL: func.func @_QPtest_size_2(
42+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>> {fir.bindc_name = "x"}) {
43+
! CHECK: %[[VAL_1:.*]] = fir.alloca i32
44+
! CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope
45+
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_2]] {uniq_name = "_QFtest_size_2Ex"} : (!fir.box<!fir.array<*:f32>>, !fir.dscope) -> (!fir.box<!fir.array<*:f32>>, !fir.box<!fir.array<*:f32>>)
46+
! CHECK: %[[VAL_4:.*]] = arith.constant 2 : i32
47+
! CHECK: fir.store %[[VAL_4]] to %[[VAL_1]] : !fir.ref<i32>
48+
! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<i32>) -> i64
49+
! CHECK: %[[VAL_6:.*]] = arith.constant 0 : i64
50+
! CHECK: %[[VAL_7:.*]] = arith.cmpi eq, %[[VAL_5]], %[[VAL_6]] : i64
51+
! CHECK: %[[VAL_8:.*]] = fir.if %[[VAL_7]] -> (i32) {
52+
! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
53+
! CHECK: %[[VAL_13:.*]] = fir.call @_FortranASize(%[[VAL_11]]
54+
! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i64) -> i32
55+
! CHECK: fir.result %[[VAL_14]] : i32
56+
! CHECK: } else {
57+
! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
58+
! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
59+
! CHECK: %[[VAL_20:.*]] = fir.call @_FortranASizeDim(%[[VAL_18]]
60+
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i64) -> i32
61+
! CHECK: fir.result %[[VAL_21]] : i32
62+
! CHECK: }
63+
! CHECK: %[[VAL_22:.*]]:3 = hlfir.associate %[[VAL_8]] {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
64+
! CHECK: fir.call @_QPtakes_integer(%[[VAL_22]]#1) fastmath<contract> : (!fir.ref<i32>) -> ()
65+
! CHECK: hlfir.end_associate %[[VAL_22]]#1, %[[VAL_22]]#2 : !fir.ref<i32>, i1
66+
! CHECK: return
67+
! CHECK: }
68+
69+
! CHECK-LABEL: func.func @_QPtest_size_3(
70+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>> {fir.bindc_name = "x"},
71+
! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32> {fir.bindc_name = "d", fir.optional}) {
72+
! CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope
73+
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_1]] dummy_scope %[[VAL_2]] {fortran_attrs = #fir.var_attrs<optional>, uniq_name = "_QFtest_size_3Ed"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
74+
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_2]] {uniq_name = "_QFtest_size_3Ex"} : (!fir.box<!fir.array<*:f32>>, !fir.dscope) -> (!fir.box<!fir.array<*:f32>>, !fir.box<!fir.array<*:f32>>)
75+
! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.ref<i32>) -> i64
76+
! CHECK: %[[VAL_6:.*]] = arith.constant 0 : i64
77+
! CHECK: %[[VAL_7:.*]] = arith.cmpi eq, %[[VAL_5]], %[[VAL_6]] : i64
78+
! CHECK: %[[VAL_8:.*]] = fir.if %[[VAL_7]] -> (i32) {
79+
! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_4]]#1 : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
80+
! CHECK: %[[VAL_13:.*]] = fir.call @_FortranASize(%[[VAL_11]],
81+
! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i64) -> i32
82+
! CHECK: fir.result %[[VAL_14]] : i32
83+
! CHECK: } else {
84+
! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_3]]#1 : !fir.ref<i32>
85+
! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_4]]#1 : (!fir.box<!fir.array<*:f32>>) -> !fir.box<none>
86+
! CHECK: %[[VAL_20:.*]] = fir.call @_FortranASizeDim(%[[VAL_18]]
87+
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i64) -> i32
88+
! CHECK: fir.result %[[VAL_21]] : i32
89+
! CHECK: }
90+
! CHECK: %[[VAL_22:.*]]:3 = hlfir.associate %[[VAL_8]] {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
91+
! CHECK: fir.call @_QPtakes_integer(%[[VAL_22]]#1) fastmath<contract> : (!fir.ref<i32>) -> ()
92+
! CHECK: hlfir.end_associate %[[VAL_22]]#1, %[[VAL_22]]#2 : !fir.ref<i32>, i1
93+
! CHECK: return
94+
! CHECK: }
95+
96+
! CHECK-LABEL: func.func @_QPtest_size_4(
97+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<*:f32>>>> {fir.bindc_name = "x"}) {
98+
! CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope
99+
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFtest_size_4Ex"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<*:f32>>>>, !fir.dscope) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<*:f32>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<*:f32>>>>)
100+
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<*:f32>>>>
101+
! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_3]] : (!fir.box<!fir.heap<!fir.array<*:f32>>>) -> !fir.box<none>
102+
! CHECK: %[[VAL_8:.*]] = fir.call @_FortranASize(%[[VAL_6]]
103+
! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_8]] : (i64) -> i32
104+
! CHECK: %[[VAL_10:.*]]:3 = hlfir.associate %[[VAL_9]] {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
105+
! CHECK: fir.call @_QPtakes_integer(%[[VAL_10]]#1) fastmath<contract> : (!fir.ref<i32>) -> ()
106+
! CHECK: hlfir.end_associate %[[VAL_10]]#1, %[[VAL_10]]#2 : !fir.ref<i32>, i1
107+
! CHECK: return
108+
! CHECK: }

0 commit comments

Comments
 (0)