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;
35std::pair<fir::ExtendedValue, bool>
37 std::optional<mlir::Type> resultType,
38 llvm::ArrayRef<fir::ExtendedValue> args,
39 Fortran::lower::AbstractConverter *converter =
nullptr);
44std::pair<fir::ExtendedValue, bool>
47 std::optional<mlir::Type> resultType,
48 llvm::ArrayRef<fir::ExtendedValue> args,
49 Fortran::lower::AbstractConverter *converter =
nullptr);
57enum class ExtremumBehavior {
136struct IntrinsicLibrary {
139 explicit IntrinsicLibrary(
142 : builder{builder}, loc{loc}, converter{converter} {}
143 IntrinsicLibrary() =
delete;
144 IntrinsicLibrary(
const IntrinsicLibrary &) =
delete;
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);
178 mlir::Value, mlir::Value)>
200 template <mlir::arith::CmpIPredicate pred>
201 mlir::Value genBitwiseCompare(mlir::Type resultType,
209 template <mlir::arith::CmpIPredicate pred>
222 mlir::Value genErfcScaled(mlir::Type resultType,
228 template <mlir::arith::CmpIPredicate pred>
248 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
250 mlir::ArrayRef<fir::ExtendedValue> args);
254 template <Extremum, ExtremumBehavior>
258 mlir::Value genFraction(mlir::Type resultType,
259 mlir::ArrayRef<mlir::Value> args);
260 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
262 mlir::ArrayRef<fir::ExtendedValue> args);
264 mlir::ArrayRef<fir::ExtendedValue> args);
267 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
268 mlir::Value genGetPID(mlir::Type resultType,
270 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
272 mlir::Value genGetGID(mlir::Type resultType,
275 mlir::Value genGetUID(mlir::Type resultType,
291 template <
bool isGet,
bool isModes>
300 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
301 template <
bool isMax,
bool isNum,
bool isMag>
303 template <mlir::arith::CmpFPredicate pred>
304 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
309 template <
bool isFlag>
313 template <mlir::arith::CmpFPredicate pred>
314 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
325 template <mlir::arith::CmpIPredicate pred>
337 template <Fortran::runtime::io::Iostat value>
349 template <
typename Shift>
364 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
365 template <NearestProc>
391 mlir::ArrayRef<fir::ExtendedValue>);
394 mlir::Value genRRSpacing(mlir::Type resultType,
403 mlir::ArrayRef<fir::ExtendedValue>);
409 mlir::Value genSetExponent(mlir::Type resultType,
413 template <
typename Shift>
422 mlir::Value genSpacing(mlir::Type resultType,
431 mlir::ArrayRef<fir::ExtendedValue> args);
462 template <
typename FN,
typename FD>
466 template <
typename FN,
typename FD,
typename FC>
469 llvm::StringRef errMsg,
470 mlir::Type resultType,
473 template <
typename FN,
typename FD>
475 mlir::Type resultType,
483 mlir::Value
genQNan(mlir::Type resultType);
488 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
489 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
501 template <
typename GeneratorType>
503 mlir::Type resultType,
505 template <
typename GeneratorType>
507 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
508 std::optional<mlir::Type> resultType,
511 template <
typename GeneratorType>
512 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
514 bool loadRefArguments =
false);
517 template <
typename GeneratorType>
519 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
524 mlir::Type resultType,
527 mlir::Type resultType,
530 mlir::Type resultType,
543 mlir::FunctionType signature);
547 mlir::Type resultType,
548 llvm::StringRef errMsg);
550 void setResultMustBeFreed() { resultMustBeFreed =
true; }
554 bool resultMustBeFreed =
false;
559 const char *name =
nullptr;
561 bool handleDynamicOptional =
false;
568 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
575 IntrinsicLibrary::Generator generator;
578 bool isElemental =
true;
586 using Key = std::string_view;
588 constexpr operator Key()
const {
return key; }
592 llvm::StringRef symbol;
593 fir::runtime::FuncTypeBuilderFunc typeGenerator;
604 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
608 using Key = std::string_view;
610 constexpr operator Key()
const {
return key; }
615 llvm::StringRef runtimeFunc;
616 FuncTypeBuilderFunc typeGenerator;
622 MathGeneratorTy funcGenerator;
626enum class ParamTypeId {
638static int getVecLen(mlir::Type eleTy) {
639 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
640 mlir::isa<mlir::FloatType>(eleTy)) &&
641 "unsupported vector element type");
642 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
645template <ParamTypeId t,
int k>
648 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
650 "Unsupported integer kind");
651 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
653 "Unsupported real kind");
654 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
655 k == 8 || k == 10 || k == 16,
656 "Unsupported complex kind");
658 static const ParamTypeId ty = t;
659 static const int kind = k;
684static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
686 ParamTypeId typeId,
int kind) {
690 case ParamTypeId::Void:
691 llvm::report_fatal_error(
"can not get type of void");
693 case ParamTypeId::Address:
695 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
696 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
698 case ParamTypeId::Integer:
699 case ParamTypeId::IntegerVector:
701 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
702 r = mlir::IntegerType::get(context, bits);
704 case ParamTypeId::UnsignedVector:
706 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
707 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
709 case ParamTypeId::Real:
710 case ParamTypeId::RealVector:
713 case ParamTypeId::Complex:
714 r = mlir::ComplexType::get(builder.
getRealType(kind));
719 case ParamTypeId::Void:
720 case ParamTypeId::Address:
721 case ParamTypeId::Integer:
722 case ParamTypeId::Real:
723 case ParamTypeId::Complex:
725 case ParamTypeId::IntegerVector:
726 case ParamTypeId::UnsignedVector:
727 case ParamTypeId::RealVector:
729 r = fir::VectorType::get(getVecLen(r), r);
736template <
typename TyR,
typename... ArgTys>
737static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
738 fir::FirOpBuilder &builder) {
739 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
740 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
741 llvm::SmallVector<mlir::Type> argTypes;
743 for (
size_t i = 0; i < argTys.size(); ++i) {
744 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
747 if (TyR::ty == ParamTypeId::Void)
748 return mlir::FunctionType::get(context, argTypes, {});
750 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
751 return mlir::FunctionType::get(context, argTypes, {resType});
755struct IntrinsicHandlerEntry {
756 using RuntimeGeneratorRange =
757 std::pair<const MathOperation *, const MathOperation *>;
759 assert(handler &&
"handler must not be nullptr");
761 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
763 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
769static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
771 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
772 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
773 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
779static inline llvm::SmallVector<mlir::Value, 4>
780getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
781 llvm::SmallVector<mlir::Value, 4> baseVec;
782 for (
auto arg : args)
783 baseVec.push_back(
getBase(arg));
787static inline llvm::SmallVector<mlir::Type, 4>
788getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
789 llvm::SmallVector<mlir::Type, 4> typeVec;
790 for (
auto arg : args)
791 typeVec.push_back(arg.getType());
795mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
797 mlir::FunctionType libFuncType,
798 llvm::ArrayRef<mlir::Value> args);
801mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
803 mlir::FunctionType mathLibFuncType,
804 llvm::ArrayRef<mlir::Value> args);
807mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
809 mlir::FunctionType mathLibFuncType,
810 llvm::ArrayRef<mlir::Value> args);
812mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
815 mlir::FunctionType libFuncType,
816 llvm::ArrayRef<mlir::Value> args);
820std::optional<IntrinsicHandlerEntry>
822 std::optional<mlir::Type> resultType);
845 llvm::StringRef name,
846 mlir::FunctionType signature);
855mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
856 llvm::ArrayRef<mlir::Value> args);
859mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
860 llvm::ArrayRef<mlir::Value> args);
864mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
865 mlir::Type resultType, mlir::Value x, mlir::Value y);
869mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
870 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:1868
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:8848
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:8905
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:8918
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:8913
fir::ExtendedValue getAbsentIntrinsicArgument()
Return place-holder for absent intrinsic arguments.
Definition IntrinsicCall.cpp:79
void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name)
Generate a TODO error message for an as yet unimplemented intrinsic.
Definition IntrinsicCall.cpp:1893
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:8876
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:8939
LowerIntrinsicArgAs
Definition IntrinsicCall.h:96
@ Addr
Definition IntrinsicCall.h:101
@ Inquired
Definition IntrinsicCall.h:107
@ Box
Lower argument to a box.
Definition IntrinsicCall.h:103
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
Definition IntrinsicCall.h:98
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:8889
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:8897
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:565
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:567
Definition IntrinsicCall.h:558
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:755
Definition IntrinsicCall.h:573
bool outline
Definition IntrinsicCall.h:581
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2400
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:1905
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:4351
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2090
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:491
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2299
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2471
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4564
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2479
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2493
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:8757
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:2434
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:487
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3141
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3134
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4571
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:2105
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:8676
Definition IntrinsicCall.h:596
Definition IntrinsicCall.h:646
Definition IntrinsicCall.h:584