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);
65enum class ExtremumBehavior {
144struct IntrinsicLibrary {
147 explicit IntrinsicLibrary(
150 : builder{builder}, loc{loc}, converter{converter} {}
151 IntrinsicLibrary() =
delete;
152 IntrinsicLibrary(
const IntrinsicLibrary &) =
delete;
157 std::pair<fir::ExtendedValue, bool>
158 genIntrinsicCall(llvm::StringRef name, std::optional<mlir::Type> resultType,
171 using RuntimeCallGenerator = std::function<mlir::Value(
174 getRuntimeCallGenerator(llvm::StringRef name,
175 mlir::FunctionType soughtFuncType);
186 mlir::Value, mlir::Value)>
208 template <mlir::arith::CmpIPredicate pred>
209 mlir::Value genBitwiseCompare(mlir::Type resultType,
217 template <mlir::arith::CmpIPredicate pred>
230 mlir::Value genErfcScaled(mlir::Type resultType,
236 template <mlir::arith::CmpIPredicate pred>
256 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
258 mlir::ArrayRef<fir::ExtendedValue> args);
262 template <Extremum, ExtremumBehavior>
266 mlir::Value genFraction(mlir::Type resultType,
267 mlir::ArrayRef<mlir::Value> args);
268 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
270 mlir::ArrayRef<fir::ExtendedValue> args);
272 mlir::ArrayRef<fir::ExtendedValue> args);
275 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
276 mlir::Value genGetPID(mlir::Type resultType,
278 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
280 mlir::Value genGetGID(mlir::Type resultType,
283 mlir::Value genGetUID(mlir::Type resultType,
299 template <
bool isGet,
bool isModes>
308 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
309 template <
bool isMax,
bool isNum,
bool isMag>
311 template <mlir::arith::CmpFPredicate pred>
312 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
317 template <
bool isFlag>
321 template <mlir::arith::CmpFPredicate pred>
322 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
333 template <mlir::arith::CmpIPredicate pred>
345 template <Fortran::runtime::io::Iostat value>
357 template <
typename Shift>
372 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
373 template <NearestProc>
399 mlir::ArrayRef<fir::ExtendedValue>);
402 mlir::Value genRRSpacing(mlir::Type resultType,
411 mlir::ArrayRef<fir::ExtendedValue>);
417 mlir::Value genSetExponent(mlir::Type resultType,
421 template <
typename Shift>
430 mlir::Value genSpacing(mlir::Type resultType,
439 mlir::ArrayRef<fir::ExtendedValue> args);
470 template <
typename FN,
typename FD>
474 template <
typename FN,
typename FD,
typename FC>
477 llvm::StringRef errMsg,
478 mlir::Type resultType,
481 template <
typename FN,
typename FD>
483 mlir::Type resultType,
491 mlir::Value
genQNan(mlir::Type resultType);
496 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
497 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
509 template <
typename GeneratorType>
511 mlir::Type resultType,
513 template <
typename GeneratorType>
515 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
516 std::optional<mlir::Type> resultType,
519 template <
typename GeneratorType>
520 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
522 bool loadRefArguments =
false);
525 template <
typename GeneratorType>
527 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
532 mlir::Type resultType,
535 mlir::Type resultType,
538 mlir::Type resultType,
551 mlir::FunctionType signature);
555 mlir::Type resultType,
556 llvm::StringRef errMsg);
558 void setResultMustBeFreed() { resultMustBeFreed =
true; }
562 bool resultMustBeFreed =
false;
567 const char *name =
nullptr;
569 bool handleDynamicOptional =
false;
576 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
583 IntrinsicLibrary::Generator generator;
586 bool isElemental =
true;
594 using Key = std::string_view;
596 constexpr operator Key()
const {
return key; }
600 llvm::StringRef symbol;
601 fir::runtime::FuncTypeBuilderFunc typeGenerator;
612 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
616 using Key = std::string_view;
618 constexpr operator Key()
const {
return key; }
623 llvm::StringRef runtimeFunc;
624 FuncTypeBuilderFunc typeGenerator;
630 MathGeneratorTy funcGenerator;
634enum class ParamTypeId {
646static int getVecLen(mlir::Type eleTy) {
647 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
648 mlir::isa<mlir::FloatType>(eleTy)) &&
649 "unsupported vector element type");
650 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
653template <ParamTypeId t,
int k>
656 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
658 "Unsupported integer kind");
659 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
661 "Unsupported real kind");
662 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
663 k == 8 || k == 10 || k == 16,
664 "Unsupported complex kind");
666 static const ParamTypeId ty = t;
667 static const int kind = k;
692static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
694 ParamTypeId typeId,
int kind) {
698 case ParamTypeId::Void:
699 llvm::report_fatal_error(
"can not get type of void");
701 case ParamTypeId::Address:
703 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
704 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
706 case ParamTypeId::Integer:
707 case ParamTypeId::IntegerVector:
709 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
710 r = mlir::IntegerType::get(context, bits);
712 case ParamTypeId::UnsignedVector:
714 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
715 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
717 case ParamTypeId::Real:
718 case ParamTypeId::RealVector:
721 case ParamTypeId::Complex:
722 r = mlir::ComplexType::get(builder.
getRealType(kind));
727 case ParamTypeId::Void:
728 case ParamTypeId::Address:
729 case ParamTypeId::Integer:
730 case ParamTypeId::Real:
731 case ParamTypeId::Complex:
733 case ParamTypeId::IntegerVector:
734 case ParamTypeId::UnsignedVector:
735 case ParamTypeId::RealVector:
737 r = fir::VectorType::get(getVecLen(r), r);
744template <
typename TyR,
typename... ArgTys>
745static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
746 fir::FirOpBuilder &builder) {
747 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
748 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
749 llvm::SmallVector<mlir::Type> argTypes;
751 for (
size_t i = 0; i < argTys.size(); ++i) {
752 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
755 if (TyR::ty == ParamTypeId::Void)
756 return mlir::FunctionType::get(context, argTypes, {});
758 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
759 return mlir::FunctionType::get(context, argTypes, {resType});
763struct IntrinsicHandlerEntry {
764 using RuntimeGeneratorRange =
765 std::pair<const MathOperation *, const MathOperation *>;
767 assert(handler &&
"handler must not be nullptr");
769 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
771 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
777static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
779 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
780 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
781 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
787static inline llvm::SmallVector<mlir::Value, 4>
788getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
789 llvm::SmallVector<mlir::Value, 4> baseVec;
790 for (
auto arg : args)
791 baseVec.push_back(
getBase(arg));
795static inline llvm::SmallVector<mlir::Type, 4>
796getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
797 llvm::SmallVector<mlir::Type, 4> typeVec;
798 for (
auto arg : args)
799 typeVec.push_back(arg.getType());
803mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
805 mlir::FunctionType libFuncType,
806 llvm::ArrayRef<mlir::Value> args);
809mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
811 mlir::FunctionType mathLibFuncType,
812 llvm::ArrayRef<mlir::Value> args);
815mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
817 mlir::FunctionType mathLibFuncType,
818 llvm::ArrayRef<mlir::Value> args);
820mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
823 mlir::FunctionType libFuncType,
824 llvm::ArrayRef<mlir::Value> args);
828std::optional<IntrinsicHandlerEntry>
830 std::optional<mlir::Type> resultType);
853 llvm::StringRef name,
854 mlir::FunctionType signature);
863mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
864 llvm::ArrayRef<mlir::Value> args);
867mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
868 llvm::ArrayRef<mlir::Value> args);
872mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
873 mlir::Type resultType, mlir::Value x, mlir::Value y);
877mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
878 mlir::Value x, mlir::Value y);
Definition AbstractConverter.h:87
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 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:1869
Extremum
Enums used to templatize and share lowering of MIN and MAX.
Definition IntrinsicCall.h:60
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition IntrinsicCall.cpp:8882
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:8939
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:8952
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:8947
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:1894
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:8910
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:8973
LowerIntrinsicArgAs
Definition IntrinsicCall.h:104
@ Addr
Definition IntrinsicCall.h:109
@ Inquired
Definition IntrinsicCall.h:115
@ Box
Lower argument to a box.
Definition IntrinsicCall.h:111
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
Definition IntrinsicCall.h:106
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:8923
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:8931
Define how a given intrinsic argument must be lowered.
Definition IntrinsicCall.h:119
bool handleDynamicOptional
Value:
Definition IntrinsicCall.h:131
This is shared by intrinsics and intrinsic module procedures.
Definition IntrinsicCall.h:573
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:575
Definition IntrinsicCall.h:566
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:763
Definition IntrinsicCall.h:581
bool outline
Definition IntrinsicCall.h:589
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2401
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:1906
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:4352
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2091
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:499
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2300
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2472
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4565
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2480
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2494
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:8791
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:2435
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:495
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3142
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3135
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4572
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:2106
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:8710
Definition IntrinsicCall.h:604
Definition IntrinsicCall.h:654
Definition IntrinsicCall.h:592