1 // Test hlfir.minval operation lowering to fir runtime call
2 // RUN: fir-opt %s -lower-hlfir-intrinsics | FileCheck %s
4 // simple one argument minval
5 func.func @_QPminval1(%arg0: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"}, %arg1: !fir.ref<i32> {fir.bindc_name = "s"}) {
6 %0:2 = hlfir.declare %arg0 {uniq_name = "_QFminval1Ea"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
7 %1:2 = hlfir.declare %arg1 {uniq_name = "_QFminval1Es"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
8 %2 = hlfir.minval %0#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?xi32>>) -> i32
9 hlfir.assign %2 to %1#0 : i32, !fir.ref<i32>
12 // CHECK-LABEL: func.func @_QPminval1(
13 // CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>>
14 // CHECK: %[[ARG1:.*]]: !fir.ref<i32>
15 // CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
16 // CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG1]]
17 // CHECK-DAG: %[[MASK:.*]] = fir.absent !fir.box<i1>
18 // CHECK-DAG: %[[ARRAY_ARG:.*]] = fir.convert %[[ARRAY]]#1 : (!fir.box<!fir.array<?xi32>>) -> !fir.box<none>
19 // CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK]] : (!fir.box<i1>) -> !fir.box<none>
20 // CHECK: %[[RET:.*]] = fir.call @_FortranAMinvalInteger4(%[[ARRAY_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]], %[[INT:.*]], %[[MASK_ARG]]) fastmath<contract> : (!fir.box<none>, !fir.ref<i8>, i32, i32, !fir.box<none>) -> i32
21 // CHECK-NEXT: hlfir.assign %[[RET]] to %[[RES]]#0 : i32, !fir.ref<i32>
25 // minval with a by-ref dimension of index type
26 func.func @_QPminval2(%arg0: !fir.box<!fir.array<?x?xi32>> {fir.bindc_name = "a"}, %arg1: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "s"}, %arg2: !fir.ref<index> {fir.bindc_name = "d"}) {
27 %0:2 = hlfir.declare %arg0 {uniq_name = "_QFminval2Ea"} : (!fir.box<!fir.array<?x?xi32>>) -> (!fir.box<!fir.array<?x?xi32>>, !fir.box<!fir.array<?x?xi32>>)
28 %1:2 = hlfir.declare %arg2 {uniq_name = "_QFminval2Ed"} : (!fir.ref<index>) -> (!fir.ref<index>, !fir.ref<index>)
29 %2:2 = hlfir.declare %arg1 {uniq_name = "_QFminval2Es"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
30 %3 = fir.load %1#0 : !fir.ref<index>
31 %4 = hlfir.minval %0#0 dim %3#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x?xi32>>, index) -> !hlfir.expr<?xi32>
32 hlfir.assign %4 to %2#0 : !hlfir.expr<?xi32>, !fir.box<!fir.array<?xi32>>
33 hlfir.destroy %4 : !hlfir.expr<?xi32>
36 // CHECK-LABEL: func.func @_QPminval2(
37 // CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?x?xi32>>
38 // CHECK: %[[ARG1:.*]]: !fir.box<!fir.array<?xi32>>
39 // CHECK: %[[ARG2:.*]]: !fir.ref<index>
40 // CHECK-DAG: %[[TRUE:.*]] = arith.constant true
41 // CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
42 // CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG1]]
43 // CHECK-DAG: %[[DIM_VAR:.*]]:2 = hlfir.declare %[[ARG2]]
45 // CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>>
46 // CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
47 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
48 // CHECK-DAG: %[[RET_SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
49 // CHECK-DAG: %[[RET_EMBOX:.*]] = fir.embox %[[RET_ADDR]](%[[RET_SHAPE]])
50 // CHECK-DAG: fir.store %[[RET_EMBOX]] to %[[RET_BOX]]
52 // CHECK-DAG: %[[MASK:.*]] = fir.absent !fir.box<i1>
53 // CHECK-DAG: %[[DIM_IDX:.*]] = fir.load %[[DIM_VAR]]#0 : !fir.ref<index>
54 // CHECK-DAG: %[[DIM:.*]] = fir.convert %[[DIM_IDX]] : (index) -> i32
56 // CHECK-DAG: %[[RET_ARG:.*]] = fir.convert %[[RET_BOX]]
57 // CHECK-DAG: %[[ARRAY_ARG:.*]] = fir.convert %[[ARRAY]]#1 : (!fir.box<!fir.array<?x?xi32>>) -> !fir.box<none>
58 // CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK]] : (!fir.box<i1>) -> !fir.box<none>
59 // CHECK: fir.call @_FortranAMinvalDim(%[[RET_ARG]], %[[ARRAY_ARG]], %[[DIM]], %[[LOC_STR:.*]], %[[LOC_N:.*]], %[[MASK_ARG]]) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, !fir.ref<i8>, i32, !fir.box<none>) -> ()
61 // CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
62 // CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[RET]]
63 // CHECK-NEXT: %[[ADDR:.*]] = fir.box_addr %[[RET]]
64 // CHECK-NEXT: %[[SHIFT:.*]] = fir.shape_shift %[[BOX_DIMS]]#0, %[[BOX_DIMS]]#1
65 // TODO: fix alias analysis in hlfir.assign bufferization
66 // CHECK-NEXT: %[[TMP:.*]]:2 = hlfir.declare %[[ADDR]](%[[SHIFT]]) {uniq_name = ".tmp.intrinsic_result"}
67 // CHECK: %[[ASEXPR:.*]] = hlfir.as_expr %[[TMP]]#0 move %[[TRUE]] : (!fir.box<!fir.array<?xi32>>, i1) -> !hlfir.expr<?xi32>
68 // CHECK: hlfir.assign %[[ASEXPR]] to %[[RES]]#0
69 // CHECK: hlfir.destroy %[[ASEXPR]]
73 // minval with scalar mask
74 func.func @_QPminval3(%arg0: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"}, %arg1: !fir.ref<i32> {fir.bindc_name = "s"}, %arg2: !fir.ref<!fir.logical<4>> {fir.bindc_name = "m"}) {
75 %0:2 = hlfir.declare %arg0 {uniq_name = "_QFminval3Ea"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
76 %1:2 = hlfir.declare %arg2 {uniq_name = "_QFminval3Em"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
77 %2:2 = hlfir.declare %arg1 {uniq_name = "_QFminval3Es"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
78 %3 = hlfir.minval %0#0 mask %1#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.logical<4>>) -> i32
79 hlfir.assign %3 to %2#0 : i32, !fir.ref<i32>
82 // CHECK-LABEL: func.func @_QPminval3(
83 // CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>>
84 // CHECK: %[[ARG1:.*]]: !fir.ref<i32>
85 // CHECK: %[[ARG2:.*]]: !fir.ref<!fir.logical<4>>
86 // CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
87 // CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG1]]
88 // CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG2]]
89 // CHECK-DAG: %[[MASK_BOX:.*]] = fir.embox %[[MASK]]#1 : (!fir.ref<!fir.logical<4>>) -> !fir.box<!fir.logical<4>>
90 // CHECK-DAG: %[[ARRAY_ARG:.*]] = fir.convert %[[ARRAY]]#1 : (!fir.box<!fir.array<?xi32>>) -> !fir.box<none>
91 // CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK_BOX]] : (!fir.box<!fir.logical<4>>) -> !fir.box<none>
92 // CHECK: %[[RET:.*]] = fir.call @_FortranAMinvalInteger4(%[[ARRAY_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]], %[[INT:.*]], %[[MASK_ARG]]) fastmath<contract> : (!fir.box<none>, !fir.ref<i8>, i32, i32, !fir.box<none>) -> i32
93 // CHECK-NEXT: hlfir.assign %[[RET]] to %[[RES]]#0 : i32, !fir.ref<i32>
97 // minval with array mask
98 func.func @_QPminval4(%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>>> {fir.bindc_name = "m"}) {
99 %0:2 = hlfir.declare %arg0 {uniq_name = "_QFminval4Ea"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
100 %1:2 = hlfir.declare %arg2 {uniq_name = "_QFminval4Em"} : (!fir.box<!fir.array<?x!fir.logical<4>>>) -> (!fir.box<!fir.array<?x!fir.logical<4>>>, !fir.box<!fir.array<?x!fir.logical<4>>>)
101 %2:2 = hlfir.declare %arg1 {uniq_name = "_QFminval4Es"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
102 %3 = hlfir.minval %0#0 mask %1#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?x!fir.logical<4>>>) -> i32
103 hlfir.assign %3 to %2#0 : i32, !fir.ref<i32>
106 // CHECK-LABEL: func.func @_QPminval4(
107 // CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?xi32>>
108 // CHECK: %[[ARG1:.*]]: !fir.ref<i32>
109 // CHECK: %[[ARG2:.*]]: !fir.box<!fir.array<?x!fir.logical<4>>>
110 // CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
111 // CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[ARG1]]
112 // CHECK-DAG: %[[MASK:.*]]:2 = hlfir.declare %[[ARG2]]
113 // CHECK-DAG: %[[ARRAY_ARG:.*]] = fir.convert %[[ARRAY]]#1 : (!fir.box<!fir.array<?xi32>>) -> !fir.box<none>
114 // CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK]]#1 : (!fir.box<!fir.array<?x!fir.logical<4>>>) -> !fir.box<none>
115 // CHECK: %[[RET:.*]] = fir.call @_FortranAMinvalInteger4(%[[ARRAY_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]], %[[INT:.*]], %[[MASK_ARG]]) fastmath<contract> : (!fir.box<none>, !fir.ref<i8>, i32, i32, !fir.box<none>) -> i32
116 // CHECK-NEXT: hlfir.assign %[[RET]] to %[[RES]]#0 : i32, !fir.ref<i32>
117 // CHECK-NEXT: return
120 fir.global internal @_QFminval5Ea : !fir.array<2x2xi32> {
121 %0 = fir.undefined !fir.array<2x2xi32>
122 %c1_i32 = arith.constant 1 : i32
123 %1 = fir.insert_value %0, %c1_i32, [0 : index, 0 : index] : (!fir.array<2x2xi32>, i32) -> !fir.array<2x2xi32>
124 %c2_i32 = arith.constant 2 : i32
125 %2 = fir.insert_value %1, %c2_i32, [1 : index, 0 : index] : (!fir.array<2x2xi32>, i32) -> !fir.array<2x2xi32>
126 %c3_i32 = arith.constant 3 : i32
127 %3 = fir.insert_value %2, %c3_i32, [0 : index, 1 : index] : (!fir.array<2x2xi32>, i32) -> !fir.array<2x2xi32>
128 %c4_i32 = arith.constant 4 : i32
129 %4 = fir.insert_value %3, %c4_i32, [1 : index, 1 : index] : (!fir.array<2x2xi32>, i32) -> !fir.array<2x2xi32>
130 %c2 = arith.constant 2 : index
131 %c2_0 = arith.constant 2 : index
132 fir.has_value %4 : !fir.array<2x2xi32>
135 // 3 argument minval, using local variables
136 func.func @_QPminval5(%arg0: !fir.ref<!fir.array<2xi32>> {fir.bindc_name = "s"}) {
137 %0 = fir.address_of(@_QFminval5Ea) : !fir.ref<!fir.array<2x2xi32>>
138 %c2 = arith.constant 2 : index
139 %c2_0 = arith.constant 2 : index
140 %1 = fir.shape %c2, %c2_0 : (index, index) -> !fir.shape<2>
141 %2:2 = hlfir.declare %0(%1) {uniq_name = "_QFminval5Ea"} : (!fir.ref<!fir.array<2x2xi32>>, !fir.shape<2>) -> (!fir.ref<!fir.array<2x2xi32>>, !fir.ref<!fir.array<2x2xi32>>)
142 %c2_1 = arith.constant 2 : index
143 %3 = fir.shape %c2_1 : (index) -> !fir.shape<1>
144 %4:2 = hlfir.declare %arg0(%3) {uniq_name = "_QFminval5Es"} : (!fir.ref<!fir.array<2xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<2xi32>>, !fir.ref<!fir.array<2xi32>>)
145 %c1_i32 = arith.constant 1 : i32
146 %true = arith.constant true
147 %5 = hlfir.minval %2#0 dim %c1_i32 mask %true {fastmath = #arith.fastmath<contract>} : (!fir.ref<!fir.array<2x2xi32>>, i32, i1) -> !hlfir.expr<2xi32>
148 hlfir.assign %5 to %4#0 : !hlfir.expr<2xi32>, !fir.ref<!fir.array<2xi32>>
149 hlfir.destroy %5 : !hlfir.expr<2xi32>
152 // CHECK-LABEL: func.func @_QPminval5(
153 // CHECK: %[[ARG0:.*]]: !fir.ref<!fir.array<2xi32>>
154 // CHECK-DAG: %[[TRUE:.*]] = arith.constant true
155 // CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>>
156 // CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
157 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
158 // CHECK-DAG: %[[RET_SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
159 // CHECK-DAG: %[[RET_EMBOX:.*]] = fir.embox %[[RET_ADDR]](%[[RET_SHAPE]])
160 // CHECK-DAG: fir.store %[[RET_EMBOX]] to %[[RET_BOX]]
162 // CHECK-DAG: %[[RES_VAR:.*]] = hlfir.declare %[[ARG0]](%[[RES_SHAPE:.*]])
164 // CHECK-DAG: %[[MASK_ALLOC:.*]] = fir.alloca !fir.logical<4>
165 // CHECK-DAG: %[[MASK_VAL:.*]] = fir.convert %[[TRUE]] : (i1) -> !fir.logical<4>
166 // CHECK-DAG: fir.store %[[MASK_VAL]] to %[[MASK_ALLOC]] : !fir.ref<!fir.logical<4>>
167 // CHECK-DAG: %[[MASK_BOX:.*]] = fir.embox %[[MASK_ALLOC]]
169 // CHECK-DAG: %[[ARRAY_ADDR:.*]] = fir.address_of
170 // CHECK-DAG: %[[ARRAY_VAR:.*]]:2 = hlfir.declare %[[ARRAY_ADDR]](%[[ARRAY_SHAPE:.*]])
171 // CHECK-DAG: %[[ARRAY_BOX:.*]] = fir.embox %[[ARRAY_VAR]]#1(%[[ARRAY_SHAPE:.*]])
173 // CHECK-DAG: %[[DIM:.*]] = arith.constant 1 : i32
175 // CHECK-DAG: %[[RET_ARG:.*]] = fir.convert %[[RET_BOX]]
176 // CHECK-DAG: %[[ARRAY_ARG:.*]] = fir.convert %[[ARRAY_BOX]] : (!fir.box<!fir.array<2x2xi32>>) -> !fir.box<none>
177 // CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK_BOX]] : (!fir.box<!fir.logical<4>>) -> !fir.box<none>
178 // CHECK: fir.call @_FortranAMinvalDim(%[[RET_ARG]], %[[ARRAY_ARG]], %[[DIM]], %[[LOC_STR:.*]], %[[LOC_N:.*]], %[[MASK_ARG]]) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, !fir.ref<i8>, i32, !fir.box<none>) -> ()
180 // simple one argument minval for character
181 func.func @_QPminval6(%arg0: !fir.box<!fir.array<?x!fir.char<1,?>>> {fir.bindc_name = "a"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "s"}) {
182 %0:2 = hlfir.declare %arg0 {uniq_name = "_QFminval6Ea"} : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> (!fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.box<!fir.array<?x!fir.char<1,?>>>)
183 %1:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
184 %2:2 = hlfir.declare %1#0 typeparams %1#1 {uniq_name = "_QFminval6Es"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
185 %3 = hlfir.minval %0#0 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> !hlfir.expr<!fir.char<1,?>>
186 hlfir.assign %3 to %2#0 : !hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>
187 hlfir.destroy %3 : !hlfir.expr<!fir.char<1,?>>
190 // CHECK-LABEL: func.func @_QPminval6(
191 // CHECK: %[[ARG0:.*]]: !fir.box<!fir.array<?x!fir.char<1,?>>>
192 // CHECK: %[[ARG1:.*]]: !fir.boxchar<1>
193 // CHECK-DAG: %[[TRUE:.*]] = arith.constant true
194 // CHECK-DAG: %[[ARRAY:.*]]:2 = hlfir.declare %[[ARG0]]
195 // CHECK-DAG: %[[UNBOXED:.*]]:2 = fir.unboxchar %[[ARG1]]
196 // CHECK-DAG: %[[RES:.*]]:2 = hlfir.declare %[[UNBOXED]]#0 typeparams %[[UNBOXED]]#1
198 // CHECK-DAG: %[[MASK:.*]] = fir.absent !fir.box<i1>
199 // CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
200 // CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.char<1,?>>
201 // CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
202 // CHECK-DAG: %[[RET_EMBOX:.*]] = fir.embox %[[RET_ADDR]] typeparams %[[C0]]
203 // CHECK-DAG: fir.store %[[RET_EMBOX]] to %[[RET_BOX]]
205 // CHECK-DAG: %[[RET_ARG:.*]] = fir.convert %[[RET_BOX]]
206 // CHECK-DAG: %[[ARRAY_ARG:.*]] = fir.convert %[[ARRAY]]#1 : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> !fir.box<none>
207 // CHECK-DAG: %[[MASK_ARG:.*]] = fir.convert %[[MASK]] : (!fir.box<i1>) -> !fir.box<none>
208 // CHECK: fir.call @_FortranAMinvalCharacter(%[[RET_ARG]], %[[ARRAY_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]], %[[MASK_ARG]]) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32, !fir.box<none>) -> ()
210 // CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
211 // CHECK: %[[BOX_ELESIZE:.*]] = fir.box_elesize %[[RET]]
212 // CHECK-NEXT: %[[ADDR:.*]] = fir.box_addr %[[RET]]
213 // CHECK-NEXT: %[[TMP:.*]]:2 = hlfir.declare %[[ADDR]] typeparams %[[BOX_ELESIZE]] {uniq_name = ".tmp.intrinsic_result"}
214 // CHECK: %[[ASEXPR:.*]] = hlfir.as_expr %[[TMP]]#0 move %[[TRUE]] : (!fir.boxchar<1>, i1) -> !hlfir.expr<!fir.char<1,?>>
215 // CHECK: hlfir.assign %[[ASEXPR]] to %[[RES]]#0
216 // CHECK: hlfir.destroy %[[ASEXPR]]
217 // CHECK-NEXT: return