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)>
199 template <mlir::arith::CmpIPredicate pred>
200 mlir::Value genBitwiseCompare(mlir::Type resultType,
206 template <mlir::arith::CmpIPredicate pred>
219 mlir::Value genErfcScaled(mlir::Type resultType,
225 template <mlir::arith::CmpIPredicate pred>
238 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
240 mlir::ArrayRef<fir::ExtendedValue> args);
244 template <Extremum, ExtremumBehavior>
247 mlir::Value genFraction(mlir::Type resultType,
248 mlir::ArrayRef<mlir::Value> args);
249 void genFree(mlir::ArrayRef<fir::ExtendedValue> args);
252 void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
253 mlir::Value genGetPID(mlir::Type resultType,
255 void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
257 mlir::Value genGetGID(mlir::Type resultType,
259 mlir::Value genGetUID(mlir::Type resultType,
273 template <
bool isGet,
bool isModes>
282 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
283 template <
bool isMax,
bool isNum,
bool isMag>
285 template <mlir::arith::CmpFPredicate pred>
286 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
291 template <
bool isFlag>
295 template <mlir::arith::CmpFPredicate pred>
296 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
304 template <mlir::arith::CmpIPredicate pred>
314 template <Fortran::runtime::io::Iostat value>
326 template <
typename Shift>
341 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
342 template <NearestProc>
361 mlir::ArrayRef<fir::ExtendedValue>);
364 mlir::Value genRRSpacing(mlir::Type resultType,
371 mlir::ArrayRef<fir::ExtendedValue>);
377 mlir::Value genSetExponent(mlir::Type resultType,
381 template <
typename Shift>
388 mlir::Value genSpacing(mlir::Type resultType,
401 mlir::ArrayRef<fir::ExtendedValue> args);
427 template <
typename FN,
typename FD>
431 template <
typename FN,
typename FD,
typename FC>
434 llvm::StringRef errMsg,
435 mlir::Type resultType,
438 template <
typename FN,
typename FD>
440 mlir::Type resultType,
448 mlir::Value
genQNan(mlir::Type resultType);
453 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
454 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
466 template <
typename GeneratorType>
468 mlir::Type resultType,
470 template <
typename GeneratorType>
472 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
473 std::optional<mlir::Type> resultType,
476 template <
typename GeneratorType>
477 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
479 bool loadRefArguments =
false);
482 template <
typename GeneratorType>
484 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
489 mlir::Type resultType,
492 mlir::Type resultType,
495 mlir::Type resultType,
508 mlir::FunctionType signature);
512 mlir::Type resultType,
513 llvm::StringRef errMsg);
515 void setResultMustBeFreed() { resultMustBeFreed =
true; }
519 bool resultMustBeFreed =
false;
524 const char *name =
nullptr;
526 bool handleDynamicOptional =
false;
533 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
540 IntrinsicLibrary::Generator generator;
543 bool isElemental =
true;
551 using Key = std::string_view;
553 constexpr operator Key()
const {
return key; }
557 llvm::StringRef symbol;
558 fir::runtime::FuncTypeBuilderFunc typeGenerator;
569 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
573 using Key = std::string_view;
575 constexpr operator Key()
const {
return key; }
580 llvm::StringRef runtimeFunc;
581 FuncTypeBuilderFunc typeGenerator;
587 MathGeneratorTy funcGenerator;
591enum class ParamTypeId {
603static int getVecLen(mlir::Type eleTy) {
604 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
605 mlir::isa<mlir::FloatType>(eleTy)) &&
606 "unsupported vector element type");
607 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
610template <ParamTypeId t,
int k>
613 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
615 "Unsupported integer kind");
616 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
618 "Unsupported real kind");
619 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
620 k == 8 || k == 10 || k == 16,
621 "Unsupported complex kind");
623 static const ParamTypeId ty = t;
624 static const int kind = k;
649static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
651 ParamTypeId typeId,
int kind) {
655 case ParamTypeId::Void:
656 llvm::report_fatal_error(
"can not get type of void");
658 case ParamTypeId::Address:
660 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
661 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
663 case ParamTypeId::Integer:
664 case ParamTypeId::IntegerVector:
666 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
667 r = mlir::IntegerType::get(context, bits);
669 case ParamTypeId::UnsignedVector:
671 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
672 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
674 case ParamTypeId::Real:
675 case ParamTypeId::RealVector:
678 case ParamTypeId::Complex:
679 r = mlir::ComplexType::get(builder.
getRealType(kind));
684 case ParamTypeId::Void:
685 case ParamTypeId::Address:
686 case ParamTypeId::Integer:
687 case ParamTypeId::Real:
688 case ParamTypeId::Complex:
690 case ParamTypeId::IntegerVector:
691 case ParamTypeId::UnsignedVector:
692 case ParamTypeId::RealVector:
694 r = fir::VectorType::get(getVecLen(r), r);
701template <
typename TyR,
typename... ArgTys>
702static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
708 for (
size_t i = 0; i < argTys.size(); ++i) {
709 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
712 if (TyR::ty == ParamTypeId::Void)
713 return mlir::FunctionType::get(context, argTypes, std::nullopt);
715 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
716 return mlir::FunctionType::get(context, argTypes, {resType});
721 using RuntimeGeneratorRange =
722 std::pair<const MathOperation *, const MathOperation *>;
724 assert(handler &&
"handler must not be nullptr");
728 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
734static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
736 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
737 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
738 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
747 for (
auto arg : args)
748 baseVec.push_back(
getBase(arg));
755 for (
auto arg : args)
756 typeVec.push_back(arg.getType());
761 const MathOperation &mathOp,
762 mlir::FunctionType libFuncType,
767 const MathOperation &mathOp,
768 mlir::FunctionType mathLibFuncType,
773 const MathOperation &mathOp,
774 mlir::FunctionType mathLibFuncType,
779 const MathOperation &mathOp,
780 mlir::FunctionType libFuncType,
785std::optional<IntrinsicHandlerEntry>
787 std::optional<mlir::Type> resultType);
794const IntrinsicArgumentLoweringRules *
810 llvm::StringRef name,
811 mlir::FunctionType signature);
830 mlir::Type resultType, mlir::Value x, mlir::Value y);
835 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:1724
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:8121
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:8175
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8188
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition: IntrinsicCall.cpp:8183
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:1746
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition: IntrinsicCall.cpp:8146
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:8201
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:8159
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:8167
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:530
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition: IntrinsicCall.h:532
Definition: IntrinsicCall.h:523
Entry into the tables describing how an intrinsic must be lowered.
Definition: IntrinsicCall.h:720
Definition: IntrinsicCall.h:538
bool outline
Definition: IntrinsicCall.h:546
Definition: IntrinsicCall.h:136
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition: IntrinsicCall.cpp:2252
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:1758
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:3904
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition: IntrinsicCall.cpp:1942
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition: IntrinsicCall.h:456
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition: IntrinsicCall.cpp:2151
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2323
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition: IntrinsicCall.cpp:4117
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2331
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition: IntrinsicCall.cpp:2345
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:8030
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:2286
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition: IntrinsicCall.h:452
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition: IntrinsicCall.cpp:2963
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition: IntrinsicCall.cpp:2956
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition: IntrinsicCall.cpp:4124
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:1957
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:7949
Definition: IntrinsicCall.h:561
Definition: IntrinsicCall.h:611
Definition: IntrinsicCall.h:549