Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / Intrinsics / size.f90
blob7e965fd65e3bc4191b8b4e29eea40316d8b39121
1 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
2 ! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
4 ! CHECK-LABEL: func @_QPsize_test() {
5 subroutine size_test()
6 real, dimension(1:10, -10:10) :: a
7 integer :: dim = 1
8 integer :: iSize
9 ! CHECK: %[[VAL_c1:.*]] = arith.constant 1 : index
10 ! CHECK: %[[VAL_c10:.*]] = arith.constant 10 : index
11 ! CHECK: %[[VAL_cneg10:.*]] = arith.constant -10 : index
12 ! CHECK: %[[VAL_c21:.*]] = arith.constant 21 : index
13 ! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.array<10x21xf32> {bindc_name = "a", uniq_name = "_QFsize_testEa"}
14 ! CHECK: %[[VAL_1:.*]] = fir.address_of(@_QFsize_testEdim) : !fir.ref<i32>
15 ! CHECK: %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "isize", uniq_name = "_QFsize_testEisize"}
16 ! CHECK: %[[VAL_c2_i64:.*]] = arith.constant 2 : i64
17 ! CHECK: %[[VAL_3:.*]] = fir.convert %[[VAL_c2_i64]] : (i64) -> index
18 ! CHECK: %[[VAL_c1_i64:.*]] = arith.constant 1 : i64
19 ! CHECK: %[[VAL_4:.*]] = fir.convert %[[VAL_c1_i64]] : (i64) -> index
20 ! CHECK: %[[VAL_c5_i64:.*]] = arith.constant 5 : i64
21 ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_c5_i64]] : (i64) -> index
22 ! CHECK: %[[VAL_neg1_i64:.*]] = arith.constant -1 : i64
23 ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_neg1_i64]] : (i64) -> index
24 ! CHECK: %[[VAL_c1_i64_0:.*]] = arith.constant 1 : i64
25 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_c1_i64_0]] : (i64) -> index
26 ! CHECK: %[[VAL_c1_i64_1:.*]] = arith.constant 1 : i64
27 ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_c1_i64_1]] : (i64) -> index
28 ! CHECK: %[[VAL_9:.*]] = fir.shape_shift %[[VAL_c1]], %[[VAL_c10]], %[[VAL_cneg10]], %[[VAL_c21]] : (index, index, index, index) -> !fir.shapeshift<2>
29 ! CHECK: %[[VAL_10:.*]] = fir.slice %[[VAL_3]], %[[VAL_5]], %[[VAL_4]], %[[VAL_6]], %[[VAL_8]], %[[VAL_7]] : (index, index, index, index, index, index) -> !fir.slice<2>
30 ! CHECK: %[[VAL_11:.*]] = fir.embox %[[VAL_0]](%[[VAL_9]]) [%[[VAL_10]]] : (!fir.ref<!fir.array<10x21xf32>>, !fir.shapeshift<2>, !fir.slice<2>) -> !fir.box<!fir.array<4x3xf32>>
31 ! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<i32>) -> i64
32 ! CHECK: %[[c0_i64:.*]] = arith.constant 0 : i64
33 ! CHECK: %[[VAL_13:.*]] = arith.cmpi eq, %[[VAL_12]], %[[c0_i64]] : i64
34 ! CHECK: %[[VAL_14:.*]] = fir.if %[[VAL_13]] -> (i64) {
35 ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_11]] : (!fir.box<!fir.array<4x3xf32>>) -> !fir.box<none>
36 ! CHECK: %[[VAL_19:.*]] = fir.call @_FortranASize(%[[VAL_17]], %{{.*}}, %{{.*}}) {{.*}}: (!fir.box<none>, !fir.ref<i8>, i32) -> i64
37 ! CHECK: fir.result %[[VAL_19]] : i64
38 ! CHECK: } else {
39 ! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
40 ! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_11]] : (!fir.box<!fir.array<4x3xf32>>) -> !fir.box<none>
41 ! CHECK: %[[VAL_20:.*]] = fir.call @_FortranASizeDim(%[[VAL_18]], %[[VAL_16]], %{{.*}}, %{{.*}}) {{.*}}: (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
42 ! CHECK: fir.result %[[VAL_20]] : i64
43 ! CHECK: }
44 ! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_14]] : (i64) -> i32
45 ! CHECK: fir.store %[[VAL_21]] to %[[VAL_2]] : !fir.ref<i32>
46 iSize = size(a(2:5, -1:1), dim, 8)
47 end subroutine size_test
49 ! CHECK-LABEL: func @_QPsize_optional_dim_1(
50 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x?xf32>> {fir.bindc_name = "array"},
51 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32> {fir.bindc_name = "dim", fir.optional},
52 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<i64> {fir.bindc_name = "isize"}) {
53 subroutine size_optional_dim_1(array, dim, iSize)
54 real, dimension(:,:) :: array
55 integer, optional :: dim
56 integer(8) :: iSize
57 iSize = size(array, dim, 8)
58 ! CHECK: %[[VAL_3:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<i32>) -> i64
59 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
60 ! CHECK: %[[VAL_5:.*]] = arith.cmpi eq, %[[VAL_3]], %[[VAL_4]] : i64
61 ! CHECK: %[[VAL_6:.*]] = fir.if %[[VAL_5]] -> (i64) {
62 ! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_0]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
63 ! CHECK: %[[VAL_11:.*]] = fir.call @_FortranASize(%[[VAL_9]], %{{.*}}, %{{.*}}) {{.*}}: (!fir.box<none>, !fir.ref<i8>, i32) -> i64
64 ! CHECK: fir.result %[[VAL_11]] : i64
65 ! CHECK: } else {
66 ! CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
67 ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_0]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
68 ! CHECK: %[[VAL_17:.*]] = fir.call @_FortranASizeDim(%[[VAL_15]], %[[VAL_12]], %{{.*}}, %{{.*}}) {{.*}}: (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
69 ! CHECK: fir.result %[[VAL_17]] : i64
70 ! CHECK: }
71 ! CHECK: fir.store %[[VAL_6]] to %[[VAL_2]] : !fir.ref<i64>
72 end subroutine
74 ! CHECK-LABEL: func @_QPsize_optional_dim_2(
75 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x?xf32>> {fir.bindc_name = "array"},
76 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.ptr<i32>>> {fir.bindc_name = "dim"},
77 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<i64> {fir.bindc_name = "isize"}) {
78 subroutine size_optional_dim_2(array, dim, iSize)
79 real, dimension(:,:) :: array
80 integer, pointer :: dim
81 integer(8) :: iSize
82 iSize = size(array, dim, 8)
83 ! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.ptr<i32>>>
84 ! CHECK: %[[VAL_4:.*]] = fir.box_addr %[[VAL_3]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
85 ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (!fir.ptr<i32>) -> i64
86 ! CHECK: %[[VAL_6:.*]] = arith.constant 0 : i64
87 ! CHECK: %[[VAL_7:.*]] = arith.cmpi eq, %[[VAL_5]], %[[VAL_6]] : i64
88 ! CHECK: %[[VAL_8:.*]] = fir.if %[[VAL_7]] -> (i64) {
89 ! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_0]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
90 ! CHECK: %[[VAL_13:.*]] = fir.call @_FortranASize(%[[VAL_11]], %{{.*}}, %{{.*}}) {{.*}}: (!fir.box<none>, !fir.ref<i8>, i32) -> i64
91 ! CHECK: fir.result %[[VAL_13]] : i64
92 ! CHECK: } else {
93 ! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_4]] : !fir.ptr<i32>
94 ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_0]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
95 ! CHECK: %[[VAL_19:.*]] = fir.call @_FortranASizeDim(%[[VAL_17]], %[[VAL_14]], %{{.*}}, %{{.*}}) {{.*}}: (!fir.box<none>, i32, !fir.ref<i8>, i32) -> i64
96 ! CHECK: fir.result %[[VAL_19]] : i64
97 ! CHECK: }
98 ! CHECK: fir.store %[[VAL_8]] to %[[VAL_2]] : !fir.ref<i64>
99 end subroutine