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
256getModel<void *(*)(void *, const void *, unsigned long)>() {
257 return [](mlir::MLIRContext *context) -> mlir::Type {
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);
269constexpr TypeBuilderFunc
270getModel<void *(*)(void *, const void *, unsigned __int64)>() {
271 return [](mlir::MLIRContext *context) -> mlir::Type {
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);
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)));
289constexpr TypeBuilderFunc getModel<long>() {
290 return [](mlir::MLIRContext *context) -> mlir::Type {
291 return mlir::IntegerType::get(context, 8 *
sizeof(
long));
295constexpr TypeBuilderFunc getModel<long &>() {
296 return [](mlir::MLIRContext *context) -> mlir::Type {
297 TypeBuilderFunc f{getModel<long>()};
298 return fir::ReferenceType::get(f(context));
302constexpr TypeBuilderFunc getModel<long *>() {
303 return getModel<long &>();
306constexpr TypeBuilderFunc getModel<const long *>() {
307 return getModel<long *>();
310constexpr TypeBuilderFunc getModel<long long>() {
311 return [](mlir::MLIRContext *context) -> mlir::Type {
312 return mlir::IntegerType::get(context, 8 *
sizeof(
long long));
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));
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));
330constexpr TypeBuilderFunc getModel<long long *>() {
331 return getModel<long long &>();
334constexpr TypeBuilderFunc getModel<const long long *>() {
335 return getModel<long long *>();
338constexpr TypeBuilderFunc getModel<unsigned long>() {
339 return [](mlir::MLIRContext *context) -> mlir::Type {
340 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long));
344constexpr TypeBuilderFunc getModel<unsigned long long>() {
345 return [](mlir::MLIRContext *context) -> mlir::Type {
346 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long long));
350constexpr TypeBuilderFunc getModel<double>() {
351 return [](mlir::MLIRContext *context) -> mlir::Type {
352 return mlir::Float64Type::get(context);
356constexpr TypeBuilderFunc getModel<double &>() {
357 return [](mlir::MLIRContext *context) -> mlir::Type {
358 TypeBuilderFunc f{getModel<double>()};
359 return fir::ReferenceType::get(f(context));
363constexpr TypeBuilderFunc getModel<double *>() {
364 return getModel<double &>();
367constexpr TypeBuilderFunc getModel<const double *>() {
368 return getModel<double *>();
371constexpr TypeBuilderFunc getModel<long double>() {
372 return [](mlir::MLIRContext *context) -> mlir::Type {
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");
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));
395constexpr TypeBuilderFunc getModel<const long double *>() {
396 return getModel<long double *>();
399constexpr TypeBuilderFunc getModel<float>() {
400 return [](mlir::MLIRContext *context) -> mlir::Type {
401 return mlir::Float32Type::get(context);
405constexpr TypeBuilderFunc getModel<float &>() {
406 return [](mlir::MLIRContext *context) -> mlir::Type {
407 TypeBuilderFunc f{getModel<float>()};
408 return fir::ReferenceType::get(f(context));
412constexpr TypeBuilderFunc getModel<float *>() {
413 return getModel<float &>();
416constexpr TypeBuilderFunc getModel<const float *>() {
417 return getModel<float *>();
420constexpr TypeBuilderFunc getModel<bool>() {
421 return [](mlir::MLIRContext *context) -> mlir::Type {
422 return mlir::IntegerType::get(context, 1);
426constexpr TypeBuilderFunc getModel<bool &>() {
427 return [](mlir::MLIRContext *context) -> mlir::Type {
428 TypeBuilderFunc f{getModel<bool>()};
429 return fir::ReferenceType::get(f(context));
433constexpr TypeBuilderFunc getModel<bool *>() {
434 return [](mlir::MLIRContext *context) -> mlir::Type {
435 TypeBuilderFunc f{getModel<bool>()};
436 return fir::ReferenceType::get(f(context));
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);
448constexpr TypeBuilderFunc getModel<unsigned char *>() {
449 return [](mlir::MLIRContext *context) -> mlir::Type {
450 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
454constexpr TypeBuilderFunc getModel<const unsigned char *>() {
455 return getModel<unsigned char *>();
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)));
465constexpr TypeBuilderFunc getModel<const unsigned short *>() {
466 return getModel<unsigned short *>();
469constexpr TypeBuilderFunc getModel<unsigned *>() {
470 return getModel<int *>();
473constexpr TypeBuilderFunc getModel<const unsigned *>() {
474 return getModel<unsigned *>();
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)));
484constexpr TypeBuilderFunc getModel<const unsigned long *>() {
485 return getModel<unsigned long *>();
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)));
495constexpr TypeBuilderFunc getModel<const unsigned long long *>() {
496 return getModel<unsigned long long *>();
499constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t>() {
500 return getModel<Fortran::common::int128_t>();
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));
510constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t *>() {
511 return getModel<Fortran::common::int128_t *>();
514constexpr TypeBuilderFunc getModel<const Fortran::common::uint128_t *>() {
515 return getModel<Fortran::common::uint128_t *>();
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));
533constexpr TypeBuilderFunc getModel<std::complex<float> *>() {
534 return getModel<std::complex<float> &>();
537constexpr TypeBuilderFunc getModel<const std::complex<float> *>() {
538 return getModel<std::complex<float> *>();
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));
548constexpr TypeBuilderFunc getModel<std::complex<double> *>() {
549 return getModel<std::complex<double> &>();
552constexpr TypeBuilderFunc getModel<const std::complex<double> *>() {
553 return getModel<std::complex<double> *>();
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);
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);
570constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor &>() {
571 return [](mlir::MLIRContext *context) -> mlir::Type {
572 return fir::BoxType::get(mlir::NoneType::get(context));
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)));
583constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor *>() {
584 return getModel<const Fortran::runtime::Descriptor &>();
587constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor *>() {
588 return getModel<Fortran::runtime::Descriptor &>();
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);
598constexpr TypeBuilderFunc
599getModel<const Fortran::runtime::typeInfo::DerivedType &>() {
600 return [](mlir::MLIRContext *context) -> mlir::Type {
601 return fir::ReferenceType::get(mlir::NoneType::get(context));
605constexpr TypeBuilderFunc
606getModel<const Fortran::runtime::typeInfo::DerivedType *>() {
607 return [](mlir::MLIRContext *context) -> mlir::Type {
608 return fir::ReferenceType::get(mlir::NoneType::get(context));
612constexpr TypeBuilderFunc getModel<void>() {
613 return [](mlir::MLIRContext *context) -> mlir::Type {
614 return mlir::NoneType::get(context);
620constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
621 return getModel<char *>();
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));
631constexpr TypeBuilderFunc
632getModel<const Fortran::runtime::io::NamelistGroup &>() {
633 return [](mlir::MLIRContext *context) -> mlir::Type {
634 return fir::ReferenceType::get(mlir::TupleType::get(context));
638constexpr TypeBuilderFunc
639getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
640 return [](mlir::MLIRContext *context) -> mlir::Type {
641 return fir::ReferenceType::get(mlir::TupleType::get(context));
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)
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)
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)
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);
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);
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);
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);
714REDUCTION_CHAR_OPERATION_MODEL(
char)
715REDUCTION_CHAR_OPERATION_MODEL(
char16_t)
716REDUCTION_CHAR_OPERATION_MODEL(
char32_t)
719constexpr TypeBuilderFunc
720getModel<Fortran::runtime::ReductionDerivedTypeOperation>() {
721 return [](mlir::MLIRContext *context) -> mlir::Type {
723 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
724 return mlir::FunctionType::get(context, {voidTy, voidTy, voidTy}, voidTy);
728template <
typename...>
730template <
typename RT,
typename... 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);
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});
752using RuntimeIdentifier = std::integer_sequence<char, Cs...>;
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...>) {
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{}...);
766static constexpr std::integer_sequence<T> concat(std::integer_sequence<T>) {
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>{};
774 return std::integer_sequence<T>{};
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>{})...);
782 return std::integer_sequence<T>{};
787template <
typename...>
789template <
typename KT,
char... Cs>
791 static constexpr FuncTypeBuilderFunc getTypeModel() {
794 static constexpr const char name[
sizeof...(Cs) + 1] = {Cs...,
'\0'};
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){}))
828 fir::runtime::RuntimeTableEntry<fir::runtime::RuntimeTableKey<decltype(X)>, \
830#define mkRTKey(X) FirmkKey(RTNAME(X))
831#define EXPAND_AND_QUOTE_KEY(S) ExpandAndQuoteKey(RTNAME(S))
835template <
typename RuntimeEntry>
836static mlir::func::FuncOp getRuntimeFunc(mlir::Location loc,
839 using namespace Fortran::runtime;
840 auto name = RuntimeEntry::name;
844 auto funTy = RuntimeEntry::getTypeModel()(builder.getContext());
850static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
851 fir::FirOpBuilder &builder) {
852 return getRuntimeFunc<E>(loc, builder,
true);
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);
867template <
typename... As>
868llvm::SmallVector<mlir::Value>
870 mlir::FunctionType fTy, As... args) {
871 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:394
Definition RTBuilder.h:788