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/Math/IR/Math.h"
27class StatementContext;
28struct IntrinsicHandlerEntry;
35std::pair<fir::ExtendedValue, bool>
37 std::optional<mlir::Type> resultType,
44std::pair<fir::ExtendedValue, bool>
46 const IntrinsicHandlerEntry &,
47 std::optional<mlir::Type> resultType,
57enum class ExtremumBehavior {
142 : builder{builder}, loc{loc}, converter{converter} {}
149 std::pair<fir::ExtendedValue, bool>
150 genIntrinsicCall(llvm::StringRef name, std::optional<mlir::Type> resultType,
163 using RuntimeCallGenerator = std::function<mlir::Value(
166 getRuntimeCallGenerator(llvm::StringRef name,
167 mlir::FunctionType soughtFuncType);
177 mlir::Value, mlir::Value)>
206 template <mlir::arith::CmpIPredicate pred>
207 mlir::Value genBitwiseCompare(mlir::Type resultType,
215 template <mlir::arith::CmpIPredicate pred>
228 mlir::Value genErfcScaled(mlir::Type resultType,
234 template <mlir::arith::CmpIPredicate pred>
247 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
249 mlir::ArrayRef<fir::ExtendedValue> args);
253 template <Extremum, ExtremumBehavior>
256 mlir::Value genFraction(mlir::Type resultType,
257 mlir::ArrayRef<mlir::Value> args);
258 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
261 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
262 mlir::Value genGetPID(mlir::Type resultType,
264 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
266 mlir::Value genGetGID(mlir::Type resultType,
268 mlir::Value genGetUID(mlir::Type resultType,
282 template <
bool isGet,
bool isModes>
291 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
292 template <
bool isMax,
bool isNum,
bool isMag>
294 template <mlir::arith::CmpFPredicate pred>
295 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
300 template <
bool isFlag>
304 template <mlir::arith::CmpFPredicate pred>
305 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
313 template <mlir::arith::CmpIPredicate pred>
323 template <Fortran::runtime::io::Iostat value>
335 template <
typename Shift>
350 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
351 template <NearestProc>
370 mlir::ArrayRef<fir::ExtendedValue>);
373 mlir::Value genRRSpacing(mlir::Type resultType,
380 mlir::ArrayRef<fir::ExtendedValue>);
386 mlir::Value genSetExponent(mlir::Type resultType,
390 template <
typename Shift>
397 mlir::Value genSpacing(mlir::Type resultType,
410 mlir::ArrayRef<fir::ExtendedValue> args);
436 template <
typename FN,
typename FD>
440 template <
typename FN,
typename FD,
typename FC>
443 llvm::StringRef errMsg,
444 mlir::Type resultType,
447 template <
typename FN,
typename FD>
449 mlir::Type resultType,
457 mlir::Value
genQNan(mlir::Type resultType);
462 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
463 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
475 template <
typename GeneratorType>
477 mlir::Type resultType,
479 template <
typename GeneratorType>
481 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
482 std::optional<mlir::Type> resultType,
485 template <
typename GeneratorType>
486 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
488 bool loadRefArguments =
false);
491 template <
typename GeneratorType>
493 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
498 mlir::Type resultType,
501 mlir::Type resultType,
504 mlir::Type resultType,
517 mlir::FunctionType signature);
521 mlir::Type resultType,
522 llvm::StringRef errMsg);
524 void setResultMustBeFreed() { resultMustBeFreed =
true; }
528 bool resultMustBeFreed =
false;
533 const char *name =
nullptr;
535 bool handleDynamicOptional =
false;
542 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
549 IntrinsicLibrary::Generator generator;
552 bool isElemental =
true;
560 using Key = std::string_view;
562 constexpr operator Key()
const {
return key; }
566 llvm::StringRef symbol;
567 fir::runtime::FuncTypeBuilderFunc typeGenerator;
578 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
582 using Key = std::string_view;
584 constexpr operator Key()
const {
return key; }
589 llvm::StringRef runtimeFunc;
590 FuncTypeBuilderFunc typeGenerator;
596 MathGeneratorTy funcGenerator;
600enum class ParamTypeId {
612static int getVecLen(mlir::Type eleTy) {
613 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
614 mlir::isa<mlir::FloatType>(eleTy)) &&
615 "unsupported vector element type");
616 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
619template <ParamTypeId t,
int k>
622 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
624 "Unsupported integer kind");
625 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
627 "Unsupported real kind");
628 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
629 k == 8 || k == 10 || k == 16,
630 "Unsupported complex kind");
632 static const ParamTypeId ty = t;
633 static const int kind = k;
658static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
660 ParamTypeId typeId,
int kind) {
664 case ParamTypeId::Void:
665 llvm::report_fatal_error(
"can not get type of void");
667 case ParamTypeId::Address:
669 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
670 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
672 case ParamTypeId::Integer:
673 case ParamTypeId::IntegerVector:
675 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
676 r = mlir::IntegerType::get(context, bits);
678 case ParamTypeId::UnsignedVector:
680 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
681 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
683 case ParamTypeId::Real:
684 case ParamTypeId::RealVector:
687 case ParamTypeId::Complex:
688 r = mlir::ComplexType::get(builder.
getRealType(kind));
693 case ParamTypeId::Void:
694 case ParamTypeId::Address:
695 case ParamTypeId::Integer:
696 case ParamTypeId::Real:
697 case ParamTypeId::Complex:
699 case ParamTypeId::IntegerVector:
700 case ParamTypeId::UnsignedVector:
701 case ParamTypeId::RealVector:
703 r = fir::VectorType::get(getVecLen(r), r);
710template <
typename TyR,
typename... ArgTys>
711static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
717 for (
size_t i = 0; i < argTys.size(); ++i) {
718 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
721 if (TyR::ty == ParamTypeId::Void)
722 return mlir::FunctionType::get(context, argTypes, std::nullopt);
724 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
725 return mlir::FunctionType::get(context, argTypes, {resType});
730 using RuntimeGeneratorRange =
731 std::pair<const MathOperation *, const MathOperation *>;
733 assert(handler &&
"handler must not be nullptr");
737 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
743static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
745 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
746 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
747 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
756 for (
auto arg : args)
757 baseVec.push_back(
getBase(arg));
764 for (
auto arg : args)
765 typeVec.push_back(arg.getType());
770 const MathOperation &mathOp,
771 mlir::FunctionType libFuncType,
776 const MathOperation &mathOp,
777 mlir::FunctionType mathLibFuncType,
782 const MathOperation &mathOp,
783 mlir::FunctionType mathLibFuncType,
788 const MathOperation &mathOp,
789 mlir::FunctionType libFuncType,
794std::optional<IntrinsicHandlerEntry>
796 std::optional<mlir::Type> resultType);
803const IntrinsicArgumentLoweringRules *
819 llvm::StringRef name,
820 mlir::FunctionType signature);
839 mlir::Type resultType, mlir::Value x, mlir::Value y);
844 mlir::Value x, mlir::Value y);
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: AbstractConverter.h:31
std::optional< IntrinsicHandlerEntry > lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName, std::optional< mlir::Type > resultType)
Definition: IntrinsicCall.cpp:1744
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:8240
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:8294
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8307
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8302
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:1766
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition: IntrinsicCall.cpp:8265
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:8320
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:8278
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:8286
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:539
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition: IntrinsicCall.h:541
Definition: IntrinsicCall.h:532
Entry into the tables describing how an intrinsic must be lowered.
Definition: IntrinsicCall.h:729
Definition: IntrinsicCall.h:547
bool outline
Definition: IntrinsicCall.h:555
Definition: IntrinsicCall.h:136
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition: IntrinsicCall.cpp:2272
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:1778
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:4022
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition: IntrinsicCall.cpp:1962
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition: IntrinsicCall.h:465
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:2171
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2343
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition: IntrinsicCall.cpp:4235
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2351
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2365
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:8149
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:2306
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition: IntrinsicCall.h:461
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition: IntrinsicCall.cpp:3052
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition: IntrinsicCall.cpp:3045
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition: IntrinsicCall.cpp:4242
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:1977
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:8068
Definition: IntrinsicCall.h:570
Definition: IntrinsicCall.h:620
Definition: IntrinsicCall.h:558