Debian Codegraph @

llvm-project/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/Builder/Todo.h"
15 #include "flang/Runtime/reduction.h"
16 #include "mlir/Dialect/Func/IR/FuncOps.h"
17
18 using namespace Fortran::runtime;
19
20 #define STRINGIFY(S) #S
21 #define JOIN2(A, B) A##B
22 #define JOIN3(A, B, C) A##B##C
23
24 /// Placeholder for real*10 version of Maxval Intrinsic
25 struct ForcedMaxvalReal10 {
26 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MaxvalReal10));
27 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
28 return [](mlir::MLIRContext *ctx) {
29 auto ty = mlir::FloatType::getF80(ctx);
30 auto boxTy =
31 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
32 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
33 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
34 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
35 {ty});
36 };
37 }
38 };
39
40 /// Placeholder for real*16 version of Maxval Intrinsic
41 struct ForcedMaxvalReal16 {
42 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MaxvalReal16));
43 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
44 return [](mlir::MLIRContext *ctx) {
45 auto ty = mlir::FloatType::getF128(ctx);
46 auto boxTy =
47 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
48 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
49 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
50 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
51 {ty});
52 };
53 }
54 };
55
56 /// Placeholder for integer*16 version of Maxval Intrinsic
57 struct ForcedMaxvalInteger16 {
58 static constexpr const char *name =
59 ExpandAndQuoteKey(RTNAME(MaxvalInteger16));
60 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
61 return [](mlir::MLIRContext *ctx) {
62 auto ty = mlir::IntegerType::get(ctx, 128);
63 auto boxTy =
64 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
65 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
66 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
67 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
68 {ty});
69 };
70 }
71 };
72
73 /// Placeholder for real*10 version of Minval Intrinsic
74 struct ForcedMinvalReal10 {
75 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MinvalReal10));
76 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
77 return [](mlir::MLIRContext *ctx) {
78 auto ty = mlir::FloatType::getF80(ctx);
79 auto boxTy =
80 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
81 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
82 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
83 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
84 {ty});
85 };
86 }
87 };
88
89 /// Placeholder for real*16 version of Minval Intrinsic
90 struct ForcedMinvalReal16 {
91 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(MinvalReal16));
92 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
93 return [](mlir::MLIRContext *ctx) {
94 auto ty = mlir::FloatType::getF128(ctx);
95 auto boxTy =
96 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
97 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
98 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
99 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
100 {ty});
101 };
102 }
103 };
104
105 /// Placeholder for integer*16 version of Minval Intrinsic
106 struct ForcedMinvalInteger16 {
107 static constexpr const char *name =
108 ExpandAndQuoteKey(RTNAME(MinvalInteger16));
109 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
110 return [](mlir::MLIRContext *ctx) {
111 auto ty = mlir::IntegerType::get(ctx, 128);
112 auto boxTy =
113 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
114 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
115 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
116 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
117 {ty});
118 };
119 }
120 };
121
122 /// Placeholder for real*10 version of Norm2 Intrinsic
123 struct ForcedNorm2Real10 {
124 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Norm2_10));
125 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
126 return [](mlir::MLIRContext *ctx) {
127 auto ty = mlir::FloatType::getF80(ctx);
128 auto boxTy =
129 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
130 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
131 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
132 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy}, {ty});
133 };
134 }
135 };
136
137 /// Placeholder for real*16 version of Norm2 Intrinsic
138 struct ForcedNorm2Real16 {
139 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(Norm2_16));
140 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
141 return [](mlir::MLIRContext *ctx) {
142 auto ty = mlir::FloatType::getF128(ctx);
143 auto boxTy =
144 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
145 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
146 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
147 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy}, {ty});
148 };
149 }
150 };
151
152 /// Placeholder for real*10 version of Product Intrinsic
153 struct ForcedProductReal10 {
154 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(ProductReal10));
155 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
156 return [](mlir::MLIRContext *ctx) {
157 auto ty = mlir::FloatType::getF80(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(ctx, {boxTy, strTy, intTy, intTy, boxTy},
163 {ty});
164 };
165 }
166 };
167
168 /// Placeholder for real*16 version of Product Intrinsic
169 struct ForcedProductReal16 {
170 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(ProductReal16));
171 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
172 return [](mlir::MLIRContext *ctx) {
173 auto ty = mlir::FloatType::getF128(ctx);
174 auto boxTy =
175 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
176 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
177 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
178 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
179 {ty});
180 };
181 }
182 };
183
184 /// Placeholder for integer*16 version of Product Intrinsic
185 struct ForcedProductInteger16 {
186 static constexpr const char *name =
187 ExpandAndQuoteKey(RTNAME(ProductInteger16));
188 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
189 return [](mlir::MLIRContext *ctx) {
190 auto ty = mlir::IntegerType::get(ctx, 128);
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 complex(10) version of Product Intrinsic
202 struct ForcedProductComplex10 {
203 static constexpr const char *name =
204 ExpandAndQuoteKey(RTNAME(CppProductComplex10));
205 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
206 return [](mlir::MLIRContext *ctx) {
207 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
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 auto resTy = fir::ReferenceType::get(ty);
213 return mlir::FunctionType::get(
214 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
215 };
216 }
217 };
218
219 /// Placeholder for complex(16) version of Product Intrinsic
220 struct ForcedProductComplex16 {
221 static constexpr const char *name =
222 ExpandAndQuoteKey(RTNAME(CppProductComplex16));
223 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
224 return [](mlir::MLIRContext *ctx) {
225 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
226 auto boxTy =
227 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
228 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
229 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
230 auto resTy = fir::ReferenceType::get(ty);
231 return mlir::FunctionType::get(
232 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
233 };
234 }
235 };
236
237 /// Placeholder for real*10 version of DotProduct Intrinsic
238 struct ForcedDotProductReal10 {
239 static constexpr const char *name =
240 ExpandAndQuoteKey(RTNAME(DotProductReal10));
241 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
242 return [](mlir::MLIRContext *ctx) {
243 auto ty = mlir::FloatType::getF80(ctx);
244 auto boxTy =
245 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
246 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
247 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
248 return mlir::FunctionType::get(ctx, {boxTy, boxTy, strTy, intTy}, {ty});
249 };
250 }
251 };
252
253 /// Placeholder for real*16 version of DotProduct Intrinsic
254 struct ForcedDotProductReal16 {
255 static constexpr const char *name =
256 ExpandAndQuoteKey(RTNAME(DotProductReal16));
257 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
258 return [](mlir::MLIRContext *ctx) {
259 auto ty = mlir::FloatType::getF128(ctx);
260 auto boxTy =
261 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
262 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
263 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
264 return mlir::FunctionType::get(ctx, {boxTy, boxTy, strTy, intTy}, {ty});
265 };
266 }
267 };
268
269 /// Placeholder for complex(10) version of DotProduct Intrinsic
270 struct ForcedDotProductComplex10 {
271 static constexpr const char *name =
272 ExpandAndQuoteKey(RTNAME(CppDotProductComplex10));
273 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
274 return [](mlir::MLIRContext *ctx) {
275 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
276 auto boxTy =
277 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
278 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
279 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
280 auto resTy = fir::ReferenceType::get(ty);
281 return mlir::FunctionType::get(ctx, {resTy, boxTy, boxTy, strTy, intTy},
282 {});
283 };
284 }
285 };
286
287 /// Placeholder for complex(16) version of DotProduct Intrinsic
288 struct ForcedDotProductComplex16 {
289 static constexpr const char *name =
290 ExpandAndQuoteKey(RTNAME(CppDotProductComplex16));
291 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
292 return [](mlir::MLIRContext *ctx) {
293 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
294 auto boxTy =
295 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
296 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
297 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
298 auto resTy = fir::ReferenceType::get(ty);
299 return mlir::FunctionType::get(ctx, {resTy, boxTy, boxTy, strTy, intTy},
300 {});
301 };
302 }
303 };
304
305 /// Placeholder for integer*16 version of DotProduct Intrinsic
306 struct ForcedDotProductInteger16 {
307 static constexpr const char *name =
308 ExpandAndQuoteKey(RTNAME(DotProductInteger16));
309 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
310 return [](mlir::MLIRContext *ctx) {
311 auto ty = mlir::IntegerType::get(ctx, 128);
312 auto boxTy =
313 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
314 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
315 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
316 return mlir::FunctionType::get(ctx, {boxTy, boxTy, strTy, intTy}, {ty});
317 };
318 }
319 };
320
321 /// Placeholder for real*10 version of Sum Intrinsic
322 struct ForcedSumReal10 {
323 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SumReal10));
324 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
325 return [](mlir::MLIRContext *ctx) {
326 auto ty = mlir::FloatType::getF80(ctx);
327 auto boxTy =
328 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
329 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
330 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
331 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
332 {ty});
333 };
334 }
335 };
336
337 /// Placeholder for real*16 version of Sum Intrinsic
338 struct ForcedSumReal16 {
339 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SumReal16));
340 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
341 return [](mlir::MLIRContext *ctx) {
342 auto ty = mlir::FloatType::getF128(ctx);
343 auto boxTy =
344 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
345 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
346 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
347 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
348 {ty});
349 };
350 }
351 };
352
353 /// Placeholder for integer*16 version of Sum Intrinsic
354 struct ForcedSumInteger16 {
355 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(SumInteger16));
356 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
357 return [](mlir::MLIRContext *ctx) {
358 auto ty = mlir::IntegerType::get(ctx, 128);
359 auto boxTy =
360 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
361 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
362 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
363 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
364 {ty});
365 };
366 }
367 };
368
369 /// Placeholder for complex(10) version of Sum Intrinsic
370 struct ForcedSumComplex10 {
371 static constexpr const char *name =
372 ExpandAndQuoteKey(RTNAME(CppSumComplex10));
373 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
374 return [](mlir::MLIRContext *ctx) {
375 auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx));
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 auto resTy = fir::ReferenceType::get(ty);
381 return mlir::FunctionType::get(
382 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
383 };
384 }
385 };
386
387 /// Placeholder for complex(16) version of Sum Intrinsic
388 struct ForcedSumComplex16 {
389 static constexpr const char *name =
390 ExpandAndQuoteKey(RTNAME(CppSumComplex16));
391 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
392 return [](mlir::MLIRContext *ctx) {
393 auto ty = mlir::ComplexType::get(mlir::FloatType::getF128(ctx));
394 auto boxTy =
395 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
396 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
397 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
398 auto resTy = fir::ReferenceType::get(ty);
399 return mlir::FunctionType::get(
400 ctx, {resTy, boxTy, strTy, intTy, intTy, boxTy}, {});
401 };
402 }
403 };
404
405 /// Placeholder for integer(16) version of IAll Intrinsic
406 struct ForcedIAll16 {
407 static constexpr const char *name = EXPAND_AND_QUOTE_KEY(IAll16);
408 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
409 return [](mlir::MLIRContext *ctx) {
410 auto ty = mlir::IntegerType::get(ctx, 128);
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 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
416 {ty});
417 };
418 }
419 };
420
421 /// Placeholder for integer(16) version of IAny Intrinsic
422 struct ForcedIAny16 {
423 static constexpr const char *name = EXPAND_AND_QUOTE_KEY(IAny16);
424 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
425 return [](mlir::MLIRContext *ctx) {
426 auto ty = mlir::IntegerType::get(ctx, 128);
427 auto boxTy =
428 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
429 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
430 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
431 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
432 {ty});
433 };
434 }
435 };
436
437 /// Placeholder for integer(16) version of IParity Intrinsic
438 struct ForcedIParity16 {
439 static constexpr const char *name = EXPAND_AND_QUOTE_KEY(IParity16);
440 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
441 return [](mlir::MLIRContext *ctx) {
442 auto ty = mlir::IntegerType::get(ctx, 128);
443 auto boxTy =
444 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
445 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
446 auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int));
447 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy, intTy, boxTy},
448 {ty});
449 };
450 }
451 };
452
453 /// Generate call to specialized runtime function that takes a mask and
454 /// dim argument. The All, Any, and Count intrinsics use this pattern.
455 template <typename FN>
456 mlir::Value genSpecial2Args(FN func, fir::FirOpBuilder &builder,
457 mlir::Location loc, mlir::Value maskBox,
458 mlir::Value dim) {
459 auto fTy = func.getFunctionType();
460 auto sourceFile = fir::factory::locationToFilename(builder, loc);
461 auto sourceLine =
462 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
463 auto args = fir::runtime::createArguments(builder, loc, fTy, maskBox,
464 sourceFile, sourceLine, dim);
465 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
466 }
467
468 /// Generate calls to reduction intrinsics such as All and Any.
469 /// These are the descriptor based implementations that take two
470 /// arguments (mask, dim).
471 template <typename FN>
472 static void genReduction2Args(FN func, fir::FirOpBuilder &builder,
473 mlir::Location loc, mlir::Value resultBox,
474 mlir::Value maskBox, mlir::Value dim) {
475 auto fTy = func.getFunctionType();
476 auto sourceFile = fir::factory::locationToFilename(builder, loc);
477 auto sourceLine =
478 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
479 auto args = fir::runtime::createArguments(
480 builder, loc, fTy, resultBox, maskBox, dim, sourceFile, sourceLine);
481 builder.create<fir::CallOp>(loc, func, args);
482 }
483
484 /// Generate calls to reduction intrinsics such as Maxval and Minval.
485 /// These take arguments such as (array, dim, mask).
486 template <typename FN>
487 static void genReduction3Args(FN func, fir::FirOpBuilder &builder,
488 mlir::Location loc, mlir::Value resultBox,
489 mlir::Value arrayBox, mlir::Value dim,
490 mlir::Value maskBox) {
491
492 auto fTy = func.getFunctionType();
493 auto sourceFile = fir::factory::locationToFilename(builder, loc);
494 auto sourceLine =
495 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
496 auto args =
497 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox, dim,
498 sourceFile, sourceLine, maskBox);
499 builder.create<fir::CallOp>(loc, func, args);
500 }
501
502 /// Generate calls to reduction intrinsics such as Maxloc and Minloc.
503 /// These take arguments such as (array, mask, kind, back).
504 template <typename FN>
505 static void genReduction4Args(FN func, fir::FirOpBuilder &builder,
506 mlir::Location loc, mlir::Value resultBox,
507 mlir::Value arrayBox, mlir::Value maskBox,
508 mlir::Value kind, mlir::Value back) {
509 auto fTy = func.getFunctionType();
510 auto sourceFile = fir::factory::locationToFilename(builder, loc);
511 auto sourceLine =
512 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
513 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
514 arrayBox, kind, sourceFile,
515 sourceLine, maskBox, back);
516 builder.create<fir::CallOp>(loc, func, args);
517 }
518
519 /// Generate calls to reduction intrinsics such as Maxloc and Minloc.
520 /// These take arguments such as (array, dim, mask, kind, back).
521 template <typename FN>
522 static void
523 genReduction5Args(FN func, fir::FirOpBuilder &builder, mlir::Location loc,
524 mlir::Value resultBox, mlir::Value arrayBox, mlir::Value dim,
525 mlir::Value maskBox, mlir::Value kind, mlir::Value back) {
526 auto fTy = func.getFunctionType();
527 auto sourceFile = fir::factory::locationToFilename(builder, loc);
528 auto sourceLine =
529 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
530 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
531 arrayBox, kind, dim, sourceFile,
532 sourceLine, maskBox, back);
533 builder.create<fir::CallOp>(loc, func, args);
534 }
535
536 /// Generate call to `AllDim` runtime routine.
537 /// This calls the descriptor based runtime call implementation of the `all`
538 /// intrinsic.
539 void fir::runtime::genAllDescriptor(fir::FirOpBuilder &builder,
540 mlir::Location loc, mlir::Value resultBox,
541 mlir::Value maskBox, mlir::Value dim) {
542 auto allFunc = fir::runtime::getRuntimeFunc<mkRTKey(AllDim)>(loc, builder);
543 genReduction2Args(allFunc, builder, loc, resultBox, maskBox, dim);
544 }
545
546 /// Generate call to `AnyDim` runtime routine.
547 /// This calls the descriptor based runtime call implementation of the `any`
548 /// intrinsic.
549 void fir::runtime::genAnyDescriptor(fir::FirOpBuilder &builder,
550 mlir::Location loc, mlir::Value resultBox,
551 mlir::Value maskBox, mlir::Value dim) {
552 auto anyFunc = fir::runtime::getRuntimeFunc<mkRTKey(AnyDim)>(loc, builder);
553 genReduction2Args(anyFunc, builder, loc, resultBox, maskBox, dim);
554 }
555
556 /// Generate call to `ParityDim` runtime routine.
557 /// This calls the descriptor based runtime call implementation of the `parity`
558 /// intrinsic.
559 void fir::runtime::genParityDescriptor(fir::FirOpBuilder &builder,
560 mlir::Location loc,
561 mlir::Value resultBox,
562 mlir::Value maskBox, mlir::Value dim) {
563 auto parityFunc =
564 fir::runtime::getRuntimeFunc<mkRTKey(ParityDim)>(loc, builder);
565 genReduction2Args(parityFunc, builder, loc, resultBox, maskBox, dim);
566 }
567
568 /// Generate call to `All` intrinsic runtime routine. This routine is
569 /// specialized for mask arguments with rank == 1.
570 mlir::Value fir::runtime::genAll(fir::FirOpBuilder &builder, mlir::Location loc,
571 mlir::Value maskBox, mlir::Value dim) {
572 auto allFunc = fir::runtime::getRuntimeFunc<mkRTKey(All)>(loc, builder);
573 return genSpecial2Args(allFunc, builder, loc, maskBox, dim);
574 }
575
576 /// Generate call to `Any` intrinsic runtime routine. This routine is
577 /// specialized for mask arguments with rank == 1.
578 mlir::Value fir::runtime::genAny(fir::FirOpBuilder &builder, mlir::Location loc,
579 mlir::Value maskBox, mlir::Value dim) {
580 auto anyFunc = fir::runtime::getRuntimeFunc<mkRTKey(Any)>(loc, builder);
581 return genSpecial2Args(anyFunc, builder, loc, maskBox, dim);
582 }
583
584 /// Generate call to `Count` runtime routine. This routine is a specialized
585 /// version when mask is a rank one array or the dim argument is not
586 /// specified by the user.
587 mlir::Value fir::runtime::genCount(fir::FirOpBuilder &builder,
588 mlir::Location loc, mlir::Value maskBox,
589 mlir::Value dim) {
590 auto countFunc = fir::runtime::getRuntimeFunc<mkRTKey(Count)>(loc, builder);
591 return genSpecial2Args(countFunc, builder, loc, maskBox, dim);
592 }
593
594 /// Generate call to general `CountDim` runtime routine. This routine has a
595 /// descriptor result.
596 void fir::runtime::genCountDim(fir::FirOpBuilder &builder, mlir::Location loc,
597 mlir::Value resultBox, mlir::Value maskBox,
598 mlir::Value dim, mlir::Value kind) {
599 auto func = fir::runtime::getRuntimeFunc<mkRTKey(CountDim)>(loc, builder);
600 auto fTy = func.getFunctionType();
601 auto sourceFile = fir::factory::locationToFilename(builder, loc);
602 auto sourceLine =
603 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
604 auto args = fir::runtime::createArguments(
605 builder, loc, fTy, resultBox, maskBox, dim, kind, sourceFile, sourceLine);
606 builder.create<fir::CallOp>(loc, func, args);
607 }
608
609 /// Generate call to `Findloc` intrinsic runtime routine. This is the version
610 /// that does not take a dim argument.
611 void fir::runtime::genFindloc(fir::FirOpBuilder &builder, mlir::Location loc,
612 mlir::Value resultBox, mlir::Value arrayBox,
613 mlir::Value valBox, mlir::Value maskBox,
614 mlir::Value kind, mlir::Value back) {
615 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Findloc)>(loc, builder);
616 auto fTy = func.getFunctionType();
617 auto sourceFile = fir::factory::locationToFilename(builder, loc);
618 auto sourceLine =
619 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
620 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
621 arrayBox, valBox, kind, sourceFile,
622 sourceLine, maskBox, back);
623 builder.create<fir::CallOp>(loc, func, args);
624 }
625
626 /// Generate call to `FindlocDim` intrinsic runtime routine. This is the version
627 /// that takes a dim argument.
628 void fir::runtime::genFindlocDim(fir::FirOpBuilder &builder, mlir::Location loc,
629 mlir::Value resultBox, mlir::Value arrayBox,
630 mlir::Value valBox, mlir::Value dim,
631 mlir::Value maskBox, mlir::Value kind,
632 mlir::Value back) {
633 auto func = fir::runtime::getRuntimeFunc<mkRTKey(FindlocDim)>(loc, builder);
634 auto fTy = func.getFunctionType();
635 auto sourceFile = fir::factory::locationToFilename(builder, loc);
636 auto sourceLine =
637 fir::factory::locationToLineNo(builder, loc, fTy.getInput(6));
638 auto args = fir::runtime::createArguments(
639 builder, loc, fTy, resultBox, arrayBox, valBox, kind, dim, sourceFile,
640 sourceLine, maskBox, back);
641 builder.create<fir::CallOp>(loc, func, args);
642 }
643
644 /// Generate call to `Maxloc` intrinsic runtime routine. This is the version
645 /// that does not take a dim argument.
646 void fir::runtime::genMaxloc(fir::FirOpBuilder &builder, mlir::Location loc,
647 mlir::Value resultBox, mlir::Value arrayBox,
648 mlir::Value maskBox, mlir::Value kind,
649 mlir::Value back) {
650 mlir::func::FuncOp func;
651 auto ty = arrayBox.getType();
652 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
653 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
654 fir::factory::CharacterExprHelper charHelper{builder, loc};
655 if (eleTy.isF16() || eleTy.isBF16())
656 TODO(loc, "half-precision MAXLOC");
657 else if (eleTy.isF32())
658 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal4)>(loc, builder);
659 else if (eleTy.isF64())
660 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal8)>(loc, builder);
661 else if (eleTy.isF80())
662 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal10)>(loc, builder);
663 else if (eleTy.isF128())
664 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal16)>(loc, builder);
665 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
666 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger1)>(loc, builder);
667 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
668 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger2)>(loc, builder);
669 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
670 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger4)>(loc, builder);
671 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
672 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger8)>(loc, builder);
673 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
674 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocInteger16)>(loc, builder);
675 else if (charHelper.isCharacterScalar(eleTy))
676 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocCharacter)>(loc, builder);
677 else
678 fir::emitFatalError(loc, "invalid type in MAXLOC");
679 genReduction4Args(func, builder, loc, resultBox, arrayBox, maskBox, kind,
680 back);
681 }
682
683 /// Generate call to `MaxlocDim` intrinsic runtime routine. This is the version
684 /// that takes a dim argument.
685 void fir::runtime::genMaxlocDim(fir::FirOpBuilder &builder, mlir::Location loc,
686 mlir::Value resultBox, mlir::Value arrayBox,
687 mlir::Value dim, mlir::Value maskBox,
688 mlir::Value kind, mlir::Value back) {
689 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocDim)>(loc, builder);
690 genReduction5Args(func, builder, loc, resultBox, arrayBox, dim, maskBox, kind,
691 back);
692 }
693
694 /// Generate call to `Maxval` intrinsic runtime routine. This is the version
695 /// that does not take a dim argument.
696 mlir::Value fir::runtime::genMaxval(fir::FirOpBuilder &builder,
697 mlir::Location loc, mlir::Value arrayBox,
698 mlir::Value maskBox) {
699 mlir::func::FuncOp func;
700 auto ty = arrayBox.getType();
701 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
702 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
703 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
704
705 if (eleTy.isF16() || eleTy.isBF16())
706 TODO(loc, "half-precision MAXVAL");
707 else if (eleTy.isF32())
708 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalReal4)>(loc, builder);
709 else if (eleTy.isF64())
710 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalReal8)>(loc, builder);
711 else if (eleTy.isF80())
712 func = fir::runtime::getRuntimeFunc<ForcedMaxvalReal10>(loc, builder);
713 else if (eleTy.isF128())
714 func = fir::runtime::getRuntimeFunc<ForcedMaxvalReal16>(loc, builder);
715 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
716 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger1)>(loc, builder);
717 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
718 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger2)>(loc, builder);
719 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
720 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger4)>(loc, builder);
721 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
722 func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalInteger8)>(loc, builder);
723 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
724 func = fir::runtime::getRuntimeFunc<ForcedMaxvalInteger16>(loc, builder);
725 else
726 fir::emitFatalError(loc, "invalid type in MAXVAL");
727
728 auto fTy = func.getFunctionType();
729 auto sourceFile = fir::factory::locationToFilename(builder, loc);
730 auto sourceLine =
731 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
732 auto args = fir::runtime::createArguments(
733 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
734
735 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
736 }
737
738 /// Generate call to `MaxvalDim` intrinsic runtime routine. This is the version
739 /// that handles any rank array with the dim argument specified.
740 void fir::runtime::genMaxvalDim(fir::FirOpBuilder &builder, mlir::Location loc,
741 mlir::Value resultBox, mlir::Value arrayBox,
742 mlir::Value dim, mlir::Value maskBox) {
743 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MaxvalDim)>(loc, builder);
744 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
745 }
746
747 /// Generate call to `MaxvalCharacter` intrinsic runtime routine. This is the
748 /// version that handles character arrays of rank 1 and without a DIM argument.
749 void fir::runtime::genMaxvalChar(fir::FirOpBuilder &builder, mlir::Location loc,
750 mlir::Value resultBox, mlir::Value arrayBox,
751 mlir::Value maskBox) {
752 auto func =
753 fir::runtime::getRuntimeFunc<mkRTKey(MaxvalCharacter)>(loc, builder);
754 auto fTy = func.getFunctionType();
755 auto sourceFile = fir::factory::locationToFilename(builder, loc);
756 auto sourceLine =
757 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
758 auto args = fir::runtime::createArguments(
759 builder, loc, fTy, resultBox, arrayBox, sourceFile, sourceLine, maskBox);
760 builder.create<fir::CallOp>(loc, func, args);
761 }
762
763 /// Generate call to `Minloc` intrinsic runtime routine. This is the version
764 /// that does not take a dim argument.
765 void fir::runtime::genMinloc(fir::FirOpBuilder &builder, mlir::Location loc,
766 mlir::Value resultBox, mlir::Value arrayBox,
767 mlir::Value maskBox, mlir::Value kind,
768 mlir::Value back) {
769 mlir::func::FuncOp func;
770 auto ty = arrayBox.getType();
771 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
772 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
773 fir::factory::CharacterExprHelper charHelper{builder, loc};
774 if (eleTy.isF16() || eleTy.isBF16())
775 TODO(loc, "half-precision MINLOC");
776 else if (eleTy.isF32())
777 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal4)>(loc, builder);
778 else if (eleTy.isF64())
779 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal8)>(loc, builder);
780 else if (eleTy.isF80())
781 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal10)>(loc, builder);
782 else if (eleTy.isF128())
783 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal16)>(loc, builder);
784 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
785 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger1)>(loc, builder);
786 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
787 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger2)>(loc, builder);
788 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
789 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger4)>(loc, builder);
790 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
791 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger8)>(loc, builder);
792 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
793 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocInteger16)>(loc, builder);
794 else if (charHelper.isCharacterScalar(eleTy))
795 func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocCharacter)>(loc, builder);
796 else
797 fir::emitFatalError(loc, "invalid type in MINLOC");
798 genReduction4Args(func, builder, loc, resultBox, arrayBox, maskBox, kind,
799 back);
800 }
801
802 /// Generate call to `MinlocDim` intrinsic runtime routine. This is the version
803 /// that takes a dim argument.
804 void fir::runtime::genMinlocDim(fir::FirOpBuilder &builder, mlir::Location loc,
805 mlir::Value resultBox, mlir::Value arrayBox,
806 mlir::Value dim, mlir::Value maskBox,
807 mlir::Value kind, mlir::Value back) {
808 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocDim)>(loc, builder);
809 genReduction5Args(func, builder, loc, resultBox, arrayBox, dim, maskBox, kind,
810 back);
811 }
812
813 /// Generate call to `MinvalDim` intrinsic runtime routine. This is the version
814 /// that handles any rank array with the dim argument specified.
815 void fir::runtime::genMinvalDim(fir::FirOpBuilder &builder, mlir::Location loc,
816 mlir::Value resultBox, mlir::Value arrayBox,
817 mlir::Value dim, mlir::Value maskBox) {
818 auto func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalDim)>(loc, builder);
819 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
820 }
821
822 /// Generate call to `MinvalCharacter` intrinsic runtime routine. This is the
823 /// version that handles character arrays of rank 1 and without a DIM argument.
824 void fir::runtime::genMinvalChar(fir::FirOpBuilder &builder, mlir::Location loc,
825 mlir::Value resultBox, mlir::Value arrayBox,
826 mlir::Value maskBox) {
827 auto func =
828 fir::runtime::getRuntimeFunc<mkRTKey(MinvalCharacter)>(loc, builder);
829 auto fTy = func.getFunctionType();
830 auto sourceFile = fir::factory::locationToFilename(builder, loc);
831 auto sourceLine =
832 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
833 auto args = fir::runtime::createArguments(
834 builder, loc, fTy, resultBox, arrayBox, sourceFile, sourceLine, maskBox);
835 builder.create<fir::CallOp>(loc, func, args);
836 }
837
838 /// Generate call to `Minval` intrinsic runtime routine. This is the version
839 /// that does not take a dim argument.
840 mlir::Value fir::runtime::genMinval(fir::FirOpBuilder &builder,
841 mlir::Location loc, mlir::Value arrayBox,
842 mlir::Value maskBox) {
843 mlir::func::FuncOp func;
844 auto ty = arrayBox.getType();
845 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
846 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
847 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
848
849 if (eleTy.isF16() || eleTy.isBF16())
850 TODO(loc, "half-precision MINVAL");
851 else if (eleTy.isF32())
852 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalReal4)>(loc, builder);
853 else if (eleTy.isF64())
854 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalReal8)>(loc, builder);
855 else if (eleTy.isF80())
856 func = fir::runtime::getRuntimeFunc<ForcedMinvalReal10>(loc, builder);
857 else if (eleTy.isF128())
858 func = fir::runtime::getRuntimeFunc<ForcedMinvalReal16>(loc, builder);
859 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
860 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger1)>(loc, builder);
861 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
862 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger2)>(loc, builder);
863 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
864 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger4)>(loc, builder);
865 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
866 func = fir::runtime::getRuntimeFunc<mkRTKey(MinvalInteger8)>(loc, builder);
867 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
868 func = fir::runtime::getRuntimeFunc<ForcedMinvalInteger16>(loc, builder);
869 else
870 fir::emitFatalError(loc, "invalid type in MINVAL");
871
872 auto fTy = func.getFunctionType();
873 auto sourceFile = fir::factory::locationToFilename(builder, loc);
874 auto sourceLine =
875 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
876 auto args = fir::runtime::createArguments(
877 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
878
879 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
880 }
881
882 /// Generate call to `Norm2Dim` intrinsic runtime routine. This is the version
883 /// that takes a dim argument.
884 void fir::runtime::genNorm2Dim(fir::FirOpBuilder &builder, mlir::Location loc,
885 mlir::Value resultBox, mlir::Value arrayBox,
886 mlir::Value dim) {
887 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Norm2Dim)>(loc, builder);
888 auto fTy = func.getFunctionType();
889 auto sourceFile = fir::factory::locationToFilename(builder, loc);
890 auto sourceLine =
891 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
892 auto args = fir::runtime::createArguments(
893 builder, loc, fTy, resultBox, arrayBox, dim, sourceFile, sourceLine);
894
895 builder.create<fir::CallOp>(loc, func, args);
896 }
897
898 /// Generate call to `Norm2` intrinsic runtime routine. This is the version
899 /// that does not take a dim argument.
900 mlir::Value fir::runtime::genNorm2(fir::FirOpBuilder &builder,
901 mlir::Location loc, mlir::Value arrayBox) {
902 mlir::func::FuncOp func;
903 auto ty = arrayBox.getType();
904 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
905 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
906 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
907
908 if (eleTy.isF16() || eleTy.isBF16())
909 TODO(loc, "half-precision NORM2");
910 else if (eleTy.isF32())
911 func = fir::runtime::getRuntimeFunc<mkRTKey(Norm2_4)>(loc, builder);
912 else if (eleTy.isF64())
913 func = fir::runtime::getRuntimeFunc<mkRTKey(Norm2_8)>(loc, builder);
914 else if (eleTy.isF80())
915 func = fir::runtime::getRuntimeFunc<ForcedNorm2Real10>(loc, builder);
916 else if (eleTy.isF128())
917 func = fir::runtime::getRuntimeFunc<ForcedNorm2Real16>(loc, builder);
918 else
919 fir::emitFatalError(loc, "invalid type in NORM2");
920
921 auto fTy = func.getFunctionType();
922 auto sourceFile = fir::factory::locationToFilename(builder, loc);
923 auto sourceLine =
924 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
925 auto args = fir::runtime::createArguments(builder, loc, fTy, arrayBox,
926 sourceFile, sourceLine, dim);
927
928 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
929 }
930
931 /// Generate call to `Parity` intrinsic runtime routine. This routine is
932 /// specialized for mask arguments with rank == 1.
933 mlir::Value fir::runtime::genParity(fir::FirOpBuilder &builder,
934 mlir::Location loc, mlir::Value maskBox,
935 mlir::Value dim) {
936 auto parityFunc = fir::runtime::getRuntimeFunc<mkRTKey(Parity)>(loc, builder);
937 return genSpecial2Args(parityFunc, builder, loc, maskBox, dim);
938 }
939
940 /// Generate call to `ProductDim` intrinsic runtime routine. This is the version
941 /// that handles any rank array with the dim argument specified.
942 void fir::runtime::genProductDim(fir::FirOpBuilder &builder, mlir::Location loc,
943 mlir::Value resultBox, mlir::Value arrayBox,
944 mlir::Value dim, mlir::Value maskBox) {
945 auto func = fir::runtime::getRuntimeFunc<mkRTKey(ProductDim)>(loc, builder);
946 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
947 }
948
949 /// Generate call to `Product` intrinsic runtime routine. This is the version
950 /// that does not take a dim argument.
951 mlir::Value fir::runtime::genProduct(fir::FirOpBuilder &builder,
952 mlir::Location loc, mlir::Value arrayBox,
953 mlir::Value maskBox,
954 mlir::Value resultBox) {
955 mlir::func::FuncOp func;
956 auto ty = arrayBox.getType();
957 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
958 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
959 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
960
961 if (eleTy.isF16() || eleTy.isBF16())
962 TODO(loc, "half-precision PRODUCT");
963 else if (eleTy.isF32())
964 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductReal4)>(loc, builder);
965 else if (eleTy.isF64())
966 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductReal8)>(loc, builder);
967 else if (eleTy.isF80())
968 func = fir::runtime::getRuntimeFunc<ForcedProductReal10>(loc, builder);
969 else if (eleTy.isF128())
970 func = fir::runtime::getRuntimeFunc<ForcedProductReal16>(loc, builder);
971 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
972 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger1)>(loc, builder);
973 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
974 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger2)>(loc, builder);
975 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
976 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger4)>(loc, builder);
977 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
978 func = fir::runtime::getRuntimeFunc<mkRTKey(ProductInteger8)>(loc, builder);
979 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
980 func = fir::runtime::getRuntimeFunc<ForcedProductInteger16>(loc, builder);
981 else if (eleTy == fir::ComplexType::get(builder.getContext(), 4))
982 func =
983 fir::runtime::getRuntimeFunc<mkRTKey(CppProductComplex4)>(loc, builder);
984 else if (eleTy == fir::ComplexType::get(builder.getContext(), 8))
985 func =
986 fir::runtime::getRuntimeFunc<mkRTKey(CppProductComplex8)>(loc, builder);
987 else if (eleTy == fir::ComplexType::get(builder.getContext(), 10))
988 func = fir::runtime::getRuntimeFunc<ForcedProductComplex10>(loc, builder);
989 else if (eleTy == fir::ComplexType::get(builder.getContext(), 16))
990 func = fir::runtime::getRuntimeFunc<ForcedProductComplex16>(loc, builder);
991 else if (eleTy == fir::ComplexType::get(builder.getContext(), 2) ||
992 eleTy == fir::ComplexType::get(builder.getContext(), 3))
993 TODO(loc, "half-precision PRODUCT");
994 else
995 fir::emitFatalError(loc, "invalid type in PRODUCT");
996
997 auto fTy = func.getFunctionType();
998 auto sourceFile = fir::factory::locationToFilename(builder, loc);
999 if (fir::isa_complex(eleTy)) {
1000 auto sourceLine =
1001 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1002 auto args =
1003 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
1004 sourceFile, sourceLine, dim, maskBox);
1005 builder.create<fir::CallOp>(loc, func, args);
1006 return resultBox;
1007 }
1008
1009 auto sourceLine =
1010 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1011 auto args = fir::runtime::createArguments(
1012 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
1013
1014 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1015 }
1016
1017 /// Generate call to `DotProduct` intrinsic runtime routine.
1018 mlir::Value fir::runtime::genDotProduct(fir::FirOpBuilder &builder,
1019 mlir::Location loc,
1020 mlir::Value vectorABox,
1021 mlir::Value vectorBBox,
1022 mlir::Value resultBox) {
1023 mlir::func::FuncOp func;
1024 // For complex data types, resultBox is !fir.ref<!fir.complex<N>>,
1025 // otherwise it is !fir.box<T>.
1026 auto ty = resultBox.getType();
1027 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1028
1029 if (eleTy.isF16() || eleTy.isBF16())
1030 TODO(loc, "half-precision DOTPRODUCT");
1031 else if (eleTy.isF32())
1032 func = fir::runtime::getRuntimeFunc<mkRTKey(DotProductReal4)>(loc, builder);
1033 else if (eleTy.isF64())
1034 func = fir::runtime::getRuntimeFunc<mkRTKey(DotProductReal8)>(loc, builder);
1035 else if (eleTy.isF80())
1036 func = fir::runtime::getRuntimeFunc<ForcedDotProductReal10>(loc, builder);
1037 else if (eleTy.isF128())
1038 func = fir::runtime::getRuntimeFunc<ForcedDotProductReal16>(loc, builder);
1039 else if (eleTy == fir::ComplexType::get(builder.getContext(), 4))
1040 func = fir::runtime::getRuntimeFunc<mkRTKey(CppDotProductComplex4)>(
1041 loc, builder);
1042 else if (eleTy == fir::ComplexType::get(builder.getContext(), 8))
1043 func = fir::runtime::getRuntimeFunc<mkRTKey(CppDotProductComplex8)>(
1044 loc, builder);
1045 else if (eleTy == fir::ComplexType::get(builder.getContext(), 10))
1046 func =
1047 fir::runtime::getRuntimeFunc<ForcedDotProductComplex10>(loc, builder);
1048 else if (eleTy == fir::ComplexType::get(builder.getContext(), 16))
1049 func =
1050 fir::runtime::getRuntimeFunc<ForcedDotProductComplex16>(loc, builder);
1051 else if (eleTy == fir::ComplexType::get(builder.getContext(), 2) ||
1052 eleTy == fir::ComplexType::get(builder.getContext(), 3))
1053 TODO(loc, "half-precision DOTPRODUCT");
1054 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1055 func =
1056 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger1)>(loc, builder);
1057 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1058 func =
1059 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger2)>(loc, builder);
1060 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1061 func =
1062 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger4)>(loc, builder);
1063 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1064 func =
1065 fir::runtime::getRuntimeFunc<mkRTKey(DotProductInteger8)>(loc, builder);
1066 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1067 func =
1068 fir::runtime::getRuntimeFunc<ForcedDotProductInteger16>(loc, builder);
1069 else if (eleTy.isa<fir::LogicalType>())
1070 func =
1071 fir::runtime::getRuntimeFunc<mkRTKey(DotProductLogical)>(loc, builder);
1072 else
1073 fir::emitFatalError(loc, "invalid type in DOTPRODUCT");
1074
1075 auto fTy = func.getFunctionType();
1076 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1077
1078 if (fir::isa_complex(eleTy)) {
1079 auto sourceLine =
1080 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
1081 auto args =
1082 fir::runtime::createArguments(builder, loc, fTy, resultBox, vectorABox,
1083 vectorBBox, sourceFile, sourceLine);
1084 builder.create<fir::CallOp>(loc, func, args);
1085 return resultBox;
1086 }
1087
1088 auto sourceLine =
1089 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1090 auto args = fir::runtime::createArguments(builder, loc, fTy, vectorABox,
1091 vectorBBox, sourceFile, sourceLine);
1092 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1093 }
1094 /// Generate call to `SumDim` intrinsic runtime routine. This is the version
1095 /// that handles any rank array with the dim argument specified.
1096 void fir::runtime::genSumDim(fir::FirOpBuilder &builder, mlir::Location loc,
1097 mlir::Value resultBox, mlir::Value arrayBox,
1098 mlir::Value dim, mlir::Value maskBox) {
1099 auto func = fir::runtime::getRuntimeFunc<mkRTKey(SumDim)>(loc, builder);
1100 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1101 }
1102
1103 /// Generate call to `Sum` intrinsic runtime routine. This is the version
1104 /// that does not take a dim argument.
1105 mlir::Value fir::runtime::genSum(fir::FirOpBuilder &builder, mlir::Location loc,
1106 mlir::Value arrayBox, mlir::Value maskBox,
1107 mlir::Value resultBox) {
1108 mlir::func::FuncOp func;
1109 auto ty = arrayBox.getType();
1110 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
1111 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
1112 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
1113
1114 if (eleTy.isF16() || eleTy.isBF16())
1115 TODO(loc, "half-precision SUM");
1116 else if (eleTy.isF32())
1117 func = fir::runtime::getRuntimeFunc<mkRTKey(SumReal4)>(loc, builder);
1118 else if (eleTy.isF64())
1119 func = fir::runtime::getRuntimeFunc<mkRTKey(SumReal8)>(loc, builder);
1120 else if (eleTy.isF80())
1121 func = fir::runtime::getRuntimeFunc<ForcedSumReal10>(loc, builder);
1122 else if (eleTy.isF128())
1123 func = fir::runtime::getRuntimeFunc<ForcedSumReal16>(loc, builder);
1124 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1)))
1125 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger1)>(loc, builder);
1126 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2)))
1127 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger2)>(loc, builder);
1128 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4)))
1129 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger4)>(loc, builder);
1130 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8)))
1131 func = fir::runtime::getRuntimeFunc<mkRTKey(SumInteger8)>(loc, builder);
1132 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
1133 func = fir::runtime::getRuntimeFunc<ForcedSumInteger16>(loc, builder);
1134 else if (eleTy == fir::ComplexType::get(builder.getContext(), 4))
1135 func = fir::runtime::getRuntimeFunc<mkRTKey(CppSumComplex4)>(loc, builder);
1136 else if (eleTy == fir::ComplexType::get(builder.getContext(), 8))
1137 func = fir::runtime::getRuntimeFunc<mkRTKey(CppSumComplex8)>(loc, builder);
1138 else if (eleTy == fir::ComplexType::get(builder.getContext(), 10))
1139 func = fir::runtime::getRuntimeFunc<ForcedSumComplex10>(loc, builder);
1140 else if (eleTy == fir::ComplexType::get(builder.getContext(), 16))
1141 func = fir::runtime::getRuntimeFunc<ForcedSumComplex16>(loc, builder);
1142 else if (eleTy == fir::ComplexType::get(builder.getContext(), 2) ||
1143 eleTy == fir::ComplexType::get(builder.getContext(), 3))
1144 TODO(loc, "half-precision SUM");
1145 else
1146 fir::emitFatalError(loc, "invalid type in SUM");
1147
1148 auto fTy = func.getFunctionType();
1149 auto sourceFile = fir::factory::locationToFilename(builder, loc);
1150 if (fir::isa_complex(eleTy)) {
1151 auto sourceLine =
1152 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
1153 auto args =
1154 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
1155 sourceFile, sourceLine, dim, maskBox);
1156 builder.create<fir::CallOp>(loc, func, args);
1157 return resultBox;
1158 }
1159
1160 auto sourceLine =
1161 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
1162 auto args = fir::runtime::createArguments(
1163 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox);
1164
1165 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
1166 }
1167
1168 // The IAll, IAny and IParity intrinsics have essentially the same
1169 // implementation. This macro will generate the function body given the
1170 // instrinsic name.
1171 #define GEN_IALL_IANY_IPARITY(F) \
1172 mlir::Value fir::runtime::JOIN2(gen, F)( \
1173 fir::FirOpBuilder & builder, mlir::Location loc, mlir::Value arrayBox, \
1174 mlir::Value maskBox, mlir::Value resultBox) { \
1175 mlir::func::FuncOp func; \
1176 auto ty = arrayBox.getType(); \
1177 auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty); \
1178 auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy(); \
1179 auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0); \
1180 \
1181 if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1))) \
1182 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 1))>(loc, builder); \
1183 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(2))) \
1184 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 2))>(loc, builder); \
1185 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(4))) \
1186 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 4))>(loc, builder); \
1187 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(8))) \
1188 func = fir::runtime::getRuntimeFunc<mkRTKey(JOIN2(F, 8))>(loc, builder); \
1189 else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16))) \
1190 func = fir::runtime::getRuntimeFunc<JOIN3(Forced, F, 16)>(loc, builder); \
1191 else \
1192 fir::emitFatalError(loc, "invalid type in " STRINGIFY(F)); \
1193 \
1194 auto fTy = func.getFunctionType(); \
1195 auto sourceFile = fir::factory::locationToFilename(builder, loc); \
1196 auto sourceLine = \
1197 fir::factory::locationToLineNo(builder, loc, fTy.getInput(2)); \
1198 auto args = fir::runtime::createArguments( \
1199 builder, loc, fTy, arrayBox, sourceFile, sourceLine, dim, maskBox); \
1200 \
1201 return builder.create<fir::CallOp>(loc, func, args).getResult(0); \
1202 }
1203
1204 /// Generate call to `IAllDim` intrinsic runtime routine. This is the version
1205 /// that handles any rank array with the dim argument specified.
1206 void fir::runtime::genIAllDim(fir::FirOpBuilder &builder, mlir::Location loc,
1207 mlir::Value resultBox, mlir::Value arrayBox,
1208 mlir::Value dim, mlir::Value maskBox) {
1209 auto func = fir::runtime::getRuntimeFunc<mkRTKey(IAllDim)>(loc, builder);
1210 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1211 }
1212
1213 /// Generate call to `IAll` intrinsic runtime routine. This is the version
1214 /// that does not take a dim argument.
1215 GEN_IALL_IANY_IPARITY(IAll)
1216
1217 /// Generate call to `IAnyDim` intrinsic runtime routine. This is the version
1218 /// that handles any rank array with the dim argument specified.
1219 void fir::runtime::genIAnyDim(fir::FirOpBuilder &builder, mlir::Location loc,
1220 mlir::Value resultBox, mlir::Value arrayBox,
1221 mlir::Value dim, mlir::Value maskBox) {
1222 auto func = fir::runtime::getRuntimeFunc<mkRTKey(IAnyDim)>(loc, builder);
1223 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1224 }
1225
1226 /// Generate call to `IAny` intrinsic runtime routine. This is the version
1227 /// that does not take a dim argument.
1228 GEN_IALL_IANY_IPARITY(IAny)
1229
1230 /// Generate call to `IParityDim` intrinsic runtime routine. This is the version
1231 /// that handles any rank array with the dim argument specified.
1232 void fir::runtime::genIParityDim(fir::FirOpBuilder &builder, mlir::Location loc,
1233 mlir::Value resultBox, mlir::Value arrayBox,
1234 mlir::Value dim, mlir::Value maskBox) {
1235 auto func = fir::runtime::getRuntimeFunc<mkRTKey(IParityDim)>(loc, builder);
1236 genReduction3Args(func, builder, loc, resultBox, arrayBox, dim, maskBox);
1237 }
1238
1239 /// Generate call to `IParity` intrinsic runtime routine. This is the version
1240 /// that does not take a dim argument.
1241 GEN_IALL_IANY_IPARITY(IParity)