9#ifndef FORTRAN_LOWER_INTRINSICCALL_H
10#define FORTRAN_LOWER_INTRINSICCALL_H
12#include "flang/Optimizer/Builder/BoxValue.h"
13#include "flang/Optimizer/Builder/FIRBuilder.h"
14#include "flang/Optimizer/Builder/Runtime/Character.h"
15#include "flang/Optimizer/Builder/Runtime/Numeric.h"
17#include "flang/Runtime/entry-names.h"
18#include "flang/Runtime/iostat-consts.h"
19#include "mlir/Dialect/Complex/IR/Complex.h"
20#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
21#include "mlir/Dialect/Math/IR/Math.h"
35class StatementContext;
43std::pair<fir::ExtendedValue, bool>
45 std::optional<mlir::Type> resultType,
46 llvm::ArrayRef<fir::ExtendedValue> args,
47 Fortran::lower::AbstractConverter *converter =
nullptr);
52std::pair<fir::ExtendedValue, bool>
55 std::optional<mlir::Type> resultType,
56 llvm::ArrayRef<fir::ExtendedValue> args,
57 Fortran::lower::AbstractConverter *converter =
nullptr);
101struct IntrinsicLibrary {
104 explicit IntrinsicLibrary(
107 : builder{builder}, loc{loc}, converter{converter} {}
108 IntrinsicLibrary() =
delete;
109 IntrinsicLibrary(
const IntrinsicLibrary &) =
delete;
114 std::pair<fir::ExtendedValue, bool>
115 genIntrinsicCall(llvm::StringRef name, std::optional<mlir::Type> resultType,
128 using RuntimeCallGenerator = std::function<mlir::Value(
131 getRuntimeCallGenerator(llvm::StringRef name,
132 mlir::FunctionType soughtFuncType);
143 mlir::Value, mlir::Value)>
165 template <mlir::arith::CmpIPredicate pred>
166 mlir::Value genBitwiseCompare(mlir::Type resultType,
174 template <mlir::arith::CmpIPredicate pred>
187 mlir::Value genErfcScaled(mlir::Type resultType,
194 template <mlir::arith::CmpIPredicate pred>
214 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
216 mlir::ArrayRef<fir::ExtendedValue> args);
220 template <
bool isMax>
226 mlir::Value genFraction(mlir::Type resultType,
227 mlir::ArrayRef<mlir::Value> args);
228 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
230 mlir::ArrayRef<fir::ExtendedValue> args);
232 mlir::ArrayRef<fir::ExtendedValue> args);
235 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
236 mlir::Value genGetPID(mlir::Type resultType,
238 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
240 mlir::Value genGetGID(mlir::Type resultType,
243 mlir::Value genGetUID(mlir::Type resultType,
259 template <
bool isGet,
bool isModes>
268 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
269 template <
bool isMax,
bool isNum,
bool isMag>
271 template <mlir::arith::CmpFPredicate pred>
272 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
277 template <
bool isFlag>
281 template <mlir::arith::CmpFPredicate pred>
282 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
293 template <mlir::arith::CmpIPredicate pred>
305 template <Fortran::runtime::io::Iostat value>
317 template <
typename Shift>
332 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
333 template <NearestProc>
359 mlir::ArrayRef<fir::ExtendedValue>);
362 mlir::Value genRRSpacing(mlir::Type resultType,
372 mlir::ArrayRef<fir::ExtendedValue>);
378 mlir::Value genSetExponent(mlir::Type resultType,
382 template <
typename Shift>
391 mlir::Value genSpacing(mlir::Type resultType,
401 mlir::ArrayRef<fir::ExtendedValue> args);
433 template <
typename FN,
typename FD>
437 template <
typename FN,
typename FD,
typename FC>
440 llvm::StringRef errMsg,
441 mlir::Type resultType,
444 template <
typename FN,
typename FD>
446 mlir::Type resultType,
454 mlir::Value
genQNan(mlir::Type resultType);
459 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
460 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
472 template <
typename GeneratorType>
474 mlir::Type resultType,
476 template <
typename GeneratorType>
478 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
479 std::optional<mlir::Type> resultType,
482 template <
typename GeneratorType>
483 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
485 bool loadRefArguments =
false);
488 template <
typename GeneratorType>
490 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
495 mlir::Type resultType,
498 mlir::Type resultType,
501 mlir::Type resultType,
514 mlir::FunctionType signature);
518 mlir::Type resultType,
519 llvm::StringRef errMsg);
521 void setResultMustBeFreed() { resultMustBeFreed =
true; }
525 bool resultMustBeFreed =
false;
530 const char *name =
nullptr;
532 bool handleDynamicOptional =
false;
539 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
546 IntrinsicLibrary::Generator generator;
549 bool isElemental =
true;
557 using Key = std::string_view;
559 constexpr operator Key()
const {
return key; }
563 llvm::StringRef symbol;
564 fir::runtime::FuncTypeBuilderFunc typeGenerator;
575 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
579 using Key = std::string_view;
581 constexpr operator Key()
const {
return key; }
586 llvm::StringRef runtimeFunc;
587 FuncTypeBuilderFunc typeGenerator;
593 MathGeneratorTy funcGenerator;
597enum class ParamTypeId {
609static int getVecLen(mlir::Type eleTy) {
610 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
611 mlir::isa<mlir::FloatType>(eleTy)) &&
612 "unsupported vector element type");
613 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
616template <ParamTypeId t,
int k>
619 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
621 "Unsupported integer kind");
622 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
624 "Unsupported real kind");
625 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
626 k == 8 || k == 10 || k == 16,
627 "Unsupported complex kind");
629 static const ParamTypeId ty = t;
630 static const int kind = k;
655static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
657 ParamTypeId typeId,
int kind) {
661 case ParamTypeId::Void:
662 llvm::report_fatal_error(
"can not get type of void");
664 case ParamTypeId::Address:
666 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
667 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
669 case ParamTypeId::Integer:
670 case ParamTypeId::IntegerVector:
672 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
673 r = mlir::IntegerType::get(context, bits);
675 case ParamTypeId::UnsignedVector:
677 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
678 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
680 case ParamTypeId::Real:
681 case ParamTypeId::RealVector:
684 case ParamTypeId::Complex:
685 r = mlir::ComplexType::get(builder.
getRealType(kind));
690 case ParamTypeId::Void:
691 case ParamTypeId::Address:
692 case ParamTypeId::Integer:
693 case ParamTypeId::Real:
694 case ParamTypeId::Complex:
696 case ParamTypeId::IntegerVector:
697 case ParamTypeId::UnsignedVector:
698 case ParamTypeId::RealVector:
700 r = fir::VectorType::get(getVecLen(r), r);
707template <
typename TyR,
typename... ArgTys>
708static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
709 fir::FirOpBuilder &builder) {
710 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
711 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
712 llvm::SmallVector<mlir::Type> argTypes;
714 for (
size_t i = 0; i < argTys.size(); ++i) {
715 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
718 if (TyR::ty == ParamTypeId::Void)
719 return mlir::FunctionType::get(context, argTypes, {});
721 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
722 return mlir::FunctionType::get(context, argTypes, {resType});
726struct IntrinsicHandlerEntry {
727 using RuntimeGeneratorRange =
728 std::pair<const MathOperation *, const MathOperation *>;
730 assert(handler &&
"handler must not be nullptr");
732 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
734 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
740static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
742 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
743 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
744 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
750static inline llvm::SmallVector<mlir::Value, 4>
751getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
752 llvm::SmallVector<mlir::Value, 4> baseVec;
753 for (
auto arg : args)
754 baseVec.push_back(
getBase(arg));
758static inline llvm::SmallVector<mlir::Type, 4>
759getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
760 llvm::SmallVector<mlir::Type, 4> typeVec;
761 for (
auto arg : args)
762 typeVec.push_back(arg.getType());
766mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
768 mlir::FunctionType libFuncType,
769 llvm::ArrayRef<mlir::Value> args);
772mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
774 mlir::FunctionType mathLibFuncType,
775 llvm::ArrayRef<mlir::Value> args);
778mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
780 mlir::FunctionType mathLibFuncType,
781 llvm::ArrayRef<mlir::Value> args);
783mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
786 mlir::FunctionType libFuncType,
787 llvm::ArrayRef<mlir::Value> args);
791std::optional<IntrinsicHandlerEntry>
793 std::optional<mlir::Type> resultType);
816 llvm::StringRef name,
817 mlir::FunctionType signature);
826mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
827 llvm::ArrayRef<mlir::Value> args);
830mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
831 llvm::ArrayRef<mlir::Value> args);
835mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
836 mlir::Type resultType, mlir::Value x, mlir::Value y);
840mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
841 mlir::Value x, mlir::Value y);
Definition AbstractConverter.h:87
Definition BoxValue.h:478
Definition FIRBuilder.h:56
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition FIRBuilder.h:123
mlir::Type getRealType(int kind)
Get the mlir float type that implements Fortran REAL(kind).
Definition FIRBuilder.cpp:119
Bitsize getIntegerBitsize(KindTy kind) const
Get the size in bits of !fir.int<kind>
Definition KindMapping.cpp:283
Definition BoxValue.h:360
Definition ParserActions.h:24
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
std::optional< IntrinsicHandlerEntry > lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName, std::optional< mlir::Type > resultType)
Definition IntrinsicCall.cpp:1896
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition IntrinsicCall.cpp:9169
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:9225
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9237
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9232
fir::ExtendedValue getAbsentIntrinsicArgument()
Return place-holder for absent intrinsic arguments.
Definition IntrinsicCall.cpp:80
void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name)
Generate a TODO error message for an as yet unimplemented intrinsic.
Definition IntrinsicCall.cpp:1921
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:9197
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:9258
LowerIntrinsicArgAs
Definition IntrinsicCall.h:61
@ Addr
Definition IntrinsicCall.h:66
@ Inquired
Definition IntrinsicCall.h:72
@ Box
Lower argument to a box.
Definition IntrinsicCall.h:68
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
Definition IntrinsicCall.h:63
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:9210
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:9218
Define how a given intrinsic argument must be lowered.
Definition IntrinsicCall.h:76
bool handleDynamicOptional
Value:
Definition IntrinsicCall.h:88
This is shared by intrinsics and intrinsic module procedures.
Definition IntrinsicCall.h:536
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:538
Definition IntrinsicCall.h:529
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:726
Definition IntrinsicCall.h:544
bool outline
Definition IntrinsicCall.h:552
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2428
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:1933
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:4495
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2118
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:462
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2327
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2499
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4708
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2507
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2521
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:9081
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:2462
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:458
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3169
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3162
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4715
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:2133
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:9000
Definition IntrinsicCall.h:567
Definition IntrinsicCall.h:617
Definition IntrinsicCall.h:555