9#ifndef FORTRAN_LOWER_INTRINSICCALL_H
10#define FORTRAN_LOWER_INTRINSICCALL_H
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/LLVMIR/NVVMDialect.h"
23#include "mlir/Dialect/Math/IR/Math.h"
28class StatementContext;
36std::pair<fir::ExtendedValue, bool>
38 std::optional<mlir::Type> resultType,
39 llvm::ArrayRef<fir::ExtendedValue> args,
40 Fortran::lower::AbstractConverter *converter =
nullptr);
45std::pair<fir::ExtendedValue, bool>
48 std::optional<mlir::Type> resultType,
49 llvm::ArrayRef<fir::ExtendedValue> args,
50 Fortran::lower::AbstractConverter *converter =
nullptr);
58enum class ExtremumBehavior {
137struct IntrinsicLibrary {
140 explicit IntrinsicLibrary(
143 : builder{builder}, loc{loc}, converter{converter} {}
144 IntrinsicLibrary() =
delete;
145 IntrinsicLibrary(
const IntrinsicLibrary &) =
delete;
150 std::pair<fir::ExtendedValue, bool>
151 genIntrinsicCall(llvm::StringRef name, std::optional<mlir::Type> resultType,
164 using RuntimeCallGenerator = std::function<mlir::Value(
167 getRuntimeCallGenerator(llvm::StringRef name,
168 mlir::FunctionType soughtFuncType);
179 mlir::Value, mlir::Value)>
215 template <mlir::arith::CmpIPredicate pred>
216 mlir::Value genBitwiseCompare(mlir::Type resultType,
224 template <mlir::arith::CmpIPredicate pred>
232 template <const
char *fctName,
int extent>
240 mlir::Value genErfcScaled(mlir::Type resultType,
246 template <mlir::arith::CmpIPredicate pred>
266 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
268 mlir::ArrayRef<fir::ExtendedValue> args);
272 template <Extremum, ExtremumBehavior>
275 mlir::Value genFraction(mlir::Type resultType,
276 mlir::ArrayRef<mlir::Value> args);
277 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
279 mlir::ArrayRef<fir::ExtendedValue> args);
281 mlir::ArrayRef<fir::ExtendedValue> args);
284 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
285 mlir::Value genGetPID(mlir::Type resultType,
287 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
289 mlir::Value genGetGID(mlir::Type resultType,
291 mlir::Value genGetUID(mlir::Type resultType,
307 template <
bool isGet,
bool isModes>
316 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
317 template <
bool isMax,
bool isNum,
bool isMag>
319 template <mlir::arith::CmpFPredicate pred>
320 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
325 template <
bool isFlag>
329 template <mlir::arith::CmpFPredicate pred>
330 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
341 template <mlir::arith::CmpIPredicate pred>
351 template <Fortran::runtime::io::Iostat value>
363 template <
typename Shift>
380 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
381 template <NearestProc>
389 template <
typename OpTy>
407 mlir::ArrayRef<fir::ExtendedValue>);
410 mlir::Value genRRSpacing(mlir::Type resultType,
419 mlir::ArrayRef<fir::ExtendedValue>);
425 mlir::Value genSetExponent(mlir::Type resultType,
429 template <
typename Shift>
437 mlir::Value genSpacing(mlir::Type resultType,
451 mlir::ArrayRef<fir::ExtendedValue> args);
475 template <mlir::NVVM::VoteSyncKind kind>
488 template <
typename FN,
typename FD>
492 template <
typename FN,
typename FD,
typename FC>
495 llvm::StringRef errMsg,
496 mlir::Type resultType,
499 template <
typename FN,
typename FD>
501 mlir::Type resultType,
509 mlir::Value
genQNan(mlir::Type resultType);
514 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
515 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
527 template <
typename GeneratorType>
529 mlir::Type resultType,
531 template <
typename GeneratorType>
533 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
534 std::optional<mlir::Type> resultType,
537 template <
typename GeneratorType>
538 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
540 bool loadRefArguments =
false);
543 template <
typename GeneratorType>
545 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
550 mlir::Type resultType,
553 mlir::Type resultType,
556 mlir::Type resultType,
569 mlir::FunctionType signature);
573 mlir::Type resultType,
574 llvm::StringRef errMsg);
576 void setResultMustBeFreed() { resultMustBeFreed =
true; }
580 bool resultMustBeFreed =
false;
585 const char *name =
nullptr;
587 bool handleDynamicOptional =
false;
594 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
601 IntrinsicLibrary::Generator generator;
604 bool isElemental =
true;
612 using Key = std::string_view;
614 constexpr operator Key()
const {
return key; }
618 llvm::StringRef symbol;
619 fir::runtime::FuncTypeBuilderFunc typeGenerator;
630 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
634 using Key = std::string_view;
636 constexpr operator Key()
const {
return key; }
641 llvm::StringRef runtimeFunc;
642 FuncTypeBuilderFunc typeGenerator;
648 MathGeneratorTy funcGenerator;
652enum class ParamTypeId {
664static int getVecLen(mlir::Type eleTy) {
665 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
666 mlir::isa<mlir::FloatType>(eleTy)) &&
667 "unsupported vector element type");
668 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
671template <ParamTypeId t,
int k>
674 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
676 "Unsupported integer kind");
677 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
679 "Unsupported real kind");
680 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
681 k == 8 || k == 10 || k == 16,
682 "Unsupported complex kind");
684 static const ParamTypeId ty = t;
685 static const int kind = k;
710static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
712 ParamTypeId typeId,
int kind) {
716 case ParamTypeId::Void:
717 llvm::report_fatal_error(
"can not get type of void");
719 case ParamTypeId::Address:
721 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
722 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
724 case ParamTypeId::Integer:
725 case ParamTypeId::IntegerVector:
727 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
728 r = mlir::IntegerType::get(context, bits);
730 case ParamTypeId::UnsignedVector:
732 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
733 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
735 case ParamTypeId::Real:
736 case ParamTypeId::RealVector:
739 case ParamTypeId::Complex:
740 r = mlir::ComplexType::get(builder.
getRealType(kind));
745 case ParamTypeId::Void:
746 case ParamTypeId::Address:
747 case ParamTypeId::Integer:
748 case ParamTypeId::Real:
749 case ParamTypeId::Complex:
751 case ParamTypeId::IntegerVector:
752 case ParamTypeId::UnsignedVector:
753 case ParamTypeId::RealVector:
755 r = fir::VectorType::get(getVecLen(r), r);
762template <
typename TyR,
typename... ArgTys>
763static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
764 fir::FirOpBuilder &builder) {
765 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
766 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
767 llvm::SmallVector<mlir::Type> argTypes;
769 for (
size_t i = 0; i < argTys.size(); ++i) {
770 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
773 if (TyR::ty == ParamTypeId::Void)
774 return mlir::FunctionType::get(context, argTypes, {});
776 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
777 return mlir::FunctionType::get(context, argTypes, {resType});
781struct IntrinsicHandlerEntry {
782 using RuntimeGeneratorRange =
783 std::pair<const MathOperation *, const MathOperation *>;
785 assert(handler &&
"handler must not be nullptr");
787 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
789 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
795static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
797 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
798 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
799 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
805static inline llvm::SmallVector<mlir::Value, 4>
806getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
807 llvm::SmallVector<mlir::Value, 4> baseVec;
808 for (
auto arg : args)
809 baseVec.push_back(
getBase(arg));
813static inline llvm::SmallVector<mlir::Type, 4>
814getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
815 llvm::SmallVector<mlir::Type, 4> typeVec;
816 for (
auto arg : args)
817 typeVec.push_back(arg.getType());
821mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
823 mlir::FunctionType libFuncType,
824 llvm::ArrayRef<mlir::Value> args);
827mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
829 mlir::FunctionType mathLibFuncType,
830 llvm::ArrayRef<mlir::Value> args);
833mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
835 mlir::FunctionType mathLibFuncType,
836 llvm::ArrayRef<mlir::Value> args);
838mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
841 mlir::FunctionType libFuncType,
842 llvm::ArrayRef<mlir::Value> args);
846std::optional<IntrinsicHandlerEntry>
848 std::optional<mlir::Type> resultType);
871 llvm::StringRef name,
872 mlir::FunctionType signature);
881mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
882 llvm::ArrayRef<mlir::Value> args);
885mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
886 llvm::ArrayRef<mlir::Value> args);
890mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
891 mlir::Type resultType, mlir::Value x, mlir::Value y);
895mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
896 mlir::Value x, mlir::Value y);
Definition AbstractConverter.h:85
Definition BoxValue.h:478
Definition FIRBuilder.h:55
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition FIRBuilder.h:122
mlir::Type getRealType(int kind)
Get the mlir float type that implements Fortran REAL(kind).
Definition FIRBuilder.cpp:118
Bitsize getIntegerBitsize(KindTy kind) const
Get the size in bits of !fir.int<kind>
Definition KindMapping.cpp:283
Definition BoxValue.h:360
Definition AbstractConverter.h:34
std::optional< IntrinsicHandlerEntry > lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName, std::optional< mlir::Type > resultType)
Definition IntrinsicCall.cpp:2101
Extremum
Enums used to templatize and share lowering of MIN and MAX.
Definition IntrinsicCall.h:53
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition IntrinsicCall.cpp:9504
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:9558
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9571
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9566
fir::ExtendedValue getAbsentIntrinsicArgument()
Return place-holder for absent intrinsic arguments.
Definition IntrinsicCall.cpp:78
void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name)
Generate a TODO error message for an as yet unimplemented intrinsic.
Definition IntrinsicCall.cpp:2123
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:9529
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:9592
LowerIntrinsicArgAs
Definition IntrinsicCall.h:97
@ Addr
Definition IntrinsicCall.h:102
@ Inquired
Definition IntrinsicCall.h:108
@ Box
Lower argument to a box.
Definition IntrinsicCall.h:104
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
Definition IntrinsicCall.h:99
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:9542
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:9550
Define how a given intrinsic argument must be lowered.
Definition IntrinsicCall.h:112
bool handleDynamicOptional
Value:
Definition IntrinsicCall.h:124
This is shared by intrinsics and intrinsic module procedures.
Definition IntrinsicCall.h:591
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:593
Definition IntrinsicCall.h:584
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:781
Definition IntrinsicCall.h:599
bool outline
Definition IntrinsicCall.h:607
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2629
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:2135
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:4739
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2319
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:517
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2528
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2700
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4952
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2708
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2722
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:9413
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:2663
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:513
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3509
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3502
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4959
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:2334
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:9332
Definition IntrinsicCall.h:622
Definition IntrinsicCall.h:672
Definition IntrinsicCall.h:610