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 getModel<void **>() {
256 return [](mlir::MLIRContext *context) -> mlir::Type {
257 return fir::ReferenceType::get(
258 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8)));
259 };
260}
261template <>
262constexpr TypeBuilderFunc getModel<long>() {
263 return [](mlir::MLIRContext *context) -> mlir::Type {
264 return mlir::IntegerType::get(context, 8 * sizeof(long));
265 };
266}
267template <>
268constexpr TypeBuilderFunc getModel<long &>() {
269 return [](mlir::MLIRContext *context) -> mlir::Type {
270 TypeBuilderFunc f{getModel<long>()};
271 return fir::ReferenceType::get(f(context));
272 };
273}
274template <>
275constexpr TypeBuilderFunc getModel<long *>() {
276 return getModel<long &>();
277}
278template <>
279constexpr TypeBuilderFunc getModel<const long *>() {
280 return getModel<long *>();
281}
282template <>
283constexpr TypeBuilderFunc getModel<long long>() {
284 return [](mlir::MLIRContext *context) -> mlir::Type {
285 return mlir::IntegerType::get(context, 8 * sizeof(long long));
286 };
287}
288template <>
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));
293 };
294}
295template <>
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));
300 };
301}
302template <>
303constexpr TypeBuilderFunc getModel<long long *>() {
304 return getModel<long long &>();
305}
306template <>
307constexpr TypeBuilderFunc getModel<const long long *>() {
308 return getModel<long long *>();
309}
310template <>
311constexpr TypeBuilderFunc getModel<unsigned long>() {
312 return [](mlir::MLIRContext *context) -> mlir::Type {
313 return mlir::IntegerType::get(context, 8 * sizeof(unsigned long));
314 };
315}
316template <>
317constexpr TypeBuilderFunc getModel<unsigned long long>() {
318 return [](mlir::MLIRContext *context) -> mlir::Type {
319 return mlir::IntegerType::get(context, 8 * sizeof(unsigned long long));
320 };
321}
322template <>
323constexpr TypeBuilderFunc getModel<double>() {
324 return [](mlir::MLIRContext *context) -> mlir::Type {
325 return mlir::Float64Type::get(context);
326 };
327}
328template <>
329constexpr TypeBuilderFunc getModel<double &>() {
330 return [](mlir::MLIRContext *context) -> mlir::Type {
331 TypeBuilderFunc f{getModel<double>()};
332 return fir::ReferenceType::get(f(context));
333 };
334}
335template <>
336constexpr TypeBuilderFunc getModel<double *>() {
337 return getModel<double &>();
338}
339template <>
340constexpr TypeBuilderFunc getModel<const double *>() {
341 return getModel<double *>();
342}
343template <>
344constexpr TypeBuilderFunc getModel<long double>() {
345 return [](mlir::MLIRContext *context) -> mlir::Type {
346 // See TODO at the top of the file. This is configuring for the host system
347 // - it might be incorrect when cross-compiling!
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");
358 };
359}
360template <>
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));
365 };
366}
367template <>
368constexpr TypeBuilderFunc getModel<const long double *>() {
369 return getModel<long double *>();
370}
371template <>
372constexpr TypeBuilderFunc getModel<float>() {
373 return [](mlir::MLIRContext *context) -> mlir::Type {
374 return mlir::Float32Type::get(context);
375 };
376}
377template <>
378constexpr TypeBuilderFunc getModel<float &>() {
379 return [](mlir::MLIRContext *context) -> mlir::Type {
380 TypeBuilderFunc f{getModel<float>()};
381 return fir::ReferenceType::get(f(context));
382 };
383}
384template <>
385constexpr TypeBuilderFunc getModel<float *>() {
386 return getModel<float &>();
387}
388template <>
389constexpr TypeBuilderFunc getModel<const float *>() {
390 return getModel<float *>();
391}
392template <>
393constexpr TypeBuilderFunc getModel<bool>() {
394 return [](mlir::MLIRContext *context) -> mlir::Type {
395 return mlir::IntegerType::get(context, 1);
396 };
397}
398template <>
399constexpr TypeBuilderFunc getModel<bool &>() {
400 return [](mlir::MLIRContext *context) -> mlir::Type {
401 TypeBuilderFunc f{getModel<bool>()};
402 return fir::ReferenceType::get(f(context));
403 };
404}
405template <>
406constexpr TypeBuilderFunc getModel<bool *>() {
407 return [](mlir::MLIRContext *context) -> mlir::Type {
408 TypeBuilderFunc f{getModel<bool>()};
409 return fir::ReferenceType::get(f(context));
410 };
411}
412template <>
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);
418 };
419}
420template <>
421constexpr TypeBuilderFunc getModel<unsigned char *>() {
422 return [](mlir::MLIRContext *context) -> mlir::Type {
423 return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
424 };
425}
426template <>
427constexpr TypeBuilderFunc getModel<const unsigned char *>() {
428 return getModel<unsigned char *>();
429}
430template <>
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)));
435 };
436}
437template <>
438constexpr TypeBuilderFunc getModel<const unsigned short *>() {
439 return getModel<unsigned short *>();
440}
441template <>
442constexpr TypeBuilderFunc getModel<unsigned *>() {
443 return getModel<int *>();
444}
445template <>
446constexpr TypeBuilderFunc getModel<const unsigned *>() {
447 return getModel<unsigned *>();
448}
449template <>
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)));
454 };
455}
456template <>
457constexpr TypeBuilderFunc getModel<const unsigned long *>() {
458 return getModel<unsigned long *>();
459}
460template <>
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)));
465 };
466}
467template <>
468constexpr TypeBuilderFunc getModel<const unsigned long long *>() {
469 return getModel<unsigned long long *>();
470}
471template <>
472constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t>() {
473 return getModel<Fortran::common::int128_t>();
474}
475template <>
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));
480 };
481}
482template <>
483constexpr TypeBuilderFunc getModel<Fortran::common::uint128_t *>() {
484 return getModel<Fortran::common::int128_t *>();
485}
486template <>
487constexpr TypeBuilderFunc getModel<const Fortran::common::uint128_t *>() {
488 return getModel<Fortran::common::uint128_t *>();
489}
490
491// getModel<std::complex<T>> are not implemented on purpose.
492// Prefer passing/returning the complex by reference in the runtime to
493// avoid ABI issues.
494// C++ std::complex is not an intrinsic type, and while it is storage
495// compatible with C/Fortran complex type, it follows the struct value passing
496// ABI rule, which may differ from how C complex are passed on some platforms.
497
498template <>
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));
503 };
504}
505template <>
506constexpr TypeBuilderFunc getModel<std::complex<float> *>() {
507 return getModel<std::complex<float> &>();
508}
509template <>
510constexpr TypeBuilderFunc getModel<const std::complex<float> *>() {
511 return getModel<std::complex<float> *>();
512}
513template <>
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));
518 };
519}
520template <>
521constexpr TypeBuilderFunc getModel<std::complex<double> *>() {
522 return getModel<std::complex<double> &>();
523}
524template <>
525constexpr TypeBuilderFunc getModel<const std::complex<double> *>() {
526 return getModel<std::complex<double> *>();
527}
528template <>
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);
533 };
534}
535template <>
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);
540 };
541}
542template <>
543constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor &>() {
544 return [](mlir::MLIRContext *context) -> mlir::Type {
545 return fir::BoxType::get(mlir::NoneType::get(context));
546 };
547}
548template <>
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)));
553 };
554}
555template <>
556constexpr TypeBuilderFunc getModel<const Fortran::runtime::Descriptor *>() {
557 return getModel<const Fortran::runtime::Descriptor &>();
558}
559template <>
560constexpr TypeBuilderFunc getModel<Fortran::runtime::Descriptor *>() {
561 return getModel<Fortran::runtime::Descriptor &>();
562}
563template <>
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);
568 };
569}
570template <>
571constexpr TypeBuilderFunc
572getModel<const Fortran::runtime::typeInfo::DerivedType &>() {
573 return [](mlir::MLIRContext *context) -> mlir::Type {
574 return fir::ReferenceType::get(mlir::NoneType::get(context));
575 };
576}
577template <>
578constexpr TypeBuilderFunc
579getModel<const Fortran::runtime::typeInfo::DerivedType *>() {
580 return [](mlir::MLIRContext *context) -> mlir::Type {
581 return fir::ReferenceType::get(mlir::NoneType::get(context));
582 };
583}
584template <>
585constexpr TypeBuilderFunc getModel<void>() {
586 return [](mlir::MLIRContext *context) -> mlir::Type {
587 return mlir::NoneType::get(context);
588 };
589}
590
591// Define additional runtime type models specific to IO.
592template <>
593constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
594 return getModel<char *>();
595}
596template <>
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));
601 };
602}
603template <>
604constexpr TypeBuilderFunc
605getModel<const Fortran::runtime::io::NamelistGroup &>() {
606 return [](mlir::MLIRContext *context) -> mlir::Type {
607 return fir::ReferenceType::get(mlir::TupleType::get(context));
608 };
609}
610template <>
611constexpr TypeBuilderFunc
612getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
613 return [](mlir::MLIRContext *context) -> mlir::Type {
614 return fir::ReferenceType::get(mlir::TupleType::get(context));
615 };
616}
617
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)
628
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)
639
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)
646
647// FIXME: the runtime is not using the correct ABIs when calling complex
648// callbacks. lowering either need to create wrappers or just have an inline
649// implementation for it. https://github.com/llvm/llvm-project/issues/110674
650template <>
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);
657 };
658}
659template <>
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);
666 };
667}
668template <>
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);
675 };
676}
677template <>
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);
684 };
685}
686
687REDUCTION_CHAR_OPERATION_MODEL(char)
688REDUCTION_CHAR_OPERATION_MODEL(char16_t)
689REDUCTION_CHAR_OPERATION_MODEL(char32_t)
690
691template <>
692constexpr TypeBuilderFunc
693getModel<Fortran::runtime::ReductionDerivedTypeOperation>() {
694 return [](mlir::MLIRContext *context) -> mlir::Type {
695 auto voidTy =
696 fir::LLVMPointerType::get(context, mlir::IntegerType::get(context, 8));
697 return mlir::FunctionType::get(context, {voidTy, voidTy, voidTy}, voidTy);
698 };
699}
700
701template <typename...>
703template <typename RT, typename... ATs>
704struct RuntimeTableKey<RT(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);
710 llvm::SmallVector<mlir::Type, sizeof...(ATs)> argTys;
711 for (auto f : args)
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});
716 };
717 }
718};
719
720//===----------------------------------------------------------------------===//
721// Runtime table building (constexpr folded)
722//===----------------------------------------------------------------------===//
723
724template <char... Cs>
725using RuntimeIdentifier = std::integer_sequence<char, Cs...>;
726
727namespace details {
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...>) {
731 return {};
732}
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{}...);
737}
738template <typename T>
739static constexpr std::integer_sequence<T> concat(std::integer_sequence<T>) {
740 return {};
741}
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>{};
746 } else {
747 return std::integer_sequence<T>{};
748 }
749}
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>{})...);
754 } else {
755 return std::integer_sequence<T>{};
756 }
757}
758} // namespace details
759
760template <typename...>
762template <typename KT, char... Cs>
763struct RuntimeTableEntry<RuntimeTableKey<KT>, RuntimeIdentifier<Cs...>> {
764 static constexpr FuncTypeBuilderFunc getTypeModel() {
766 }
767 static constexpr const char name[sizeof...(Cs) + 1] = {Cs..., '\0'};
768};
769
781#undef FirE
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){}))
800#define FirmkKey(X) \
801 fir::runtime::RuntimeTableEntry<fir::runtime::RuntimeTableKey<decltype(X)>, \
802 FirAsSequence(X)>
803#define mkRTKey(X) FirmkKey(RTNAME(X))
804#define EXPAND_AND_QUOTE_KEY(S) ExpandAndQuoteKey(RTNAME(S))
805
808template <typename RuntimeEntry>
809static mlir::func::FuncOp getRuntimeFunc(mlir::Location loc,
810 fir::FirOpBuilder &builder,
811 bool isIO = false) {
812 using namespace Fortran::runtime;
813 auto name = RuntimeEntry::name;
814 auto func = builder.getNamedFunction(name);
815 if (func)
816 return func;
817 auto funTy = RuntimeEntry::getTypeModel()(builder.getContext());
818 return builder.createRuntimeFunction(loc, name, funTy, isIO);
819}
820
822template <typename E>
823static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
824 fir::FirOpBuilder &builder) {
825 return getRuntimeFunc<E>(loc, builder, /*isIO=*/true);
826}
827
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);
836 });
837}
838
840template <typename... As>
841llvm::SmallVector<mlir::Value>
842createArguments(fir::FirOpBuilder &builder, mlir::Location loc,
843 mlir::FunctionType fTy, As... args) {
844 return createArguments(builder, loc, fTy, {args...});
845}
846
847} // namespace fir::runtime
848
849#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:387
Definition OpenACC.h:20
Definition RTBuilder.h:761
Definition RTBuilder.h:702