17#ifndef FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RTBUILDER_H
18#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RTBUILDER_H
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"
36typedef std::uint32_t pid_t;
43struct c_float_complex_t;
44struct c_double_complex_t;
46namespace Fortran::runtime {
53namespace fir::runtime {
55using TypeBuilderFunc = mlir::Type (*)(mlir::MLIRContext *);
56using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *);
58#define REDUCTION_REF_OPERATION_MODEL(T) \
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); \
69#define REDUCTION_VALUE_OPERATION_MODEL(T) \
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)}, \
81#define REDUCTION_CHAR_OPERATION_MODEL(T) \
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)); \
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}, \
113static constexpr TypeBuilderFunc getModel();
116constexpr TypeBuilderFunc getModel<unsigned int>() {
117 return [](mlir::MLIRContext *context) -> mlir::Type {
118 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned int));
122constexpr TypeBuilderFunc getModel<short int>() {
123 return [](mlir::MLIRContext *context) -> mlir::Type {
124 return mlir::IntegerType::get(context, 8 *
sizeof(
short int));
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));
135constexpr TypeBuilderFunc getModel<const short int *>() {
136 return getModel<short int *>();
139constexpr TypeBuilderFunc getModel<int>() {
140 return [](mlir::MLIRContext *context) -> mlir::Type {
141 return mlir::IntegerType::get(context, 8 *
sizeof(
int));
145constexpr TypeBuilderFunc getModel<int &>() {
146 return [](mlir::MLIRContext *context) -> mlir::Type {
147 TypeBuilderFunc f{getModel<int>()};
148 return fir::ReferenceType::get(f(context));
152constexpr TypeBuilderFunc getModel<int *>() {
153 return getModel<int &>();
156constexpr TypeBuilderFunc getModel<const int *>() {
157 return [](mlir::MLIRContext *context) -> mlir::Type {
158 TypeBuilderFunc f{getModel<int>()};
159 return fir::ReferenceType::get(f(context));
163constexpr TypeBuilderFunc getModel<char *>() {
164 return [](mlir::MLIRContext *context) -> mlir::Type {
165 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
169constexpr TypeBuilderFunc getModel<const char *>() {
170 return getModel<char *>();
173constexpr TypeBuilderFunc getModel<const char16_t *>() {
174 return [](mlir::MLIRContext *context) -> mlir::Type {
175 return fir::ReferenceType::get(mlir::IntegerType::get(context, 16));
179constexpr TypeBuilderFunc getModel<const char32_t *>() {
180 return [](mlir::MLIRContext *context) -> mlir::Type {
181 return fir::ReferenceType::get(mlir::IntegerType::get(context, 32));
185constexpr TypeBuilderFunc getModel<char>() {
186 return [](mlir::MLIRContext *context) -> mlir::Type {
187 return mlir::IntegerType::get(context, 8 *
sizeof(
char));
191constexpr TypeBuilderFunc getModel<signed char>() {
192 return [](mlir::MLIRContext *context) -> mlir::Type {
193 return mlir::IntegerType::get(context, 8 *
sizeof(
signed char));
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));
204constexpr TypeBuilderFunc getModel<const signed char *>() {
205 return getModel<signed char *>();
208constexpr TypeBuilderFunc getModel<char16_t>() {
209 return [](mlir::MLIRContext *context) -> mlir::Type {
210 return mlir::IntegerType::get(context, 8 *
sizeof(
char16_t));
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));
221constexpr TypeBuilderFunc getModel<char32_t>() {
222 return [](mlir::MLIRContext *context) -> mlir::Type {
223 return mlir::IntegerType::get(context, 8 *
sizeof(
char32_t));
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));
234constexpr TypeBuilderFunc getModel<unsigned char>() {
235 return [](mlir::MLIRContext *context) -> mlir::Type {
236 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned char));
240constexpr TypeBuilderFunc getModel<void *>() {
241 return [](mlir::MLIRContext *context) -> mlir::Type {
242 return fir::LLVMPointerType::get(context,
243 mlir::IntegerType::get(context, 8));
247constexpr TypeBuilderFunc getModel<void (*)(int)>() {
248 return [](mlir::MLIRContext *context) -> mlir::Type {
249 return fir::LLVMPointerType::get(
251 mlir::FunctionType::get(context, {}, {}));
255constexpr TypeBuilderFunc getModel<void **>() {
256 return [](mlir::MLIRContext *context) -> mlir::Type {
257 return fir::ReferenceType::get(
258 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8)));
262constexpr TypeBuilderFunc getModel<long>() {
263 return [](mlir::MLIRContext *context) -> mlir::Type {
264 return mlir::IntegerType::get(context, 8 *
sizeof(
long));
268constexpr TypeBuilderFunc getModel<long &>() {
269 return [](mlir::MLIRContext *context) -> mlir::Type {
270 TypeBuilderFunc f{getModel<long>()};
271 return fir::ReferenceType::get(f(context));
275constexpr TypeBuilderFunc getModel<long *>() {
276 return getModel<long &>();
279constexpr TypeBuilderFunc getModel<const long *>() {
280 return getModel<long *>();
283constexpr TypeBuilderFunc getModel<long long>() {
284 return [](mlir::MLIRContext *context) -> mlir::Type {
285 return mlir::IntegerType::get(context, 8 *
sizeof(
long long));
289constexpr TypeBuilderFunc getModel<Fortran::common::int128_t>() {
290 return [](mlir::MLIRContext *context) -> mlir::Type {
291 return mlir::IntegerType::get(context,
292 8 *
sizeof(Fortran::common::int128_t));
296constexpr TypeBuilderFunc getModel<long long &>() {
297 return [](mlir::MLIRContext *context) -> mlir::Type {
298 TypeBuilderFunc f{getModel<long long>()};
299 return fir::ReferenceType::get(f(context));
303constexpr TypeBuilderFunc getModel<long long *>() {
304 return getModel<long long &>();
307constexpr TypeBuilderFunc getModel<const long long *>() {
308 return getModel<long long *>();
311constexpr TypeBuilderFunc getModel<unsigned long>() {
312 return [](mlir::MLIRContext *context) -> mlir::Type {
313 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long));
317constexpr TypeBuilderFunc getModel<unsigned long long>() {
318 return [](mlir::MLIRContext *context) -> mlir::Type {
319 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long long));
323constexpr TypeBuilderFunc getModel<double>() {
324 return [](mlir::MLIRContext *context) -> mlir::Type {
325 return mlir::Float64Type::get(context);
329constexpr TypeBuilderFunc getModel<double &>() {
330 return [](mlir::MLIRContext *context) -> mlir::Type {
331 TypeBuilderFunc f{getModel<double>()};
332 return fir::ReferenceType::get(f(context));
336constexpr TypeBuilderFunc getModel<double *>() {
337 return getModel<double &>();
340constexpr TypeBuilderFunc getModel<const double *>() {
341 return getModel<double *>();
344constexpr TypeBuilderFunc getModel<long double>() {
345 return [](mlir::MLIRContext *context) -> mlir::Type {
348 constexpr size_t size =
sizeof(
long double);
349 static_assert(size == 16 || size == 10 || size == 8,
350 "unsupported long double size");
351 if constexpr (size == 16)
352 return mlir::Float128Type::get(context);
353 if constexpr (size == 10)
354 return mlir::Float80Type::get(context);
355 if constexpr (size == 8)
356 return mlir::Float64Type::get(context);
357 llvm_unreachable(
"failed static assert");
361constexpr TypeBuilderFunc getModel<long double *>() {
362 return [](mlir::MLIRContext *context) -> mlir::Type {
363 TypeBuilderFunc f{getModel<long double>()};
364 return fir::ReferenceType::get(f(context));
368constexpr TypeBuilderFunc getModel<const long double *>() {
369 return getModel<long double *>();
372constexpr TypeBuilderFunc getModel<float>() {
373 return [](mlir::MLIRContext *context) -> mlir::Type {
374 return mlir::Float32Type::get(context);
378constexpr TypeBuilderFunc getModel<float &>() {
379 return [](mlir::MLIRContext *context) -> mlir::Type {
380 TypeBuilderFunc f{getModel<float>()};
381 return fir::ReferenceType::get(f(context));
385constexpr TypeBuilderFunc getModel<float *>() {
386 return getModel<float &>();
389constexpr TypeBuilderFunc getModel<const float *>() {
390 return getModel<float *>();
393constexpr TypeBuilderFunc getModel<bool>() {
394 return [](mlir::MLIRContext *context) -> mlir::Type {
395 return mlir::IntegerType::get(context, 1);
399constexpr TypeBuilderFunc getModel<bool &>() {
400 return [](mlir::MLIRContext *context) -> mlir::Type {
401 TypeBuilderFunc f{getModel<bool>()};
402 return fir::ReferenceType::get(f(context));
406constexpr TypeBuilderFunc getModel<bool *>() {
407 return [](mlir::MLIRContext *context) -> mlir::Type {
408 TypeBuilderFunc f{getModel<bool>()};
409 return fir::ReferenceType::get(f(context));
413constexpr TypeBuilderFunc getModel<unsigned short>() {
414 return [](mlir::MLIRContext *context) -> mlir::Type {
415 return mlir::IntegerType::get(
416 context, 8 *
sizeof(
unsigned short),
417 mlir::IntegerType::SignednessSemantics::Unsigned);
421constexpr TypeBuilderFunc getModel<unsigned char *>() {
422 return [](mlir::MLIRContext *context) -> mlir::Type {
423 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
427constexpr TypeBuilderFunc getModel<const unsigned char *>() {
428 return getModel<unsigned char *>();
431constexpr TypeBuilderFunc getModel<unsigned short *>() {
432 return [](mlir::MLIRContext *context) -> mlir::Type {
433 return fir::ReferenceType::get(
434 mlir::IntegerType::get(context, 8 *
sizeof(
unsigned short)));
438constexpr TypeBuilderFunc getModel<const unsigned short *>() {
439 return getModel<unsigned short *>();
442constexpr TypeBuilderFunc getModel<unsigned *>() {
443 return getModel<int *>();
446constexpr TypeBuilderFunc getModel<const unsigned *>() {
447 return getModel<unsigned *>();
450constexpr TypeBuilderFunc getModel<unsigned long *>() {
451 return [](mlir::MLIRContext *context) -> mlir::Type {
452 return fir::ReferenceType::get(
453 mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long)));
457constexpr TypeBuilderFunc getModel<const unsigned long *>() {
458 return getModel<unsigned long *>();
461constexpr TypeBuilderFunc getModel<unsigned long long *>() {
462 return [](mlir::MLIRContext *context) -> mlir::Type {
463 return fir::ReferenceType::get(
464 mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long long)));
468constexpr TypeBuilderFunc getModel<const unsigned long long *>() {
469 return getModel<unsigned long long *>();
472constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t>() {
473 return getModel<Fortran::common::int128_t>();
476constexpr TypeBuilderFunc getModel<Fortran::common::int128_t *>() {
477 return [](mlir::MLIRContext *context) -> mlir::Type {
478 TypeBuilderFunc f{getModel<Fortran::common::int128_t>()};
479 return fir::ReferenceType::get(f(context));
483constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t *>() {
484 return getModel<Fortran::common::int128_t *>();
487constexpr TypeBuilderFunc getModel<const Fortran::common::uint128_t *>() {
488 return getModel<Fortran::common::uint128_t *>();
499constexpr TypeBuilderFunc getModel<std::complex<float> &>() {
500 return [](mlir::MLIRContext *context) -> mlir::Type {
501 mlir::Type floatTy = getModel<float>()(context);
502 return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
506constexpr TypeBuilderFunc getModel<std::complex<float> *>() {
507 return getModel<std::complex<float> &>();
510constexpr TypeBuilderFunc getModel<const std::complex<float> *>() {
511 return getModel<std::complex<float> *>();
514constexpr TypeBuilderFunc getModel<std::complex<double> &>() {
515 return [](mlir::MLIRContext *context) -> mlir::Type {
516 mlir::Type floatTy = getModel<double>()(context);
517 return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
521constexpr TypeBuilderFunc getModel<std::complex<double> *>() {
522 return getModel<std::complex<double> &>();
525constexpr TypeBuilderFunc getModel<const std::complex<double> *>() {
526 return getModel<std::complex<double> *>();
529constexpr TypeBuilderFunc getModel<c_float_complex_t>() {
530 return [](mlir::MLIRContext *context) -> mlir::Type {
531 mlir::Type floatTy = getModel<float>()(context);
532 return mlir::ComplexType::get(floatTy);
536constexpr TypeBuilderFunc getModel<c_double_complex_t>() {
537 return [](mlir::MLIRContext *context) -> mlir::Type {
538 mlir::Type floatTy = getModel<double>()(context);
539 return mlir::ComplexType::get(floatTy);
543constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor &>() {
544 return [](mlir::MLIRContext *context) -> mlir::Type {
545 return fir::BoxType::get(mlir::NoneType::get(context));
549constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor &>() {
550 return [](mlir::MLIRContext *context) -> mlir::Type {
551 return fir::ReferenceType::get(
552 fir::BoxType::get(mlir::NoneType::get(context)));
556constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor *>() {
557 return getModel<const Fortran::runtime::Descriptor &>();
560constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor *>() {
561 return getModel<Fortran::runtime::Descriptor &>();
564constexpr TypeBuilderFunc getModel<Fortran::common::TypeCategory>() {
565 return [](mlir::MLIRContext *context) -> mlir::Type {
566 return mlir::IntegerType::get(context,
567 sizeof(Fortran::common::TypeCategory) * 8);
571constexpr TypeBuilderFunc
572getModel<const Fortran::runtime::typeInfo::DerivedType &>() {
573 return [](mlir::MLIRContext *context) -> mlir::Type {
574 return fir::ReferenceType::get(mlir::NoneType::get(context));
578constexpr TypeBuilderFunc
579getModel<const Fortran::runtime::typeInfo::DerivedType *>() {
580 return [](mlir::MLIRContext *context) -> mlir::Type {
581 return fir::ReferenceType::get(mlir::NoneType::get(context));
585constexpr TypeBuilderFunc getModel<void>() {
586 return [](mlir::MLIRContext *context) -> mlir::Type {
587 return mlir::NoneType::get(context);
593constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
594 return getModel<char *>();
597constexpr TypeBuilderFunc getModel<Fortran::runtime::io::Iostat>() {
598 return [](mlir::MLIRContext *context) -> mlir::Type {
599 return mlir::IntegerType::get(context,
600 8 *
sizeof(Fortran::runtime::io::Iostat));
604constexpr TypeBuilderFunc
605getModel<const Fortran::runtime::io::NamelistGroup &>() {
606 return [](mlir::MLIRContext *context) -> mlir::Type {
607 return fir::ReferenceType::get(mlir::TupleType::get(context));
611constexpr TypeBuilderFunc
612getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
613 return [](mlir::MLIRContext *context) -> mlir::Type {
614 return fir::ReferenceType::get(mlir::TupleType::get(context));
618REDUCTION_REF_OPERATION_MODEL(std::int8_t)
619REDUCTION_VALUE_OPERATION_MODEL(std::int8_t)
620REDUCTION_REF_OPERATION_MODEL(std::int16_t)
621REDUCTION_VALUE_OPERATION_MODEL(std::int16_t)
622REDUCTION_REF_OPERATION_MODEL(std::int32_t)
623REDUCTION_VALUE_OPERATION_MODEL(std::int32_t)
624REDUCTION_REF_OPERATION_MODEL(std::int64_t)
625REDUCTION_VALUE_OPERATION_MODEL(std::int64_t)
626REDUCTION_REF_OPERATION_MODEL(Fortran::common::int128_t)
627REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::int128_t)
629REDUCTION_REF_OPERATION_MODEL(std::uint8_t)
630REDUCTION_VALUE_OPERATION_MODEL(std::uint8_t)
631REDUCTION_REF_OPERATION_MODEL(std::uint16_t)
632REDUCTION_VALUE_OPERATION_MODEL(std::uint16_t)
633REDUCTION_REF_OPERATION_MODEL(std::uint32_t)
634REDUCTION_VALUE_OPERATION_MODEL(std::uint32_t)
635REDUCTION_REF_OPERATION_MODEL(std::uint64_t)
636REDUCTION_VALUE_OPERATION_MODEL(std::uint64_t)
637REDUCTION_REF_OPERATION_MODEL(Fortran::common::uint128_t)
638REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::uint128_t)
640REDUCTION_REF_OPERATION_MODEL(
float)
641REDUCTION_VALUE_OPERATION_MODEL(
float)
642REDUCTION_REF_OPERATION_MODEL(
double)
643REDUCTION_VALUE_OPERATION_MODEL(
double)
644REDUCTION_REF_OPERATION_MODEL(
long double)
645REDUCTION_VALUE_OPERATION_MODEL(
long double)
651constexpr TypeBuilderFunc
652getModel<Fortran::runtime::ValueReductionOperation<std::complex<float>>>() {
653 return [](mlir::MLIRContext *context) -> mlir::Type {
654 mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
655 auto refTy = fir::ReferenceType::get(cplx);
656 return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
660constexpr TypeBuilderFunc
661getModel<Fortran::runtime::ValueReductionOperation<std::complex<double>>>() {
662 return [](mlir::MLIRContext *context) -> mlir::Type {
663 mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
664 auto refTy = fir::ReferenceType::get(cplx);
665 return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
669constexpr TypeBuilderFunc
670getModel<Fortran::runtime::ReferenceReductionOperation<std::complex<float>>>() {
671 return [](mlir::MLIRContext *context) -> mlir::Type {
672 mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
673 auto refTy = fir::ReferenceType::get(cplx);
674 return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
678constexpr TypeBuilderFunc getModel<
679 Fortran::runtime::ReferenceReductionOperation<std::complex<double>>>() {
680 return [](mlir::MLIRContext *context) -> mlir::Type {
681 mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
682 auto refTy = fir::ReferenceType::get(cplx);
683 return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
687REDUCTION_CHAR_OPERATION_MODEL(
char)
688REDUCTION_CHAR_OPERATION_MODEL(
char16_t)
689REDUCTION_CHAR_OPERATION_MODEL(
char32_t)
692constexpr TypeBuilderFunc
693getModel<Fortran::runtime::ReductionDerivedTypeOperation>() {
694 return [](mlir::MLIRContext *context) -> mlir::Type {
696 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
697 return mlir::FunctionType::get(context, {voidTy, voidTy, voidTy}, voidTy);
701template <
typename...>
703template <
typename RT,
typename... ATs>
705 static constexpr FuncTypeBuilderFunc getTypeModel() {
706 return [](mlir::MLIRContext *ctxt) {
707 TypeBuilderFunc ret = getModel<RT>();
708 std::array<TypeBuilderFunc,
sizeof...(ATs)> args = {getModel<ATs>()...};
709 mlir::Type retTy = ret(ctxt);
712 argTys.push_back(f(ctxt));
713 if (mlir::isa<mlir::NoneType>(retTy))
714 return mlir::FunctionType::get(ctxt, argTys, {});
715 return mlir::FunctionType::get(ctxt, argTys, {retTy});
725using RuntimeIdentifier = std::integer_sequence<char, Cs...>;
728template <
typename T, T... As, T... Bs>
729static constexpr std::integer_sequence<T, As..., Bs...>
730concat(std::integer_sequence<T, As...>, std::integer_sequence<T, Bs...>) {
733template <
typename T, T... As, T... Bs,
typename... Cs>
734static constexpr auto concat(std::integer_sequence<T, As...>,
735 std::integer_sequence<T, Bs...>, Cs...) {
736 return concat(std::integer_sequence<T, As..., Bs...>{}, Cs{}...);
739static constexpr std::integer_sequence<T> concat(std::integer_sequence<T>) {
742template <
typename T, T a>
743static constexpr auto filterZero(std::integer_sequence<T, a>) {
744 if constexpr (a != 0) {
745 return std::integer_sequence<T, a>{};
747 return std::integer_sequence<T>{};
750template <
typename T, T... b>
751static constexpr auto filter(std::integer_sequence<T, b...>) {
752 if constexpr (
sizeof...(b) > 0) {
753 return details::concat(filterZero(std::integer_sequence<T, b>{})...);
755 return std::integer_sequence<T>{};
760template <
typename...>
762template <
typename KT,
char... Cs>
764 static constexpr FuncTypeBuilderFunc getTypeModel() {
767 static constexpr const char name[
sizeof...(Cs) + 1] = {Cs...,
'\0'};
782#define FirE(L, I) (I < sizeof(L) / sizeof(*L) ? L[I] : 0)
783#define FirQuoteKey(X) #X
784#define ExpandAndQuoteKey(X) FirQuoteKey(X)
785#define FirMacroExpandKey(X) \
786 FirE(X, 0), FirE(X, 1), FirE(X, 2), FirE(X, 3), FirE(X, 4), FirE(X, 5), \
787 FirE(X, 6), FirE(X, 7), FirE(X, 8), FirE(X, 9), FirE(X, 10), \
788 FirE(X, 11), FirE(X, 12), FirE(X, 13), FirE(X, 14), FirE(X, 15), \
789 FirE(X, 16), FirE(X, 17), FirE(X, 18), FirE(X, 19), FirE(X, 20), \
790 FirE(X, 21), FirE(X, 22), FirE(X, 23), FirE(X, 24), FirE(X, 25), \
791 FirE(X, 26), FirE(X, 27), FirE(X, 28), FirE(X, 29), FirE(X, 30), \
792 FirE(X, 31), FirE(X, 32), FirE(X, 33), FirE(X, 34), FirE(X, 35), \
793 FirE(X, 36), FirE(X, 37), FirE(X, 38), FirE(X, 39), FirE(X, 40), \
794 FirE(X, 41), FirE(X, 42), FirE(X, 43), FirE(X, 44), FirE(X, 45), \
795 FirE(X, 46), FirE(X, 47), FirE(X, 48), FirE(X, 49)
796#define FirExpandKey(X) FirMacroExpandKey(FirQuoteKey(X))
797#define FirFullSeq(X) std::integer_sequence<char, FirExpandKey(X)>
798#define FirAsSequence(X) \
799 decltype(fir::runtime::details::filter(FirFullSeq(X){}))
801 fir::runtime::RuntimeTableEntry<fir::runtime::RuntimeTableKey<decltype(X)>, \
803#define mkRTKey(X) FirmkKey(RTNAME(X))
804#define EXPAND_AND_QUOTE_KEY(S) ExpandAndQuoteKey(RTNAME(S))
808template <
typename RuntimeEntry>
809static mlir::func::FuncOp getRuntimeFunc(mlir::Location loc,
812 using namespace Fortran::runtime;
813 auto name = RuntimeEntry::name;
817 auto funTy = RuntimeEntry::getTypeModel()(builder.getContext());
823static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
824 fir::FirOpBuilder &builder) {
825 return getRuntimeFunc<E>(loc, builder,
true);
828inline llvm::SmallVector<mlir::Value>
829createArguments(fir::FirOpBuilder &builder, mlir::Location loc,
830 mlir::FunctionType fTy, llvm::ArrayRef<mlir::Value> args) {
831 return llvm::map_to_vector(llvm::zip_equal(fTy.getInputs(), args),
832 [&](
const auto &pair) -> mlir::Value {
833 auto [type, argument] = pair;
834 return builder.createConvertWithVolatileCast(
835 loc, type, argument);
840template <
typename... As>
841llvm::SmallVector<mlir::Value>
843 mlir::FunctionType fTy, As... args) {
844 return createArguments(builder, loc, fTy, {args...});
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:387
Definition RTBuilder.h:761