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);
221 template <
bool isMax>
227 mlir::Value genFraction(mlir::Type resultType,
228 mlir::ArrayRef<mlir::Value> args);
229 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
231 mlir::ArrayRef<fir::ExtendedValue> args);
233 mlir::ArrayRef<fir::ExtendedValue> args);
236 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
237 mlir::Value genGetPID(mlir::Type resultType,
239 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
241 mlir::Value genGetGID(mlir::Type resultType,
244 mlir::Value genGetUID(mlir::Type resultType,
260 template <
bool isGet,
bool isModes>
269 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
270 template <
bool isMax,
bool isNum,
bool isMag>
272 template <mlir::arith::CmpFPredicate pred>
273 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
278 template <
bool isFlag>
282 template <mlir::arith::CmpFPredicate pred>
283 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
294 template <mlir::arith::CmpIPredicate pred>
306 template <Fortran::runtime::io::Iostat value>
318 template <
typename Shift>
333 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
334 template <NearestProc>
360 mlir::ArrayRef<fir::ExtendedValue>);
363 mlir::Value genRRSpacing(mlir::Type resultType,
373 mlir::ArrayRef<fir::ExtendedValue>);
379 mlir::Value genSetExponent(mlir::Type resultType,
383 template <
typename Shift>
392 mlir::Value genSpacing(mlir::Type resultType,
402 mlir::ArrayRef<fir::ExtendedValue> args);
434 template <
typename FN,
typename FD>
438 template <
typename FN,
typename FD,
typename FC>
441 llvm::StringRef errMsg,
442 mlir::Type resultType,
445 template <
typename FN,
typename FD>
447 mlir::Type resultType,
455 mlir::Value
genQNan(mlir::Type resultType);
460 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
461 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
473 template <
typename GeneratorType>
475 mlir::Type resultType,
477 template <
typename GeneratorType>
479 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
480 std::optional<mlir::Type> resultType,
483 template <
typename GeneratorType>
484 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
486 bool loadRefArguments =
false);
489 template <
typename GeneratorType>
491 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
496 mlir::Type resultType,
499 mlir::Type resultType,
502 mlir::Type resultType,
515 mlir::FunctionType signature);
519 mlir::Type resultType,
520 llvm::StringRef errMsg);
522 void setResultMustBeFreed() { resultMustBeFreed =
true; }
526 bool resultMustBeFreed =
false;
531 const char *name =
nullptr;
533 bool handleDynamicOptional =
false;
540 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
547 IntrinsicLibrary::Generator generator;
550 bool isElemental =
true;
558 using Key = std::string_view;
560 constexpr operator Key()
const {
return key; }
564 llvm::StringRef symbol;
565 fir::runtime::FuncTypeBuilderFunc typeGenerator;
576 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
580 using Key = std::string_view;
582 constexpr operator Key()
const {
return key; }
587 llvm::StringRef runtimeFunc;
588 FuncTypeBuilderFunc typeGenerator;
594 MathGeneratorTy funcGenerator;
598enum class ParamTypeId {
610static int getVecLen(mlir::Type eleTy) {
611 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
612 mlir::isa<mlir::FloatType>(eleTy)) &&
613 "unsupported vector element type");
614 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
617template <ParamTypeId t,
int k>
620 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
622 "Unsupported integer kind");
623 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
625 "Unsupported real kind");
626 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
627 k == 8 || k == 10 || k == 16,
628 "Unsupported complex kind");
630 static const ParamTypeId ty = t;
631 static const int kind = k;
656static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
658 ParamTypeId typeId,
int kind) {
662 case ParamTypeId::Void:
663 llvm::report_fatal_error(
"can not get type of void");
665 case ParamTypeId::Address:
667 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
668 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
670 case ParamTypeId::Integer:
671 case ParamTypeId::IntegerVector:
673 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
674 r = mlir::IntegerType::get(context, bits);
676 case ParamTypeId::UnsignedVector:
678 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
679 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
681 case ParamTypeId::Real:
682 case ParamTypeId::RealVector:
685 case ParamTypeId::Complex:
686 r = mlir::ComplexType::get(builder.
getRealType(kind));
691 case ParamTypeId::Void:
692 case ParamTypeId::Address:
693 case ParamTypeId::Integer:
694 case ParamTypeId::Real:
695 case ParamTypeId::Complex:
697 case ParamTypeId::IntegerVector:
698 case ParamTypeId::UnsignedVector:
699 case ParamTypeId::RealVector:
701 r = fir::VectorType::get(getVecLen(r), r);
708template <
typename TyR,
typename... ArgTys>
709static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
710 fir::FirOpBuilder &builder) {
711 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
712 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
713 llvm::SmallVector<mlir::Type> argTypes;
715 for (
size_t i = 0; i < argTys.size(); ++i) {
716 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
719 if (TyR::ty == ParamTypeId::Void)
720 return mlir::FunctionType::get(context, argTypes, {});
722 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
723 return mlir::FunctionType::get(context, argTypes, {resType});
727struct IntrinsicHandlerEntry {
728 using RuntimeGeneratorRange =
729 std::pair<const MathOperation *, const MathOperation *>;
731 assert(handler &&
"handler must not be nullptr");
733 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
735 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
741static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
743 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
744 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
745 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
751static inline llvm::SmallVector<mlir::Value, 4>
752getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
753 llvm::SmallVector<mlir::Value, 4> baseVec;
754 for (
auto arg : args)
755 baseVec.push_back(
getBase(arg));
759static inline llvm::SmallVector<mlir::Type, 4>
760getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
761 llvm::SmallVector<mlir::Type, 4> typeVec;
762 for (
auto arg : args)
763 typeVec.push_back(arg.getType());
767mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
769 mlir::FunctionType libFuncType,
770 llvm::ArrayRef<mlir::Value> args);
773mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
775 mlir::FunctionType mathLibFuncType,
776 llvm::ArrayRef<mlir::Value> args);
779mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
781 mlir::FunctionType mathLibFuncType,
782 llvm::ArrayRef<mlir::Value> args);
784mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
787 mlir::FunctionType libFuncType,
788 llvm::ArrayRef<mlir::Value> args);
792std::optional<IntrinsicHandlerEntry>
794 std::optional<mlir::Type> resultType);
817 llvm::StringRef name,
818 mlir::FunctionType signature);
827mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
828 llvm::ArrayRef<mlir::Value> args);
831mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
832 llvm::ArrayRef<mlir::Value> args);
836mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
837 mlir::Type resultType, mlir::Value x, mlir::Value y);
841mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
842 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:1899
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition IntrinsicCall.cpp:9211
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:9267
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9279
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9274
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:1924
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:9239
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:9300
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:9252
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:9260
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:537
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:539
Definition IntrinsicCall.h:530
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:727
Definition IntrinsicCall.h:545
bool outline
Definition IntrinsicCall.h:553
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2431
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:1936
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:4498
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2121
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:463
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2330
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2502
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4711
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2510
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2524
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:9123
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:2465
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:459
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3172
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3165
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4718
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:2136
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:9042
Definition IntrinsicCall.h:568
Definition IntrinsicCall.h:618
Definition IntrinsicCall.h:556