Debian Codegraph @

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

raw
1 //===-- Reduction.cpp -- generate reduction intrinsics runtime calls- -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "flang/Optimizer/Builder/Runtime/Reduction.h"
10 #include "flang/Optimizer/Builder/BoxValue.h"
11 #include "flang/Optimizer/Builder/Character.h"
12 #include "flang/Optimizer/Builder/FIRBuilder.h"
13 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
14 #include "flang/Optimizer/Support/Utils.h"
15 #include "flang/Runtime/reduce.h"
16 #include "flang/Runtime/reduction.h"
17 #include "mlir/Dialect/Func/IR/FuncOps.h"
18
19 using namespace Fortran::runtime;
20
21 #define STRINGIFY(S) #S
22 #define JOIN2(A, B) A##B
23 #define JOIN3(A, B, C) A##B##C
24
25 /// Placeholder for real*10 version of Maxval Intrinsic
26 struct ForcedMaxvalReal10 {
27 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MaxvalReal10));
28 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
29 return [](mlir::MLIRContext *ctx) {
30 auto ty = mlir::FloatType::getF80(ctx);
31 auto boxTy =
32 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
33 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
34 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
35 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
36 {ty});
37 };
38 }
39 };
40
41 /// Placeholder for real*16 version of Maxval Intrinsic
42 struct ForcedMaxvalReal16 {
43 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MaxvalReal16));
44 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
45 return [](mlir::MLIRContext *ctx) {
46 auto ty = mlir::FloatType::getF128(ctx);
47 auto boxTy =
48 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
49 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
50 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
51 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
52 {ty});
53 };
54 }
55 };
56
57 /// Placeholder for integer*16 version of Maxval Intrinsic
58 struct ForcedMaxvalInteger16 {
59 static constexpr const char *name =
60 ExpandAndQuoteKey(RTNAME(MaxvalInteger16));
61 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
62 return [](mlir::MLIRContext *ctx) {
63 auto ty = mlir::IntegerType::get(ctx, 128);
64 auto boxTy =
65 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
66 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
67 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
68 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
69 {ty});
70 };
71 }
72 };
73
74 /// Placeholder for real*10 version of Minval Intrinsic
75 struct ForcedMinvalReal10 {
76 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MinvalReal10));
77 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
78 return [](mlir::MLIRContext *ctx) {
79 auto ty = mlir::FloatType::getF80(ctx);
80 auto boxTy =
81 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
82 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
83 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
84 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
85 {ty});
86 };
87 }
88 };
89
90 /// Placeholder for real*16 version of Minval Intrinsic
91 struct ForcedMinvalReal16 {
92 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MinvalReal16));
93 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
94 return [](mlir::MLIRContext *ctx) {
95 auto ty = mlir::FloatType::getF128(ctx);
96 auto boxTy =
97 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
98 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
99 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
100 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
101 {ty});
102 };
103 }
104 };
105
106 /// Placeholder for integer*16 version of Minval Intrinsic
107 struct ForcedMinvalInteger16 {
108 static constexpr const char *name =
109 ExpandAndQuoteKey(RTNAME(MinvalInteger16));
110 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
111 return [](mlir::MLIRContext *ctx) {
112 auto ty = mlir::IntegerType::get(ctx, 128);
113 auto boxTy =
114 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
115 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
116 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
117 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
118 {ty});
119 };
120 }
121 };
122
123 /// Placeholder for real*10 version of Norm2 Intrinsic
124 struct ForcedNorm2Real10 {
125 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Norm2_10));
126 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
127 return [](mlir::MLIRContext *ctx) {
128 auto ty = mlir::FloatType::getF80(ctx);
129 auto boxTy =
130 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
131 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
132 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
133 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy}, {ty});
134 };
135 }
136 };
137
138 /// Placeholder for real*16 version of Norm2 Intrinsic
139 struct ForcedNorm2Real16 {
140 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Norm2_16));
141 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
142 return [](mlir::MLIRContext *ctx) {
143 auto ty = mlir::FloatType::getF128(ctx);
144 auto boxTy =
145 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
146 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
147 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
148 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy}, {ty});
149 };
150 }
151 };
152
153 /// Placeholder for real*16 version of Norm2Dim Intrinsic
154 struct ForcedNorm2DimReal16 {
155 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Norm2DimReal16));
156 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
157 return [](mlir::MLIRContext *ctx) {
158 auto boxTy =
159 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
160 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
161 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
162 return mlir::FunctionType::get(
163 ctx, {fir::ReferenceType::get(boxTy), boxTy, intTy, strTy, intTy},
164 mlir::NoneType::get(ctx));
165 };
166 }
167 };
168
169 /// Placeholder for real*10 version of Product Intrinsic
170 struct ForcedProductReal10 {
171 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(ProductReal10));
172 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
173 return [](mlir::MLIRContext *ctx) {
174 auto ty = mlir::FloatType::getF80(ctx);
175 auto boxTy =
176 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
177 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
178 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
179 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
180 {ty});
181 };
182 }
183 };
184
185 /// Placeholder for real*16 version of Product Intrinsic
186 struct ForcedProductReal16 {
187 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(ProductReal16));
188 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
189 return [](mlir::MLIRContext *ctx) {
190 auto ty = mlir::FloatType::getF128(ctx);
191 auto boxTy =
192 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
193 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
194 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
195 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
196 {ty});
197 };
198 }
199 };
200
201 /// Placeholder for integer*16 version of Product Intrinsic
202 struct ForcedProductInteger16 {
203 static constexpr const char *name =
204 ExpandAndQuoteKey(RTNAME(ProductInteger16));
205 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
206 return [](mlir::MLIRContext *ctx) {
207 auto ty = mlir::IntegerType::get(ctx, 128);
208 auto boxTy =
209 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
210 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
211 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
212 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
213 {ty});
214 };
215 }
216 };
217
218 /// Placeholder for complex(10) version of Product Intrinsic
219 struct ForcedProductComplex10 {
220 static constexpr const char *name =
221 ExpandAndQuoteKey(RTNAME(CppProductComplex10));
222 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
223 return [](mlir::MLIRContext *ctx) {
224 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
225 auto boxTy =
226 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
227 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
228 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
229 auto resTy = fir::ReferenceType::get(ty);
230 return mlir::FunctionType::get(
231 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
232 };
233 }
234 };
235
236 /// Placeholder for complex(16) version of Product Intrinsic
237 struct ForcedProductComplex16 {
238 static constexpr const char *name =
239 ExpandAndQuoteKey(RTNAME(CppProductComplex16));
240 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
241 return [](mlir::MLIRContext *ctx) {
242 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
243 auto boxTy =
244 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
245 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
246 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
247 auto resTy = fir::ReferenceType::get(ty);
248 return mlir::FunctionType::get(
249 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
250 };
251 }
252 };
253
254 /// Placeholder for real*10 version of DotProduct Intrinsic
255 struct ForcedDotProductReal10 {
256 static constexpr const char *name =
257 ExpandAndQuoteKey(RTNAME(DotProductReal10));
258 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
259 return [](mlir::MLIRContext *ctx) {
260 auto ty = mlir::FloatType::getF80(ctx);
261 auto boxTy =
262 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
263 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
264 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
265 return mlir::FunctionType::get(ctx, {boxTy, boxTy, strTy, intTy}, {ty});
266 };
267 }
268 };
269
270 /// Placeholder for real*16 version of DotProduct Intrinsic
271 struct ForcedDotProductReal16 {
272 static constexpr const char *name =
273 ExpandAndQuoteKey(RTNAME(DotProductReal16));
274 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
275 return [](mlir::MLIRContext *ctx) {
276 auto ty = mlir::FloatType::getF128(ctx);
277 auto boxTy =
278 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
279 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
280 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
281 return mlir::FunctionType::get(ctx, {boxTy, boxTy, strTy, intTy}, {ty});
282 };
283 }
284 };
285
286 /// Placeholder for complex(10) version of DotProduct Intrinsic
287 struct ForcedDotProductComplex10 {
288 static constexpr const char *name =
289 ExpandAndQuoteKey(RTNAME(CppDotProductComplex10));
290 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
291 return [](mlir::MLIRContext *ctx) {
292 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
293 auto boxTy =
294 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
295 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
296 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
297 auto resTy = fir::ReferenceType::get(ty);
298 return mlir::FunctionType::get(ctx, {resTy, boxTy, boxTy, strTy, intTy},
299 {});
300 };
301 }
302 };
303
304 /// Placeholder for complex(16) version of DotProduct Intrinsic
305 struct ForcedDotProductComplex16 {
306 static constexpr const char *name =
307 ExpandAndQuoteKey(RTNAME(CppDotProductComplex16));
308 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
309 return [](mlir::MLIRContext *ctx) {
310 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
311 auto boxTy =
312 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
313 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
314 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
315 auto resTy = fir::ReferenceType::get(ty);
316 return mlir::FunctionType::get(ctx, {resTy, boxTy, boxTy, strTy, intTy},
317 {});
318 };
319 }
320 };
321
322 /// Placeholder for integer*16 version of DotProduct Intrinsic
323 struct ForcedDotProductInteger16 {
324 static constexpr const char *name =
325 ExpandAndQuoteKey(RTNAME(DotProductInteger16));
326 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
327 return [](mlir::MLIRContext *ctx) {
328 auto ty = mlir::IntegerType::get(ctx, 128);
329 auto boxTy =
330 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
331 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
332 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
333 return mlir::FunctionType::get(ctx, {boxTy, boxTy, strTy, intTy}, {ty});
334 };
335 }
336 };
337
338 /// Placeholder for real*10 version of Sum Intrinsic
339 struct ForcedSumReal10 {
340 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SumReal10));
341 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
342 return [](mlir::MLIRContext *ctx) {
343 auto ty = mlir::FloatType::getF80(ctx);
344 auto boxTy =
345 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
346 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
347 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
348 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
349 {ty});
350 };
351 }
352 };
353
354 /// Placeholder for real*16 version of Sum Intrinsic
355 struct ForcedSumReal16 {
356 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SumReal16));
357 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
358 return [](mlir::MLIRContext *ctx) {
359 auto ty = mlir::FloatType::getF128(ctx);
360 auto boxTy =
361 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
362 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
363 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
364 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
365 {ty});
366 };
367 }
368 };
369
370 /// Placeholder for integer*16 version of Sum Intrinsic
371 struct ForcedSumInteger16 {
372 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SumInteger16));
373 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
374 return [](mlir::MLIRContext *ctx) {
375 auto ty = mlir::IntegerType::get(ctx, 128);
376 auto boxTy =
377 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
378 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
379 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
380 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
381 {ty});
382 };
383 }
384 };
385
386 /// Placeholder for complex(10) version of Sum Intrinsic
387 struct ForcedSumComplex10 {
388 static constexpr const char *name =
389 ExpandAndQuoteKey(RTNAME(CppSumComplex10));
390 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
391 return [](mlir::MLIRContext *ctx) {
392 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
393 auto boxTy =
394 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
395 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
396 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
397 auto resTy = fir::ReferenceType::get(ty);
398 return mlir::FunctionType::get(
399 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
400 };
401 }
402 };
403
404 /// Placeholder for complex(16) version of Sum Intrinsic
405 struct ForcedSumComplex16 {
406 static constexpr const char *name =
407 ExpandAndQuoteKey(RTNAME(CppSumComplex16));
408 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
409 return [](mlir::MLIRContext *ctx) {
410 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
411 auto boxTy =
412 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
413 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
414 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
415 auto resTy = fir::ReferenceType::get(ty);
416 return mlir::FunctionType::get(
417 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
418 };
419 }
420 };
421
422 /// Placeholder for integer(16) version of IAll Intrinsic
423 struct ForcedIAll16 {
424 static constexpr const char *name = EXPAND_AND_QUOTE_KEY(IAll16);
425 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
426 return [](mlir::MLIRContext *ctx) {
427 auto ty = mlir::IntegerType::get(ctx, 128);
428 auto boxTy =
429 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
430 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
431 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
432 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
433 {ty});
434 };
435 }
436 };
437
438 /// Placeholder for integer(16) version of IAny Intrinsic
439 struct ForcedIAny16 {
440 static constexpr const char *name = EXPAND_AND_QUOTE_KEY(IAny16);
441 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
442 return [](mlir::MLIRContext *ctx) {
443 auto ty = mlir::IntegerType::get(ctx, 128);
444 auto boxTy =
445 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
446 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
447 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
448 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
449 {ty});
450 };
451 }
452 };
453
454 /// Placeholder for integer(16) version of IParity Intrinsic
455 struct ForcedIParity16 {
456 static constexpr const char *name = EXPAND_AND_QUOTE_KEY(IParity16);
457 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
458 return [](mlir::MLIRContext *ctx) {
459 auto ty = mlir::IntegerType::get(ctx, 128);
460 auto boxTy =
461 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
462 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
463 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
464 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
465 {ty});
466 };
467 }
468 };
469
470 /// Placeholder for real*10 version of Reduce Intrinsic
471 struct ForcedReduceReal10 {
472 static constexpr const char *name =
473 ExpandAndQuoteKey(RTNAME(ReduceReal10Ref));
474 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
475 return [](mlir::MLIRContext *ctx) {
476 auto ty = mlir::FloatType::getF80(ctx);
477 auto boxTy =
478 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
479 auto refTy = fir::ReferenceType::get(ty);
480 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
481 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
482 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
483 auto i1Ty = mlir::IntegerType::get(ctx, 1);
484 return mlir::FunctionType::get(
485 ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty});
486 };
487 }
488 };
489
490 /// Placeholder for real*10 version of Reduce Intrinsic
491 struct ForcedReduceReal10Value {
492 static constexpr const char *name =
493 ExpandAndQuoteKey(RTNAME(ReduceReal10Value));
494 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
495 return [](mlir::MLIRContext *ctx) {
496 auto ty = mlir::FloatType::getF80(ctx);
497 auto boxTy =
498 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
499 auto refTy = fir::ReferenceType::get(ty);
500 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
501 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
502 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
503 auto i1Ty = mlir::IntegerType::get(ctx, 1);
504 return mlir::FunctionType::get(
505 ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty});
506 };
507 }
508 };
509
510 /// Placeholder for real*16 version of Reduce Intrinsic
511 struct ForcedReduceReal16 {
512 static constexpr const char *name =
513 ExpandAndQuoteKey(RTNAME(ReduceReal16Ref));
514 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
515 return [](mlir::MLIRContext *ctx) {
516 auto ty = mlir::FloatType::getF128(ctx);
517 auto boxTy =
518 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
519 auto refTy = fir::ReferenceType::get(ty);
520 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
521 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
522 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
523 auto i1Ty = mlir::IntegerType::get(ctx, 1);
524 return mlir::FunctionType::get(
525 ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty});
526 };
527 }
528 };
529
530 /// Placeholder for real*16 version of Reduce Intrinsic
531 struct ForcedReduceReal16Value {
532 static constexpr const char *name =
533 ExpandAndQuoteKey(RTNAME(ReduceReal16Value));
534 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
535 return [](mlir::MLIRContext *ctx) {
536 auto ty = mlir::FloatType::getF128(ctx);
537 auto boxTy =
538 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
539 auto refTy = fir::ReferenceType::get(ty);
540 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
541 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
542 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
543 auto i1Ty = mlir::IntegerType::get(ctx, 1);
544 return mlir::FunctionType::get(
545 ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty});
546 };
547 }
548 };
549
550 /// Placeholder for DIM real*10 version of Reduce Intrinsic
551 struct ForcedReduceReal10Dim {
552 static constexpr const char *name =
553 ExpandAndQuoteKey(RTNAME(ReduceReal10DimRef));
554 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
555 return [](mlir::MLIRContext *ctx) {
556 auto ty = mlir::FloatType::getF80(ctx);
557 auto boxTy =
558 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
559 auto refTy = fir::ReferenceType::get(ty);
560 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
561 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
562 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
563 auto refBoxTy = fir::ReferenceType::get(boxTy);
564 auto i1Ty = mlir::IntegerType::get(ctx, 1);
565 return mlir::FunctionType::get(
566 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
567 {});
568 };
569 }
570 };
571
572 /// Placeholder for DIM real*10 with value version of Reduce Intrinsic
573 struct ForcedReduceReal10DimValue {
574 static constexpr const char *name =
575 ExpandAndQuoteKey(RTNAME(ReduceReal10DimValue));
576 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
577 return [](mlir::MLIRContext *ctx) {
578 auto ty = mlir::FloatType::getF80(ctx);
579 auto boxTy =
580 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
581 auto refTy = fir::ReferenceType::get(ty);
582 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
583 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
584 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
585 auto refBoxTy = fir::ReferenceType::get(boxTy);
586 auto i1Ty = mlir::IntegerType::get(ctx, 1);
587 return mlir::FunctionType::get(
588 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
589 {});
590 };
591 }
592 };
593
594 /// Placeholder for DIM real*16 version of Reduce Intrinsic
595 struct ForcedReduceReal16Dim {
596 static constexpr const char *name =
597 ExpandAndQuoteKey(RTNAME(ReduceReal16DimRef));
598 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
599 return [](mlir::MLIRContext *ctx) {
600 auto ty = mlir::FloatType::getF128(ctx);
601 auto boxTy =
602 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
603 auto refTy = fir::ReferenceType::get(ty);
604 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
605 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
606 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
607 auto refBoxTy = fir::ReferenceType::get(boxTy);
608 auto i1Ty = mlir::IntegerType::get(ctx, 1);
609 return mlir::FunctionType::get(
610 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
611 {});
612 };
613 }
614 };
615
616 /// Placeholder for DIM real*16 with value version of Reduce Intrinsic
617 struct ForcedReduceReal16DimValue {
618 static constexpr const char *name =
619 ExpandAndQuoteKey(RTNAME(ReduceReal16DimValue));
620 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
621 return [](mlir::MLIRContext *ctx) {
622 auto ty = mlir::FloatType::getF128(ctx);
623 auto boxTy =
624 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
625 auto refTy = fir::ReferenceType::get(ty);
626 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
627 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
628 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
629 auto refBoxTy = fir::ReferenceType::get(boxTy);
630 auto i1Ty = mlir::IntegerType::get(ctx, 1);
631 return mlir::FunctionType::get(
632 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
633 {});
634 };
635 }
636 };
637
638 /// Placeholder for integer*16 version of Reduce Intrinsic
639 struct ForcedReduceInteger16 {
640 static constexpr const char *name =
641 ExpandAndQuoteKey(RTNAME(ReduceInteger16Ref));
642 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
643 return [](mlir::MLIRContext *ctx) {
644 auto ty = mlir::IntegerType::get(ctx, 128);
645 auto boxTy =
646 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
647 auto refTy = fir::ReferenceType::get(ty);
648 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
649 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
650 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
651 auto i1Ty = mlir::IntegerType::get(ctx, 1);
652 return mlir::FunctionType::get(
653 ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty});
654 };
655 }
656 };
657
658 /// Placeholder for integer*16 with value version of Reduce Intrinsic
659 struct ForcedReduceInteger16Value {
660 static constexpr const char *name =
661 ExpandAndQuoteKey(RTNAME(ReduceInteger16Value));
662 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
663 return [](mlir::MLIRContext *ctx) {
664 auto ty = mlir::IntegerType::get(ctx, 128);
665 auto boxTy =
666 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
667 auto refTy = fir::ReferenceType::get(ty);
668 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
669 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
670 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
671 auto i1Ty = mlir::IntegerType::get(ctx, 1);
672 return mlir::FunctionType::get(
673 ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty});
674 };
675 }
676 };
677
678 /// Placeholder for DIM integer*16 version of Reduce Intrinsic
679 struct ForcedReduceInteger16Dim {
680 static constexpr const char *name =
681 ExpandAndQuoteKey(RTNAME(ReduceInteger16DimRef));
682 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
683 return [](mlir::MLIRContext *ctx) {
684 auto ty = mlir::IntegerType::get(ctx, 128);
685 auto boxTy =
686 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
687 auto refTy = fir::ReferenceType::get(ty);
688 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
689 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
690 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
691 auto refBoxTy = fir::ReferenceType::get(boxTy);
692 auto i1Ty = mlir::IntegerType::get(ctx, 1);
693 return mlir::FunctionType::get(
694 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
695 {});
696 };
697 }
698 };
699
700 /// Placeholder for DIM integer*16 with value version of Reduce Intrinsic
701 struct ForcedReduceInteger16DimValue {
702 static constexpr const char *name =
703 ExpandAndQuoteKey(RTNAME(ReduceInteger16DimValue));
704 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
705 return [](mlir::MLIRContext *ctx) {
706 auto ty = mlir::IntegerType::get(ctx, 128);
707 auto boxTy =
708 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
709 auto refTy = fir::ReferenceType::get(ty);
710 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
711 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
712 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
713 auto refBoxTy = fir::ReferenceType::get(boxTy);
714 auto i1Ty = mlir::IntegerType::get(ctx, 1);
715 return mlir::FunctionType::get(
716 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
717 {});
718 };
719 }
720 };
721
722 /// Placeholder for complex(10) version of Reduce Intrinsic
723 struct ForcedReduceComplex10 {
724 static constexpr const char *name =
725 ExpandAndQuoteKey(RTNAME(CppReduceComplex10Ref));
726 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
727 return [](mlir::MLIRContext *ctx) {
728 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
729 auto boxTy =
730 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
731 auto refTy = fir::ReferenceType::get(ty);
732 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
733 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
734 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
735 auto i1Ty = mlir::IntegerType::get(ctx, 1);
736 return mlir::FunctionType::get(
737 ctx, {refTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
738 {});
739 };
740 }
741 };
742
743 /// Placeholder for complex(10) with value version of Reduce Intrinsic
744 struct ForcedReduceComplex10Value {
745 static constexpr const char *name =
746 ExpandAndQuoteKey(RTNAME(CppReduceComplex10Value));
747 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
748 return [](mlir::MLIRContext *ctx) {
749 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
750 auto boxTy =
751 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
752 auto refTy = fir::ReferenceType::get(ty);
753 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
754 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
755 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
756 auto i1Ty = mlir::IntegerType::get(ctx, 1);
757 return mlir::FunctionType::get(
758 ctx, {refTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
759 {});
760 };
761 }
762 };
763
764 /// Placeholder for Dim complex(10) version of Reduce Intrinsic
765 struct ForcedReduceComplex10Dim {
766 static constexpr const char *name =
767 ExpandAndQuoteKey(RTNAME(CppReduceComplex10DimRef));
768 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
769 return [](mlir::MLIRContext *ctx) {
770 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
771 auto boxTy =
772 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
773 auto refTy = fir::ReferenceType::get(ty);
774 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
775 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
776 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
777 auto refBoxTy = fir::ReferenceType::get(boxTy);
778 auto i1Ty = mlir::IntegerType::get(ctx, 1);
779 return mlir::FunctionType::get(
780 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
781 {});
782 };
783 }
784 };
785
786 /// Placeholder for Dim complex(10) with value version of Reduce Intrinsic
787 struct ForcedReduceComplex10DimValue {
788 static constexpr const char *name =
789 ExpandAndQuoteKey(RTNAME(CppReduceComplex10DimValue));
790 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
791 return [](mlir::MLIRContext *ctx) {
792 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
793 auto boxTy =
794 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
795 auto refTy = fir::ReferenceType::get(ty);
796 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
797 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
798 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
799 auto refBoxTy = fir::ReferenceType::get(boxTy);
800 auto i1Ty = mlir::IntegerType::get(ctx, 1);
801 return mlir::FunctionType::get(
802 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
803 {});
804 };
805 }
806 };
807
808 /// Placeholder for complex(16) version of Reduce Intrinsic
809 struct ForcedReduceComplex16 {
810 static constexpr const char *name =
811 ExpandAndQuoteKey(RTNAME(CppReduceComplex16Ref));
812 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
813 return [](mlir::MLIRContext *ctx) {
814 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
815 auto boxTy =
816 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
817 auto refTy = fir::ReferenceType::get(ty);
818 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
819 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
820 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
821 auto i1Ty = mlir::IntegerType::get(ctx, 1);
822 return mlir::FunctionType::get(
823 ctx, {refTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
824 {});
825 };
826 }
827 };
828
829 /// Placeholder for complex(16) with value version of Reduce Intrinsic
830 struct ForcedReduceComplex16Value {
831 static constexpr const char *name =
832 ExpandAndQuoteKey(RTNAME(CppReduceComplex16Value));
833 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
834 return [](mlir::MLIRContext *ctx) {
835 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
836 auto boxTy =
837 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
838 auto refTy = fir::ReferenceType::get(ty);
839 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
840 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
841 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
842 auto i1Ty = mlir::IntegerType::get(ctx, 1);
843 return mlir::FunctionType::get(
844 ctx, {refTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
845 {});
846 };
847 }
848 };
849
850 /// Placeholder for Dim complex(16) version of Reduce Intrinsic
851 struct ForcedReduceComplex16Dim {
852 static constexpr const char *name =
853 ExpandAndQuoteKey(RTNAME(CppReduceComplex16DimRef));
854 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
855 return [](mlir::MLIRContext *ctx) {
856 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
857 auto boxTy =
858 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
859 auto refTy = fir::ReferenceType::get(ty);
860 auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy);
861 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
862 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
863 auto refBoxTy = fir::ReferenceType::get(boxTy);
864 auto i1Ty = mlir::IntegerType::get(ctx, 1);
865 return mlir::FunctionType::get(
866 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
867 {});
868 };
869 }
870 };
871
872 /// Placeholder for Dim complex(16) with value version of Reduce Intrinsic
873 struct ForcedReduceComplex16DimValue {
874 static constexpr const char *name =
875 ExpandAndQuoteKey(RTNAME(CppReduceComplex16DimValue));
876 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
877 return [](mlir::MLIRContext *ctx) {
878 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
879 auto boxTy =
880 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
881 auto refTy = fir::ReferenceType::get(ty);
882 auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy);
883 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
884 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
885 auto refBoxTy = fir::ReferenceType::get(boxTy);
886 auto i1Ty = mlir::IntegerType::get(ctx, 1);
887 return mlir::FunctionType::get(
888 ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty},
889 {});
890 };
891 }
892 };
893
894 /// Generate call to specialized runtime function that takes a mask and
895 /// dim argument. The All, Any, and Count intrinsics use this pattern.
896 template <typename FN>
897 mlir::Value genSpecial2Args(FN func, fir::FirOpBuilder &builder,
898 mlir::Location loc, mlir::Value maskBox,
899 mlir::Value dim) {
900 auto fTy = func.getFunctionType();
901 auto sourceFile = fir::factory::locationToFilename(builder, loc);
902 auto sourceLine =
903 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
904 auto args = fir::runtime::createArguments(builder, loc, fTy, maskBox,
905 sourceFile, sourceLine, dim);
906 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
907 }
908
909 /// Generate calls to reduction intrinsics such as All and Any.
910 /// These are the descriptor based implementations that take two
911 /// arguments (mask, dim).
912 template <typename FN>
913 static void genReduction2Args(FN func, fir::FirOpBuilder &builder,
914 mlir::Location loc, mlir::Value resultBox,
915 mlir::Value maskBox, mlir::Value dim) {
916 auto fTy = func.getFunctionType();
917 auto sourceFile = fir::factory::locationToFilename(builder, loc);
918 auto sourceLine =
919 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
920 auto args = fir::runtime::createArguments(
921 builder, loc, fTy, resultBox, maskBox, dim, sourceFile, sourceLine);
922 builder.create<fir::CallOp>(loc, func, args);
923 }
924
925 /// Generate calls to reduction intrinsics such as Maxval and Minval.
926 /// These take arguments such as (array, dim, mask).
927 template <typename FN>
928 static void genReduction3Args(FN func, fir::FirOpBuilder &builder,
929 mlir::Location loc, mlir::Value resultBox,
930 mlir::Value arrayBox, mlir::Value dim,
931 mlir::Value maskBox) {
932
933 auto fTy = func.getFunctionType();
934 auto sourceFile = fir::factory::locationToFilename(builder, loc);
935 auto sourceLine =
936 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
937 auto args =
938 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox, dim,
939 sourceFile, sourceLine, maskBox);
940 builder.create<fir::CallOp>(loc, func, args);
941 }
942
943 /// Generate calls to reduction intrinsics such as Maxloc and Minloc.
944 /// These take arguments such as (array, mask, kind, back).
945 template <typename FN>
946 static void genReduction4Args(FN func, fir::FirOpBuilder &builder,
947 mlir::Location loc, mlir::Value resultBox,
948 mlir::Value arrayBox, mlir::Value maskBox,
949 mlir::Value kind, mlir::Value back) {
950 auto fTy = func.getFunctionType();
951 auto sourceFile = fir::factory::locationToFilename(builder, loc);
952 auto sourceLine =
953 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
954 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
955 arrayBox, kind, sourceFile,
956 sourceLine, maskBox, back);
957 builder.create<fir::CallOp>(loc, func, args);
958 }
959
960 /// Generate calls to reduction intrinsics such as Maxloc and Minloc.
961 /// These take arguments such as (array, dim, mask, kind, back).
962 template <typename FN>
963 static void
964 genReduction5Args(FN func, fir::FirOpBuilder &builder, mlir::Location loc,
965 mlir::Value resultBox, mlir::Value arrayBox, mlir::Value dim,
966 mlir::Value maskBox, mlir::Value kind, mlir::Value back) {
967 auto fTy = func.getFunctionType();
968 auto sourceFile = fir::factory::locationToFilename(builder, loc);
969 auto sourceLine =
970 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
971 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
972 arrayBox, kind, dim, sourceFile,
973 sourceLine, maskBox, back);
974 builder.create<fir::CallOp>(loc, func, args);
975 }
976
977 /// Generate call to `AllDim` runtime routine.
978 /// This calls the descriptor based runtime call implementation of the `all`
979 /// intrinsic.
980 void fir::runtime::genAllDescriptor(fir::FirOpBuilder &builder,
981 mlir::Location loc, mlir::Value resultBox,
982 mlir::Value maskBox, mlir::Value dim) {
983 auto allFunc = fir::runtime::getRuntimeFunc<mkRTKey(AllDim)>(loc, builder);
984 genReduction2Args(allFunc, builder, loc, resultBox, maskBox, dim);
985 }
986
987 /// Generate call to `AnyDim` runtime routine.
988 /// This calls the descriptor based runtime call implementation of the `any`
989 /// intrinsic.
990 void fir::runtime::genAnyDescriptor(fir::FirOpBuilder &builder,
991 mlir::Location loc, mlir::Value resultBox,
992 mlir::Value maskBox, mlir::Value dim) {
993 auto anyFunc = fir::runtime::getRuntimeFunc<mkRTKey(AnyDim)>(loc, builder);
994 genReduction2Args(anyFunc, builder, loc, resultBox, maskBox, dim);
995 }
996
997 /// Generate call to `ParityDim` runtime routine.
998 /// This calls the descriptor based runtime call implementation of the `parity`
999 /// intrinsic.
1000 void fir::runtime::genParityDescriptor(fir::FirOpBuilder &builder,
1001 mlir::Location loc,
1002 mlir::Value resultBox,
1003 mlir::Value maskBox, mlir::Value dim) {
1004 auto parityFunc =
1005 fir::runtime::getRuntimeFunc<mkRTKey(ParityDim)>(loc, builder);
1006 genReduction2Args(parityFunc, builder, loc, resultBox, maskBox, dim);
1007 }
1008
1009 /// Generate call to `All` intrinsic runtime routine. This routine is
1010 /// specialized for mask arguments with rank == 1.
1011 mlir::Value fir::runtime::genAll(fir::FirOpBuilder &builder, mlir::Location loc,
1012 mlir::Value maskBox, mlir::Value dim) {
1013 auto allFunc = fir::runtime::getRuntimeFunc<mkRTKey(All)>(loc, builder);
1014 return genSpecial2Args(allFunc, builder, loc, maskBox, dim);
1015 }
1016
1017 /// Generate call to `Any` intrinsic runtime routine. This routine is
1018 /// specialized for mask arguments with rank == 1.
1019 mlir::Value fir::runtime::genAny(fir::FirOpBuilder &builder, mlir::Location loc,
1020 mlir::Value maskBox, mlir::Value dim) {
1021 auto anyFunc = fir::runtime::getRuntimeFunc<mkRTKey(Any)>(loc, builder);
1022 return genSpecial2Args(anyFunc, builder, loc, maskBox, dim);
1023 }
1024
1025 /// Generate call to `Count` runtime routine. This routine is a specialized
1026 /// version when mask is a rank one array or the dim argument is not
1027 /// specified by the user.
1028 mlir::Value fir::runtime::genCount(fir::FirOpBuilder &builder,
1029 mlir::Location loc, mlir::Value maskBox,
1030 mlir::Value dim) {
1031 auto countFunc = fir::runtime::getRuntimeFunc<mkRTKey(Count)>(loc, builder);
1032 return genSpecial2Args(countFunc, builder, loc, maskBox, dim);
1033 }
1034
1035 /// Generate call to general `CountDim` runtime routine. This routine has a
1036 /// descriptor result.
1037 void fir::runtime::genCountDim(fir::FirOpBuilder &builder, mlir::Location loc,
1038 mlir::Value resultBox, mlir::Value maskBox,
1039 mlir::Value dim, mlir::Value kind) {
1040 auto func = fir::runtime::getRuntimeFunc<mkRTKey(CountDim)>(loc, builder);
1041 auto fTy = func.getFunctionType();
1042 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1043 auto sourceLine =
1044 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
1045 auto args = fir::runtime::createArguments(
1046 builder, loc, fTy, resultBox, maskBox, dim, kind, sourceFile, sourceLine);
1047 builder.create<fir::CallOp>(loc, func, args);
1048 }
1049
1050 /// Generate call to `Findloc` intrinsic runtime routine. This is the version
1051 /// that does not take a dim argument.
1052 void fir::runtime::genFindloc(fir::FirOpBuilder &builder, mlir::Location loc,
1053 mlir::Value resultBox, mlir::Value arrayBox,
1054 mlir::Value valBox, mlir::Value maskBox,
1055 mlir::Value kind, mlir::Value back) {
1056 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Findloc)>(loc, builder);
1057 auto fTy = func.getFunctionType();
1058 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1059 auto sourceLine =
1060 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
1061 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
1062 arrayBox, valBox, kind, sourceFile,
1063 sourceLine, maskBox, back);
1064 builder.create<fir::CallOp>(loc, func, args);
1065 }
1066
1067 /// Generate call to `FindlocDim` intrinsic runtime routine. This is the version
1068 /// that takes a dim argument.
1069 void fir::runtime::genFindlocDim(fir::FirOpBuilder &builder, mlir::Location loc,
1070 mlir::Value resultBox, mlir::Value arrayBox,
1071 mlir::Value valBox, mlir::Value dim,
1072 mlir::Value maskBox, mlir::Value kind,
1073 mlir::Value back) {
1074 auto func = fir::runtime::getRuntimeFunc<mkRTKey(FindlocDim)>(loc, builder);
1075 auto fTy = func.getFunctionType();
1076 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1077 auto sourceLine =
1078 fir::factory::locationToLineNo(builder, loc, fTy.getInput(6));
1079 auto args = fir::runtime::createArguments(
1080 builder, loc, fTy, resultBox, arrayBox, valBox, kind, dim, sourceFile,
1081 sourceLine, maskBox, back);
1082 builder.create<fir::CallOp>(loc, func, args);
1083 }
1084
1085 /// Generate call to `Maxloc` intrinsic runtime routine. This is the version
1086 /// that does not take a dim argument.
1087 void fir::runtime::genMaxloc(fir::FirOpBuilder &builder, mlir::Location loc,
1088 mlir::Value resultBox, mlir::Value arrayBox,
1089 mlir::Value maskBox, mlir::Value kind,
1090 mlir::Value back) {
1091 mlir::func::FuncOp func;
1092 auto ty = arrayBox.getType();
1093 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1094 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1095 fir::factory::CharacterExprHelper charHelper{builder, loc};
1096 if (eleTy.isF32())
1097 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal4)>(loc, builder);
1098 else if (eleTy.isF64())
1099 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal8)>(loc, builder);
1100 else if (eleTy.isF80())
1101 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal10)>(loc, builder);
1102 else if (eleTy.isF128())
1103 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal16)>(loc, builder);
1104 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1105 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger1)>(loc, builder);
1106 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1107 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger2)>(loc, builder);
1108 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1109 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger4)>(loc, builder);
1110 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1111 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger8)>(loc, builder);
1112 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1113 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger16)>(loc, builder);
1114 else if (charHelper.isCharacterScalar(eleTy))
1115 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocCharacter)>(loc, builder);
1116 else
1117 fir::intrinsicTypeTODO(builder, eleTy, loc, "MAXLOC");
1118 genReduction4Args(func, builder, loc, resultBox, arrayBox, maskBox, kind,
1119 back);
1120 }
1121
1122 /// Generate call to `MaxlocDim` intrinsic runtime routine. This is the version
1123 /// that takes a dim argument.
1124 void fir::runtime::genMaxlocDim(fir::FirOpBuilder &builder, mlir::Location loc,
1125 mlir::Value resultBox, mlir::Value arrayBox,
1126 mlir::Value dim, mlir::Value maskBox,
1127 mlir::Value kind, mlir::Value back) {
1128 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocDim)>(loc, builder);
1129 genReduction5Args(func, builder, loc, resultBox, arrayBox, dim, maskBox, kind,
1130 back);
1131 }
1132
1133 /// Generate call to `Maxval` intrinsic runtime routine. This is the version
1134 /// that does not take a dim argument.
1135 mlir::Value fir::runtime::genMaxval(fir::FirOpBuilder &builder,
1136 mlir::Location loc, mlir::Value arrayBox,
1137 mlir::Value maskBox) {
1138 mlir::func::FuncOp func;
1139 auto ty = arrayBox.getType();
1140 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1141 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1142 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
1143
1144 if (eleTy.isF32())
1145 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalReal4)>(loc, builder);
1146 else if (eleTy.isF64())
1147 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalReal8)>(loc, builder);
1148 else if (eleTy.isF80())
1149 func = fir::runtime::getRuntimeFunc<ForcedMaxvalReal10>(loc, builder);
1150 else if (eleTy.isF128())
1151 func = fir::runtime::getRuntimeFunc<ForcedMaxvalReal16>(loc, builder);
1152 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1153 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger1)>(loc, builder);
1154 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1155 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger2)>(loc, builder);
1156 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1157 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger4)>(loc, builder);
1158 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1159 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger8)>(loc, builder);
1160 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1161 func = fir::runtime::getRuntimeFunc<ForcedMaxvalInteger16>(loc, builder);
1162 else
1163 fir::intrinsicTypeTODO(builder, eleTy, loc, "MAXVAL");
1164
1165 auto fTy = func.getFunctionType();
1166 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1167 auto sourceLine =
1168 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1169 auto args = fir::runtime::createArguments(
1170 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
1171
1172 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1173 }
1174
1175 /// Generate call to `MaxvalDim` intrinsic runtime routine. This is the version
1176 /// that handles any rank array with the dim argument specified.
1177 void fir::runtime::genMaxvalDim(fir::FirOpBuilder &builder, mlir::Location loc,
1178 mlir::Value resultBox, mlir::Value arrayBox,
1179 mlir::Value dim, mlir::Value maskBox) {
1180 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalDim)>(loc, builder);
1181 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1182 }
1183
1184 /// Generate call to `MaxvalCharacter` intrinsic runtime routine. This is the
1185 /// version that handles character arrays of rank 1 and without a DIM argument.
1186 void fir::runtime::genMaxvalChar(fir::FirOpBuilder &builder, mlir::Location loc,
1187 mlir::Value resultBox, mlir::Value arrayBox,
1188 mlir::Value maskBox) {
1189 auto func =
1190 fir::runtime::getRuntimeFunc<mkRTKey(MaxvalCharacter)>(loc, builder);
1191 auto fTy = func.getFunctionType();
1192 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1193 auto sourceLine =
1194 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1195 auto args = fir::runtime::createArguments(
1196 builder, loc, fTy, resultBox, arrayBox, sourceFile, sourceLine, maskBox);
1197 builder.create<fir::CallOp>(loc, func, args);
1198 }
1199
1200 /// Generate call to `Minloc` intrinsic runtime routine. This is the version
1201 /// that does not take a dim argument.
1202 void fir::runtime::genMinloc(fir::FirOpBuilder &builder, mlir::Location loc,
1203 mlir::Value resultBox, mlir::Value arrayBox,
1204 mlir::Value maskBox, mlir::Value kind,
1205 mlir::Value back) {
1206 mlir::func::FuncOp func;
1207 auto ty = arrayBox.getType();
1208 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1209 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1210 fir::factory::CharacterExprHelper charHelper{builder, loc};
1211 if (eleTy.isF32())
1212 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal4)>(loc, builder);
1213 else if (eleTy.isF64())
1214 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal8)>(loc, builder);
1215 else if (eleTy.isF80())
1216 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal10)>(loc, builder);
1217 else if (eleTy.isF128())
1218 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal16)>(loc, builder);
1219 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1220 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger1)>(loc, builder);
1221 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1222 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger2)>(loc, builder);
1223 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1224 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger4)>(loc, builder);
1225 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1226 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger8)>(loc, builder);
1227 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1228 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger16)>(loc, builder);
1229 else if (charHelper.isCharacterScalar(eleTy))
1230 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocCharacter)>(loc, builder);
1231 else
1232 fir::intrinsicTypeTODO(builder, eleTy, loc, "MINLOC");
1233 genReduction4Args(func, builder, loc, resultBox, arrayBox, maskBox, kind,
1234 back);
1235 }
1236
1237 /// Generate call to `MinlocDim` intrinsic runtime routine. This is the version
1238 /// that takes a dim argument.
1239 void fir::runtime::genMinlocDim(fir::FirOpBuilder &builder, mlir::Location loc,
1240 mlir::Value resultBox, mlir::Value arrayBox,
1241 mlir::Value dim, mlir::Value maskBox,
1242 mlir::Value kind, mlir::Value back) {
1243 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocDim)>(loc, builder);
1244 genReduction5Args(func, builder, loc, resultBox, arrayBox, dim, maskBox, kind,
1245 back);
1246 }
1247
1248 /// Generate call to `MinvalDim` intrinsic runtime routine. This is the version
1249 /// that handles any rank array with the dim argument specified.
1250 void fir::runtime::genMinvalDim(fir::FirOpBuilder &builder, mlir::Location loc,
1251 mlir::Value resultBox, mlir::Value arrayBox,
1252 mlir::Value dim, mlir::Value maskBox) {
1253 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalDim)>(loc, builder);
1254 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1255 }
1256
1257 /// Generate call to `MinvalCharacter` intrinsic runtime routine. This is the
1258 /// version that handles character arrays of rank 1 and without a DIM argument.
1259 void fir::runtime::genMinvalChar(fir::FirOpBuilder &builder, mlir::Location loc,
1260 mlir::Value resultBox, mlir::Value arrayBox,
1261 mlir::Value maskBox) {
1262 auto func =
1263 fir::runtime::getRuntimeFunc<mkRTKey(MinvalCharacter)>(loc, builder);
1264 auto fTy = func.getFunctionType();
1265 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1266 auto sourceLine =
1267 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1268 auto args = fir::runtime::createArguments(
1269 builder, loc, fTy, resultBox, arrayBox, sourceFile, sourceLine, maskBox);
1270 builder.create<fir::CallOp>(loc, func, args);
1271 }
1272
1273 /// Generate call to `Minval` intrinsic runtime routine. This is the version
1274 /// that does not take a dim argument.
1275 mlir::Value fir::runtime::genMinval(fir::FirOpBuilder &builder,
1276 mlir::Location loc, mlir::Value arrayBox,
1277 mlir::Value maskBox) {
1278 mlir::func::FuncOp func;
1279 auto ty = arrayBox.getType();
1280 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1281 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1282 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
1283
1284 if (eleTy.isF32())
1285 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalReal4)>(loc, builder);
1286 else if (eleTy.isF64())
1287 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalReal8)>(loc, builder);
1288 else if (eleTy.isF80())
1289 func = fir::runtime::getRuntimeFunc<ForcedMinvalReal10>(loc, builder);
1290 else if (eleTy.isF128())
1291 func = fir::runtime::getRuntimeFunc<ForcedMinvalReal16>(loc, builder);
1292 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1293 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger1)>(loc, builder);
1294 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1295 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger2)>(loc, builder);
1296 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1297 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger4)>(loc, builder);
1298 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1299 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger8)>(loc, builder);
1300 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1301 func = fir::runtime::getRuntimeFunc<ForcedMinvalInteger16>(loc, builder);
1302 else
1303 fir::intrinsicTypeTODO(builder, eleTy, loc, "MINVAL");
1304
1305 auto fTy = func.getFunctionType();
1306 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1307 auto sourceLine =
1308 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1309 auto args = fir::runtime::createArguments(
1310 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
1311
1312 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1313 }
1314
1315 /// Generate call to `Norm2Dim` intrinsic runtime routine. This is the version
1316 /// that takes a dim argument.
1317 void fir::runtime::genNorm2Dim(fir::FirOpBuilder &builder, mlir::Location loc,
1318 mlir::Value resultBox, mlir::Value arrayBox,
1319 mlir::Value dim) {
1320 mlir::func::FuncOp func;
1321 auto ty = arrayBox.getType();
1322 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1323 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1324 if (eleTy.isF128())
1325 func = fir::runtime::getRuntimeFunc<ForcedNorm2DimReal16>(loc, builder);
1326 else
1327 func = fir::runtime::getRuntimeFunc<mkRTKey(Norm2Dim)>(loc, builder);
1328 auto fTy = func.getFunctionType();
1329 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1330 auto sourceLine =
1331 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
1332 auto args = fir::runtime::createArguments(
1333 builder, loc, fTy, resultBox, arrayBox, dim, sourceFile, sourceLine);
1334
1335 builder.create<fir::CallOp>(loc, func, args);
1336 }
1337
1338 /// Generate call to `Norm2` intrinsic runtime routine. This is the version
1339 /// that does not take a dim argument.
1340 mlir::Value fir::runtime::genNorm2(fir::FirOpBuilder &builder,
1341 mlir::Location loc, mlir::Value arrayBox) {
1342 mlir::func::FuncOp func;
1343 auto ty = arrayBox.getType();
1344 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1345 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1346 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
1347
1348 if (eleTy.isF32())
1349 func = fir::runtime::getRuntimeFunc<mkRTKey(Norm2_4)>(loc, builder);
1350 else if (eleTy.isF64())
1351 func = fir::runtime::getRuntimeFunc<mkRTKey(Norm2_8)>(loc, builder);
1352 else if (eleTy.isF80())
1353 func = fir::runtime::getRuntimeFunc<ForcedNorm2Real10>(loc, builder);
1354 else if (eleTy.isF128())
1355 func = fir::runtime::getRuntimeFunc<ForcedNorm2Real16>(loc, builder);
1356 else
1357 fir::intrinsicTypeTODO(builder, eleTy, loc, "NORM2");
1358
1359 auto fTy = func.getFunctionType();
1360 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1361 auto sourceLine =
1362 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1363 auto args = fir::runtime::createArguments(builder, loc, fTy, arrayBox,
1364 sourceFile, sourceLine, dim);
1365
1366 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1367 }
1368
1369 /// Generate call to `Parity` intrinsic runtime routine. This routine is
1370 /// specialized for mask arguments with rank == 1.
1371 mlir::Value fir::runtime::genParity(fir::FirOpBuilder &builder,
1372 mlir::Location loc, mlir::Value maskBox,
1373 mlir::Value dim) {
1374 auto parityFunc = fir::runtime::getRuntimeFunc<mkRTKey(Parity)>(loc, builder);
1375 return genSpecial2Args(parityFunc, builder, loc, maskBox, dim);
1376 }
1377
1378 /// Generate call to `ProductDim` intrinsic runtime routine. This is the version
1379 /// that handles any rank array with the dim argument specified.
1380 void fir::runtime::genProductDim(fir::FirOpBuilder &builder, mlir::Location loc,
1381 mlir::Value resultBox, mlir::Value arrayBox,
1382 mlir::Value dim, mlir::Value maskBox) {
1383 auto func = fir::runtime::getRuntimeFunc<mkRTKey(ProductDim)>(loc, builder);
1384 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1385 }
1386
1387 /// Generate call to `Product` intrinsic runtime routine. This is the version
1388 /// that does not take a dim argument.
1389 mlir::Value fir::runtime::genProduct(fir::FirOpBuilder &builder,
1390 mlir::Location loc, mlir::Value arrayBox,
1391 mlir::Value maskBox,
1392 mlir::Value resultBox) {
1393 mlir::func::FuncOp func;
1394 auto ty = arrayBox.getType();
1395 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1396 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1397 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
1398
1399 if (eleTy.isF32())
1400 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductReal4)>(loc, builder);
1401 else if (eleTy.isF64())
1402 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductReal8)>(loc, builder);
1403 else if (eleTy.isF80())
1404 func = fir::runtime::getRuntimeFunc<ForcedProductReal10>(loc, builder);
1405 else if (eleTy.isF128())
1406 func = fir::runtime::getRuntimeFunc<ForcedProductReal16>(loc, builder);
1407 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1408 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger1)>(loc, builder);
1409 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1410 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger2)>(loc, builder);
1411 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1412 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger4)>(loc, builder);
1413 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1414 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger8)>(loc, builder);
1415 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1416 func = fir::runtime::getRuntimeFunc<ForcedProductInteger16>(loc, builder);
1417 else if (eleTy == fir::ComplexType::get(builder.getContext(), 4))
1418 func =
1419 fir::runtime::getRuntimeFunc<mkRTKey(CppProductComplex4)>(loc, builder);
1420 else if (eleTy == fir::ComplexType::get(builder.getContext(), 8))
1421 func =
1422 fir::runtime::getRuntimeFunc<mkRTKey(CppProductComplex8)>(loc, builder);
1423 else if (eleTy == fir::ComplexType::get(builder.getContext(), 10))
1424 func = fir::runtime::getRuntimeFunc<ForcedProductComplex10>(loc, builder);
1425 else if (eleTy == fir::ComplexType::get(builder.getContext(), 16))
1426 func = fir::runtime::getRuntimeFunc<ForcedProductComplex16>(loc, builder);
1427 else
1428 fir::intrinsicTypeTODO(builder, eleTy, loc, "PRODUCT");
1429
1430 auto fTy = func.getFunctionType();
1431 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1432 if (fir::isa_complex(eleTy)) {
1433 auto sourceLine =
1434 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1435 auto args =
1436 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
1437 sourceFile, sourceLine, dim, maskBox);
1438 builder.create<fir::CallOp>(loc, func, args);
1439 return resultBox;
1440 }
1441
1442 auto sourceLine =
1443 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1444 auto args = fir::runtime::createArguments(
1445 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
1446
1447 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1448 }
1449
1450 /// Generate call to `DotProduct` intrinsic runtime routine.
1451 mlir::Value fir::runtime::genDotProduct(fir::FirOpBuilder &builder,
1452 mlir::Location loc,
1453 mlir::Value vectorABox,
1454 mlir::Value vectorBBox,
1455 mlir::Value resultBox) {
1456 mlir::func::FuncOp func;
1457 // For complex data types, resultBox is !fir.ref<!fir.complex<N>>,
1458 // otherwise it is !fir.box<T>.
1459 auto ty = resultBox.getType();
1460 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1461
1462 if (eleTy.isF32())
1463 func = fir::runtime::getRuntimeFunc<mkRTKey(DotProductReal4)>(loc, builder);
1464 else if (eleTy.isF64())
1465 func = fir::runtime::getRuntimeFunc<mkRTKey(DotProductReal8)>(loc, builder);
1466 else if (eleTy.isF80())
1467 func = fir::runtime::getRuntimeFunc<ForcedDotProductReal10>(loc, builder);
1468 else if (eleTy.isF128())
1469 func = fir::runtime::getRuntimeFunc<ForcedDotProductReal16>(loc, builder);
1470 else if (eleTy == fir::ComplexType::get(builder.getContext(), 4))
1471 func = fir::runtime::getRuntimeFunc<mkRTKey(CppDotProductComplex4)>(
1472 loc, builder);
1473 else if (eleTy == fir::ComplexType::get(builder.getContext(), 8))
1474 func = fir::runtime::getRuntimeFunc<mkRTKey(CppDotProductComplex8)>(
1475 loc, builder);
1476 else if (eleTy == fir::ComplexType::get(builder.getContext(), 10))
1477 func =
1478 fir::runtime::getRuntimeFunc<ForcedDotProductComplex10>(loc, builder);
1479 else if (eleTy == fir::ComplexType::get(builder.getContext(), 16))
1480 func =
1481 fir::runtime::getRuntimeFunc<ForcedDotProductComplex16>(loc, builder);
1482 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1483 func =
1484 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger1)>(loc, builder);
1485 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1486 func =
1487 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger2)>(loc, builder);
1488 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1489 func =
1490 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger4)>(loc, builder);
1491 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1492 func =
1493 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger8)>(loc, builder);
1494 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1495 func =
1496 fir::runtime::getRuntimeFunc<ForcedDotProductInteger16>(loc, builder);
1497 else if (mlir::isa<fir::LogicalType>(eleTy))
1498 func =
1499 fir::runtime::getRuntimeFunc<mkRTKey(DotProductLogical)>(loc, builder);
1500 else
1501 fir::intrinsicTypeTODO(builder, eleTy, loc, "DOTPRODUCT");
1502
1503 auto fTy = func.getFunctionType();
1504 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1505
1506 if (fir::isa_complex(eleTy)) {
1507 auto sourceLine =
1508 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
1509 auto args =
1510 fir::runtime::createArguments(builder, loc, fTy, resultBox, vectorABox,
1511 vectorBBox, sourceFile, sourceLine);
1512 builder.create<fir::CallOp>(loc, func, args);
1513 return resultBox;
1514 }
1515
1516 auto sourceLine =
1517 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1518 auto args = fir::runtime::createArguments(builder, loc, fTy, vectorABox,
1519 vectorBBox, sourceFile, sourceLine);
1520 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1521 }
1522 /// Generate call to `SumDim` intrinsic runtime routine. This is the version
1523 /// that handles any rank array with the dim argument specified.
1524 void fir::runtime::genSumDim(fir::FirOpBuilder &builder, mlir::Location loc,
1525 mlir::Value resultBox, mlir::Value arrayBox,
1526 mlir::Value dim, mlir::Value maskBox) {
1527 auto func = fir::runtime::getRuntimeFunc<mkRTKey(SumDim)>(loc, builder);
1528 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1529 }
1530
1531 /// Generate call to `Sum` intrinsic runtime routine. This is the version
1532 /// that does not take a dim argument.
1533 mlir::Value fir::runtime::genSum(fir::FirOpBuilder &builder, mlir::Location loc,
1534 mlir::Value arrayBox, mlir::Value maskBox,
1535 mlir::Value resultBox) {
1536 mlir::func::FuncOp func;
1537 auto ty = arrayBox.getType();
1538 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1539 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1540 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
1541
1542 if (eleTy.isF32())
1543 func = fir::runtime::getRuntimeFunc<mkRTKey(SumReal4)>(loc, builder);
1544 else if (eleTy.isF64())
1545 func = fir::runtime::getRuntimeFunc<mkRTKey(SumReal8)>(loc, builder);
1546 else if (eleTy.isF80())
1547 func = fir::runtime::getRuntimeFunc<ForcedSumReal10>(loc, builder);
1548 else if (eleTy.isF128())
1549 func = fir::runtime::getRuntimeFunc<ForcedSumReal16>(loc, builder);
1550 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1551 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger1)>(loc, builder);
1552 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1553 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger2)>(loc, builder);
1554 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1555 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger4)>(loc, builder);
1556 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1557 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger8)>(loc, builder);
1558 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1559 func = fir::runtime::getRuntimeFunc<ForcedSumInteger16>(loc, builder);
1560 else if (eleTy == fir::ComplexType::get(builder.getContext(), 4))
1561 func = fir::runtime::getRuntimeFunc<mkRTKey(CppSumComplex4)>(loc, builder);
1562 else if (eleTy == fir::ComplexType::get(builder.getContext(), 8))
1563 func = fir::runtime::getRuntimeFunc<mkRTKey(CppSumComplex8)>(loc, builder);
1564 else if (eleTy == fir::ComplexType::get(builder.getContext(), 10))
1565 func = fir::runtime::getRuntimeFunc<ForcedSumComplex10>(loc, builder);
1566 else if (eleTy == fir::ComplexType::get(builder.getContext(), 16))
1567 func = fir::runtime::getRuntimeFunc<ForcedSumComplex16>(loc, builder);
1568 else
1569 fir::intrinsicTypeTODO(builder, eleTy, loc, "SUM");
1570
1571 auto fTy = func.getFunctionType();
1572 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1573 if (fir::isa_complex(eleTy)) {
1574 auto sourceLine =
1575 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1576 auto args =
1577 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
1578 sourceFile, sourceLine, dim, maskBox);
1579 builder.create<fir::CallOp>(loc, func, args);
1580 return resultBox;
1581 }
1582
1583 auto sourceLine =
1584 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1585 auto args = fir::runtime::createArguments(
1586 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
1587
1588 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1589 }
1590
1591 // The IAll, IAny and IParity intrinsics have essentially the same
1592 // implementation. This macro will generate the function body given the
1593 // instrinsic name.
1594 #define GEN_IALL_IANY_IPARITY(F) \
1595 mlir::Value fir::runtime::JOIN2(gen, F)( \
1596 fir::FirOpBuilder & builder, mlir::Location loc, mlir::Value arrayBox, \
1597 mlir::Value maskBox, mlir::Value resultBox) { \
1598 mlir::func::FuncOp func; \
1599 auto ty = arrayBox.getType(); \
1600 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty); \
1601 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy(); \
1602 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0); \
1603 \
1604 if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1))) \
1605 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 1))>(loc, builder); \
1606 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2))) \
1607 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 2))>(loc, builder); \
1608 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4))) \
1609 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 4))>(loc, builder); \
1610 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8))) \
1611 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 8))>(loc, builder); \
1612 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16))) \
1613 func = fir::runtime::getRuntimeFunc<JOIN3(Forced, F, 16)>(loc, builder); \
1614 else \
1615 fir::emitFatalError(loc, "invalid type in " STRINGIFY(F)); \
1616 \
1617 auto fTy = func.getFunctionType(); \
1618 auto sourceFile = fir::factory::locationToFilename(builder, loc); \
1619 auto sourceLine = \
1620 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2)); \
1621 auto args = fir::runtime::createArguments( \
1622 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox); \
1623 \
1624 return builder.create<fir::CallOp>(loc, func, args).getResult(0); \
1625 }
1626
1627 /// Generate call to `IAllDim` intrinsic runtime routine. This is the version
1628 /// that handles any rank array with the dim argument specified.
1629 void fir::runtime::genIAllDim(fir::FirOpBuilder &builder, mlir::Location loc,
1630 mlir::Value resultBox, mlir::Value arrayBox,
1631 mlir::Value dim, mlir::Value maskBox) {
1632 auto func = fir::runtime::getRuntimeFunc<mkRTKey(IAllDim)>(loc, builder);
1633 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1634 }
1635
1636 /// Generate call to `IAll` intrinsic runtime routine. This is the version
1637 /// that does not take a dim argument.
1638 GEN_IALL_IANY_IPARITY(IAll)
1639
1640 /// Generate call to `IAnyDim` intrinsic runtime routine. This is the version
1641 /// that handles any rank array with the dim argument specified.
1642 void fir::runtime::genIAnyDim(fir::FirOpBuilder &builder, mlir::Location loc,
1643 mlir::Value resultBox, mlir::Value arrayBox,
1644 mlir::Value dim, mlir::Value maskBox) {
1645 auto func = fir::runtime::getRuntimeFunc<mkRTKey(IAnyDim)>(loc, builder);
1646 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1647 }
1648
1649 /// Generate call to `IAny` intrinsic runtime routine. This is the version
1650 /// that does not take a dim argument.
1651 GEN_IALL_IANY_IPARITY(IAny)
1652
1653 /// Generate call to `IParityDim` intrinsic runtime routine. This is the version
1654 /// that handles any rank array with the dim argument specified.
1655 void fir::runtime::genIParityDim(fir::FirOpBuilder &builder, mlir::Location loc,
1656 mlir::Value resultBox, mlir::Value arrayBox,
1657 mlir::Value dim, mlir::Value maskBox) {
1658 auto func = fir::runtime::getRuntimeFunc<mkRTKey(IParityDim)>(loc, builder);
1659 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1660 }
1661
1662 /// Generate call to `IParity` intrinsic runtime routine. This is the version
1663 /// that does not take a dim argument.
1664 GEN_IALL_IANY_IPARITY(IParity)
1665
1666 /// Generate call to `Reduce` intrinsic runtime routine. This is the version
1667 /// that does not take a DIM argument and store result in the passed result
1668 /// value.
1669 void fir::runtime::genReduce(fir::FirOpBuilder &builder, mlir::Location loc,
1670 mlir::Value arrayBox, mlir::Value operation,
1671 mlir::Value maskBox, mlir::Value identity,
1672 mlir::Value ordered, mlir::Value resultBox,
1673 bool argByRef) {
1674 mlir::func::FuncOp func;
1675 auto ty = arrayBox.getType();
1676 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1677 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1678 auto dim = builder.createIntegerConstant(loc, builder.getI32Type(), 1);
1679
1680 assert(resultBox && "expect non null value for the result");
1681 assert((fir::isa_char(eleTy) || fir::isa_complex(eleTy) ||
1682 fir::isa_derived(eleTy)) &&
1683 "expect character, complex or derived-type");
1684
1685 mlir::MLIRContext *ctx = builder.getContext();
1686 fir::factory::CharacterExprHelper charHelper{builder, loc};
1687
1688 if (eleTy == fir::ComplexType::get(ctx, 2) && argByRef)
1689 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex2Ref)>(loc,
1690 builder);
1691 else if (eleTy == fir::ComplexType::get(ctx, 2) && !argByRef)
1692 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex2Value)>(
1693 loc, builder);
1694 else if (eleTy == fir::ComplexType::get(ctx, 3) && argByRef)
1695 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex3Ref)>(loc,
1696 builder);
1697 else if (eleTy == fir::ComplexType::get(ctx, 3) && !argByRef)
1698 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex3Value)>(
1699 loc, builder);
1700 else if (eleTy == fir::ComplexType::get(ctx, 4) && argByRef)
1701 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex4Ref)>(loc,
1702 builder);
1703 else if (eleTy == fir::ComplexType::get(ctx, 4) && !argByRef)
1704 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex4Value)>(
1705 loc, builder);
1706 else if (eleTy == fir::ComplexType::get(ctx, 8) && argByRef)
1707 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex8Ref)>(loc,
1708 builder);
1709 else if (eleTy == fir::ComplexType::get(ctx, 8) && !argByRef)
1710 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex8Value)>(
1711 loc, builder);
1712 else if (eleTy == fir::ComplexType::get(ctx, 10) && argByRef)
1713 func = fir::runtime::getRuntimeFunc<ForcedReduceComplex10>(loc, builder);
1714 else if (eleTy == fir::ComplexType::get(ctx, 10) && !argByRef)
1715 func =
1716 fir::runtime::getRuntimeFunc<ForcedReduceComplex10Value>(loc, builder);
1717 else if (eleTy == fir::ComplexType::get(ctx, 16) && argByRef)
1718 func = fir::runtime::getRuntimeFunc<ForcedReduceComplex16>(loc, builder);
1719 else if (eleTy == fir::ComplexType::get(ctx, 16) && !argByRef)
1720 func =
1721 fir::runtime::getRuntimeFunc<ForcedReduceComplex16Value>(loc, builder);
1722 else if (fir::isa_char(eleTy) && charHelper.getCharacterKind(eleTy) == 1)
1723 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceChar1)>(loc, builder);
1724 else if (fir::isa_char(eleTy) && charHelper.getCharacterKind(eleTy) == 2)
1725 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceChar2)>(loc, builder);
1726 else if (fir::isa_char(eleTy) && charHelper.getCharacterKind(eleTy) == 4)
1727 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceChar4)>(loc, builder);
1728 else if (fir::isa_derived(eleTy))
1729 func =
1730 fir::runtime::getRuntimeFunc<mkRTKey(ReduceDerivedType)>(loc, builder);
1731 else
1732 fir::intrinsicTypeTODO(builder, eleTy, loc, "REDUCE");
1733
1734 auto fTy = func.getFunctionType();
1735 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1736 auto sourceLine =
1737 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
1738 auto opAddr = builder.create<fir::BoxAddrOp>(loc, fTy.getInput(2), operation);
1739 auto args = fir::runtime::createArguments(
1740 builder, loc, fTy, resultBox, arrayBox, opAddr, sourceFile, sourceLine,
1741 dim, maskBox, identity, ordered);
1742 builder.create<fir::CallOp>(loc, func, args);
1743 }
1744
1745 /// Generate call to `Reduce` intrinsic runtime routine. This is the version
1746 /// that does not take DIM argument and return a scalar result.
1747 mlir::Value fir::runtime::genReduce(fir::FirOpBuilder &builder,
1748 mlir::Location loc, mlir::Value arrayBox,
1749 mlir::Value operation, mlir::Value maskBox,
1750 mlir::Value identity, mlir::Value ordered,
1751 bool argByRef) {
1752 mlir::func::FuncOp func;
1753 auto ty = arrayBox.getType();
1754 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1755 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1756 auto dim = builder.createIntegerConstant(loc, builder.getI32Type(), 1);
1757
1758 mlir::MLIRContext *ctx = builder.getContext();
1759 fir::factory::CharacterExprHelper charHelper{builder, loc};
1760
1761 assert((fir::isa_real(eleTy) || fir::isa_integer(eleTy) ||
1762 mlir::isa<fir::LogicalType>(eleTy)) &&
1763 "expect real, interger or logical");
1764
1765 if (eleTy.isF16() && argByRef)
1766 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal2Ref)>(loc, builder);
1767 else if (eleTy.isF16() && !argByRef)
1768 func =
1769 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal2Value)>(loc, builder);
1770 else if (eleTy.isBF16() && argByRef)
1771 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal3Ref)>(loc, builder);
1772 else if (eleTy.isBF16() && !argByRef)
1773 func =
1774 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal3Value)>(loc, builder);
1775 else if (eleTy.isF32() && argByRef)
1776 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal4Ref)>(loc, builder);
1777 else if (eleTy.isF32() && !argByRef)
1778 func =
1779 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal4Value)>(loc, builder);
1780 else if (eleTy.isF64() && argByRef)
1781 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal8Ref)>(loc, builder);
1782 else if (eleTy.isF64() && !argByRef)
1783 func =
1784 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal8Value)>(loc, builder);
1785 else if (eleTy.isF80() && argByRef)
1786 func = fir::runtime::getRuntimeFunc<ForcedReduceReal10>(loc, builder);
1787 else if (eleTy.isF80() && !argByRef)
1788 func = fir::runtime::getRuntimeFunc<ForcedReduceReal10Value>(loc, builder);
1789 else if (eleTy.isF128() && argByRef)
1790 func = fir::runtime::getRuntimeFunc<ForcedReduceReal16>(loc, builder);
1791 else if (eleTy.isF128() && !argByRef)
1792 func = fir::runtime::getRuntimeFunc<ForcedReduceReal16Value>(loc, builder);
1793 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)) &&
1794 argByRef)
1795 func =
1796 fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger1Ref)>(loc, builder);
1797 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)) &&
1798 !argByRef)
1799 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger1Value)>(loc,
1800 builder);
1801 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)) &&
1802 argByRef)
1803 func =
1804 fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger2Ref)>(loc, builder);
1805 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)) &&
1806 !argByRef)
1807 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger2Value)>(loc,
1808 builder);
1809 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)) &&
1810 argByRef)
1811 func =
1812 fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger4Ref)>(loc, builder);
1813 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)) &&
1814 !argByRef)
1815 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger4Value)>(loc,
1816 builder);
1817 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)) &&
1818 argByRef)
1819 func =
1820 fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger8Ref)>(loc, builder);
1821 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)) &&
1822 !argByRef)
1823 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger8Value)>(loc,
1824 builder);
1825 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)) &&
1826 argByRef)
1827 func = fir::runtime::getRuntimeFunc<ForcedReduceInteger16>(loc, builder);
1828 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)) &&
1829 !argByRef)
1830 func =
1831 fir::runtime::getRuntimeFunc<ForcedReduceInteger16Value>(loc, builder);
1832 else if (eleTy == fir::LogicalType::get(ctx, 1) && argByRef)
1833 func =
1834 fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical1Ref)>(loc, builder);
1835 else if (eleTy == fir::LogicalType::get(ctx, 1) && !argByRef)
1836 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical1Value)>(loc,
1837 builder);
1838 else if (eleTy == fir::LogicalType::get(ctx, 2) && argByRef)
1839 func =
1840 fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical2Ref)>(loc, builder);
1841 else if (eleTy == fir::LogicalType::get(ctx, 2) && !argByRef)
1842 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical2Value)>(loc,
1843 builder);
1844 else if (eleTy == fir::LogicalType::get(ctx, 4) && argByRef)
1845 func =
1846 fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical4Ref)>(loc, builder);
1847 else if (eleTy == fir::LogicalType::get(ctx, 4) && !argByRef)
1848 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical4Value)>(loc,
1849 builder);
1850 else if (eleTy == fir::LogicalType::get(ctx, 8) && argByRef)
1851 func =
1852 fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical8Ref)>(loc, builder);
1853 else if (eleTy == fir::LogicalType::get(ctx, 8) && !argByRef)
1854 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical8Value)>(loc,
1855 builder);
1856 else
1857 fir::intrinsicTypeTODO(builder, eleTy, loc, "REDUCE");
1858
1859 auto fTy = func.getFunctionType();
1860 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1861 auto sourceLine =
1862 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1863 auto opAddr = builder.create<fir::BoxAddrOp>(loc, fTy.getInput(1), operation);
1864 auto args = fir::runtime::createArguments(builder, loc, fTy, arrayBox, opAddr,
1865 sourceFile, sourceLine, dim,
1866 maskBox, identity, ordered);
1867 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1868 }
1869
1870 void fir::runtime::genReduceDim(fir::FirOpBuilder &builder, mlir::Location loc,
1871 mlir::Value arrayBox, mlir::Value operation,
1872 mlir::Value dim, mlir::Value maskBox,
1873 mlir::Value identity, mlir::Value ordered,
1874 mlir::Value resultBox, bool argByRef) {
1875 mlir::func::FuncOp func;
1876 auto ty = arrayBox.getType();
1877 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1878 auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
1879
1880 mlir::MLIRContext *ctx = builder.getContext();
1881 fir::factory::CharacterExprHelper charHelper{builder, loc};
1882
1883 if (eleTy.isF16() && argByRef)
1884 func =
1885 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal2DimRef)>(loc, builder);
1886 else if (eleTy.isF16() && !argByRef)
1887 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal2DimValue)>(loc,
1888 builder);
1889 else if (eleTy.isBF16() && argByRef)
1890 func =
1891 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal3DimRef)>(loc, builder);
1892 else if (eleTy.isBF16() && !argByRef)
1893 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal3DimValue)>(loc,
1894 builder);
1895 else if (eleTy.isF32() && argByRef)
1896 func =
1897 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal4DimRef)>(loc, builder);
1898 else if (eleTy.isF32() && !argByRef)
1899 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal4DimValue)>(loc,
1900 builder);
1901 else if (eleTy.isF64() && argByRef)
1902 func =
1903 fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal8DimRef)>(loc, builder);
1904 else if (eleTy.isF64() && !argByRef)
1905 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceReal8DimValue)>(loc,
1906 builder);
1907 else if (eleTy.isF80() && argByRef)
1908 func = fir::runtime::getRuntimeFunc<ForcedReduceReal10Dim>(loc, builder);
1909 else if (eleTy.isF80() && !argByRef)
1910 func =
1911 fir::runtime::getRuntimeFunc<ForcedReduceReal10DimValue>(loc, builder);
1912 else if (eleTy.isF128() && argByRef)
1913 func = fir::runtime::getRuntimeFunc<ForcedReduceReal16Dim>(loc, builder);
1914 else if (eleTy.isF128() && !argByRef)
1915 func =
1916 fir::runtime::getRuntimeFunc<ForcedReduceReal16DimValue>(loc, builder);
1917 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)) &&
1918 argByRef)
1919 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger1DimRef)>(loc,
1920 builder);
1921 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)) &&
1922 !argByRef)
1923 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger1DimValue)>(
1924 loc, builder);
1925 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)) &&
1926 argByRef)
1927 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger2DimRef)>(loc,
1928 builder);
1929 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)) &&
1930 !argByRef)
1931 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger2DimValue)>(
1932 loc, builder);
1933 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)) &&
1934 argByRef)
1935 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger4DimRef)>(loc,
1936 builder);
1937 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)) &&
1938 !argByRef)
1939 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger4DimValue)>(
1940 loc, builder);
1941 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)) &&
1942 argByRef)
1943 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger8DimRef)>(loc,
1944 builder);
1945 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)) &&
1946 !argByRef)
1947 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceInteger8DimValue)>(
1948 loc, builder);
1949 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)) &&
1950 argByRef)
1951 func = fir::runtime::getRuntimeFunc<ForcedReduceInteger16Dim>(loc, builder);
1952 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)) &&
1953 !argByRef)
1954 func = fir::runtime::getRuntimeFunc<ForcedReduceInteger16DimValue>(loc,
1955 builder);
1956 else if (eleTy == fir::ComplexType::get(ctx, 2) && argByRef)
1957 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex2DimRef)>(
1958 loc, builder);
1959 else if (eleTy == fir::ComplexType::get(ctx, 2) && !argByRef)
1960 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex2DimValue)>(
1961 loc, builder);
1962 else if (eleTy == fir::ComplexType::get(ctx, 3) && argByRef)
1963 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex3DimRef)>(
1964 loc, builder);
1965 else if (eleTy == fir::ComplexType::get(ctx, 3) && !argByRef)
1966 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex3DimValue)>(
1967 loc, builder);
1968 else if (eleTy == fir::ComplexType::get(ctx, 4) && argByRef)
1969 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex4DimRef)>(
1970 loc, builder);
1971 else if (eleTy == fir::ComplexType::get(ctx, 4) && !argByRef)
1972 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex4DimValue)>(
1973 loc, builder);
1974 else if (eleTy == fir::ComplexType::get(ctx, 8) && argByRef)
1975 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex8DimRef)>(
1976 loc, builder);
1977 else if (eleTy == fir::ComplexType::get(ctx, 8) && !argByRef)
1978 func = fir::runtime::getRuntimeFunc<mkRTKey(CppReduceComplex8DimValue)>(
1979 loc, builder);
1980 else if (eleTy == fir::ComplexType::get(ctx, 10) && argByRef)
1981 func = fir::runtime::getRuntimeFunc<ForcedReduceComplex10Dim>(loc, builder);
1982 else if (eleTy == fir::ComplexType::get(ctx, 10) && !argByRef)
1983 func = fir::runtime::getRuntimeFunc<ForcedReduceComplex10DimValue>(loc,
1984 builder);
1985 else if (eleTy == fir::ComplexType::get(ctx, 16) && argByRef)
1986 func = fir::runtime::getRuntimeFunc<ForcedReduceComplex16Dim>(loc, builder);
1987 else if (eleTy == fir::ComplexType::get(ctx, 16) && !argByRef)
1988 func = fir::runtime::getRuntimeFunc<ForcedReduceComplex16DimValue>(loc,
1989 builder);
1990 else if (eleTy == fir::LogicalType::get(ctx, 1) && argByRef)
1991 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical1DimRef)>(loc,
1992 builder);
1993 else if (eleTy == fir::LogicalType::get(ctx, 1) && !argByRef)
1994 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical1DimValue)>(
1995 loc, builder);
1996 else if (eleTy == fir::LogicalType::get(ctx, 2) && argByRef)
1997 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical2DimRef)>(loc,
1998 builder);
1999 else if (eleTy == fir::LogicalType::get(ctx, 2) && !argByRef)
2000 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical2DimValue)>(
2001 loc, builder);
2002 else if (eleTy == fir::LogicalType::get(ctx, 4) && argByRef)
2003 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical4DimRef)>(loc,
2004 builder);
2005 else if (eleTy == fir::LogicalType::get(ctx, 4) && !argByRef)
2006 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical4DimValue)>(
2007 loc, builder);
2008 else if (eleTy == fir::LogicalType::get(ctx, 8) && argByRef)
2009 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical8DimRef)>(loc,
2010 builder);
2011 else if (eleTy == fir::LogicalType::get(ctx, 8) && !argByRef)
2012 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceLogical8DimValue)>(
2013 loc, builder);
2014 else if (fir::isa_char(eleTy) && charHelper.getCharacterKind(eleTy) == 1)
2015 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceCharacter1Dim)>(loc,
2016 builder);
2017 else if (fir::isa_char(eleTy) && charHelper.getCharacterKind(eleTy) == 2)
2018 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceCharacter2Dim)>(loc,
2019 builder);
2020 else if (fir::isa_char(eleTy) && charHelper.getCharacterKind(eleTy) == 4)
2021 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceCharacter4Dim)>(loc,
2022 builder);
2023 else if (fir::isa_derived(eleTy))
2024 func = fir::runtime::getRuntimeFunc<mkRTKey(ReduceDerivedTypeDim)>(loc,
2025 builder);
2026 else
2027 fir::intrinsicTypeTODO(builder, eleTy, loc, "REDUCE");
2028
2029 auto fTy = func.getFunctionType();
2030 auto sourceFile = fir::factory::locationToFilename(builder, loc);
2031
2032 auto sourceLine =
2033 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
2034 auto opAddr = builder.create<fir::BoxAddrOp>(loc, fTy.getInput(2), operation);
2035 auto args = fir::runtime::createArguments(
2036 builder, loc, fTy, resultBox, arrayBox, opAddr, sourceFile, sourceLine,
2037 dim, maskBox, identity, ordered);
2038 builder.create<fir::CallOp>(loc, func, args);
2039 }