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 (*)(void)>() {
283 return [](mlir::MLIRContext *context) -> mlir::Type {
284 return fir::LLVMPointerType::get(
286 mlir::FunctionType::get(context, {}, {}));
290constexpr TypeBuilderFunc getModel<void **>() {
291 return [](mlir::MLIRContext *context) -> mlir::Type {
292 return fir::ReferenceType::get(
293 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8)));
297constexpr TypeBuilderFunc getModel<long>() {
298 return [](mlir::MLIRContext *context) -> mlir::Type {
299 return mlir::IntegerType::get(context, 8 *
sizeof(
long));
303constexpr TypeBuilderFunc getModel<long &>() {
304 return [](mlir::MLIRContext *context) -> mlir::Type {
305 TypeBuilderFunc f{getModel<long>()};
306 return fir::ReferenceType::get(f(context));
310constexpr TypeBuilderFunc getModel<long *>() {
311 return getModel<long &>();
314constexpr TypeBuilderFunc getModel<const long *>() {
315 return getModel<long *>();
318constexpr TypeBuilderFunc getModel<long long>() {
319 return [](mlir::MLIRContext *context) -> mlir::Type {
320 return mlir::IntegerType::get(context, 8 *
sizeof(
long long));
324constexpr TypeBuilderFunc getModel<Fortran::common::int128_t>() {
325 return [](mlir::MLIRContext *context) -> mlir::Type {
326 return mlir::IntegerType::get(context,
327 8 *
sizeof(Fortran::common::int128_t));
331constexpr TypeBuilderFunc getModel<long long &>() {
332 return [](mlir::MLIRContext *context) -> mlir::Type {
333 TypeBuilderFunc f{getModel<long long>()};
334 return fir::ReferenceType::get(f(context));
338constexpr TypeBuilderFunc getModel<long long *>() {
339 return getModel<long long &>();
342constexpr TypeBuilderFunc getModel<const long long *>() {
343 return getModel<long long *>();
346constexpr TypeBuilderFunc getModel<unsigned long>() {
347 return [](mlir::MLIRContext *context) -> mlir::Type {
348 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long));
352constexpr TypeBuilderFunc getModel<unsigned long long>() {
353 return [](mlir::MLIRContext *context) -> mlir::Type {
354 return mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long long));
358constexpr TypeBuilderFunc getModel<double>() {
359 return [](mlir::MLIRContext *context) -> mlir::Type {
360 return mlir::Float64Type::get(context);
364constexpr TypeBuilderFunc getModel<double &>() {
365 return [](mlir::MLIRContext *context) -> mlir::Type {
366 TypeBuilderFunc f{getModel<double>()};
367 return fir::ReferenceType::get(f(context));
371constexpr TypeBuilderFunc getModel<double *>() {
372 return getModel<double &>();
375constexpr TypeBuilderFunc getModel<const double *>() {
376 return getModel<double *>();
379constexpr TypeBuilderFunc getModel<long double>() {
380 return [](mlir::MLIRContext *context) -> mlir::Type {
383 constexpr size_t size =
sizeof(
long double);
384 static_assert(size == 16 || size == 10 || size == 8,
385 "unsupported long double size");
386 if constexpr (size == 16)
387 return mlir::Float128Type::get(context);
388 if constexpr (size == 10)
389 return mlir::Float80Type::get(context);
390 if constexpr (size == 8)
391 return mlir::Float64Type::get(context);
392 llvm_unreachable(
"failed static assert");
396constexpr TypeBuilderFunc getModel<long double *>() {
397 return [](mlir::MLIRContext *context) -> mlir::Type {
398 TypeBuilderFunc f{getModel<long double>()};
399 return fir::ReferenceType::get(f(context));
403constexpr TypeBuilderFunc getModel<const long double *>() {
404 return getModel<long double *>();
407constexpr TypeBuilderFunc getModel<float>() {
408 return [](mlir::MLIRContext *context) -> mlir::Type {
409 return mlir::Float32Type::get(context);
413constexpr TypeBuilderFunc getModel<float &>() {
414 return [](mlir::MLIRContext *context) -> mlir::Type {
415 TypeBuilderFunc f{getModel<float>()};
416 return fir::ReferenceType::get(f(context));
420constexpr TypeBuilderFunc getModel<float *>() {
421 return getModel<float &>();
424constexpr TypeBuilderFunc getModel<const float *>() {
425 return getModel<float *>();
428constexpr TypeBuilderFunc getModel<bool>() {
429 return [](mlir::MLIRContext *context) -> mlir::Type {
430 return mlir::IntegerType::get(context, 1);
434constexpr TypeBuilderFunc getModel<bool &>() {
435 return [](mlir::MLIRContext *context) -> mlir::Type {
436 TypeBuilderFunc f{getModel<bool>()};
437 return fir::ReferenceType::get(f(context));
441constexpr TypeBuilderFunc getModel<bool *>() {
442 return [](mlir::MLIRContext *context) -> mlir::Type {
443 TypeBuilderFunc f{getModel<bool>()};
444 return fir::ReferenceType::get(f(context));
448constexpr TypeBuilderFunc getModel<unsigned short>() {
449 return [](mlir::MLIRContext *context) -> mlir::Type {
450 return mlir::IntegerType::get(
451 context, 8 *
sizeof(
unsigned short),
452 mlir::IntegerType::SignednessSemantics::Unsigned);
456constexpr TypeBuilderFunc getModel<unsigned char *>() {
457 return [](mlir::MLIRContext *context) -> mlir::Type {
458 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
462constexpr TypeBuilderFunc getModel<const unsigned char *>() {
463 return getModel<unsigned char *>();
466constexpr TypeBuilderFunc getModel<unsigned short *>() {
467 return [](mlir::MLIRContext *context) -> mlir::Type {
468 return fir::ReferenceType::get(
469 mlir::IntegerType::get(context, 8 *
sizeof(
unsigned short)));
473constexpr TypeBuilderFunc getModel<const unsigned short *>() {
474 return getModel<unsigned short *>();
477constexpr TypeBuilderFunc getModel<unsigned *>() {
478 return getModel<int *>();
481constexpr TypeBuilderFunc getModel<const unsigned *>() {
482 return getModel<unsigned *>();
485constexpr TypeBuilderFunc getModel<unsigned long *>() {
486 return [](mlir::MLIRContext *context) -> mlir::Type {
487 return fir::ReferenceType::get(
488 mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long)));
492constexpr TypeBuilderFunc getModel<const unsigned long *>() {
493 return getModel<unsigned long *>();
496constexpr TypeBuilderFunc getModel<unsigned long long *>() {
497 return [](mlir::MLIRContext *context) -> mlir::Type {
498 return fir::ReferenceType::get(
499 mlir::IntegerType::get(context, 8 *
sizeof(
unsigned long long)));
503constexpr TypeBuilderFunc getModel<const unsigned long long *>() {
504 return getModel<unsigned long long *>();
507constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t>() {
508 return getModel<Fortran::common::int128_t>();
511constexpr TypeBuilderFunc getModel<Fortran::common::int128_t *>() {
512 return [](mlir::MLIRContext *context) -> mlir::Type {
513 TypeBuilderFunc f{getModel<Fortran::common::int128_t>()};
514 return fir::ReferenceType::get(f(context));
518constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t *>() {
519 return getModel<Fortran::common::int128_t *>();
522constexpr TypeBuilderFunc getModel<const Fortran::common::uint128_t *>() {
523 return getModel<Fortran::common::uint128_t *>();
534constexpr TypeBuilderFunc getModel<std::complex<float> &>() {
535 return [](mlir::MLIRContext *context) -> mlir::Type {
536 mlir::Type floatTy = getModel<float>()(context);
537 return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
541constexpr TypeBuilderFunc getModel<std::complex<float> *>() {
542 return getModel<std::complex<float> &>();
545constexpr TypeBuilderFunc getModel<const std::complex<float> *>() {
546 return getModel<std::complex<float> *>();
549constexpr TypeBuilderFunc getModel<std::complex<double> &>() {
550 return [](mlir::MLIRContext *context) -> mlir::Type {
551 mlir::Type floatTy = getModel<double>()(context);
552 return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
556constexpr TypeBuilderFunc getModel<std::complex<double> *>() {
557 return getModel<std::complex<double> &>();
560constexpr TypeBuilderFunc getModel<const std::complex<double> *>() {
561 return getModel<std::complex<double> *>();
564constexpr TypeBuilderFunc getModel<c_float_complex_t>() {
565 return [](mlir::MLIRContext *context) -> mlir::Type {
566 mlir::Type floatTy = getModel<float>()(context);
567 return mlir::ComplexType::get(floatTy);
571constexpr TypeBuilderFunc getModel<c_double_complex_t>() {
572 return [](mlir::MLIRContext *context) -> mlir::Type {
573 mlir::Type floatTy = getModel<double>()(context);
574 return mlir::ComplexType::get(floatTy);
578constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor &>() {
579 return [](mlir::MLIRContext *context) -> mlir::Type {
580 return fir::BoxType::get(mlir::NoneType::get(context));
584constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor &>() {
585 return [](mlir::MLIRContext *context) -> mlir::Type {
586 return fir::ReferenceType::get(
587 fir::BoxType::get(mlir::NoneType::get(context)));
591constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor *>() {
592 return getModel<const Fortran::runtime::Descriptor &>();
595constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor *>() {
596 return getModel<Fortran::runtime::Descriptor &>();
599constexpr TypeBuilderFunc getModel<Fortran::common::TypeCategory>() {
600 return [](mlir::MLIRContext *context) -> mlir::Type {
601 return mlir::IntegerType::get(context,
602 sizeof(Fortran::common::TypeCategory) * 8);
606constexpr TypeBuilderFunc
607getModel<const Fortran::runtime::typeInfo::DerivedType &>() {
608 return [](mlir::MLIRContext *context) -> mlir::Type {
609 return fir::ReferenceType::get(mlir::NoneType::get(context));
613constexpr TypeBuilderFunc
614getModel<const Fortran::runtime::typeInfo::DerivedType *>() {
615 return [](mlir::MLIRContext *context) -> mlir::Type {
616 return fir::ReferenceType::get(mlir::NoneType::get(context));
620constexpr TypeBuilderFunc getModel<void>() {
621 return [](mlir::MLIRContext *context) -> mlir::Type {
622 return mlir::NoneType::get(context);
628constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
629 return getModel<char *>();
632constexpr TypeBuilderFunc getModel<Fortran::runtime::io::Iostat>() {
633 return [](mlir::MLIRContext *context) -> mlir::Type {
634 return mlir::IntegerType::get(context,
635 8 *
sizeof(Fortran::runtime::io::Iostat));
639constexpr TypeBuilderFunc
640getModel<const Fortran::runtime::io::NamelistGroup &>() {
641 return [](mlir::MLIRContext *context) -> mlir::Type {
642 return fir::ReferenceType::get(mlir::TupleType::get(context));
646constexpr TypeBuilderFunc
647getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
648 return [](mlir::MLIRContext *context) -> mlir::Type {
649 return fir::ReferenceType::get(mlir::TupleType::get(context));
653REDUCTION_REF_OPERATION_MODEL(std::int8_t)
654REDUCTION_VALUE_OPERATION_MODEL(std::int8_t)
655REDUCTION_REF_OPERATION_MODEL(std::int16_t)
656REDUCTION_VALUE_OPERATION_MODEL(std::int16_t)
657REDUCTION_REF_OPERATION_MODEL(std::int32_t)
658REDUCTION_VALUE_OPERATION_MODEL(std::int32_t)
659REDUCTION_REF_OPERATION_MODEL(std::int64_t)
660REDUCTION_VALUE_OPERATION_MODEL(std::int64_t)
661REDUCTION_REF_OPERATION_MODEL(Fortran::common::int128_t)
662REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::int128_t)
664REDUCTION_REF_OPERATION_MODEL(std::uint8_t)
665REDUCTION_VALUE_OPERATION_MODEL(std::uint8_t)
666REDUCTION_REF_OPERATION_MODEL(std::uint16_t)
667REDUCTION_VALUE_OPERATION_MODEL(std::uint16_t)
668REDUCTION_REF_OPERATION_MODEL(std::uint32_t)
669REDUCTION_VALUE_OPERATION_MODEL(std::uint32_t)
670REDUCTION_REF_OPERATION_MODEL(std::uint64_t)
671REDUCTION_VALUE_OPERATION_MODEL(std::uint64_t)
672REDUCTION_REF_OPERATION_MODEL(Fortran::common::uint128_t)
673REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::uint128_t)
675REDUCTION_REF_OPERATION_MODEL(
float)
676REDUCTION_VALUE_OPERATION_MODEL(
float)
677REDUCTION_REF_OPERATION_MODEL(
double)
678REDUCTION_VALUE_OPERATION_MODEL(
double)
679REDUCTION_REF_OPERATION_MODEL(
long double)
680REDUCTION_VALUE_OPERATION_MODEL(
long double)
686constexpr TypeBuilderFunc
687getModel<Fortran::runtime::ValueReductionOperation<std::complex<float>>>() {
688 return [](mlir::MLIRContext *context) -> mlir::Type {
689 mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
690 auto refTy = fir::ReferenceType::get(cplx);
691 return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
695constexpr TypeBuilderFunc
696getModel<Fortran::runtime::ValueReductionOperation<std::complex<double>>>() {
697 return [](mlir::MLIRContext *context) -> mlir::Type {
698 mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
699 auto refTy = fir::ReferenceType::get(cplx);
700 return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
704constexpr TypeBuilderFunc
705getModel<Fortran::runtime::ReferenceReductionOperation<std::complex<float>>>() {
706 return [](mlir::MLIRContext *context) -> mlir::Type {
707 mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
708 auto refTy = fir::ReferenceType::get(cplx);
709 return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
713constexpr TypeBuilderFunc getModel<
714 Fortran::runtime::ReferenceReductionOperation<std::complex<double>>>() {
715 return [](mlir::MLIRContext *context) -> mlir::Type {
716 mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
717 auto refTy = fir::ReferenceType::get(cplx);
718 return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
722REDUCTION_CHAR_OPERATION_MODEL(
char)
723REDUCTION_CHAR_OPERATION_MODEL(
char16_t)
724REDUCTION_CHAR_OPERATION_MODEL(
char32_t)
727constexpr TypeBuilderFunc
728getModel<Fortran::runtime::ReductionDerivedTypeOperation>() {
729 return [](mlir::MLIRContext *context) -> mlir::Type {
731 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
732 return mlir::FunctionType::get(context, {voidTy, voidTy, voidTy}, voidTy);
736template <
typename...>
738template <
typename RT,
typename... ATs>
740 static constexpr FuncTypeBuilderFunc getTypeModel() {
741 return [](mlir::MLIRContext *ctxt) {
742 TypeBuilderFunc ret = getModel<RT>();
743 std::array<TypeBuilderFunc,
sizeof...(ATs)> args = {getModel<ATs>()...};
744 mlir::Type retTy = ret(ctxt);
747 argTys.push_back(f(ctxt));
748 if (mlir::isa<mlir::NoneType>(retTy))
749 return mlir::FunctionType::get(ctxt, argTys, {});
750 return mlir::FunctionType::get(ctxt, argTys, {retTy});
760using RuntimeIdentifier = std::integer_sequence<char, Cs...>;
763template <
typename T, T... As, T... Bs>
764static constexpr std::integer_sequence<T, As..., Bs...>
765concat(std::integer_sequence<T, As...>, std::integer_sequence<T, Bs...>) {
768template <
typename T, T... As, T... Bs,
typename... Cs>
769static constexpr auto concat(std::integer_sequence<T, As...>,
770 std::integer_sequence<T, Bs...>, Cs...) {
771 return concat(std::integer_sequence<T, As..., Bs...>{}, Cs{}...);
774static constexpr std::integer_sequence<T> concat(std::integer_sequence<T>) {
777template <
typename T, T a>
778static constexpr auto filterZero(std::integer_sequence<T, a>) {
779 if constexpr (a != 0) {
780 return std::integer_sequence<T, a>{};
782 return std::integer_sequence<T>{};
785template <
typename T, T... b>
786static constexpr auto filter(std::integer_sequence<T, b...>) {
787 if constexpr (
sizeof...(b) > 0) {
788 return details::concat(filterZero(std::integer_sequence<T, b>{})...);
790 return std::integer_sequence<T>{};
795template <
typename...>
797template <
typename KT,
char... Cs>
799 static constexpr FuncTypeBuilderFunc getTypeModel() {
802 static constexpr const char name[
sizeof...(Cs) + 1] = {Cs...,
'\0'};
817#define FirE(L, I) (I < sizeof(L) / sizeof(*L) ? L[I] : 0)
818#define FirQuoteKey(X) #X
819#define ExpandAndQuoteKey(X) FirQuoteKey(X)
820#define FirMacroExpandKey(X) \
821 FirE(X, 0), FirE(X, 1), FirE(X, 2), FirE(X, 3), FirE(X, 4), FirE(X, 5), \
822 FirE(X, 6), FirE(X, 7), FirE(X, 8), FirE(X, 9), FirE(X, 10), \
823 FirE(X, 11), FirE(X, 12), FirE(X, 13), FirE(X, 14), FirE(X, 15), \
824 FirE(X, 16), FirE(X, 17), FirE(X, 18), FirE(X, 19), FirE(X, 20), \
825 FirE(X, 21), FirE(X, 22), FirE(X, 23), FirE(X, 24), FirE(X, 25), \
826 FirE(X, 26), FirE(X, 27), FirE(X, 28), FirE(X, 29), FirE(X, 30), \
827 FirE(X, 31), FirE(X, 32), FirE(X, 33), FirE(X, 34), FirE(X, 35), \
828 FirE(X, 36), FirE(X, 37), FirE(X, 38), FirE(X, 39), FirE(X, 40), \
829 FirE(X, 41), FirE(X, 42), FirE(X, 43), FirE(X, 44), FirE(X, 45), \
830 FirE(X, 46), FirE(X, 47), FirE(X, 48), FirE(X, 49)
831#define FirExpandKey(X) FirMacroExpandKey(FirQuoteKey(X))
832#define FirFullSeq(X) std::integer_sequence<char, FirExpandKey(X)>
833#define FirAsSequence(X) \
834 decltype(fir::runtime::details::filter(FirFullSeq(X){}))
836 fir::runtime::RuntimeTableEntry<fir::runtime::RuntimeTableKey<decltype(X)>, \
838#define mkRTKey(X) FirmkKey(RTNAME(X))
839#define EXPAND_AND_QUOTE_KEY(S) ExpandAndQuoteKey(RTNAME(S))
843template <
typename RuntimeEntry>
844static mlir::func::FuncOp getRuntimeFunc(mlir::Location loc,
847 using namespace Fortran::runtime;
848 auto name = RuntimeEntry::name;
852 auto funTy = RuntimeEntry::getTypeModel()(builder.getContext());
858static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
859 fir::FirOpBuilder &builder) {
860 return getRuntimeFunc<E>(loc, builder,
true);
863inline llvm::SmallVector<mlir::Value>
864createArguments(fir::FirOpBuilder &builder, mlir::Location loc,
865 mlir::FunctionType fTy, llvm::ArrayRef<mlir::Value> args) {
866 return llvm::map_to_vector(llvm::zip_equal(fTy.getInputs(), args),
867 [&](
const auto &pair) -> mlir::Value {
868 auto [type, argument] = pair;
869 return builder.createConvertWithVolatileCast(
870 loc, type, argument);
875template <
typename... As>
876llvm::SmallVector<mlir::Value>
878 mlir::FunctionType fTy, As... args) {
879 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:796