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>);
188 mlir::Value genAtomicAdd(mlir::Type, llvm::ArrayRef<mlir::Value>);
190 genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
191 mlir::Value genAsind(mlir::Type, llvm::ArrayRef<mlir::Value>);
192 fir::ExtendedValue genAssociated(mlir::Type,
194 mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);
195 fir::ExtendedValue genBesselJn(mlir::Type,
197 fir::ExtendedValue genBesselYn(mlir::Type,
199 template <mlir::arith::CmpIPredicate pred>
200 mlir::Value genBitwiseCompare(mlir::Type resultType,
202
203 mlir::Value genBtest(mlir::Type, llvm::ArrayRef<mlir::Value>);
204 mlir::Value genCeiling(mlir::Type, llvm::ArrayRef<mlir::Value>);
206 template <mlir::arith::CmpIPredicate pred>
207 fir::ExtendedValue genCharacterCompare(mlir::Type,
209 mlir::Value genCmplx(mlir::Type, llvm::ArrayRef<mlir::Value>);
210 mlir::Value genConjg(mlir::Type, llvm::ArrayRef<mlir::Value>);
212 void genCpuTime(llvm::ArrayRef<fir::ExtendedValue>);
219 mlir::Value genErfcScaled(mlir::Type resultType,
221 void genCFPointer(llvm::ArrayRef<fir::ExtendedValue>);
222 void genCFProcPointer(llvm::ArrayRef<fir::ExtendedValue>);
225 template <mlir::arith::CmpIPredicate pred>
226 fir::ExtendedValue genCPtrCompare(mlir::Type,
228 mlir::Value genCosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
229 void genDateAndTime(llvm::ArrayRef<fir::ExtendedValue>);
230 mlir::Value genDim(mlir::Type, llvm::ArrayRef<mlir::Value>);
231 fir::ExtendedValue genDotProduct(mlir::Type,
233 mlir::Value genDprod(mlir::Type, llvm::ArrayRef<mlir::Value>);
234 mlir::Value genDshiftl(mlir::Type, llvm::ArrayRef<mlir::Value>);
235 mlir::Value genDshiftr(mlir::Type, llvm::ArrayRef<mlir::Value>);
238 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
239 fir::ExtendedValue genEtime(std::optional<mlir::Type>,
240 mlir::ArrayRef<fir::ExtendedValue> args);
241 mlir::Value genExponent(mlir::Type, llvm::ArrayRef<mlir::Value>);
242 fir::ExtendedValue genExtendsTypeOf(mlir::Type,
244 template <Extremum, ExtremumBehavior>
245 mlir::Value genExtremum(mlir::Type, llvm::ArrayRef<mlir::Value>);
246 mlir::Value genFloor(mlir::Type, llvm::ArrayRef<mlir::Value>);
247 mlir::Value genFraction(mlir::Type resultType,
248 mlir::ArrayRef<mlir::Value> args);
249 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
250 fir::ExtendedValue genGetCwd(std::optional<mlir::Type> resultType,
252 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
253 mlir::Value genGetPID(mlir::Type resultType,
255 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
256 void genGetEnvironmentVariable(llvm::ArrayRef<fir::ExtendedValue>);
257 mlir::Value genGetGID(mlir::Type resultType,
259 mlir::Value genGetUID(mlir::Type resultType,
262 mlir::Value genIand(mlir::Type, llvm::ArrayRef<mlir::Value>);
264 mlir::Value genIbclr(mlir::Type, llvm::ArrayRef<mlir::Value>);
265 mlir::Value genIbits(mlir::Type, llvm::ArrayRef<mlir::Value>);
266 mlir::Value genIbset(mlir::Type, llvm::ArrayRef<mlir::Value>);
269 mlir::Value genIeeeClass(mlir::Type, llvm::ArrayRef<mlir::Value>);
270 mlir::Value genIeeeCopySign(mlir::Type, llvm::ArrayRef<mlir::Value>);
271 void genIeeeGetFlag(llvm::ArrayRef<fir::ExtendedValue>);
272 void genIeeeGetHaltingMode(llvm::ArrayRef<fir::ExtendedValue>);
273 template <bool isGet, bool isModes>
274 void genIeeeGetOrSetModesOrStatus(llvm::ArrayRef<fir::ExtendedValue>);
275 void genIeeeGetRoundingMode(llvm::ArrayRef<fir::ExtendedValue>);
276 void genIeeeGetUnderflowMode(llvm::ArrayRef<fir::ExtendedValue>);
277 mlir::Value genIeeeInt(mlir::Type, llvm::ArrayRef<mlir::Value>);
278 mlir::Value genIeeeIsFinite(mlir::Type, llvm::ArrayRef<mlir::Value>);
279 mlir::Value genIeeeIsNan(mlir::Type, llvm::ArrayRef<mlir::Value>);
280 mlir::Value genIeeeIsNegative(mlir::Type, llvm::ArrayRef<mlir::Value>);
281 mlir::Value genIeeeIsNormal(mlir::Type, llvm::ArrayRef<mlir::Value>);
282 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
283 template <bool isMax, bool isNum, bool isMag>
284 mlir::Value genIeeeMaxMin(mlir::Type, llvm::ArrayRef<mlir::Value>);
285 template <mlir::arith::CmpFPredicate pred>
286 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
288 mlir::Value genIeeeReal(mlir::Type, llvm::ArrayRef<mlir::Value>);
289 mlir::Value genIeeeRem(mlir::Type, llvm::ArrayRef<mlir::Value>);
290 mlir::Value genIeeeRint(mlir::Type, llvm::ArrayRef<mlir::Value>);
291 template <bool isFlag>
292 void genIeeeSetFlagOrHaltingMode(llvm::ArrayRef<fir::ExtendedValue>);
293 void genIeeeSetRoundingMode(llvm::ArrayRef<fir::ExtendedValue>);
294 void genIeeeSetUnderflowMode(llvm::ArrayRef<fir::ExtendedValue>);
295 template <mlir::arith::CmpFPredicate pred>
296 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
298 mlir::Value genIeeeSignbit(mlir::Type, llvm::ArrayRef<mlir::Value>);
299 fir::ExtendedValue genIeeeSupportFlag(mlir::Type,
301 fir::ExtendedValue genIeeeSupportHalting(mlir::Type,
303 mlir::Value genIeeeSupportRounding(mlir::Type, llvm::ArrayRef<mlir::Value>);
304 template <mlir::arith::CmpIPredicate pred>
305 mlir::Value genIeeeTypeCompare(mlir::Type, llvm::ArrayRef<mlir::Value>);
306 mlir::Value genIeeeUnordered(mlir::Type, llvm::ArrayRef<mlir::Value>);
307 mlir::Value genIeeeValue(mlir::Type, llvm::ArrayRef<mlir::Value>);
308 mlir::Value genIeor(mlir::Type, llvm::ArrayRef<mlir::Value>);
310 mlir::Value genIor(mlir::Type, llvm::ArrayRef<mlir::Value>);
312 fir::ExtendedValue genIsContiguous(mlir::Type,
314 template <Fortran::runtime::io::Iostat value>
315 mlir::Value genIsIostatValue(mlir::Type, llvm::ArrayRef<mlir::Value>);
316 mlir::Value genIsFPClass(mlir::Type, llvm::ArrayRef<mlir::Value>,
317 int fpclass);
318 mlir::Value genIshft(mlir::Type, llvm::ArrayRef<mlir::Value>);
319 mlir::Value genIshftc(mlir::Type, llvm::ArrayRef<mlir::Value>);
321 mlir::Value genLeadz(mlir::Type, llvm::ArrayRef<mlir::Value>);
325 mlir::Value genMalloc(mlir::Type, llvm::ArrayRef<mlir::Value>);
326 template <typename Shift>
327 mlir::Value genMask(mlir::Type, llvm::ArrayRef<mlir::Value>);
329 fir::ExtendedValue genMatmulTranspose(mlir::Type,
334 mlir::Value genMergeBits(mlir::Type, llvm::ArrayRef<mlir::Value>);
337 mlir::Value genMod(mlir::Type, llvm::ArrayRef<mlir::Value>);
338 mlir::Value genModulo(mlir::Type, llvm::ArrayRef<mlir::Value>);
339 void genMoveAlloc(llvm::ArrayRef<fir::ExtendedValue>);
341 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
342 template <NearestProc>
343 mlir::Value genNearest(mlir::Type, llvm::ArrayRef<mlir::Value>);
344 mlir::Value genNint(mlir::Type, llvm::ArrayRef<mlir::Value>);
346 mlir::Value genNot(mlir::Type, llvm::ArrayRef<mlir::Value>);
350 mlir::Value genPopcnt(mlir::Type, llvm::ArrayRef<mlir::Value>);
351 mlir::Value genPoppar(mlir::Type, llvm::ArrayRef<mlir::Value>);
354 void genRandomInit(llvm::ArrayRef<fir::ExtendedValue>);
355 void genRandomNumber(llvm::ArrayRef<fir::ExtendedValue>);
356 void genRandomSeed(llvm::ArrayRef<fir::ExtendedValue>);
358 fir::ExtendedValue genReduceDim(mlir::Type,
360 fir::ExtendedValue genRename(std::optional<mlir::Type>,
361 mlir::ArrayRef<fir::ExtendedValue>);
364 mlir::Value genRRSpacing(mlir::Type resultType,
366 fir::ExtendedValue genSameTypeAs(mlir::Type,
368 mlir::Value genScale(mlir::Type, llvm::ArrayRef<mlir::Value>);
370 fir::ExtendedValue genSecond(std::optional<mlir::Type>,
371 mlir::ArrayRef<fir::ExtendedValue>);
372 fir::ExtendedValue genSelectedCharKind(mlir::Type,
374 mlir::Value genSelectedIntKind(mlir::Type, llvm::ArrayRef<mlir::Value>);
375 mlir::Value genSelectedLogicalKind(mlir::Type, llvm::ArrayRef<mlir::Value>);
376 mlir::Value genSelectedRealKind(mlir::Type, llvm::ArrayRef<mlir::Value>);
377 mlir::Value genSetExponent(mlir::Type resultType,
379 fir::ExtendedValue genShape(mlir::Type resultType,
381 template <typename Shift>
382 mlir::Value genShift(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
383 mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
384 mlir::Value genSign(mlir::Type, llvm::ArrayRef<mlir::Value>);
385 mlir::Value genSind(mlir::Type, llvm::ArrayRef<mlir::Value>);
388 mlir::Value genSpacing(mlir::Type resultType,
391 fir::ExtendedValue genStorageSize(mlir::Type,
394 void genSignalSubroutine(llvm::ArrayRef<fir::ExtendedValue>);
396 void genSyncThreads(llvm::ArrayRef<fir::ExtendedValue>);
397 mlir::Value genSyncThreadsAnd(mlir::Type, llvm::ArrayRef<mlir::Value>);
398 mlir::Value genSyncThreadsCount(mlir::Type, llvm::ArrayRef<mlir::Value>);
399 mlir::Value genSyncThreadsOr(mlir::Type, llvm::ArrayRef<mlir::Value>);
400 fir::ExtendedValue genSystem(std::optional<mlir::Type>,
401 mlir::ArrayRef<fir::ExtendedValue> args);
402 void genSystemClock(llvm::ArrayRef<fir::ExtendedValue>);
403 mlir::Value genTand(mlir::Type, llvm::ArrayRef<mlir::Value>);
404 mlir::Value genTrailz(mlir::Type, llvm::ArrayRef<mlir::Value>);
405 fir::ExtendedValue genTransfer(mlir::Type,
407 fir::ExtendedValue genTranspose(mlir::Type,
409 void genThreadFence(llvm::ArrayRef<fir::ExtendedValue>);
410 void genThreadFenceBlock(llvm::ArrayRef<fir::ExtendedValue>);
411 void genThreadFenceSystem(llvm::ArrayRef<fir::ExtendedValue>);
416
420 mlir::Value genConversion(mlir::Type, llvm::ArrayRef<mlir::Value>);
421
427 template <typename FN, typename FD>
428 fir::ExtendedValue genExtremumloc(FN func, FD funcDim, llvm::StringRef errMsg,
429 mlir::Type,
431 template <typename FN, typename FD, typename FC>
433 fir::ExtendedValue genExtremumVal(FN func, FD funcDim, FC funcChar,
434 llvm::StringRef errMsg,
435 mlir::Type resultType,
438 template <typename FN, typename FD>
439 fir::ExtendedValue genReduction(FN func, FD funcDim, llvm::StringRef errMsg,
440 mlir::Type resultType,
442
445 void genRaiseExcept(int excepts, mlir::Value cond = {});
446
448 mlir::Value genQNan(mlir::Type resultType);
449
453 using ExtendedGenerator = decltype(&IntrinsicLibrary::genLenTrim);
454 using SubroutineGenerator = decltype(&IntrinsicLibrary::genDateAndTime);
456 using DualGenerator = decltype(&IntrinsicLibrary::genEtime);
457 using Generator = std::variant<ElementalGenerator, ExtendedGenerator,
458 SubroutineGenerator, DualGenerator>;
459
466 template <typename GeneratorType>
467 mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name,
468 mlir::Type resultType,
470 template <typename GeneratorType>
472 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
473 std::optional<mlir::Type> resultType,
475
476 template <typename GeneratorType>
477 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
478 mlir::FunctionType,
479 bool loadRefArguments = false);
480
482 template <typename GeneratorType>
484 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
485 llvm::ArrayRef<fir::ExtendedValue> args, bool outline);
486
488 mlir::Value invokeGenerator(ElementalGenerator generator,
489 mlir::Type resultType,
491 mlir::Value invokeGenerator(RuntimeCallGenerator generator,
492 mlir::Type resultType,
494 mlir::Value invokeGenerator(ExtendedGenerator generator,
495 mlir::Type resultType,
497 mlir::Value invokeGenerator(SubroutineGenerator generator,
499 mlir::Value invokeGenerator(DualGenerator generator,
501 mlir::Value invokeGenerator(DualGenerator generator, mlir::Type resultType,
503
506 mlir::SymbolRefAttr
507 getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name,
508 mlir::FunctionType signature);
509
512 mlir::Type resultType,
513 llvm::StringRef errMsg);
514
515 void setResultMustBeFreed() { resultMustBeFreed = true; }
516
517 fir::FirOpBuilder &builder;
518 mlir::Location loc;
519 bool resultMustBeFreed = false;
520 Fortran::lower::AbstractConverter *converter = nullptr;
521};
522
524 const char *name = nullptr;
526 bool handleDynamicOptional = false;
527};
528
533 constexpr bool hasDefaultRules() const { return args[0].name == nullptr; }
534};
535
539 const char *name;
540 IntrinsicLibrary::Generator generator;
541 // The following may be omitted in the table below.
542 fir::IntrinsicArgumentLoweringRules argLoweringRules = {};
543 bool isElemental = true;
546 bool outline = false;
547};
548
550 // llvm::StringRef comparison operator are not constexpr, so use string_view.
551 using Key = std::string_view;
552 // Needed for implicit compare with keys.
553 constexpr operator Key() const { return key; }
554 Key key; // intrinsic name
555
556 // Name of a runtime function that implements the operation.
557 llvm::StringRef symbol;
558 fir::runtime::FuncTypeBuilderFunc typeGenerator;
559};
560
562 // Callback type for generating lowering for a math operation.
563 using MathGeneratorTy = mlir::Value (*)(fir::FirOpBuilder &, mlir::Location,
564 const MathOperation &,
565 mlir::FunctionType,
567
568 // Overrides fir::runtime::FuncTypeBuilderFunc to add FirOpBuilder argument.
569 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
571
572 // llvm::StringRef comparison operator are not constexpr, so use string_view.
573 using Key = std::string_view;
574 // Needed for implicit compare with keys.
575 constexpr operator Key() const { return key; }
576 // Intrinsic name.
577 Key key;
578
579 // Name of a runtime function that implements the operation.
580 llvm::StringRef runtimeFunc;
581 FuncTypeBuilderFunc typeGenerator;
582
583 // A callback to generate FIR for the intrinsic defined by 'key'.
584 // A callback may generate either dedicated MLIR operation(s) or
585 // a function call to a runtime function with name defined by
586 // 'runtimeFunc'.
587 MathGeneratorTy funcGenerator;
588};
589
590// Enum of most supported intrinsic argument or return types.
591enum class ParamTypeId {
592 Void,
593 Address, // pointer (to an [array of] Integers of some kind)
594 Integer,
595 Real,
596 Complex,
597 IntegerVector,
598 UnsignedVector,
599 RealVector,
600};
601
602// Helper function to get length of a 16-byte vector of element type eleTy.
603static int getVecLen(mlir::Type eleTy) {
604 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
605 mlir::isa<mlir::FloatType>(eleTy)) &&
606 "unsupported vector element type");
607 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
608}
609
610template <ParamTypeId t, int k>
611struct ParamType {
612 // Supported kinds can be checked with static asserts at compile time.
613 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
614 k == 8,
615 "Unsupported integer kind");
616 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
617 k == 16,
618 "Unsupported real kind");
619 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
620 k == 8 || k == 10 || k == 16,
621 "Unsupported complex kind");
622
623 static const ParamTypeId ty = t;
624 static const int kind = k;
625};
626
627// Namespace encapsulating type definitions for parameter types.
628namespace Ty {
630template <int k>
632template <int k>
634template <int k>
636template <int k>
638template <int k>
639using IntegerVector = ParamType<ParamTypeId::IntegerVector, k>;
640template <int k>
641using UnsignedVector = ParamType<ParamTypeId::UnsignedVector, k>;
642template <int k>
644} // namespace Ty
645
646// Helper function that generates most types that are supported for intrinsic
647// arguments and return type. Used by `genFuncType` to generate function
648// types for most of the intrinsics.
649static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
650 fir::FirOpBuilder &builder,
651 ParamTypeId typeId, int kind) {
652 mlir::Type r;
653 unsigned bits{0};
654 switch (typeId) {
655 case ParamTypeId::Void:
656 llvm::report_fatal_error("can not get type of void");
657 break;
658 case ParamTypeId::Address:
659 bits = builder.getKindMap().getIntegerBitsize(kind);
660 assert(bits != 0 && "failed to convert address kind to integer bitsize");
661 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
662 break;
663 case ParamTypeId::Integer:
664 case ParamTypeId::IntegerVector:
665 bits = builder.getKindMap().getIntegerBitsize(kind);
666 assert(bits != 0 && "failed to convert kind to integer bitsize");
667 r = mlir::IntegerType::get(context, bits);
668 break;
669 case ParamTypeId::UnsignedVector:
670 bits = builder.getKindMap().getIntegerBitsize(kind);
671 assert(bits != 0 && "failed to convert kind to unsigned bitsize");
672 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
673 break;
674 case ParamTypeId::Real:
675 case ParamTypeId::RealVector:
676 r = builder.getRealType(kind);
677 break;
678 case ParamTypeId::Complex:
679 r = mlir::ComplexType::get(builder.getRealType(kind));
680 break;
681 }
682
683 switch (typeId) {
684 case ParamTypeId::Void:
685 case ParamTypeId::Address:
686 case ParamTypeId::Integer:
687 case ParamTypeId::Real:
688 case ParamTypeId::Complex:
689 break;
690 case ParamTypeId::IntegerVector:
691 case ParamTypeId::UnsignedVector:
692 case ParamTypeId::RealVector:
693 // convert to vector type
694 r = fir::VectorType::get(getVecLen(r), r);
695 }
696 return r;
697}
698
699// Generic function type generator that supports most of the function types
700// used by intrinsics.
701template <typename TyR, typename... ArgTys>
702static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
703 fir::FirOpBuilder &builder) {
704 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
705 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
707
708 for (size_t i = 0; i < argTys.size(); ++i) {
709 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
710 }
711
712 if (TyR::ty == ParamTypeId::Void)
713 return mlir::FunctionType::get(context, argTypes, std::nullopt);
714
715 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
716 return mlir::FunctionType::get(context, argTypes, {resType});
717}
718
721 using RuntimeGeneratorRange =
722 std::pair<const MathOperation *, const MathOperation *>;
723 IntrinsicHandlerEntry(const IntrinsicHandler *handler) : entry{handler} {
724 assert(handler && "handler must not be nullptr");
725 };
726 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
727 const IntrinsicArgumentLoweringRules *getArgumentLoweringRules() const;
728 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
729};
730
731//===----------------------------------------------------------------------===//
732// Helper functions for argument handling.
733//===----------------------------------------------------------------------===//
734static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
735 mlir::Type eleTy) {
736 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
737 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
738 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
739 return newEleTy;
740 }
741 return eleTy;
742}
743
745getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
747 for (auto arg : args)
748 baseVec.push_back(getBase(arg));
749 return baseVec;
750}
751
753getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
755 for (auto arg : args)
756 typeVec.push_back(arg.getType());
757 return typeVec;
758}
759
760mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
761 const MathOperation &mathOp,
762 mlir::FunctionType libFuncType,
764
765template <typename T>
766mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
767 const MathOperation &mathOp,
768 mlir::FunctionType mathLibFuncType,
770
771template <typename T>
772mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
773 const MathOperation &mathOp,
774 mlir::FunctionType mathLibFuncType,
776
777mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
778 mlir::Location loc,
779 const MathOperation &mathOp,
780 mlir::FunctionType libFuncType,
782
785std::optional<IntrinsicHandlerEntry>
786lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName,
787 std::optional<mlir::Type> resultType);
788
790void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name);
791
794const IntrinsicArgumentLoweringRules *
795getIntrinsicArgumentLowering(llvm::StringRef intrinsicName);
796
799ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &,
800 unsigned position);
801
804
806// implementation) of an unrestricted intrinsic (defined by its signature
807// and generic name)
808mlir::SymbolRefAttr
810 llvm::StringRef name,
811 mlir::FunctionType signature);
812
813//===----------------------------------------------------------------------===//
814// Direct access to intrinsics that may be used by lowering outside
815// of intrinsic call lowering.
816//===----------------------------------------------------------------------===//
817
820mlir::Value genMax(fir::FirOpBuilder &, mlir::Location,
822
824mlir::Value genMin(fir::FirOpBuilder &, mlir::Location,
826
829mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
830 mlir::Type resultType, mlir::Value x, mlir::Value y);
831
834mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
835 mlir::Value x, mlir::Value y);
836
837} // namespace fir
838
839#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:1724
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:8121
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:8175
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8188
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8183
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:1746
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition: IntrinsicCall.cpp:8146
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:8201
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:8159
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:8167
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:530
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition: IntrinsicCall.h:532
Definition: IntrinsicCall.h:523
Entry into the tables describing how an intrinsic must be lowered.
Definition: IntrinsicCall.h:720
Definition: IntrinsicCall.h:538
bool outline
Definition: IntrinsicCall.h:546
Definition: IntrinsicCall.h:136
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition: IntrinsicCall.cpp:2252
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:1758
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:3904
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition: IntrinsicCall.cpp:1942
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition: IntrinsicCall.h:456
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:2151
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2323
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition: IntrinsicCall.cpp:4117
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2331
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2345
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:8030
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:2286
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition: IntrinsicCall.h:452
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition: IntrinsicCall.cpp:2963
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition: IntrinsicCall.cpp:2956
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition: IntrinsicCall.cpp:4124
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:1957
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:7949
Definition: IntrinsicCall.h:561
Definition: IntrinsicCall.h:611
Definition: IntrinsicCall.h:549