Revert "[InstCombine] Support gep nuw in icmp folds" (#118698)
[llvm-project.git] / flang / test / Lower / HLFIR / sum.f90
blob339582088b0321cdeb1a9491cab036dbf4a1c460
1 ! Test lowering of SUM intrinsic to HLFIR
2 ! RUN: bbc -emit-hlfir -o - %s 2>&1 | FileCheck %s
4 ! simple 1 argument SUM
5 subroutine sum1(a, s)
6 integer :: a(:), s
7 s = SUM(a)
8 end subroutine
9 ! CHECK-LABEL: func.func @_QPsum1(
10 ! CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"}, %[[ARG1:.*]]: !fir.ref<i32>
11 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
12 ! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
13 ! CHECK-NEXT: %[[EXPR:.*]] = hlfir.sum %[[ARRAY]]#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?xi32>>) -> i32
14 ! CHECK-NEXT: hlfir.assign %[[EXPR]] to %[[OUT]]#0 : i32, !fir.ref<i32>
15 ! CHECK-NEXT: return
16 ! CHECK-NEXT: }
18 ! sum with by-ref DIM argument
19 subroutine sum2(a, s, d)
20 integer :: a(:,:), s(:), d
21 s = SUM(a, d)
22 end subroutine
23 ! CHECK-LABEL: func.func @_QPsum2(
24 ! CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?x?xi32>> {fir.bindc_name = "a"}, %[[ARG1:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "s"}, %[[ARG2:.*]]: !fir.ref<i32>
25 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
26 ! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
27 ! CHECK-DAG: %[[DIM_REF:.*]]:2 = hlfir.declare %[[ARG2]]
28 ! CHECK-NEXT: %[[DIM:.*]] = fir.load %[[DIM_REF]]#0 : !fir.ref<i32>
29 ! CHECK-NEXT: %[[EXPR:.*]] = hlfir.sum %[[ARRAY]]#0 dim %[[DIM]] {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x?xi32>>, i32) -> !hlfir.expr<?xi32>
30 ! CHECK-NEXT: hlfir.assign %[[EXPR]] to %[[OUT]]#0 : !hlfir.expr<?xi32>, !fir.box<!fir.array<?xi32>>
31 ! CHECK-NEXT: hlfir.destroy %[[EXPR]]
32 ! CHECK-NEXT: return
33 ! CHECK-NEXT: }
35 ! sum with scalar mask argument
36 subroutine sum3(a, s, m)
37 integer :: a(:), s
38 logical :: m
39 s = SUM(a, m)
40 end subroutine
41 ! CHECK-LABEL: func.func @_QPsum3(
42 ! CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"}, %[[ARG1:.*]]: !fir.ref<i32> {fir.bindc_name = "s"}, %[[ARG2:.*]]: !fir.ref<!fir.logical<4>>
43 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
44 ! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
45 ! CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG2]]
46 ! CHECK-NEXT: %[[EXPR:.*]] = hlfir.sum %[[ARRAY]]#0 mask %[[MASK]]#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.logical<4>>) -> i32
47 ! CHECK-NEXT: hlfir.assign %[[EXPR]] to %[[OUT]]#0 : i32, !fir.ref<i32>
48 ! CHECK-NEXT: return
49 ! CHECK-NEXT: }
51 ! sum with array mask argument
52 subroutine sum4(a, s, m)
53 integer :: a(:), s
54 logical :: m(:)
55 s = SUM(a, m)
56 end subroutine
57 ! CHECK-LABEL: func.func @_QPsum4(
58 ! CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"}, %[[ARG1:.*]]: !fir.ref<i32> {fir.bindc_name = "s"}, %[[ARG2:.*]]: !fir.box<!fir.array<?x!fir.logical<4>>>
59 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
60 ! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
61 ! CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG2]]
62 ! CHECK-NEXT: %[[EXPR:.*]] = hlfir.sum %[[ARRAY]]#0 mask %[[MASK]]#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?x!fir.logical<4>>>) -> i32
63 ! CHECK-NEXT: hlfir.assign %[[EXPR]] to %[[OUT]]#0 : i32, !fir.ref<i32>
64 ! CHECK-NEXT: return
65 ! CHECK-NEXT: }
67 ! sum with all 3 arguments, dim is by-val, array isn't boxed
68 subroutine sum5(s)
69 integer :: s(2)
70 integer :: a(2,2) = reshape((/1, 2, 3, 4/), [2,2])
71 s = sum(a, 1, .true.)
72 end subroutine
73 ! CHECK-LABEL: func.func @_QPsum5
74 ! CHECK: %[[ARG0:.*]]: !fir.ref<!fir.array<2xi32>>
75 ! CHECK-DAG: %[[ADDR:.*]] = fir.address_of({{.*}}) : !fir.ref<!fir.array<2x2xi32>>
76 ! CHECK-DAG: %[[ARRAY_SHAPE:.*]] = fir.shape {{.*}} -> !fir.shape<2>
77 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ADDR]](%[[ARRAY_SHAPE]])
78 ! CHECK-DAG: %[[OUT_SHAPE:.*]] = fir.shape {{.*}} -> !fir.shape<1>
79 ! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG0]](%[[OUT_SHAPE]])
80 ! CHECK-DAG: %[[TRUE:.*]] = arith.constant true
81 ! CHECK-DAG: %[[C1:.*]] = arith.constant 1 : i32
82 ! CHECK-NEXT: %[[EXPR:.*]] = hlfir.sum %[[ARRAY]]#0 dim %[[C1]] mask %[[TRUE]] {fastmath = #arith.fastmath<contract>} : (!fir.ref<!fir.array<2x2xi32>>, i32, i1) -> !hlfir.expr<2xi32>
83 ! CHECK-NEXT: hlfir.assign %[[EXPR]] to %[[OUT]]#0 : !hlfir.expr<2xi32>, !fir.ref<!fir.array<2xi32>>
84 ! CHECK-NEXT: hlfir.destroy %[[EXPR]] : !hlfir.expr<2xi32>
85 ! CHECK-NEXT: return
86 ! CHECK-nEXT: }
88 subroutine sum6(a, s, d)
89 integer, pointer :: d
90 real :: a(:,:), s(:)
91 s = sum(a, (d))
92 end subroutine
93 ! CHECK-LABEL: func.func @_QPsum6(
94 ! CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?x?xf32>>
95 ! CHECK: %[[ARG1:.*]]: !fir.box<!fir.array<?xf32>>
96 ! CHECK: %[[ARG2:.*]]: !fir.ref<!fir.box<!fir.ptr<i32>>>
97 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
98 ! CHECK-DAG: %[[OUT:.*]]:2 = hlfir.declare %[[ARG1]]
99 ! CHECK-DAG: %[[DIM_VAR:.*]]:2 = hlfir.declare %[[ARG2]]
100 ! CHECK-NEXT: %[[DIM_BOX:.*]] = fir.load %[[DIM_VAR]]#0 : !fir.ref<!fir.box<!fir.ptr<i32>>>
101 ! CHECK-NEXT: %[[DIM_ADDR:.*]] = fir.box_addr %[[DIM_BOX]] : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
102 ! CHECK-NEXT: %[[DIM0:.*]] = fir.load %[[DIM_ADDR]] : !fir.ptr<i32>
103 ! CHECK-NEXT: %[[DIM1:.*]] = hlfir.no_reassoc %[[DIM0]] : i32
104 ! CHECK-NEXT: %[[EXPR:.*]] = hlfir.sum %[[ARRAY]]#0 dim %[[DIM1]] {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x?xf32>>, i32) -> !hlfir.expr<?xf32>
105 ! CHECK-NEXT: hlfir.assign %[[EXPR]] to %[[OUT]]#0 : !hlfir.expr<?xf32>, !fir.box<!fir.array<?xf32>>
106 ! CHECK-NEXT: hlfir.destroy %[[EXPR]]
107 ! CHECK-NEXT: return
108 ! CHECK-NEXT: }
110 subroutine testDynamicallyOptionalMask(array, mask, res)
111 integer :: array(:), res
112 logical, allocatable :: mask(:)
113 res = SUM(array, mask=mask)
114 end subroutine
115 ! CHECK-LABEL: func.func @_QPtestdynamicallyoptionalmask(
116 ! CHECK-SAME: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>>
117 ! CHECK-SAME: %[[ARG1:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.logical<4>>>>>
118 ! CHECK-SAME: %[[ARG2:.*]]: !fir.ref<i32>
119 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
120 ! CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG1]]
121 ! CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG2]]
122 ! CHECK-NEXT: %[[MASK_LOAD:.*]] = fir.load %[[MASK]]#1
123 ! CHECK-NEXT: %[[MASK_ADDR:.*]] = fir.box_addr %[[MASK_LOAD]]
124 ! CHECK-NEXT: %[[MASK_ADDR_INT:.*]] = fir.convert %[[MASK_ADDR]]
125 ! CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : i64
126 ! CHECK-NEXT: %[[CMP:.*]] = arith.cmpi ne, %[[MASK_ADDR_INT]], %[[C0]] : i64
127 ! it is a shame there is a second load here. The first is generated for
128 ! PreparedActualArgument::isPresent, the second is for optional handling
129 ! CHECK-NEXT: %[[MASK_LOAD2:.*]] = fir.load %[[MASK]]#1
130 ! CHECK-NEXT: %[[ABSENT:.*]] = fir.absent !fir.box<!fir.heap<!fir.array<?x!fir.logical<4>>>>
131 ! CHECK-NEXT: %[[SELECT:.*]] = arith.select %[[CMP]], %[[MASK_LOAD2]], %[[ABSENT]]
132 ! CHECK-NEXT: %[[SUM:.*]] = hlfir.sum %[[ARRAY]]#0 mask %[[SELECT]]
133 ! CHECK-NEXT: hlfir.assign %[[SUM]] to %[[RES]]#0
134 ! CHECK-NEXT: return
135 ! CHECK-NEXT: }
137 subroutine testAllocatableArray(array, mask, res)
138 integer, allocatable :: array(:)
139 integer :: res
140 logical :: mask(:)
141 res = SUM(array, mask=mask)
142 end subroutine
143 ! CHECK-LABEL: func.func @_QPtestallocatablearray(
144 ! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
145 ! CHECK-SAME: %[[ARG1:.*]]: !fir.box<!fir.array<?x!fir.logical<4>>>
146 ! CHECK-SAME: %[[ARG2:.*]]: !fir.ref<i32>
147 ! CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
148 ! CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG1]]
149 ! CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG2]]
150 ! CHECK-NEXT: %[[LOADED_ARRAY:.*]] = fir.load %[[ARRAY]]#0
151 ! CHECK-NEXT: %[[SUM:.*]] = hlfir.sum %[[LOADED_ARRAY]] mask %[[MASK]]#0
152 ! CHECK-NEXT: hlfir.assign %[[SUM]] to %[[RES]]#0
153 ! CHECK-NEXT: return
154 ! CHECK-NEXT: }
156 function testOptionalScalar(array, mask)
157 integer :: array(:)
158 logical, optional :: mask
159 integer :: testOptionalScalar
160 testOptionalScalar = sum(array, mask)
161 end function
162 ! CHECK-LABEL: func.func @_QPtestoptionalscalar(
163 ! CHECK-SAME: %[[ARRAY_ARG:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "array"},
164 ! CHECK-SAME: %[[MASK_ARG:.*]]: !fir.ref<!fir.logical<4>> {fir.bindc_name = "mask", fir.optional}) -> i32
165 ! CHECK: %[[ARRAY_VAR:.*]]:2 = hlfir.declare %[[ARRAY_ARG]]
166 ! CHECK: %[[MASK_VAR:.*]]:2 = hlfir.declare %[[MASK_ARG]]
167 ! CHECK: %[[RET_ALLOC:.*]] = fir.alloca i32 {bindc_name = "testoptionalscalar", uniq_name = "_QFtestoptionalscalarEtestoptionalscalar"}
168 ! CHECK: %[[RET_VAR:.*]]:2 = hlfir.declare %[[RET_ALLOC]]
169 ! CHECK: %[[MASK_IS_PRESENT:.*]] = fir.is_present %[[MASK_VAR]]#0 : (!fir.ref<!fir.logical<4>>) -> i1
170 ! CHECK: %[[MASK_BOX:.*]] = fir.embox %[[MASK_VAR]]#1
171 ! CHECK: %[[ABSENT:.*]] = fir.absent !fir.box<!fir.logical<4>>
172 ! CHECK: %[[MASK_SELECT:.*]] = arith.select %[[MASK_IS_PRESENT]], %[[MASK_BOX]], %[[ABSENT]]
173 ! CHECK: %[[RES:.*]] = hlfir.sum %[[ARRAY_VAR]]#0 mask %[[MASK_SELECT]] {{.*}}: (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.logical<4>>) -> i32
174 ! CHECK: hlfir.assign %[[RES]] to %[[RET_VAR]]#0
175 ! CHECK: %[[RET:.*]] = fir.load %[[RET_VAR]]#1 : !fir.ref<i32>
176 ! CHECK: return %[[RET]] : i32
177 ! CHECK: }