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,
237 template <mlir::arith::CmpIPredicate pred>
257 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
259 mlir::ArrayRef<fir::ExtendedValue> args);
263 template <Extremum, ExtremumBehavior>
269 mlir::Value genFraction(mlir::Type resultType,
270 mlir::ArrayRef<mlir::Value> args);
271 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
273 mlir::ArrayRef<fir::ExtendedValue> args);
275 mlir::ArrayRef<fir::ExtendedValue> args);
278 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
279 mlir::Value genGetPID(mlir::Type resultType,
281 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
283 mlir::Value genGetGID(mlir::Type resultType,
286 mlir::Value genGetUID(mlir::Type resultType,
302 template <
bool isGet,
bool isModes>
311 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
312 template <
bool isMax,
bool isNum,
bool isMag>
314 template <mlir::arith::CmpFPredicate pred>
315 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
320 template <
bool isFlag>
324 template <mlir::arith::CmpFPredicate pred>
325 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
336 template <mlir::arith::CmpIPredicate pred>
348 template <Fortran::runtime::io::Iostat value>
360 template <
typename Shift>
375 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
376 template <NearestProc>
402 mlir::ArrayRef<fir::ExtendedValue>);
405 mlir::Value genRRSpacing(mlir::Type resultType,
415 mlir::ArrayRef<fir::ExtendedValue>);
421 mlir::Value genSetExponent(mlir::Type resultType,
425 template <
typename Shift>
434 mlir::Value genSpacing(mlir::Type resultType,
443 mlir::ArrayRef<fir::ExtendedValue> args);
475 template <
typename FN,
typename FD>
479 template <
typename FN,
typename FD,
typename FC>
482 llvm::StringRef errMsg,
483 mlir::Type resultType,
486 template <
typename FN,
typename FD>
488 mlir::Type resultType,
496 mlir::Value
genQNan(mlir::Type resultType);
501 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
502 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
514 template <
typename GeneratorType>
516 mlir::Type resultType,
518 template <
typename GeneratorType>
520 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
521 std::optional<mlir::Type> resultType,
524 template <
typename GeneratorType>
525 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
527 bool loadRefArguments =
false);
530 template <
typename GeneratorType>
532 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
537 mlir::Type resultType,
540 mlir::Type resultType,
543 mlir::Type resultType,
556 mlir::FunctionType signature);
560 mlir::Type resultType,
561 llvm::StringRef errMsg);
563 void setResultMustBeFreed() { resultMustBeFreed =
true; }
567 bool resultMustBeFreed =
false;
572 const char *name =
nullptr;
574 bool handleDynamicOptional =
false;
581 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
588 IntrinsicLibrary::Generator generator;
591 bool isElemental =
true;
599 using Key = std::string_view;
601 constexpr operator Key()
const {
return key; }
605 llvm::StringRef symbol;
606 fir::runtime::FuncTypeBuilderFunc typeGenerator;
617 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
621 using Key = std::string_view;
623 constexpr operator Key()
const {
return key; }
628 llvm::StringRef runtimeFunc;
629 FuncTypeBuilderFunc typeGenerator;
635 MathGeneratorTy funcGenerator;
639enum class ParamTypeId {
651static int getVecLen(mlir::Type eleTy) {
652 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
653 mlir::isa<mlir::FloatType>(eleTy)) &&
654 "unsupported vector element type");
655 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
658template <ParamTypeId t,
int k>
661 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
663 "Unsupported integer kind");
664 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
666 "Unsupported real kind");
667 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
668 k == 8 || k == 10 || k == 16,
669 "Unsupported complex kind");
671 static const ParamTypeId ty = t;
672 static const int kind = k;
697static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
699 ParamTypeId typeId,
int kind) {
703 case ParamTypeId::Void:
704 llvm::report_fatal_error(
"can not get type of void");
706 case ParamTypeId::Address:
708 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
709 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
711 case ParamTypeId::Integer:
712 case ParamTypeId::IntegerVector:
714 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
715 r = mlir::IntegerType::get(context, bits);
717 case ParamTypeId::UnsignedVector:
719 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
720 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
722 case ParamTypeId::Real:
723 case ParamTypeId::RealVector:
726 case ParamTypeId::Complex:
727 r = mlir::ComplexType::get(builder.
getRealType(kind));
732 case ParamTypeId::Void:
733 case ParamTypeId::Address:
734 case ParamTypeId::Integer:
735 case ParamTypeId::Real:
736 case ParamTypeId::Complex:
738 case ParamTypeId::IntegerVector:
739 case ParamTypeId::UnsignedVector:
740 case ParamTypeId::RealVector:
742 r = fir::VectorType::get(getVecLen(r), r);
749template <
typename TyR,
typename... ArgTys>
750static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
751 fir::FirOpBuilder &builder) {
752 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
753 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
754 llvm::SmallVector<mlir::Type> argTypes;
756 for (
size_t i = 0; i < argTys.size(); ++i) {
757 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
760 if (TyR::ty == ParamTypeId::Void)
761 return mlir::FunctionType::get(context, argTypes, {});
763 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
764 return mlir::FunctionType::get(context, argTypes, {resType});
768struct IntrinsicHandlerEntry {
769 using RuntimeGeneratorRange =
770 std::pair<const MathOperation *, const MathOperation *>;
772 assert(handler &&
"handler must not be nullptr");
774 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
776 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
782static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
784 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
785 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
786 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
792static inline llvm::SmallVector<mlir::Value, 4>
793getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
794 llvm::SmallVector<mlir::Value, 4> baseVec;
795 for (
auto arg : args)
796 baseVec.push_back(
getBase(arg));
800static inline llvm::SmallVector<mlir::Type, 4>
801getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
802 llvm::SmallVector<mlir::Type, 4> typeVec;
803 for (
auto arg : args)
804 typeVec.push_back(arg.getType());
808mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
810 mlir::FunctionType libFuncType,
811 llvm::ArrayRef<mlir::Value> args);
814mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
816 mlir::FunctionType mathLibFuncType,
817 llvm::ArrayRef<mlir::Value> args);
820mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
822 mlir::FunctionType mathLibFuncType,
823 llvm::ArrayRef<mlir::Value> args);
825mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
828 mlir::FunctionType libFuncType,
829 llvm::ArrayRef<mlir::Value> args);
833std::optional<IntrinsicHandlerEntry>
835 std::optional<mlir::Type> resultType);
858 llvm::StringRef name,
859 mlir::FunctionType signature);
868mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
869 llvm::ArrayRef<mlir::Value> args);
872mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
873 llvm::ArrayRef<mlir::Value> args);
877mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
878 mlir::Type resultType, mlir::Value x, mlir::Value y);
882mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
883 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: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:1887
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:9119
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:9176
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9189
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9184
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:1912
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:9147
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:9210
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:9160
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:9168
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:578
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:580
Definition IntrinsicCall.h:571
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:768
Definition IntrinsicCall.h:586
bool outline
Definition IntrinsicCall.h:594
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2419
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:1924
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:4486
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2109
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:504
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2318
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2490
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4699
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2498
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2512
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:9028
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:2453
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:500
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3160
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3153
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4706
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:2124
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:8947
Definition IntrinsicCall.h:609
Definition IntrinsicCall.h:659
Definition IntrinsicCall.h:597