FLANG
RTBuilder.h
Go to the documentation of this file.
1//===-- RTBuilder.h ---------------------------------------------*- C++ -*-===//
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//===----------------------------------------------------------------------===//
15//===----------------------------------------------------------------------===//
16
17#ifndef FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RTBUILDER_H
18#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RTBUILDER_H
19
20#include "flang/Common/uint128.h"
21#include "flang/Optimizer/Builder/FIRBuilder.h"
22#include "flang/Optimizer/Dialect/FIRDialect.h"
23#include "flang/Optimizer/Dialect/FIRType.h"
24#include "flang/Runtime/io-api.h"
25#include "flang/Runtime/reduce.h"
26#include "flang/Support/Fortran.h"
27#include "mlir/IR/BuiltinTypes.h"
28#include "mlir/IR/MLIRContext.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/SmallVector.h"
31#include <cstdint>
32#include <functional>
33
34#ifdef _MSC_VER
35// On Windows* OS GetCurrentProcessId returns DWORD aka uint32_t
36typedef std::uint32_t pid_t;
37#endif
38
39// Incomplete type indicating C99 complex ABI in interfaces. Beware, _Complex
40// and std::complex are layout compatible, but not compatible in all ABI call
41// interfaces (e.g. X86 32 bits). _Complex is not standard C++, so do not use
42// it here.
43struct c_float_complex_t;
44struct c_double_complex_t;
45
46namespace Fortran::runtime {
47class Descriptor;
48namespace typeInfo {
49class DerivedType;
50}
51} // namespace Fortran::runtime
52
53namespace fir::runtime {
54
55using TypeBuilderFunc = mlir::Type (*)(mlir::MLIRContext *);
56using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *);
57
58#define REDUCTION_REF_OPERATION_MODEL(T) \
59 template <> \
60 constexpr TypeBuilderFunc \
61 getModel<Fortran::runtime::ReferenceReductionOperation<T>>() { \
62 return [](mlir::MLIRContext *context) -> mlir::Type { \
63 TypeBuilderFunc f{getModel<T>()}; \
64 auto refTy = fir::ReferenceType::get(f(context)); \
65 return mlir::FunctionType::get(context, {refTy, refTy}, refTy); \
66 }; \
67 }
68
69#define REDUCTION_VALUE_OPERATION_MODEL(T) \
70 template <> \
71 constexpr TypeBuilderFunc \
72 getModel<Fortran::runtime::ValueReductionOperation<T>>() { \
73 return [](mlir::MLIRContext *context) -> mlir::Type { \
74 TypeBuilderFunc f{getModel<T>()}; \
75 auto refTy = fir::ReferenceType::get(f(context)); \
76 return mlir::FunctionType::get(context, {f(context), f(context)}, \
77 refTy); \
78 }; \
79 }
80
81#define REDUCTION_CHAR_OPERATION_MODEL(T) \
82 template <> \
83 constexpr TypeBuilderFunc \
84 getModel<Fortran::runtime::ReductionCharOperation<T>>() { \
85 return [](mlir::MLIRContext *context) -> mlir::Type { \
86 TypeBuilderFunc f{getModel<T>()}; \
87 auto voidTy = fir::LLVMPointerType::get( \
88 context, mlir::IntegerType::get(context, 8)); \
89 auto size_tTy = \
90 mlir::IntegerType::get(context, 8 * sizeof(std::size_t)); \
91 auto refTy = fir::ReferenceType::get(f(context)); \
92 return mlir::FunctionType::get( \
93 context, {refTy, size_tTy, refTy, refTy, size_tTy, size_tTy}, \
94 voidTy); \
95 }; \
96 }
97
98//===----------------------------------------------------------------------===//
99// Type builder models
100//===----------------------------------------------------------------------===//
101
102// TODO: all usages of sizeof in this file assume build == host == target.
103// This will need to be re-visited for cross compilation.
104
112template <typename T>
113static constexpr TypeBuilderFunc getModel();
114
115template <>
116constexpr TypeBuilderFunc getModel<unsigned int>() {
117 return [](mlir::MLIRContext *context) -> mlir::Type {
118 return mlir::IntegerType::get(context, 8 * sizeof(unsigned int));
119 };
120}
121template <>
122constexpr TypeBuilderFunc getModel<short int>() {
123 return [](mlir::MLIRContext *context) -> mlir::Type {
124 return mlir::IntegerType::get(context, 8 * sizeof(short int));
125 };
126}
127template <>
128constexpr TypeBuilderFunc getModel<short int *>() {
129 return [](mlir::MLIRContext *context) -> mlir::Type {
130 TypeBuilderFunc f{getModel<short int>()};
131 return fir::ReferenceType::get(f(context));
132 };
133}
134template <>
135constexpr TypeBuilderFunc getModel<const short int *>() {
136 return getModel<short int *>();
137}
138template <>
139constexpr TypeBuilderFunc getModel<int>() {
140 return [](mlir::MLIRContext *context) -> mlir::Type {
141 return mlir::IntegerType::get(context, 8 * sizeof(int));
142 };
143}
144template <>
145constexpr TypeBuilderFunc getModel<int &>() {
146 return [](mlir::MLIRContext *context) -> mlir::Type {
147 TypeBuilderFunc f{getModel<int>()};
148 return fir::ReferenceType::get(f(context));
149 };
150}
151template <>
152constexpr TypeBuilderFunc getModel<int *>() {
153 return getModel<int &>();
154}
155template <>
156constexpr TypeBuilderFunc getModel<const int *>() {
157 return [](mlir::MLIRContext *context) -> mlir::Type {
158 TypeBuilderFunc f{getModel<int>()};
159 return fir::ReferenceType::get(f(context));
160 };
161}
162template <>
163constexpr TypeBuilderFunc getModel<char *>() {
164 return [](mlir::MLIRContext *context) -> mlir::Type {
165 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
166 };
167}
168template <>
169constexpr TypeBuilderFunc getModel<const char *>() {
170 return getModel<char *>();
171}
172template <>
173constexpr TypeBuilderFunc getModel<const char16_t *>() {
174 return [](mlir::MLIRContext *context) -> mlir::Type {
175 return fir::ReferenceType::get(mlir::IntegerType::get(context, 16));
176 };
177}
178template <>
179constexpr TypeBuilderFunc getModel<const char32_t *>() {
180 return [](mlir::MLIRContext *context) -> mlir::Type {
181 return fir::ReferenceType::get(mlir::IntegerType::get(context, 32));
182 };
183}
184template <>
185constexpr TypeBuilderFunc getModel<char>() {
186 return [](mlir::MLIRContext *context) -> mlir::Type {
187 return mlir::IntegerType::get(context, 8 * sizeof(char));
188 };
189}
190template <>
191constexpr TypeBuilderFunc getModel<signed char>() {
192 return [](mlir::MLIRContext *context) -> mlir::Type {
193 return mlir::IntegerType::get(context, 8 * sizeof(signed char));
194 };
195}
196template <>
197constexpr TypeBuilderFunc getModel<signed char *>() {
198 return [](mlir::MLIRContext *context) -> mlir::Type {
199 TypeBuilderFunc f{getModel<signed char>()};
200 return fir::ReferenceType::get(f(context));
201 };
202}
203template <>
204constexpr TypeBuilderFunc getModel<const signed char *>() {
205 return getModel<signed char *>();
206}
207template <>
208constexpr TypeBuilderFunc getModel<char16_t>() {
209 return [](mlir::MLIRContext *context) -> mlir::Type {
210 return mlir::IntegerType::get(context, 8 * sizeof(char16_t));
211 };
212}
213template <>
214constexpr TypeBuilderFunc getModel<char16_t *>() {
215 return [](mlir::MLIRContext *context) -> mlir::Type {
216 TypeBuilderFunc f{getModel<char16_t>()};
217 return fir::ReferenceType::get(f(context));
218 };
219}
220template <>
221constexpr TypeBuilderFunc getModel<char32_t>() {
222 return [](mlir::MLIRContext *context) -> mlir::Type {
223 return mlir::IntegerType::get(context, 8 * sizeof(char32_t));
224 };
225}
226template <>
227constexpr TypeBuilderFunc getModel<char32_t *>() {
228 return [](mlir::MLIRContext *context) -> mlir::Type {
229 TypeBuilderFunc f{getModel<char32_t>()};
230 return fir::ReferenceType::get(f(context));
231 };
232}
233template <>
234constexpr TypeBuilderFunc getModel<unsigned char>() {
235 return [](mlir::MLIRContext *context) -> mlir::Type {
236 return mlir::IntegerType::get(context, 8 * sizeof(unsigned char));
237 };
238}
239template <>
240constexpr TypeBuilderFunc getModel<void *>() {
241 return [](mlir::MLIRContext *context) -> mlir::Type {
242 return fir::LLVMPointerType::get(context,
243 mlir::IntegerType::get(context, 8));
244 };
245}
246template <>
247constexpr TypeBuilderFunc getModel<void (*)(int)>() {
248 return [](mlir::MLIRContext *context) -> mlir::Type {
249 return fir::LLVMPointerType::get(
250 context,
251 mlir::FunctionType::get(context, /*inputs=*/{}, /*results*/ {}));
252 };
253}
254template <>
255constexpr TypeBuilderFunc
256getModel<void *(*)(void *, const void *, unsigned long)>() {
257 return [](mlir::MLIRContext *context) -> mlir::Type {
258 auto voidPtrTy =
259 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
260 auto unsignedLongTy =
261 mlir::IntegerType::get(context, 8 * sizeof(unsigned long));
262 auto funcTy = mlir::FunctionType::get(
263 context, {voidPtrTy, voidPtrTy, unsignedLongTy}, {voidPtrTy});
264 return fir::LLVMPointerType::get(context, funcTy);
265 };
266}
267#ifdef _MSC_VER
268template <>
269constexpr TypeBuilderFunc
270getModel<void *(*)(void *, const void *, unsigned __int64)>() {
271 return [](mlir::MLIRContext *context) -> mlir::Type {
272 auto voidPtrTy =
273 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
274 auto uint64Ty = mlir::IntegerType::get(context, 64);
275 auto funcTy = mlir::FunctionType::get(
276 context, {voidPtrTy, voidPtrTy, uint64Ty}, {voidPtrTy});
277 return fir::LLVMPointerType::get(context, funcTy);
278 };
279}
280#endif
281template <>
282constexpr TypeBuilderFunc getModel<void **>() {
283 return [](mlir::MLIRContext *context) -> mlir::Type {
284 return fir::ReferenceType::get(
285 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8)));
286 };
287}
288template <>
289constexpr TypeBuilderFunc getModel<long>() {
290 return [](mlir::MLIRContext *context) -> mlir::Type {
291 return mlir::IntegerType::get(context, 8 * sizeof(long));
292 };
293}
294template <>
295constexpr TypeBuilderFunc getModel<long &>() {
296 return [](mlir::MLIRContext *context) -> mlir::Type {
297 TypeBuilderFunc f{getModel<long>()};
298 return fir::ReferenceType::get(f(context));
299 };
300}
301template <>
302constexpr TypeBuilderFunc getModel<long *>() {
303 return getModel<long &>();
304}
305template <>
306constexpr TypeBuilderFunc getModel<const long *>() {
307 return getModel<long *>();
308}
309template <>
310constexpr TypeBuilderFunc getModel<long long>() {
311 return [](mlir::MLIRContext *context) -> mlir::Type {
312 return mlir::IntegerType::get(context, 8 * sizeof(long long));
313 };
314}
315template <>
316constexpr TypeBuilderFunc getModel<Fortran::common::int128_t>() {
317 return [](mlir::MLIRContext *context) -> mlir::Type {
318 return mlir::IntegerType::get(context,
319 8 * sizeof(Fortran::common::int128_t));
320 };
321}
322template <>
323constexpr TypeBuilderFunc getModel<long long &>() {
324 return [](mlir::MLIRContext *context) -> mlir::Type {
325 TypeBuilderFunc f{getModel<long long>()};
326 return fir::ReferenceType::get(f(context));
327 };
328}
329template <>
330constexpr TypeBuilderFunc getModel<long long *>() {
331 return getModel<long long &>();
332}
333template <>
334constexpr TypeBuilderFunc getModel<const long long *>() {
335 return getModel<long long *>();
336}
337template <>
338constexpr TypeBuilderFunc getModel<unsigned long>() {
339 return [](mlir::MLIRContext *context) -> mlir::Type {
340 return mlir::IntegerType::get(context, 8 * sizeof(unsigned long));
341 };
342}
343template <>
344constexpr TypeBuilderFunc getModel<unsigned long long>() {
345 return [](mlir::MLIRContext *context) -> mlir::Type {
346 return mlir::IntegerType::get(context, 8 * sizeof(unsigned long long));
347 };
348}
349template <>
350constexpr TypeBuilderFunc getModel<double>() {
351 return [](mlir::MLIRContext *context) -> mlir::Type {
352 return mlir::Float64Type::get(context);
353 };
354}
355template <>
356constexpr TypeBuilderFunc getModel<double &>() {
357 return [](mlir::MLIRContext *context) -> mlir::Type {
358 TypeBuilderFunc f{getModel<double>()};
359 return fir::ReferenceType::get(f(context));
360 };
361}
362template <>
363constexpr TypeBuilderFunc getModel<double *>() {
364 return getModel<double &>();
365}
366template <>
367constexpr TypeBuilderFunc getModel<const double *>() {
368 return getModel<double *>();
369}
370template <>
371constexpr TypeBuilderFunc getModel<long double>() {
372 return [](mlir::MLIRContext *context) -> mlir::Type {
373 // See TODO at the top of the file. This is configuring for the host system
374 // - it might be incorrect when cross-compiling!
375 constexpr size_t size = sizeof(long double);
376 static_assert(size == 16 || size == 10 || size == 8,
377 "unsupported long double size");
378 if constexpr (size == 16)
379 return mlir::Float128Type::get(context);
380 if constexpr (size == 10)
381 return mlir::Float80Type::get(context);
382 if constexpr (size == 8)
383 return mlir::Float64Type::get(context);
384 llvm_unreachable("failed static assert");
385 };
386}
387template <>
388constexpr TypeBuilderFunc getModel<long double *>() {
389 return [](mlir::MLIRContext *context) -> mlir::Type {
390 TypeBuilderFunc f{getModel<long double>()};
391 return fir::ReferenceType::get(f(context));
392 };
393}
394template <>
395constexpr TypeBuilderFunc getModel<const long double *>() {
396 return getModel<long double *>();
397}
398template <>
399constexpr TypeBuilderFunc getModel<float>() {
400 return [](mlir::MLIRContext *context) -> mlir::Type {
401 return mlir::Float32Type::get(context);
402 };
403}
404template <>
405constexpr TypeBuilderFunc getModel<float &>() {
406 return [](mlir::MLIRContext *context) -> mlir::Type {
407 TypeBuilderFunc f{getModel<float>()};
408 return fir::ReferenceType::get(f(context));
409 };
410}
411template <>
412constexpr TypeBuilderFunc getModel<float *>() {
413 return getModel<float &>();
414}
415template <>
416constexpr TypeBuilderFunc getModel<const float *>() {
417 return getModel<float *>();
418}
419template <>
420constexpr TypeBuilderFunc getModel<bool>() {
421 return [](mlir::MLIRContext *context) -> mlir::Type {
422 return mlir::IntegerType::get(context, 1);
423 };
424}
425template <>
426constexpr TypeBuilderFunc getModel<bool &>() {
427 return [](mlir::MLIRContext *context) -> mlir::Type {
428 TypeBuilderFunc f{getModel<bool>()};
429 return fir::ReferenceType::get(f(context));
430 };
431}
432template <>
433constexpr TypeBuilderFunc getModel<bool *>() {
434 return [](mlir::MLIRContext *context) -> mlir::Type {
435 TypeBuilderFunc f{getModel<bool>()};
436 return fir::ReferenceType::get(f(context));
437 };
438}
439template <>
440constexpr TypeBuilderFunc getModel<unsigned short>() {
441 return [](mlir::MLIRContext *context) -> mlir::Type {
442 return mlir::IntegerType::get(
443 context, 8 * sizeof(unsigned short),
444 mlir::IntegerType::SignednessSemantics::Unsigned);
445 };
446}
447template <>
448constexpr TypeBuilderFunc getModel<unsigned char *>() {
449 return [](mlir::MLIRContext *context) -> mlir::Type {
450 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
451 };
452}
453template <>
454constexpr TypeBuilderFunc getModel<const unsigned char *>() {
455 return getModel<unsigned char *>();
456}
457template <>
458constexpr TypeBuilderFunc getModel<unsigned short *>() {
459 return [](mlir::MLIRContext *context) -> mlir::Type {
460 return fir::ReferenceType::get(
461 mlir::IntegerType::get(context, 8 * sizeof(unsigned short)));
462 };
463}
464template <>
465constexpr TypeBuilderFunc getModel<const unsigned short *>() {
466 return getModel<unsigned short *>();
467}
468template <>
469constexpr TypeBuilderFunc getModel<unsigned *>() {
470 return getModel<int *>();
471}
472template <>
473constexpr TypeBuilderFunc getModel<const unsigned *>() {
474 return getModel<unsigned *>();
475}
476template <>
477constexpr TypeBuilderFunc getModel<unsigned long *>() {
478 return [](mlir::MLIRContext *context) -> mlir::Type {
479 return fir::ReferenceType::get(
480 mlir::IntegerType::get(context, 8 * sizeof(unsigned long)));
481 };
482}
483template <>
484constexpr TypeBuilderFunc getModel<const unsigned long *>() {
485 return getModel<unsigned long *>();
486}
487template <>
488constexpr TypeBuilderFunc getModel<unsigned long long *>() {
489 return [](mlir::MLIRContext *context) -> mlir::Type {
490 return fir::ReferenceType::get(
491 mlir::IntegerType::get(context, 8 * sizeof(unsigned long long)));
492 };
493}
494template <>
495constexpr TypeBuilderFunc getModel<const unsigned long long *>() {
496 return getModel<unsigned long long *>();
497}
498template <>
499constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t>() {
500 return getModel<Fortran::common::int128_t>();
501}
502template <>
503constexpr TypeBuilderFunc getModel<Fortran::common::int128_t *>() {
504 return [](mlir::MLIRContext *context) -> mlir::Type {
505 TypeBuilderFunc f{getModel<Fortran::common::int128_t>()};
506 return fir::ReferenceType::get(f(context));
507 };
508}
509template <>
510constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t *>() {
511 return getModel<Fortran::common::int128_t *>();
512}
513template <>
514constexpr TypeBuilderFunc getModel<const Fortran::common::uint128_t *>() {
515 return getModel<Fortran::common::uint128_t *>();
516}
517
518// getModel<std::complex<T>> are not implemented on purpose.
519// Prefer passing/returning the complex by reference in the runtime to
520// avoid ABI issues.
521// C++ std::complex is not an intrinsic type, and while it is storage
522// compatible with C/Fortran complex type, it follows the struct value passing
523// ABI rule, which may differ from how C complex are passed on some platforms.
524
525template <>
526constexpr TypeBuilderFunc getModel<std::complex<float> &>() {
527 return [](mlir::MLIRContext *context) -> mlir::Type {
528 mlir::Type floatTy = getModel<float>()(context);
529 return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
530 };
531}
532template <>
533constexpr TypeBuilderFunc getModel<std::complex<float> *>() {
534 return getModel<std::complex<float> &>();
535}
536template <>
537constexpr TypeBuilderFunc getModel<const std::complex<float> *>() {
538 return getModel<std::complex<float> *>();
539}
540template <>
541constexpr TypeBuilderFunc getModel<std::complex<double> &>() {
542 return [](mlir::MLIRContext *context) -> mlir::Type {
543 mlir::Type floatTy = getModel<double>()(context);
544 return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
545 };
546}
547template <>
548constexpr TypeBuilderFunc getModel<std::complex<double> *>() {
549 return getModel<std::complex<double> &>();
550}
551template <>
552constexpr TypeBuilderFunc getModel<const std::complex<double> *>() {
553 return getModel<std::complex<double> *>();
554}
555template <>
556constexpr TypeBuilderFunc getModel<c_float_complex_t>() {
557 return [](mlir::MLIRContext *context) -> mlir::Type {
558 mlir::Type floatTy = getModel<float>()(context);
559 return mlir::ComplexType::get(floatTy);
560 };
561}
562template <>
563constexpr TypeBuilderFunc getModel<c_double_complex_t>() {
564 return [](mlir::MLIRContext *context) -> mlir::Type {
565 mlir::Type floatTy = getModel<double>()(context);
566 return mlir::ComplexType::get(floatTy);
567 };
568}
569template <>
570constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor &>() {
571 return [](mlir::MLIRContext *context) -> mlir::Type {
572 return fir::BoxType::get(mlir::NoneType::get(context));
573 };
574}
575template <>
576constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor &>() {
577 return [](mlir::MLIRContext *context) -> mlir::Type {
578 return fir::ReferenceType::get(
579 fir::BoxType::get(mlir::NoneType::get(context)));
580 };
581}
582template <>
583constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor *>() {
584 return getModel<const Fortran::runtime::Descriptor &>();
585}
586template <>
587constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor *>() {
588 return getModel<Fortran::runtime::Descriptor &>();
589}
590template <>
591constexpr TypeBuilderFunc getModel<Fortran::common::TypeCategory>() {
592 return [](mlir::MLIRContext *context) -> mlir::Type {
593 return mlir::IntegerType::get(context,
594 sizeof(Fortran::common::TypeCategory) * 8);
595 };
596}
597template <>
598constexpr TypeBuilderFunc
599getModel<const Fortran::runtime::typeInfo::DerivedType &>() {
600 return [](mlir::MLIRContext *context) -> mlir::Type {
601 return fir::ReferenceType::get(mlir::NoneType::get(context));
602 };
603}
604template <>
605constexpr TypeBuilderFunc
606getModel<const Fortran::runtime::typeInfo::DerivedType *>() {
607 return [](mlir::MLIRContext *context) -> mlir::Type {
608 return fir::ReferenceType::get(mlir::NoneType::get(context));
609 };
610}
611template <>
612constexpr TypeBuilderFunc getModel<void>() {
613 return [](mlir::MLIRContext *context) -> mlir::Type {
614 return mlir::NoneType::get(context);
615 };
616}
617
618// Define additional runtime type models specific to IO.
619template <>
620constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
621 return getModel<char *>();
622}
623template <>
624constexpr TypeBuilderFunc getModel<Fortran::runtime::io::Iostat>() {
625 return [](mlir::MLIRContext *context) -> mlir::Type {
626 return mlir::IntegerType::get(context,
627 8 * sizeof(Fortran::runtime::io::Iostat));
628 };
629}
630template <>
631constexpr TypeBuilderFunc
632getModel<const Fortran::runtime::io::NamelistGroup &>() {
633 return [](mlir::MLIRContext *context) -> mlir::Type {
634 return fir::ReferenceType::get(mlir::TupleType::get(context));
635 };
636}
637template <>
638constexpr TypeBuilderFunc
639getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
640 return [](mlir::MLIRContext *context) -> mlir::Type {
641 return fir::ReferenceType::get(mlir::TupleType::get(context));
642 };
643}
644
645REDUCTION_REF_OPERATION_MODEL(std::int8_t)
646REDUCTION_VALUE_OPERATION_MODEL(std::int8_t)
647REDUCTION_REF_OPERATION_MODEL(std::int16_t)
648REDUCTION_VALUE_OPERATION_MODEL(std::int16_t)
649REDUCTION_REF_OPERATION_MODEL(std::int32_t)
650REDUCTION_VALUE_OPERATION_MODEL(std::int32_t)
651REDUCTION_REF_OPERATION_MODEL(std::int64_t)
652REDUCTION_VALUE_OPERATION_MODEL(std::int64_t)
653REDUCTION_REF_OPERATION_MODEL(Fortran::common::int128_t)
654REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::int128_t)
655
656REDUCTION_REF_OPERATION_MODEL(std::uint8_t)
657REDUCTION_VALUE_OPERATION_MODEL(std::uint8_t)
658REDUCTION_REF_OPERATION_MODEL(std::uint16_t)
659REDUCTION_VALUE_OPERATION_MODEL(std::uint16_t)
660REDUCTION_REF_OPERATION_MODEL(std::uint32_t)
661REDUCTION_VALUE_OPERATION_MODEL(std::uint32_t)
662REDUCTION_REF_OPERATION_MODEL(std::uint64_t)
663REDUCTION_VALUE_OPERATION_MODEL(std::uint64_t)
664REDUCTION_REF_OPERATION_MODEL(Fortran::common::uint128_t)
665REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::uint128_t)
666
667REDUCTION_REF_OPERATION_MODEL(float)
668REDUCTION_VALUE_OPERATION_MODEL(float)
669REDUCTION_REF_OPERATION_MODEL(double)
670REDUCTION_VALUE_OPERATION_MODEL(double)
671REDUCTION_REF_OPERATION_MODEL(long double)
672REDUCTION_VALUE_OPERATION_MODEL(long double)
673
674// FIXME: the runtime is not using the correct ABIs when calling complex
675// callbacks. lowering either need to create wrappers or just have an inline
676// implementation for it. https://github.com/llvm/llvm-project/issues/110674
677template <>
678constexpr TypeBuilderFunc
679getModel<Fortran::runtime::ValueReductionOperation<std::complex<float>>>() {
680 return [](mlir::MLIRContext *context) -> mlir::Type {
681 mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
682 auto refTy = fir::ReferenceType::get(cplx);
683 return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
684 };
685}
686template <>
687constexpr TypeBuilderFunc
688getModel<Fortran::runtime::ValueReductionOperation<std::complex<double>>>() {
689 return [](mlir::MLIRContext *context) -> mlir::Type {
690 mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
691 auto refTy = fir::ReferenceType::get(cplx);
692 return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
693 };
694}
695template <>
696constexpr TypeBuilderFunc
697getModel<Fortran::runtime::ReferenceReductionOperation<std::complex<float>>>() {
698 return [](mlir::MLIRContext *context) -> mlir::Type {
699 mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
700 auto refTy = fir::ReferenceType::get(cplx);
701 return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
702 };
703}
704template <>
705constexpr TypeBuilderFunc getModel<
706 Fortran::runtime::ReferenceReductionOperation<std::complex<double>>>() {
707 return [](mlir::MLIRContext *context) -> mlir::Type {
708 mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
709 auto refTy = fir::ReferenceType::get(cplx);
710 return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
711 };
712}
713
714REDUCTION_CHAR_OPERATION_MODEL(char)
715REDUCTION_CHAR_OPERATION_MODEL(char16_t)
716REDUCTION_CHAR_OPERATION_MODEL(char32_t)
717
718template <>
719constexpr TypeBuilderFunc
720getModel<Fortran::runtime::ReductionDerivedTypeOperation>() {
721 return [](mlir::MLIRContext *context) -> mlir::Type {
722 auto voidTy =
723 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
724 return mlir::FunctionType::get(context, {voidTy, voidTy, voidTy}, voidTy);
725 };
726}
727
728template <typename...>
730template <typename RT, typename... ATs>
731struct RuntimeTableKey<RT(ATs...)> {
732 static constexpr FuncTypeBuilderFunc getTypeModel() {
733 return [](mlir::MLIRContext *ctxt) {
734 TypeBuilderFunc ret = getModel<RT>();
735 std::array<TypeBuilderFunc, sizeof...(ATs)> args = {getModel<ATs>()...};
736 mlir::Type retTy = ret(ctxt);
737 llvm::SmallVector<mlir::Type, sizeof...(ATs)> argTys;
738 for (auto f : args)
739 argTys.push_back(f(ctxt));
740 if (mlir::isa<mlir::NoneType>(retTy))
741 return mlir::FunctionType::get(ctxt, argTys, {});
742 return mlir::FunctionType::get(ctxt, argTys, {retTy});
743 };
744 }
745};
746
747//===----------------------------------------------------------------------===//
748// Runtime table building (constexpr folded)
749//===----------------------------------------------------------------------===//
750
751template <char... Cs>
752using RuntimeIdentifier = std::integer_sequence<char, Cs...>;
753
754namespace details {
755template <typename T, T... As, T... Bs>
756static constexpr std::integer_sequence<T, As..., Bs...>
757concat(std::integer_sequence<T, As...>, std::integer_sequence<T, Bs...>) {
758 return {};
759}
760template <typename T, T... As, T... Bs, typename... Cs>
761static constexpr auto concat(std::integer_sequence<T, As...>,
762 std::integer_sequence<T, Bs...>, Cs...) {
763 return concat(std::integer_sequence<T, As..., Bs...>{}, Cs{}...);
764}
765template <typename T>
766static constexpr std::integer_sequence<T> concat(std::integer_sequence<T>) {
767 return {};
768}
769template <typename T, T a>
770static constexpr auto filterZero(std::integer_sequence<T, a>) {
771 if constexpr (a != 0) {
772 return std::integer_sequence<T, a>{};
773 } else {
774 return std::integer_sequence<T>{};
775 }
776}
777template <typename T, T... b>
778static constexpr auto filter(std::integer_sequence<T, b...>) {
779 if constexpr (sizeof...(b) > 0) {
780 return details::concat(filterZero(std::integer_sequence<T, b>{})...);
781 } else {
782 return std::integer_sequence<T>{};
783 }
784}
785} // namespace details
786
787template <typename...>
789template <typename KT, char... Cs>
790struct RuntimeTableEntry<RuntimeTableKey<KT>, RuntimeIdentifier<Cs...>> {
791 static constexpr FuncTypeBuilderFunc getTypeModel() {
793 }
794 static constexpr const char name[sizeof...(Cs) + 1] = {Cs..., '\0'};
795};
796
808#undef FirE
809#define FirE(L, I) (I < sizeof(L) / sizeof(*L) ? L[I] : 0)
810#define FirQuoteKey(X) #X
811#define ExpandAndQuoteKey(X) FirQuoteKey(X)
812#define FirMacroExpandKey(X) \
813 FirE(X, 0), FirE(X, 1), FirE(X, 2), FirE(X, 3), FirE(X, 4), FirE(X, 5), \
814 FirE(X, 6), FirE(X, 7), FirE(X, 8), FirE(X, 9), FirE(X, 10), \
815 FirE(X, 11), FirE(X, 12), FirE(X, 13), FirE(X, 14), FirE(X, 15), \
816 FirE(X, 16), FirE(X, 17), FirE(X, 18), FirE(X, 19), FirE(X, 20), \
817 FirE(X, 21), FirE(X, 22), FirE(X, 23), FirE(X, 24), FirE(X, 25), \
818 FirE(X, 26), FirE(X, 27), FirE(X, 28), FirE(X, 29), FirE(X, 30), \
819 FirE(X, 31), FirE(X, 32), FirE(X, 33), FirE(X, 34), FirE(X, 35), \
820 FirE(X, 36), FirE(X, 37), FirE(X, 38), FirE(X, 39), FirE(X, 40), \
821 FirE(X, 41), FirE(X, 42), FirE(X, 43), FirE(X, 44), FirE(X, 45), \
822 FirE(X, 46), FirE(X, 47), FirE(X, 48), FirE(X, 49)
823#define FirExpandKey(X) FirMacroExpandKey(FirQuoteKey(X))
824#define FirFullSeq(X) std::integer_sequence<char, FirExpandKey(X)>
825#define FirAsSequence(X) \
826 decltype(fir::runtime::details::filter(FirFullSeq(X){}))
827#define FirmkKey(X) \
828 fir::runtime::RuntimeTableEntry<fir::runtime::RuntimeTableKey<decltype(X)>, \
829 FirAsSequence(X)>
830#define mkRTKey(X) FirmkKey(RTNAME(X))
831#define EXPAND_AND_QUOTE_KEY(S) ExpandAndQuoteKey(RTNAME(S))
832
835template <typename RuntimeEntry>
836static mlir::func::FuncOp getRuntimeFunc(mlir::Location loc,
837 fir::FirOpBuilder &builder,
838 bool isIO = false) {
839 using namespace Fortran::runtime;
840 auto name = RuntimeEntry::name;
841 auto func = builder.getNamedFunction(name);
842 if (func)
843 return func;
844 auto funTy = RuntimeEntry::getTypeModel()(builder.getContext());
845 return builder.createRuntimeFunction(loc, name, funTy, isIO);
846}
847
849template <typename E>
850static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
851 fir::FirOpBuilder &builder) {
852 return getRuntimeFunc<E>(loc, builder, /*isIO=*/true);
853}
854
855inline llvm::SmallVector<mlir::Value>
856createArguments(fir::FirOpBuilder &builder, mlir::Location loc,
857 mlir::FunctionType fTy, llvm::ArrayRef<mlir::Value> args) {
858 return llvm::map_to_vector(llvm::zip_equal(fTy.getInputs(), args),
859 [&](const auto &pair) -> mlir::Value {
860 auto [type, argument] = pair;
861 return builder.createConvertWithVolatileCast(
862 loc, type, argument);
863 });
864}
865
867template <typename... As>
868llvm::SmallVector<mlir::Value>
869createArguments(fir::FirOpBuilder &builder, mlir::Location loc,
870 mlir::FunctionType fTy, As... args) {
871 return createArguments(builder, loc, fTy, {args...});
872}
873
874} // namespace fir::runtime
875
876#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RTBUILDER_H
Definition FIRBuilder.h:55
mlir::func::FuncOp createRuntimeFunction(mlir::Location loc, llvm::StringRef name, mlir::FunctionType ty, bool isIO=false)
Definition FIRBuilder.cpp:52
mlir::func::FuncOp getNamedFunction(llvm::StringRef name)
Definition FIRBuilder.h:394
Definition OpenACC.h:20
Definition RTBuilder.h:788
Definition RTBuilder.h:729