22
22
#include " flang/Optimizer/Builder/FIRBuilder.h"
23
23
#include " flang/Optimizer/Dialect/FIRDialect.h"
24
24
#include " flang/Optimizer/Dialect/FIRType.h"
25
+ #include " flang/Runtime/reduce.h"
25
26
#include " mlir/IR/BuiltinTypes.h"
26
27
#include " mlir/IR/MLIRContext.h"
27
28
#include " llvm/ADT/SmallVector.h"
@@ -52,6 +53,34 @@ namespace fir::runtime {
52
53
using TypeBuilderFunc = mlir::Type (*)(mlir::MLIRContext *);
53
54
using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *);
54
55
56
+ #define REDUCTION_OPERATION_MODEL (T ) \
57
+ template <> \
58
+ constexpr TypeBuilderFunc \
59
+ getModel<Fortran::runtime::ReductionOperation<T>>() { \
60
+ return [](mlir::MLIRContext *context) -> mlir::Type { \
61
+ TypeBuilderFunc f{getModel<T>()}; \
62
+ auto refTy = fir::ReferenceType::get (f (context)); \
63
+ return mlir::FunctionType::get (context, {refTy, refTy}, refTy); \
64
+ }; \
65
+ }
66
+
67
+ #define REDUCTION_CHAR_OPERATION_MODEL (T ) \
68
+ template <> \
69
+ constexpr TypeBuilderFunc \
70
+ getModel<Fortran::runtime::ReductionCharOperation<T>>() { \
71
+ return [](mlir::MLIRContext *context) -> mlir::Type { \
72
+ TypeBuilderFunc f{getModel<T>()}; \
73
+ auto voidTy = fir::LLVMPointerType::get ( \
74
+ context, mlir::IntegerType::get (context, 8 )); \
75
+ auto size_tTy = \
76
+ mlir::IntegerType::get (context, 8 * sizeof (std::size_t )); \
77
+ auto refTy = fir::ReferenceType::get (f (context)); \
78
+ return mlir::FunctionType::get ( \
79
+ context, {refTy, size_tTy, refTy, refTy, size_tTy, size_tTy}, \
80
+ voidTy); \
81
+ }; \
82
+ }
83
+
55
84
// ===----------------------------------------------------------------------===//
56
85
// Type builder models
57
86
// ===----------------------------------------------------------------------===//
@@ -75,14 +104,24 @@ constexpr TypeBuilderFunc getModel<unsigned int>() {
75
104
return mlir::IntegerType::get (context, 8 * sizeof (unsigned int ));
76
105
};
77
106
}
78
-
79
107
template <>
80
108
constexpr TypeBuilderFunc getModel<short int >() {
81
109
return [](mlir::MLIRContext *context) -> mlir::Type {
82
110
return mlir::IntegerType::get (context, 8 * sizeof (short int ));
83
111
};
84
112
}
85
113
template <>
114
+ constexpr TypeBuilderFunc getModel<short int *>() {
115
+ return [](mlir::MLIRContext *context) -> mlir::Type {
116
+ TypeBuilderFunc f{getModel<short int >()};
117
+ return fir::ReferenceType::get (f (context));
118
+ };
119
+ }
120
+ template <>
121
+ constexpr TypeBuilderFunc getModel<const short int *>() {
122
+ return getModel<short int *>();
123
+ }
124
+ template <>
86
125
constexpr TypeBuilderFunc getModel<int >() {
87
126
return [](mlir::MLIRContext *context) -> mlir::Type {
88
127
return mlir::IntegerType::get (context, 8 * sizeof (int ));
@@ -96,6 +135,17 @@ constexpr TypeBuilderFunc getModel<int &>() {
96
135
};
97
136
}
98
137
template <>
138
+ constexpr TypeBuilderFunc getModel<int *>() {
139
+ return getModel<int &>();
140
+ }
141
+ template <>
142
+ constexpr TypeBuilderFunc getModel<const int *>() {
143
+ return [](mlir::MLIRContext *context) -> mlir::Type {
144
+ TypeBuilderFunc f{getModel<int >()};
145
+ return fir::ReferenceType::get (f (context));
146
+ };
147
+ }
148
+ template <>
99
149
constexpr TypeBuilderFunc getModel<char *>() {
100
150
return [](mlir::MLIRContext *context) -> mlir::Type {
101
151
return fir::ReferenceType::get (mlir::IntegerType::get (context, 8 ));
@@ -130,6 +180,43 @@ constexpr TypeBuilderFunc getModel<signed char>() {
130
180
};
131
181
}
132
182
template <>
183
+ constexpr TypeBuilderFunc getModel<signed char *>() {
184
+ return [](mlir::MLIRContext *context) -> mlir::Type {
185
+ TypeBuilderFunc f{getModel<signed char >()};
186
+ return fir::ReferenceType::get (f (context));
187
+ };
188
+ }
189
+ template <>
190
+ constexpr TypeBuilderFunc getModel<const signed char *>() {
191
+ return getModel<signed char *>();
192
+ }
193
+ template <>
194
+ constexpr TypeBuilderFunc getModel<char16_t >() {
195
+ return [](mlir::MLIRContext *context) -> mlir::Type {
196
+ return mlir::IntegerType::get (context, 8 * sizeof (char16_t ));
197
+ };
198
+ }
199
+ template <>
200
+ constexpr TypeBuilderFunc getModel<char16_t *>() {
201
+ return [](mlir::MLIRContext *context) -> mlir::Type {
202
+ TypeBuilderFunc f{getModel<char16_t >()};
203
+ return fir::ReferenceType::get (f (context));
204
+ };
205
+ }
206
+ template <>
207
+ constexpr TypeBuilderFunc getModel<char32_t >() {
208
+ return [](mlir::MLIRContext *context) -> mlir::Type {
209
+ return mlir::IntegerType::get (context, 8 * sizeof (char32_t ));
210
+ };
211
+ }
212
+ template <>
213
+ constexpr TypeBuilderFunc getModel<char32_t *>() {
214
+ return [](mlir::MLIRContext *context) -> mlir::Type {
215
+ TypeBuilderFunc f{getModel<char32_t >()};
216
+ return fir::ReferenceType::get (f (context));
217
+ };
218
+ }
219
+ template <>
133
220
constexpr TypeBuilderFunc getModel<unsigned char >() {
134
221
return [](mlir::MLIRContext *context) -> mlir::Type {
135
222
return mlir::IntegerType::get (context, 8 * sizeof (unsigned char ));
@@ -175,6 +262,10 @@ constexpr TypeBuilderFunc getModel<long *>() {
175
262
return getModel<long &>();
176
263
}
177
264
template <>
265
+ constexpr TypeBuilderFunc getModel<const long *>() {
266
+ return getModel<long *>();
267
+ }
268
+ template <>
178
269
constexpr TypeBuilderFunc getModel<long long >() {
179
270
return [](mlir::MLIRContext *context) -> mlir::Type {
180
271
return mlir::IntegerType::get (context, 8 * sizeof (long long ));
@@ -198,6 +289,7 @@ template <>
198
289
constexpr TypeBuilderFunc getModel<long long *>() {
199
290
return getModel<long long &>();
200
291
}
292
+
201
293
template <>
202
294
constexpr TypeBuilderFunc getModel<unsigned long >() {
203
295
return [](mlir::MLIRContext *context) -> mlir::Type {
@@ -228,6 +320,27 @@ constexpr TypeBuilderFunc getModel<double *>() {
228
320
return getModel<double &>();
229
321
}
230
322
template <>
323
+ constexpr TypeBuilderFunc getModel<const double *>() {
324
+ return getModel<double *>();
325
+ }
326
+ template <>
327
+ constexpr TypeBuilderFunc getModel<long double >() {
328
+ return [](mlir::MLIRContext *context) -> mlir::Type {
329
+ return mlir::FloatType::getF80 (context);
330
+ };
331
+ }
332
+ template <>
333
+ constexpr TypeBuilderFunc getModel<long double *>() {
334
+ return [](mlir::MLIRContext *context) -> mlir::Type {
335
+ TypeBuilderFunc f{getModel<long double >()};
336
+ return fir::ReferenceType::get (f (context));
337
+ };
338
+ }
339
+ template <>
340
+ constexpr TypeBuilderFunc getModel<const long double *>() {
341
+ return getModel<long double *>();
342
+ }
343
+ template <>
231
344
constexpr TypeBuilderFunc getModel<float >() {
232
345
return [](mlir::MLIRContext *context) -> mlir::Type {
233
346
return mlir::FloatType::getF32 (context);
@@ -245,6 +358,10 @@ constexpr TypeBuilderFunc getModel<float *>() {
245
358
return getModel<float &>();
246
359
}
247
360
template <>
361
+ constexpr TypeBuilderFunc getModel<const float *>() {
362
+ return getModel<float *>();
363
+ }
364
+ template <>
248
365
constexpr TypeBuilderFunc getModel<bool >() {
249
366
return [](mlir::MLIRContext *context) -> mlir::Type {
250
367
return mlir::IntegerType::get (context, 1 );
@@ -258,20 +375,48 @@ constexpr TypeBuilderFunc getModel<bool &>() {
258
375
};
259
376
}
260
377
template <>
378
+ constexpr TypeBuilderFunc getModel<std::complex<float >>() {
379
+ return [](mlir::MLIRContext *context) -> mlir::Type {
380
+ return mlir::ComplexType::get (mlir::FloatType::getF32 (context));
381
+ };
382
+ }
383
+ template <>
261
384
constexpr TypeBuilderFunc getModel<std::complex<float > &>() {
262
385
return [](mlir::MLIRContext *context) -> mlir::Type {
263
- auto ty = mlir::ComplexType::get (mlir::FloatType::getF32 (context));
264
- return fir::ReferenceType::get (ty);
386
+ TypeBuilderFunc f{getModel<std::complex<float >>()};
387
+ return fir::ReferenceType::get (f (context));
388
+ };
389
+ }
390
+ template <>
391
+ constexpr TypeBuilderFunc getModel<std::complex<float > *>() {
392
+ return getModel<std::complex<float > &>();
393
+ }
394
+ template <>
395
+ constexpr TypeBuilderFunc getModel<const std::complex<float > *>() {
396
+ return getModel<std::complex<float > *>();
397
+ }
398
+ template <>
399
+ constexpr TypeBuilderFunc getModel<std::complex<double >>() {
400
+ return [](mlir::MLIRContext *context) -> mlir::Type {
401
+ return mlir::ComplexType::get (mlir::FloatType::getF64 (context));
265
402
};
266
403
}
267
404
template <>
268
405
constexpr TypeBuilderFunc getModel<std::complex<double > &>() {
269
406
return [](mlir::MLIRContext *context) -> mlir::Type {
270
- auto ty = mlir::ComplexType::get ( mlir::FloatType::getF64 (context)) ;
271
- return fir::ReferenceType::get (ty );
407
+ TypeBuilderFunc f{getModel<std::complex< double >>()} ;
408
+ return fir::ReferenceType::get (f (context) );
272
409
};
273
410
}
274
411
template <>
412
+ constexpr TypeBuilderFunc getModel<std::complex<double > *>() {
413
+ return getModel<std::complex<double > &>();
414
+ }
415
+ template <>
416
+ constexpr TypeBuilderFunc getModel<const std::complex<double > *>() {
417
+ return getModel<std::complex<double > *>();
418
+ }
419
+ template <>
275
420
constexpr TypeBuilderFunc getModel<c_float_complex_t >() {
276
421
return [](mlir::MLIRContext *context) -> mlir::Type {
277
422
return fir::ComplexType::get (context, sizeof (float ));
@@ -332,6 +477,33 @@ constexpr TypeBuilderFunc getModel<void>() {
332
477
};
333
478
}
334
479
480
+ REDUCTION_OPERATION_MODEL (std::int8_t )
481
+ REDUCTION_OPERATION_MODEL (std::int16_t )
482
+ REDUCTION_OPERATION_MODEL (std::int32_t )
483
+ REDUCTION_OPERATION_MODEL (std::int64_t )
484
+ REDUCTION_OPERATION_MODEL (Fortran::common::int128_t )
485
+
486
+ REDUCTION_OPERATION_MODEL (float )
487
+ REDUCTION_OPERATION_MODEL (double )
488
+ REDUCTION_OPERATION_MODEL (long double )
489
+
490
+ REDUCTION_OPERATION_MODEL (std::complex<float >)
491
+ REDUCTION_OPERATION_MODEL (std::complex<double >)
492
+
493
+ REDUCTION_CHAR_OPERATION_MODEL (char )
494
+ REDUCTION_CHAR_OPERATION_MODEL (char16_t )
495
+ REDUCTION_CHAR_OPERATION_MODEL (char32_t )
496
+
497
+ template <>
498
+ constexpr TypeBuilderFunc
499
+ getModel<Fortran::runtime::ReductionDerivedTypeOperation>() {
500
+ return [](mlir::MLIRContext *context) -> mlir::Type {
501
+ auto voidTy =
502
+ fir::LLVMPointerType::get (context, mlir::IntegerType::get (context, 8 ));
503
+ return mlir::FunctionType::get (context, {voidTy, voidTy, voidTy}, voidTy);
504
+ };
505
+ }
506
+
335
507
template <typename ...>
336
508
struct RuntimeTableKey ;
337
509
template <typename RT, typename ... ATs>
0 commit comments