Debian Codegraph @

llvm-project/flang/lib/Optimizer/Builder/Runtime/Transformational.cpp

raw
1 //===-- Transformational.cpp ------------------------------------*- C++ -*-===//
2 // Generate transformational intrinsic runtime API calls.
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "flang/Optimizer/Builder/Runtime/Transformational.h"
11 #include "flang/Optimizer/Builder/BoxValue.h"
12 #include "flang/Optimizer/Builder/Character.h"
13 #include "flang/Optimizer/Builder/FIRBuilder.h"
14 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
15 #include "flang/Optimizer/Builder/Todo.h"
16 #include "flang/Runtime/matmul.h"
17 #include "flang/Runtime/transformational.h"
18 #include "mlir/Dialect/Func/IR/FuncOps.h"
19
20 using namespace Fortran::runtime;
21
22 /// Placeholder for real*10 version of BesselJn intrinsic.
23 struct ForcedBesselJn_10 {
24 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselJn_10));
25 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
26 return [](mlir::MLIRContext *ctx) {
27 auto ty = mlir::FloatType::getF80(ctx);
28 auto boxTy =
29 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
30 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
31 auto intTy = mlir::IntegerType::get(ctx, 32);
32 auto noneTy = mlir::NoneType::get(ctx);
33 return mlir::FunctionType::get(
34 ctx, {boxTy, intTy, intTy, ty, ty, ty, strTy, intTy}, {noneTy});
35 };
36 }
37 };
38
39 /// Placeholder for real*16 version of BesselJn intrinsic.
40 struct ForcedBesselJn_16 {
41 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselJn_16));
42 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
43 return [](mlir::MLIRContext *ctx) {
44 auto ty = mlir::FloatType::getF128(ctx);
45 auto boxTy =
46 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
47 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
48 auto intTy = mlir::IntegerType::get(ctx, 32);
49 auto noneTy = mlir::NoneType::get(ctx);
50 return mlir::FunctionType::get(
51 ctx, {boxTy, intTy, intTy, ty, ty, ty, strTy, intTy}, {noneTy});
52 };
53 }
54 };
55
56 /// Placeholder for real*10 version of BesselJn intrinsic when `x == 0.0`.
57 struct ForcedBesselJnX0_10 {
58 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselJnX0_10));
59 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
60 return [](mlir::MLIRContext *ctx) {
61 auto boxTy =
62 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
63 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
64 auto intTy = mlir::IntegerType::get(ctx, 32);
65 auto noneTy = mlir::NoneType::get(ctx);
66 return mlir::FunctionType::get(ctx, {boxTy, intTy, intTy, strTy, intTy},
67 {noneTy});
68 };
69 }
70 };
71
72 /// Placeholder for real*16 version of BesselJn intrinsic when `x == 0.0`.
73 struct ForcedBesselJnX0_16 {
74 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselJnX0_16));
75 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
76 return [](mlir::MLIRContext *ctx) {
77 auto boxTy =
78 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
79 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
80 auto intTy = mlir::IntegerType::get(ctx, 32);
81 auto noneTy = mlir::NoneType::get(ctx);
82 return mlir::FunctionType::get(ctx, {boxTy, intTy, intTy, strTy, intTy},
83 {noneTy});
84 };
85 }
86 };
87
88 /// Placeholder for real*10 version of BesselYn intrinsic.
89 struct ForcedBesselYn_10 {
90 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselYn_10));
91 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
92 return [](mlir::MLIRContext *ctx) {
93 auto ty = mlir::FloatType::getF80(ctx);
94 auto boxTy =
95 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
96 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
97 auto intTy = mlir::IntegerType::get(ctx, 32);
98 auto noneTy = mlir::NoneType::get(ctx);
99 return mlir::FunctionType::get(
100 ctx, {boxTy, intTy, intTy, ty, ty, ty, strTy, intTy}, {noneTy});
101 };
102 }
103 };
104
105 /// Placeholder for real*16 version of BesselYn intrinsic.
106 struct ForcedBesselYn_16 {
107 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselYn_16));
108 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
109 return [](mlir::MLIRContext *ctx) {
110 auto ty = mlir::FloatType::getF128(ctx);
111 auto boxTy =
112 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
113 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
114 auto intTy = mlir::IntegerType::get(ctx, 32);
115 auto noneTy = mlir::NoneType::get(ctx);
116 return mlir::FunctionType::get(
117 ctx, {boxTy, intTy, intTy, ty, ty, ty, strTy, intTy}, {noneTy});
118 };
119 }
120 };
121
122 /// Placeholder for real*10 version of BesselYn intrinsic when `x == 0.0`.
123 struct ForcedBesselYnX0_10 {
124 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselYnX0_10));
125 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
126 return [](mlir::MLIRContext *ctx) {
127 auto boxTy =
128 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
129 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
130 auto intTy = mlir::IntegerType::get(ctx, 32);
131 auto noneTy = mlir::NoneType::get(ctx);
132 return mlir::FunctionType::get(ctx, {boxTy, intTy, intTy, strTy, intTy},
133 {noneTy});
134 };
135 }
136 };
137
138 /// Placeholder for real*16 version of BesselYn intrinsic when `x == 0.0`.
139 struct ForcedBesselYnX0_16 {
140 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(BesselYnX0_16));
141 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
142 return [](mlir::MLIRContext *ctx) {
143 auto boxTy =
144 fir::runtime::getModel<Fortran::runtime::Descriptor &>()(ctx);
145 auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8));
146 auto intTy = mlir::IntegerType::get(ctx, 32);
147 auto noneTy = mlir::NoneType::get(ctx);
148 return mlir::FunctionType::get(ctx, {boxTy, intTy, intTy, strTy, intTy},
149 {noneTy});
150 };
151 }
152 };
153
154 /// Generate call to `BesselJn` intrinsic.
155 void fir::runtime::genBesselJn(fir::FirOpBuilder &builder, mlir::Location loc,
156 mlir::Value resultBox, mlir::Value n1,
157 mlir::Value n2, mlir::Value x, mlir::Value bn2,
158 mlir::Value bn2_1) {
159 mlir::func::FuncOp func;
160 auto xTy = x.getType();
161
162 if (xTy.isF16() || xTy.isBF16())
163 TODO(loc, "half-precision BESSEL_JN");
164 else if (xTy.isF32())
165 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselJn_4)>(loc, builder);
166 else if (xTy.isF64())
167 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselJn_8)>(loc, builder);
168 else if (xTy.isF80())
169 func = fir::runtime::getRuntimeFunc<ForcedBesselJn_10>(loc, builder);
170 else if (xTy.isF128())
171 func = fir::runtime::getRuntimeFunc<ForcedBesselJn_16>(loc, builder);
172 else
173 fir::emitFatalError(loc, "invalid type in BESSEL_JN");
174
175 auto fTy = func.getFunctionType();
176 auto sourceFile = fir::factory::locationToFilename(builder, loc);
177 auto sourceLine =
178 fir::factory::locationToLineNo(builder, loc, fTy.getInput(7));
179 auto args =
180 fir::runtime::createArguments(builder, loc, fTy, resultBox, n1, n2, x,
181 bn2, bn2_1, sourceFile, sourceLine);
182 builder.create<fir::CallOp>(loc, func, args);
183 }
184
185 /// Generate call to `BesselJn` intrinsic. This is used when `x == 0.0`.
186 void fir::runtime::genBesselJnX0(fir::FirOpBuilder &builder, mlir::Location loc,
187 mlir::Type xTy, mlir::Value resultBox,
188 mlir::Value n1, mlir::Value n2) {
189 mlir::func::FuncOp func;
190
191 if (xTy.isF16() || xTy.isBF16())
192 TODO(loc, "half-precision BESSEL_JN");
193 else if (xTy.isF32())
194 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselJnX0_4)>(loc, builder);
195 else if (xTy.isF64())
196 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselJnX0_8)>(loc, builder);
197 else if (xTy.isF80())
198 func = fir::runtime::getRuntimeFunc<ForcedBesselJnX0_10>(loc, builder);
199 else if (xTy.isF128())
200 func = fir::runtime::getRuntimeFunc<ForcedBesselJnX0_16>(loc, builder);
201 else
202 fir::emitFatalError(loc, "invalid type in BESSEL_JN");
203
204 auto fTy = func.getFunctionType();
205 auto sourceFile = fir::factory::locationToFilename(builder, loc);
206 auto sourceLine =
207 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
208 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox, n1,
209 n2, sourceFile, sourceLine);
210 builder.create<fir::CallOp>(loc, func, args);
211 }
212
213 /// Generate call to `BesselYn` intrinsic.
214 void fir::runtime::genBesselYn(fir::FirOpBuilder &builder, mlir::Location loc,
215 mlir::Value resultBox, mlir::Value n1,
216 mlir::Value n2, mlir::Value x, mlir::Value bn1,
217 mlir::Value bn1_1) {
218 mlir::func::FuncOp func;
219 auto xTy = x.getType();
220
221 if (xTy.isF16() || xTy.isBF16())
222 TODO(loc, "half-precision BESSEL_YN");
223 else if (xTy.isF32())
224 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselYn_4)>(loc, builder);
225 else if (xTy.isF64())
226 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselYn_8)>(loc, builder);
227 else if (xTy.isF80())
228 func = fir::runtime::getRuntimeFunc<ForcedBesselYn_10>(loc, builder);
229 else if (xTy.isF128())
230 func = fir::runtime::getRuntimeFunc<ForcedBesselYn_16>(loc, builder);
231 else
232 fir::emitFatalError(loc, "invalid type in BESSEL_YN");
233
234 auto fTy = func.getFunctionType();
235 auto sourceFile = fir::factory::locationToFilename(builder, loc);
236 auto sourceLine =
237 fir::factory::locationToLineNo(builder, loc, fTy.getInput(7));
238 auto args =
239 fir::runtime::createArguments(builder, loc, fTy, resultBox, n1, n2, x,
240 bn1, bn1_1, sourceFile, sourceLine);
241 builder.create<fir::CallOp>(loc, func, args);
242 }
243
244 /// Generate call to `BesselYn` intrinsic. This is used when `x == 0.0`.
245 void fir::runtime::genBesselYnX0(fir::FirOpBuilder &builder, mlir::Location loc,
246 mlir::Type xTy, mlir::Value resultBox,
247 mlir::Value n1, mlir::Value n2) {
248 mlir::func::FuncOp func;
249
250 if (xTy.isF16() || xTy.isBF16())
251 TODO(loc, "half-precision BESSEL_YN");
252 else if (xTy.isF32())
253 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselYnX0_4)>(loc, builder);
254 else if (xTy.isF64())
255 func = fir::runtime::getRuntimeFunc<mkRTKey(BesselYnX0_8)>(loc, builder);
256 else if (xTy.isF80())
257 func = fir::runtime::getRuntimeFunc<ForcedBesselYnX0_10>(loc, builder);
258 else if (xTy.isF128())
259 func = fir::runtime::getRuntimeFunc<ForcedBesselYnX0_16>(loc, builder);
260 else
261 fir::emitFatalError(loc, "invalid type in BESSEL_YN");
262
263 auto fTy = func.getFunctionType();
264 auto sourceFile = fir::factory::locationToFilename(builder, loc);
265 auto sourceLine =
266 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
267 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox, n1,
268 n2, sourceFile, sourceLine);
269 builder.create<fir::CallOp>(loc, func, args);
270 }
271
272 /// Generate call to Cshift intrinsic
273 void fir::runtime::genCshift(fir::FirOpBuilder &builder, mlir::Location loc,
274 mlir::Value resultBox, mlir::Value arrayBox,
275 mlir::Value shiftBox, mlir::Value dimBox) {
276 auto cshiftFunc = fir::runtime::getRuntimeFunc<mkRTKey(Cshift)>(loc, builder);
277 auto fTy = cshiftFunc.getFunctionType();
278 auto sourceFile = fir::factory::locationToFilename(builder, loc);
279 auto sourceLine =
280 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
281 auto args =
282 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
283 shiftBox, dimBox, sourceFile, sourceLine);
284 builder.create<fir::CallOp>(loc, cshiftFunc, args);
285 }
286
287 /// Generate call to the vector version of the Cshift intrinsic
288 void fir::runtime::genCshiftVector(fir::FirOpBuilder &builder,
289 mlir::Location loc, mlir::Value resultBox,
290 mlir::Value arrayBox, mlir::Value shiftBox) {
291 auto cshiftFunc =
292 fir::runtime::getRuntimeFunc<mkRTKey(CshiftVector)>(loc, builder);
293 auto fTy = cshiftFunc.getFunctionType();
294
295 auto sourceFile = fir::factory::locationToFilename(builder, loc);
296 auto sourceLine =
297 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
298 auto args = fir::runtime::createArguments(
299 builder, loc, fTy, resultBox, arrayBox, shiftBox, sourceFile, sourceLine);
300 builder.create<fir::CallOp>(loc, cshiftFunc, args);
301 }
302
303 /// Generate call to Eoshift intrinsic
304 void fir::runtime::genEoshift(fir::FirOpBuilder &builder, mlir::Location loc,
305 mlir::Value resultBox, mlir::Value arrayBox,
306 mlir::Value shiftBox, mlir::Value boundBox,
307 mlir::Value dimBox) {
308 auto eoshiftFunc =
309 fir::runtime::getRuntimeFunc<mkRTKey(Eoshift)>(loc, builder);
310 auto fTy = eoshiftFunc.getFunctionType();
311 auto sourceFile = fir::factory::locationToFilename(builder, loc);
312 auto sourceLine =
313 fir::factory::locationToLineNo(builder, loc, fTy.getInput(6));
314 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
315 arrayBox, shiftBox, boundBox,
316 dimBox, sourceFile, sourceLine);
317 builder.create<fir::CallOp>(loc, eoshiftFunc, args);
318 }
319
320 /// Generate call to the vector version of the Eoshift intrinsic
321 void fir::runtime::genEoshiftVector(fir::FirOpBuilder &builder,
322 mlir::Location loc, mlir::Value resultBox,
323 mlir::Value arrayBox, mlir::Value shiftBox,
324 mlir::Value boundBox) {
325 auto eoshiftFunc =
326 fir::runtime::getRuntimeFunc<mkRTKey(EoshiftVector)>(loc, builder);
327 auto fTy = eoshiftFunc.getFunctionType();
328
329 auto sourceFile = fir::factory::locationToFilename(builder, loc);
330 auto sourceLine =
331 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
332
333 auto args =
334 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
335 shiftBox, boundBox, sourceFile, sourceLine);
336 builder.create<fir::CallOp>(loc, eoshiftFunc, args);
337 }
338
339 /// Generate call to Matmul intrinsic runtime routine.
340 void fir::runtime::genMatmul(fir::FirOpBuilder &builder, mlir::Location loc,
341 mlir::Value resultBox, mlir::Value matrixABox,
342 mlir::Value matrixBBox) {
343 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Matmul)>(loc, builder);
344 auto fTy = func.getFunctionType();
345 auto sourceFile = fir::factory::locationToFilename(builder, loc);
346 auto sourceLine =
347 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
348 auto args =
349 fir::runtime::createArguments(builder, loc, fTy, resultBox, matrixABox,
350 matrixBBox, sourceFile, sourceLine);
351 builder.create<fir::CallOp>(loc, func, args);
352 }
353
354 /// Generate call to Pack intrinsic runtime routine.
355 void fir::runtime::genPack(fir::FirOpBuilder &builder, mlir::Location loc,
356 mlir::Value resultBox, mlir::Value arrayBox,
357 mlir::Value maskBox, mlir::Value vectorBox) {
358 auto packFunc = fir::runtime::getRuntimeFunc<mkRTKey(Pack)>(loc, builder);
359 auto fTy = packFunc.getFunctionType();
360 auto sourceFile = fir::factory::locationToFilename(builder, loc);
361 auto sourceLine =
362 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
363 auto args =
364 fir::runtime::createArguments(builder, loc, fTy, resultBox, arrayBox,
365 maskBox, vectorBox, sourceFile, sourceLine);
366 builder.create<fir::CallOp>(loc, packFunc, args);
367 }
368
369 /// Generate call to Reshape intrinsic runtime routine.
370 void fir::runtime::genReshape(fir::FirOpBuilder &builder, mlir::Location loc,
371 mlir::Value resultBox, mlir::Value sourceBox,
372 mlir::Value shapeBox, mlir::Value padBox,
373 mlir::Value orderBox) {
374 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Reshape)>(loc, builder);
375 auto fTy = func.getFunctionType();
376 auto sourceFile = fir::factory::locationToFilename(builder, loc);
377 auto sourceLine =
378 fir::factory::locationToLineNo(builder, loc, fTy.getInput(6));
379 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
380 sourceBox, shapeBox, padBox,
381 orderBox, sourceFile, sourceLine);
382 builder.create<fir::CallOp>(loc, func, args);
383 }
384
385 /// Generate call to Spread intrinsic runtime routine.
386 void fir::runtime::genSpread(fir::FirOpBuilder &builder, mlir::Location loc,
387 mlir::Value resultBox, mlir::Value sourceBox,
388 mlir::Value dim, mlir::Value ncopies) {
389 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Spread)>(loc, builder);
390 auto fTy = func.getFunctionType();
391 auto sourceFile = fir::factory::locationToFilename(builder, loc);
392 auto sourceLine =
393 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
394 auto args =
395 fir::runtime::createArguments(builder, loc, fTy, resultBox, sourceBox,
396 dim, ncopies, sourceFile, sourceLine);
397 builder.create<fir::CallOp>(loc, func, args);
398 }
399
400 /// Generate call to Transpose intrinsic runtime routine.
401 void fir::runtime::genTranspose(fir::FirOpBuilder &builder, mlir::Location loc,
402 mlir::Value resultBox, mlir::Value sourceBox) {
403 auto func = fir::runtime::getRuntimeFunc<mkRTKey(Transpose)>(loc, builder);
404 auto fTy = func.getFunctionType();
405 auto sourceFile = fir::factory::locationToFilename(builder, loc);
406 auto sourceLine =
407 fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
408 auto args = fir::runtime::createArguments(builder, loc, fTy, resultBox,
409 sourceBox, sourceFile, sourceLine);
410 builder.create<fir::CallOp>(loc, func, args);
411 }
412
413 /// Generate call to Unpack intrinsic runtime routine.
414 void fir::runtime::genUnpack(fir::FirOpBuilder &builder, mlir::Location loc,
415 mlir::Value resultBox, mlir::Value vectorBox,
416 mlir::Value maskBox, mlir::Value fieldBox) {
417 auto unpackFunc = fir::runtime::getRuntimeFunc<mkRTKey(Unpack)>(loc, builder);
418 auto fTy = unpackFunc.getFunctionType();
419 auto sourceFile = fir::factory::locationToFilename(builder, loc);
420 auto sourceLine =
421 fir::factory::locationToLineNo(builder, loc, fTy.getInput(5));
422 auto args =
423 fir::runtime::createArguments(builder, loc, fTy, resultBox, vectorBox,
424 maskBox, fieldBox, sourceFile, sourceLine);
425 builder.create<fir::CallOp>(loc, unpackFunc, args);
426 }