FLANG
IntrinsicCall.h
1//===-- Builder/IntrinsicCall.h -- lowering of intrinsics -------*- 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//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_LOWER_INTRINSICCALL_H
10#define FORTRAN_LOWER_INTRINSICCALL_H
11
12#include "flang/Lower/AbstractConverter.h"
13#include "flang/Optimizer/Builder/BoxValue.h"
14#include "flang/Optimizer/Builder/FIRBuilder.h"
15#include "flang/Optimizer/Builder/Runtime/Character.h"
16#include "flang/Optimizer/Builder/Runtime/Numeric.h"
18#include "flang/Runtime/entry-names.h"
19#include "flang/Runtime/iostat-consts.h"
20#include "mlir/Dialect/Complex/IR/Complex.h"
21#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
22#include "mlir/Dialect/Math/IR/Math.h"
23#include <optional>
24
25namespace fir {
26
27class StatementContext;
28struct IntrinsicHandlerEntry;
29
35std::pair<fir::ExtendedValue, bool>
36genIntrinsicCall(fir::FirOpBuilder &, mlir::Location, llvm::StringRef name,
37 std::optional<mlir::Type> resultType,
39 Fortran::lower::AbstractConverter *converter = nullptr);
40
44std::pair<fir::ExtendedValue, bool>
45genIntrinsicCall(fir::FirOpBuilder &, mlir::Location,
46 const IntrinsicHandlerEntry &,
47 std::optional<mlir::Type> resultType,
49 Fortran::lower::AbstractConverter *converter = nullptr);
50
52enum class Extremum { Min, Max };
53
54// There are different ways to deal with NaNs in MIN and MAX.
55// Known existing behaviors are listed below and can be selected for
56// f18 MIN/MAX implementation.
57enum class ExtremumBehavior {
58 // Note: the Signaling/quiet aspect of NaNs in the behaviors below are
59 // not described because there is no way to control/observe such aspect in
60 // MLIR/LLVM yet. The IEEE behaviors come with requirements regarding this
61 // aspect that are therefore currently not enforced. In the descriptions
62 // below, NaNs can be signaling or quite. Returned NaNs may be signaling
63 // if one of the input NaN was signaling but it cannot be guaranteed either.
64 // Existing compilers using an IEEE behavior (gfortran) also do not fulfill
65 // signaling/quiet requirements.
66 IeeeMinMaximumNumber,
67 // IEEE minimumNumber/maximumNumber behavior (754-2019, section 9.6):
68 // If one of the argument is and number and the other is NaN, return the
69 // number. If both arguements are NaN, return NaN.
70 // Compilers: gfortran.
71 IeeeMinMaximum,
72 // IEEE minimum/maximum behavior (754-2019, section 9.6):
73 // If one of the argument is NaN, return NaN.
74 MinMaxss,
75 // x86 minss/maxss behavior:
76 // If the second argument is a number and the other is NaN, return the number.
77 // In all other cases where at least one operand is NaN, return NaN.
78 // Compilers: xlf (only for MAX), ifort, pgfortran -nollvm, and nagfor.
79 PgfortranLlvm,
80 // "Opposite of" x86 minss/maxss behavior:
81 // If the first argument is a number and the other is NaN, return the
82 // number.
83 // In all other cases where at least one operand is NaN, return NaN.
84 // Compilers: xlf (only for MIN), and pgfortran (with llvm).
85 IeeeMinMaxNum
86 // IEEE minNum/maxNum behavior (754-2008, section 5.3.1):
87 // TODO: Not implemented.
88 // It is the only behavior where the signaling/quiet aspect of a NaN argument
89 // impacts if the result should be NaN or the argument that is a number.
90 // LLVM/MLIR do not provide ways to observe this aspect, so it is not
91 // possible to implement it without some target dependent runtime.
92};
93
98 Value,
101 Addr,
103 Box,
108};
109
112 LowerIntrinsicArgAs lowerAs;
114 // - Numerical: 0
115 // - Logical : false
116 // - Derived/character: not possible. Need custom intrinsic lowering.
117 // Addr:
118 // - nullptr
119 // Box:
120 // - absent box
121 // AsInquired:
122 // - no-op
124};
125
126constexpr auto asValue = fir::LowerIntrinsicArgAs::Value;
127constexpr auto asAddr = fir::LowerIntrinsicArgAs::Addr;
128constexpr auto asBox = fir::LowerIntrinsicArgAs::Box;
129constexpr auto asInquired = fir::LowerIntrinsicArgAs::Inquired;
130
134
135// TODO error handling -> return a code or directly emit messages ?
137
138 // Constructors.
139 explicit IntrinsicLibrary(
140 fir::FirOpBuilder &builder, mlir::Location loc,
141 Fortran::lower::AbstractConverter *converter = nullptr)
142 : builder{builder}, loc{loc}, converter{converter} {}
143 IntrinsicLibrary() = delete;
144 IntrinsicLibrary(const IntrinsicLibrary &) = delete;
145
149 std::pair<fir::ExtendedValue, bool>
150 genIntrinsicCall(llvm::StringRef name, std::optional<mlir::Type> resultType,
152
160 mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type,
162
163 using RuntimeCallGenerator = std::function<mlir::Value(
165 RuntimeCallGenerator
166 getRuntimeCallGenerator(llvm::StringRef name,
167 mlir::FunctionType soughtFuncType);
168
174 mlir::Value genAbs(mlir::Type, llvm::ArrayRef<mlir::Value>);
175 mlir::Value genAcosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
176 template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
177 mlir::Value, mlir::Value)>
178 fir::ExtendedValue genAdjustRtCall(mlir::Type,
180 mlir::Value genAimag(mlir::Type, llvm::ArrayRef<mlir::Value>);
181 mlir::Value genAint(mlir::Type, llvm::ArrayRef<mlir::Value>);
183 fir::ExtendedValue genAllocated(mlir::Type,
185 mlir::Value genAnint(mlir::Type, llvm::ArrayRef<mlir::Value>);
187 mlir::Value genAtanpi(mlir::Type, llvm::ArrayRef<mlir::Value>);
189 genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
190 mlir::Value genAsind(mlir::Type, llvm::ArrayRef<mlir::Value>);
191 fir::ExtendedValue genAssociated(mlir::Type,
193 mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);
194 fir::ExtendedValue genBesselJn(mlir::Type,
196 fir::ExtendedValue genBesselYn(mlir::Type,
198 template <mlir::arith::CmpIPredicate pred>
199 mlir::Value genBitwiseCompare(mlir::Type resultType,
201
202 mlir::Value genBtest(mlir::Type, llvm::ArrayRef<mlir::Value>);
203 mlir::Value genCeiling(mlir::Type, llvm::ArrayRef<mlir::Value>);
205 template <mlir::arith::CmpIPredicate pred>
206 fir::ExtendedValue genCharacterCompare(mlir::Type,
208 mlir::Value genCmplx(mlir::Type, llvm::ArrayRef<mlir::Value>);
209 mlir::Value genConjg(mlir::Type, llvm::ArrayRef<mlir::Value>);
211 void genCpuTime(llvm::ArrayRef<fir::ExtendedValue>);
218 mlir::Value genErfcScaled(mlir::Type resultType,
220 void genCFPointer(llvm::ArrayRef<fir::ExtendedValue>);
221 void genCFProcPointer(llvm::ArrayRef<fir::ExtendedValue>);
224 template <mlir::arith::CmpIPredicate pred>
225 fir::ExtendedValue genCPtrCompare(mlir::Type,
227 mlir::Value genCosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
228 void genDateAndTime(llvm::ArrayRef<fir::ExtendedValue>);
229 mlir::Value genDim(mlir::Type, llvm::ArrayRef<mlir::Value>);
230 fir::ExtendedValue genDotProduct(mlir::Type,
232 mlir::Value genDprod(mlir::Type, llvm::ArrayRef<mlir::Value>);
233 mlir::Value genDshiftl(mlir::Type, llvm::ArrayRef<mlir::Value>);
234 mlir::Value genDshiftr(mlir::Type, llvm::ArrayRef<mlir::Value>);
237 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
238 fir::ExtendedValue genEtime(std::optional<mlir::Type>,
239 mlir::ArrayRef<fir::ExtendedValue> args);
240 mlir::Value genExponent(mlir::Type, llvm::ArrayRef<mlir::Value>);
241 fir::ExtendedValue genExtendsTypeOf(mlir::Type,
243 template <Extremum, ExtremumBehavior>
244 mlir::Value genExtremum(mlir::Type, llvm::ArrayRef<mlir::Value>);
245 mlir::Value genFloor(mlir::Type, llvm::ArrayRef<mlir::Value>);
246 mlir::Value genFraction(mlir::Type resultType,
247 mlir::ArrayRef<mlir::Value> args);
248 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
249 fir::ExtendedValue genGetCwd(std::optional<mlir::Type> resultType,
251 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
252 mlir::Value genGetPID(mlir::Type resultType,
254 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
255 void genGetEnvironmentVariable(llvm::ArrayRef<fir::ExtendedValue>);
256 mlir::Value genGetGID(mlir::Type resultType,
258 mlir::Value genGetUID(mlir::Type resultType,
261 mlir::Value genIand(mlir::Type, llvm::ArrayRef<mlir::Value>);
263 mlir::Value genIbclr(mlir::Type, llvm::ArrayRef<mlir::Value>);
264 mlir::Value genIbits(mlir::Type, llvm::ArrayRef<mlir::Value>);
265 mlir::Value genIbset(mlir::Type, llvm::ArrayRef<mlir::Value>);
268 mlir::Value genIeeeClass(mlir::Type, llvm::ArrayRef<mlir::Value>);
269 mlir::Value genIeeeCopySign(mlir::Type, llvm::ArrayRef<mlir::Value>);
270 void genIeeeGetFlag(llvm::ArrayRef<fir::ExtendedValue>);
271 void genIeeeGetHaltingMode(llvm::ArrayRef<fir::ExtendedValue>);
272 template <bool isGet, bool isModes>
273 void genIeeeGetOrSetModesOrStatus(llvm::ArrayRef<fir::ExtendedValue>);
274 void genIeeeGetRoundingMode(llvm::ArrayRef<fir::ExtendedValue>);
275 void genIeeeGetUnderflowMode(llvm::ArrayRef<fir::ExtendedValue>);
276 mlir::Value genIeeeInt(mlir::Type, llvm::ArrayRef<mlir::Value>);
277 mlir::Value genIeeeIsFinite(mlir::Type, llvm::ArrayRef<mlir::Value>);
278 mlir::Value genIeeeIsNan(mlir::Type, llvm::ArrayRef<mlir::Value>);
279 mlir::Value genIeeeIsNegative(mlir::Type, llvm::ArrayRef<mlir::Value>);
280 mlir::Value genIeeeIsNormal(mlir::Type, llvm::ArrayRef<mlir::Value>);
281 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
282 template <bool isMax, bool isNum, bool isMag>
283 mlir::Value genIeeeMaxMin(mlir::Type, llvm::ArrayRef<mlir::Value>);
284 template <mlir::arith::CmpFPredicate pred>
285 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
287 mlir::Value genIeeeReal(mlir::Type, llvm::ArrayRef<mlir::Value>);
288 mlir::Value genIeeeRem(mlir::Type, llvm::ArrayRef<mlir::Value>);
289 mlir::Value genIeeeRint(mlir::Type, llvm::ArrayRef<mlir::Value>);
290 template <bool isFlag>
291 void genIeeeSetFlagOrHaltingMode(llvm::ArrayRef<fir::ExtendedValue>);
292 void genIeeeSetRoundingMode(llvm::ArrayRef<fir::ExtendedValue>);
293 void genIeeeSetUnderflowMode(llvm::ArrayRef<fir::ExtendedValue>);
294 template <mlir::arith::CmpFPredicate pred>
295 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
297 mlir::Value genIeeeSignbit(mlir::Type, llvm::ArrayRef<mlir::Value>);
298 fir::ExtendedValue genIeeeSupportFlag(mlir::Type,
300 fir::ExtendedValue genIeeeSupportHalting(mlir::Type,
302 mlir::Value genIeeeSupportRounding(mlir::Type, llvm::ArrayRef<mlir::Value>);
303 template <mlir::arith::CmpIPredicate pred>
304 mlir::Value genIeeeTypeCompare(mlir::Type, llvm::ArrayRef<mlir::Value>);
305 mlir::Value genIeeeUnordered(mlir::Type, llvm::ArrayRef<mlir::Value>);
306 mlir::Value genIeeeValue(mlir::Type, llvm::ArrayRef<mlir::Value>);
307 mlir::Value genIeor(mlir::Type, llvm::ArrayRef<mlir::Value>);
309 mlir::Value genIor(mlir::Type, llvm::ArrayRef<mlir::Value>);
311 fir::ExtendedValue genIsContiguous(mlir::Type,
313 template <Fortran::runtime::io::Iostat value>
314 mlir::Value genIsIostatValue(mlir::Type, llvm::ArrayRef<mlir::Value>);
315 mlir::Value genIsFPClass(mlir::Type, llvm::ArrayRef<mlir::Value>,
316 int fpclass);
317 mlir::Value genIshft(mlir::Type, llvm::ArrayRef<mlir::Value>);
318 mlir::Value genIshftc(mlir::Type, llvm::ArrayRef<mlir::Value>);
320 mlir::Value genLeadz(mlir::Type, llvm::ArrayRef<mlir::Value>);
324 mlir::Value genMalloc(mlir::Type, llvm::ArrayRef<mlir::Value>);
325 template <typename Shift>
326 mlir::Value genMask(mlir::Type, llvm::ArrayRef<mlir::Value>);
328 fir::ExtendedValue genMatmulTranspose(mlir::Type,
333 mlir::Value genMergeBits(mlir::Type, llvm::ArrayRef<mlir::Value>);
336 mlir::Value genMod(mlir::Type, llvm::ArrayRef<mlir::Value>);
337 mlir::Value genModulo(mlir::Type, llvm::ArrayRef<mlir::Value>);
338 void genMoveAlloc(llvm::ArrayRef<fir::ExtendedValue>);
340 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
341 template <NearestProc>
342 mlir::Value genNearest(mlir::Type, llvm::ArrayRef<mlir::Value>);
343 mlir::Value genNint(mlir::Type, llvm::ArrayRef<mlir::Value>);
345 mlir::Value genNot(mlir::Type, llvm::ArrayRef<mlir::Value>);
349 mlir::Value genPopcnt(mlir::Type, llvm::ArrayRef<mlir::Value>);
350 mlir::Value genPoppar(mlir::Type, llvm::ArrayRef<mlir::Value>);
353 void genRandomInit(llvm::ArrayRef<fir::ExtendedValue>);
354 void genRandomNumber(llvm::ArrayRef<fir::ExtendedValue>);
355 void genRandomSeed(llvm::ArrayRef<fir::ExtendedValue>);
357 fir::ExtendedValue genReduceDim(mlir::Type,
359 fir::ExtendedValue genRename(std::optional<mlir::Type>,
360 mlir::ArrayRef<fir::ExtendedValue>);
363 mlir::Value genRRSpacing(mlir::Type resultType,
365 fir::ExtendedValue genSameTypeAs(mlir::Type,
367 mlir::Value genScale(mlir::Type, llvm::ArrayRef<mlir::Value>);
369 fir::ExtendedValue genSecond(std::optional<mlir::Type>,
370 mlir::ArrayRef<fir::ExtendedValue>);
371 fir::ExtendedValue genSelectedCharKind(mlir::Type,
373 mlir::Value genSelectedIntKind(mlir::Type, llvm::ArrayRef<mlir::Value>);
374 mlir::Value genSelectedLogicalKind(mlir::Type, llvm::ArrayRef<mlir::Value>);
375 mlir::Value genSelectedRealKind(mlir::Type, llvm::ArrayRef<mlir::Value>);
376 mlir::Value genSetExponent(mlir::Type resultType,
378 fir::ExtendedValue genShape(mlir::Type resultType,
380 template <typename Shift>
381 mlir::Value genShift(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
382 mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
383 mlir::Value genSign(mlir::Type, llvm::ArrayRef<mlir::Value>);
384 mlir::Value genSind(mlir::Type, llvm::ArrayRef<mlir::Value>);
387 mlir::Value genSpacing(mlir::Type resultType,
390 fir::ExtendedValue genStorageSize(mlir::Type,
393 void genSignalSubroutine(llvm::ArrayRef<fir::ExtendedValue>);
395 void genSyncThreads(llvm::ArrayRef<fir::ExtendedValue>);
396 mlir::Value genSyncThreadsAnd(mlir::Type, llvm::ArrayRef<mlir::Value>);
397 mlir::Value genSyncThreadsCount(mlir::Type, llvm::ArrayRef<mlir::Value>);
398 mlir::Value genSyncThreadsOr(mlir::Type, llvm::ArrayRef<mlir::Value>);
399 fir::ExtendedValue genSystem(std::optional<mlir::Type>,
400 mlir::ArrayRef<fir::ExtendedValue> args);
401 void genSystemClock(llvm::ArrayRef<fir::ExtendedValue>);
402 mlir::Value genTand(mlir::Type, llvm::ArrayRef<mlir::Value>);
403 mlir::Value genTrailz(mlir::Type, llvm::ArrayRef<mlir::Value>);
404 fir::ExtendedValue genTransfer(mlir::Type,
406 fir::ExtendedValue genTranspose(mlir::Type,
408 void genThreadFence(llvm::ArrayRef<fir::ExtendedValue>);
409 void genThreadFenceBlock(llvm::ArrayRef<fir::ExtendedValue>);
410 void genThreadFenceSystem(llvm::ArrayRef<fir::ExtendedValue>);
415
419 mlir::Value genConversion(mlir::Type, llvm::ArrayRef<mlir::Value>);
420
426 template <typename FN, typename FD>
427 fir::ExtendedValue genExtremumloc(FN func, FD funcDim, llvm::StringRef errMsg,
428 mlir::Type,
430 template <typename FN, typename FD, typename FC>
432 fir::ExtendedValue genExtremumVal(FN func, FD funcDim, FC funcChar,
433 llvm::StringRef errMsg,
434 mlir::Type resultType,
437 template <typename FN, typename FD>
438 fir::ExtendedValue genReduction(FN func, FD funcDim, llvm::StringRef errMsg,
439 mlir::Type resultType,
441
444 void genRaiseExcept(int excepts, mlir::Value cond = {});
445
447 mlir::Value genQNan(mlir::Type resultType);
448
452 using ExtendedGenerator = decltype(&IntrinsicLibrary::genLenTrim);
453 using SubroutineGenerator = decltype(&IntrinsicLibrary::genDateAndTime);
455 using DualGenerator = decltype(&IntrinsicLibrary::genEtime);
456 using Generator = std::variant<ElementalGenerator, ExtendedGenerator,
457 SubroutineGenerator, DualGenerator>;
458
465 template <typename GeneratorType>
466 mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name,
467 mlir::Type resultType,
469 template <typename GeneratorType>
471 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
472 std::optional<mlir::Type> resultType,
474
475 template <typename GeneratorType>
476 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
477 mlir::FunctionType,
478 bool loadRefArguments = false);
479
481 template <typename GeneratorType>
483 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
484 llvm::ArrayRef<fir::ExtendedValue> args, bool outline);
485
487 mlir::Value invokeGenerator(ElementalGenerator generator,
488 mlir::Type resultType,
490 mlir::Value invokeGenerator(RuntimeCallGenerator generator,
491 mlir::Type resultType,
493 mlir::Value invokeGenerator(ExtendedGenerator generator,
494 mlir::Type resultType,
496 mlir::Value invokeGenerator(SubroutineGenerator generator,
498 mlir::Value invokeGenerator(DualGenerator generator,
500 mlir::Value invokeGenerator(DualGenerator generator, mlir::Type resultType,
502
505 mlir::SymbolRefAttr
506 getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name,
507 mlir::FunctionType signature);
508
511 mlir::Type resultType,
512 llvm::StringRef errMsg);
513
514 void setResultMustBeFreed() { resultMustBeFreed = true; }
515
516 fir::FirOpBuilder &builder;
517 mlir::Location loc;
518 bool resultMustBeFreed = false;
519 Fortran::lower::AbstractConverter *converter = nullptr;
520};
521
523 const char *name = nullptr;
525 bool handleDynamicOptional = false;
526};
527
532 constexpr bool hasDefaultRules() const { return args[0].name == nullptr; }
533};
534
538 const char *name;
539 IntrinsicLibrary::Generator generator;
540 // The following may be omitted in the table below.
541 fir::IntrinsicArgumentLoweringRules argLoweringRules = {};
542 bool isElemental = true;
545 bool outline = false;
546};
547
549 // llvm::StringRef comparison operator are not constexpr, so use string_view.
550 using Key = std::string_view;
551 // Needed for implicit compare with keys.
552 constexpr operator Key() const { return key; }
553 Key key; // intrinsic name
554
555 // Name of a runtime function that implements the operation.
556 llvm::StringRef symbol;
557 fir::runtime::FuncTypeBuilderFunc typeGenerator;
558};
559
561 // Callback type for generating lowering for a math operation.
562 using MathGeneratorTy = mlir::Value (*)(fir::FirOpBuilder &, mlir::Location,
563 const MathOperation &,
564 mlir::FunctionType,
566
567 // Overrides fir::runtime::FuncTypeBuilderFunc to add FirOpBuilder argument.
568 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
570
571 // llvm::StringRef comparison operator are not constexpr, so use string_view.
572 using Key = std::string_view;
573 // Needed for implicit compare with keys.
574 constexpr operator Key() const { return key; }
575 // Intrinsic name.
576 Key key;
577
578 // Name of a runtime function that implements the operation.
579 llvm::StringRef runtimeFunc;
580 FuncTypeBuilderFunc typeGenerator;
581
582 // A callback to generate FIR for the intrinsic defined by 'key'.
583 // A callback may generate either dedicated MLIR operation(s) or
584 // a function call to a runtime function with name defined by
585 // 'runtimeFunc'.
586 MathGeneratorTy funcGenerator;
587};
588
589// Enum of most supported intrinsic argument or return types.
590enum class ParamTypeId {
591 Void,
592 Address, // pointer (to an [array of] Integers of some kind)
593 Integer,
594 Real,
595 Complex,
596 IntegerVector,
597 UnsignedVector,
598 RealVector,
599};
600
601// Helper function to get length of a 16-byte vector of element type eleTy.
602static int getVecLen(mlir::Type eleTy) {
603 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
604 mlir::isa<mlir::FloatType>(eleTy)) &&
605 "unsupported vector element type");
606 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
607}
608
609template <ParamTypeId t, int k>
610struct ParamType {
611 // Supported kinds can be checked with static asserts at compile time.
612 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
613 k == 8,
614 "Unsupported integer kind");
615 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
616 k == 16,
617 "Unsupported real kind");
618 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
619 k == 8 || k == 10 || k == 16,
620 "Unsupported complex kind");
621
622 static const ParamTypeId ty = t;
623 static const int kind = k;
624};
625
626// Namespace encapsulating type definitions for parameter types.
627namespace Ty {
629template <int k>
631template <int k>
633template <int k>
635template <int k>
637template <int k>
638using IntegerVector = ParamType<ParamTypeId::IntegerVector, k>;
639template <int k>
640using UnsignedVector = ParamType<ParamTypeId::UnsignedVector, k>;
641template <int k>
643} // namespace Ty
644
645// Helper function that generates most types that are supported for intrinsic
646// arguments and return type. Used by `genFuncType` to generate function
647// types for most of the intrinsics.
648static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
649 fir::FirOpBuilder &builder,
650 ParamTypeId typeId, int kind) {
651 mlir::Type r;
652 unsigned bits{0};
653 switch (typeId) {
654 case ParamTypeId::Void:
655 llvm::report_fatal_error("can not get type of void");
656 break;
657 case ParamTypeId::Address:
658 bits = builder.getKindMap().getIntegerBitsize(kind);
659 assert(bits != 0 && "failed to convert address kind to integer bitsize");
660 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
661 break;
662 case ParamTypeId::Integer:
663 case ParamTypeId::IntegerVector:
664 bits = builder.getKindMap().getIntegerBitsize(kind);
665 assert(bits != 0 && "failed to convert kind to integer bitsize");
666 r = mlir::IntegerType::get(context, bits);
667 break;
668 case ParamTypeId::UnsignedVector:
669 bits = builder.getKindMap().getIntegerBitsize(kind);
670 assert(bits != 0 && "failed to convert kind to unsigned bitsize");
671 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
672 break;
673 case ParamTypeId::Real:
674 case ParamTypeId::RealVector:
675 r = builder.getRealType(kind);
676 break;
677 case ParamTypeId::Complex:
678 r = mlir::ComplexType::get(builder.getRealType(kind));
679 break;
680 }
681
682 switch (typeId) {
683 case ParamTypeId::Void:
684 case ParamTypeId::Address:
685 case ParamTypeId::Integer:
686 case ParamTypeId::Real:
687 case ParamTypeId::Complex:
688 break;
689 case ParamTypeId::IntegerVector:
690 case ParamTypeId::UnsignedVector:
691 case ParamTypeId::RealVector:
692 // convert to vector type
693 r = fir::VectorType::get(getVecLen(r), r);
694 }
695 return r;
696}
697
698// Generic function type generator that supports most of the function types
699// used by intrinsics.
700template <typename TyR, typename... ArgTys>
701static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
702 fir::FirOpBuilder &builder) {
703 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
704 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
706
707 for (size_t i = 0; i < argTys.size(); ++i) {
708 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
709 }
710
711 if (TyR::ty == ParamTypeId::Void)
712 return mlir::FunctionType::get(context, argTypes, std::nullopt);
713
714 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
715 return mlir::FunctionType::get(context, argTypes, {resType});
716}
717
720 using RuntimeGeneratorRange =
721 std::pair<const MathOperation *, const MathOperation *>;
722 IntrinsicHandlerEntry(const IntrinsicHandler *handler) : entry{handler} {
723 assert(handler && "handler must not be nullptr");
724 };
725 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
726 const IntrinsicArgumentLoweringRules *getArgumentLoweringRules() const;
727 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
728};
729
730//===----------------------------------------------------------------------===//
731// Helper functions for argument handling.
732//===----------------------------------------------------------------------===//
733static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
734 mlir::Type eleTy) {
735 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
736 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
737 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
738 return newEleTy;
739 }
740 return eleTy;
741}
742
744getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
746 for (auto arg : args)
747 baseVec.push_back(getBase(arg));
748 return baseVec;
749}
750
752getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
754 for (auto arg : args)
755 typeVec.push_back(arg.getType());
756 return typeVec;
757}
758
759mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
760 const MathOperation &mathOp,
761 mlir::FunctionType libFuncType,
763
764template <typename T>
765mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
766 const MathOperation &mathOp,
767 mlir::FunctionType mathLibFuncType,
769
770template <typename T>
771mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
772 const MathOperation &mathOp,
773 mlir::FunctionType mathLibFuncType,
775
776mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
777 mlir::Location loc,
778 const MathOperation &mathOp,
779 mlir::FunctionType libFuncType,
781
784std::optional<IntrinsicHandlerEntry>
785lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName,
786 std::optional<mlir::Type> resultType);
787
789void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name);
790
793const IntrinsicArgumentLoweringRules *
794getIntrinsicArgumentLowering(llvm::StringRef intrinsicName);
795
798ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &,
799 unsigned position);
800
803
805// implementation) of an unrestricted intrinsic (defined by its signature
806// and generic name)
807mlir::SymbolRefAttr
809 llvm::StringRef name,
810 mlir::FunctionType signature);
811
812//===----------------------------------------------------------------------===//
813// Direct access to intrinsics that may be used by lowering outside
814// of intrinsic call lowering.
815//===----------------------------------------------------------------------===//
816
819mlir::Value genMax(fir::FirOpBuilder &, mlir::Location,
821
823mlir::Value genMin(fir::FirOpBuilder &, mlir::Location,
825
828mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
829 mlir::Type resultType, mlir::Value x, mlir::Value y);
830
833mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
834 mlir::Value x, mlir::Value y);
835
836} // namespace fir
837
838#endif // FORTRAN_LOWER_INTRINSICCALL_H
Definition: AbstractConverter.h:82
Definition: BoxValue.h:478
Definition: FIRBuilder.h:55
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition: FIRBuilder.h:116
mlir::Type getRealType(int kind)
Get the mlir float type that implements Fortran REAL(kind).
Definition: FIRBuilder.cpp:105
Bitsize getIntegerBitsize(KindTy kind) const
Get the size in bits of !fir.int<kind>
Definition: KindMapping.cpp:282
Definition: BoxValue.h:360
Definition: FIRType.h:77
Definition: OpenACC.h:20
Definition: AbstractConverter.h:31
std::optional< IntrinsicHandlerEntry > lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName, std::optional< mlir::Type > resultType)
Definition: IntrinsicCall.cpp:1718
Extremum
Enums used to templatize and share lowering of MIN and MAX.
Definition: IntrinsicCall.h:52
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition: IntrinsicCall.cpp:8095
mlir::Value getBase(const ExtendedValue &exv)
Definition: BoxValue.cpp:21
mlir::Value genMin(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Generate minimum. Same constraints as genMax.
Definition: IntrinsicCall.cpp:8149
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8162
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8157
fir::ExtendedValue getAbsentIntrinsicArgument()
Return place-holder for absent intrinsic arguments.
Definition: IntrinsicCall.cpp:74
void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name)
Generate a TODO error message for an as yet unimplemented intrinsic.
Definition: IntrinsicCall.cpp:1740
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition: IntrinsicCall.cpp:8120
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(fir::FirOpBuilder &, mlir::Location, llvm::StringRef name, mlir::FunctionType signature)
Get SymbolRefAttr of runtime (or wrapper function containing inlined.
Definition: IntrinsicCall.cpp:8175
LowerIntrinsicArgAs
Definition: IntrinsicCall.h:96
@ Box
Lower argument to a box.
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(fir::FirOpBuilder &, mlir::Location, llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > args, Fortran::lower::AbstractConverter *converter=nullptr)
Definition: IntrinsicCall.cpp:8133
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:8141
Define how a given intrinsic argument must be lowered.
Definition: IntrinsicCall.h:111
bool handleDynamicOptional
Value:
Definition: IntrinsicCall.h:123
This is shared by intrinsics and intrinsic module procedures.
Definition: IntrinsicCall.h:529
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition: IntrinsicCall.h:531
Definition: IntrinsicCall.h:522
Entry into the tables describing how an intrinsic must be lowered.
Definition: IntrinsicCall.h:719
Definition: IntrinsicCall.h:537
bool outline
Definition: IntrinsicCall.h:545
Definition: IntrinsicCall.h:136
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition: IntrinsicCall.cpp:2246
fir::ExtendedValue genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< fir::ExtendedValue > args, bool outline)
Generate calls to ElementalGenerator, handling the elemental aspects.
Definition: IntrinsicCall.cpp:1752
fir::ExtendedValue genReduction(FN func, FD funcDim, llvm::StringRef errMsg, mlir::Type resultType, llvm::ArrayRef< fir::ExtendedValue > args)
Process calls to Product, Sum, IAll, IAny, IParity intrinsic functions.
Definition: IntrinsicCall.cpp:3878
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition: IntrinsicCall.cpp:1936
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition: IntrinsicCall.h:455
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:2145
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2317
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition: IntrinsicCall.cpp:4091
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2325
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2339
fir::ExtendedValue genExtremumVal(FN func, FD funcDim, FC funcChar, llvm::StringRef errMsg, mlir::Type resultType, llvm::ArrayRef< fir::ExtendedValue > args)
Helper for MinVal/MaxVal.
Definition: IntrinsicCall.cpp:8004
fir::ExtendedValue readAndAddCleanUp(fir::MutableBoxValue resultMutableBox, mlir::Type resultType, llvm::StringRef errMsg)
Helper function for generating code clean-up for result descriptors.
Definition: IntrinsicCall.cpp:2280
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition: IntrinsicCall.h:451
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition: IntrinsicCall.cpp:2937
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition: IntrinsicCall.cpp:2930
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition: IntrinsicCall.cpp:4098
mlir::Value invokeGenerator(ElementalGenerator generator, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Helper to invoke code generator for the intrinsics given arguments.
Definition: IntrinsicCall.cpp:1951
fir::ExtendedValue genExtremumloc(FN func, FD funcDim, llvm::StringRef errMsg, mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
Process calls to Minloc, Maxloc intrinsic functions.
Definition: IntrinsicCall.cpp:7923
Definition: IntrinsicCall.h:560
Definition: IntrinsicCall.h:610
Definition: IntrinsicCall.h:548