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,
400 mlir::ArrayRef<fir::ExtendedValue> args);
432 template <
typename FN,
typename FD>
436 template <
typename FN,
typename FD,
typename FC>
439 llvm::StringRef errMsg,
440 mlir::Type resultType,
443 template <
typename FN,
typename FD>
445 mlir::Type resultType,
453 mlir::Value
genQNan(mlir::Type resultType);
458 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
459 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
471 template <
typename GeneratorType>
473 mlir::Type resultType,
475 template <
typename GeneratorType>
477 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
478 std::optional<mlir::Type> resultType,
481 template <
typename GeneratorType>
482 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
484 bool loadRefArguments =
false);
487 template <
typename GeneratorType>
489 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
494 mlir::Type resultType,
497 mlir::Type resultType,
500 mlir::Type resultType,
513 mlir::FunctionType signature);
517 mlir::Type resultType,
518 llvm::StringRef errMsg);
520 void setResultMustBeFreed() { resultMustBeFreed =
true; }
524 bool resultMustBeFreed =
false;
529 const char *name =
nullptr;
531 bool handleDynamicOptional =
false;
538 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
545 IntrinsicLibrary::Generator generator;
548 bool isElemental =
true;
556 using Key = std::string_view;
558 constexpr operator Key()
const {
return key; }
562 llvm::StringRef symbol;
563 fir::runtime::FuncTypeBuilderFunc typeGenerator;
574 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
578 using Key = std::string_view;
580 constexpr operator Key()
const {
return key; }
585 llvm::StringRef runtimeFunc;
586 FuncTypeBuilderFunc typeGenerator;
592 MathGeneratorTy funcGenerator;
596enum class ParamTypeId {
608static int getVecLen(mlir::Type eleTy) {
609 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
610 mlir::isa<mlir::FloatType>(eleTy)) &&
611 "unsupported vector element type");
612 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
615template <ParamTypeId t,
int k>
618 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
620 "Unsupported integer kind");
621 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
623 "Unsupported real kind");
624 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
625 k == 8 || k == 10 || k == 16,
626 "Unsupported complex kind");
628 static const ParamTypeId ty = t;
629 static const int kind = k;
654static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
656 ParamTypeId typeId,
int kind) {
660 case ParamTypeId::Void:
661 llvm::report_fatal_error(
"can not get type of void");
663 case ParamTypeId::Address:
665 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
666 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
668 case ParamTypeId::Integer:
669 case ParamTypeId::IntegerVector:
671 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
672 r = mlir::IntegerType::get(context, bits);
674 case ParamTypeId::UnsignedVector:
676 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
677 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
679 case ParamTypeId::Real:
680 case ParamTypeId::RealVector:
683 case ParamTypeId::Complex:
684 r = mlir::ComplexType::get(builder.
getRealType(kind));
689 case ParamTypeId::Void:
690 case ParamTypeId::Address:
691 case ParamTypeId::Integer:
692 case ParamTypeId::Real:
693 case ParamTypeId::Complex:
695 case ParamTypeId::IntegerVector:
696 case ParamTypeId::UnsignedVector:
697 case ParamTypeId::RealVector:
699 r = fir::VectorType::get(getVecLen(r), r);
706template <
typename TyR,
typename... ArgTys>
707static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
708 fir::FirOpBuilder &builder) {
709 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
710 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
711 llvm::SmallVector<mlir::Type> argTypes;
713 for (
size_t i = 0; i < argTys.size(); ++i) {
714 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
717 if (TyR::ty == ParamTypeId::Void)
718 return mlir::FunctionType::get(context, argTypes, {});
720 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
721 return mlir::FunctionType::get(context, argTypes, {resType});
725struct IntrinsicHandlerEntry {
726 using RuntimeGeneratorRange =
727 std::pair<const MathOperation *, const MathOperation *>;
729 assert(handler &&
"handler must not be nullptr");
731 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
733 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
739static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
741 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
742 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
743 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
749static inline llvm::SmallVector<mlir::Value, 4>
750getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
751 llvm::SmallVector<mlir::Value, 4> baseVec;
752 for (
auto arg : args)
753 baseVec.push_back(
getBase(arg));
757static inline llvm::SmallVector<mlir::Type, 4>
758getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
759 llvm::SmallVector<mlir::Type, 4> typeVec;
760 for (
auto arg : args)
761 typeVec.push_back(arg.getType());
765mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
767 mlir::FunctionType libFuncType,
768 llvm::ArrayRef<mlir::Value> args);
771mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
773 mlir::FunctionType mathLibFuncType,
774 llvm::ArrayRef<mlir::Value> args);
777mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
779 mlir::FunctionType mathLibFuncType,
780 llvm::ArrayRef<mlir::Value> args);
782mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
785 mlir::FunctionType libFuncType,
786 llvm::ArrayRef<mlir::Value> args);
790std::optional<IntrinsicHandlerEntry>
792 std::optional<mlir::Type> resultType);
815 llvm::StringRef name,
816 mlir::FunctionType signature);
825mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
826 llvm::ArrayRef<mlir::Value> args);
829mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
830 llvm::ArrayRef<mlir::Value> args);
834mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
835 mlir::Type resultType, mlir::Value x, mlir::Value y);
839mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
840 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:1889
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition IntrinsicCall.cpp:9129
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:9185
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9197
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9192
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:1914
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:9157
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:9218
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:9170
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:9178
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:535
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:537
Definition IntrinsicCall.h:528
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:725
Definition IntrinsicCall.h:543
bool outline
Definition IntrinsicCall.h:551
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2421
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:1926
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:4488
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2111
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:461
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2320
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2492
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4701
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2500
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2514
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:9041
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:2455
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:457
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3162
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3155
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4708
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:2126
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:8960
Definition IntrinsicCall.h:566
Definition IntrinsicCall.h:616
Definition IntrinsicCall.h:554