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"
26class StatementContext;
42std::pair<fir::ExtendedValue, bool>
44 std::optional<mlir::Type> resultType,
51std::pair<fir::ExtendedValue, bool>
53 const IntrinsicHandlerEntry &,
54 std::optional<mlir::Type> resultType,
56 IntrinsicLoweringOptions options = {});
100struct IntrinsicLibrary {
105 : builder{builder}, loc{loc}, options{options} {}
106 IntrinsicLibrary() =
delete;
107 IntrinsicLibrary(
const IntrinsicLibrary &) =
delete;
112 std::pair<fir::ExtendedValue, bool>
113 genIntrinsicCall(llvm::StringRef name, std::optional<mlir::Type> resultType,
126 using RuntimeCallGenerator = std::function<mlir::Value(
129 getRuntimeCallGenerator(llvm::StringRef name,
130 mlir::FunctionType soughtFuncType);
141 mlir::Value, mlir::Value)>
163 template <mlir::arith::CmpIPredicate pred>
164 mlir::Value genBitwiseCompare(mlir::Type resultType,
172 template <mlir::arith::CmpIPredicate pred>
186 mlir::Value genErfcScaled(mlir::Type resultType,
193 template <mlir::arith::CmpIPredicate pred>
213 void genExecuteCommandLine(mlir::ArrayRef<fir::ExtendedValue> args);
215 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);
239 void genGetarg(mlir::ArrayRef<fir::ExtendedValue> args);
241 mlir::Value genGetGID(mlir::Type resultType,
244 mlir::Value genGetUID(mlir::Type resultType,
261 template <
bool isGet,
bool isModes>
270 mlir::Value genIeeeLogb(mlir::Type, mlir::ArrayRef<mlir::Value>);
271 template <
bool isMax,
bool isNum,
bool isMag>
273 template <mlir::arith::CmpFPredicate pred>
274 mlir::Value genIeeeQuietCompare(mlir::Type resultType,
279 template <
bool isFlag>
283 template <mlir::arith::CmpFPredicate pred>
284 mlir::Value genIeeeSignalingCompare(mlir::Type resultType,
295 template <mlir::arith::CmpIPredicate pred>
309 template <Fortran::runtime::io::Iostat value>
323 template <
typename Shift>
338 enum class NearestProc { Nearest, NextAfter, NextDown, NextUp };
339 template <NearestProc>
365 mlir::ArrayRef<fir::ExtendedValue>);
368 mlir::Value genRRSpacing(mlir::Type resultType,
378 mlir::ArrayRef<fir::ExtendedValue>);
384 mlir::Value genSetExponent(mlir::Type resultType,
388 template <
typename Shift>
397 mlir::Value genSpacing(mlir::Type resultType,
407 mlir::ArrayRef<fir::ExtendedValue> args);
441 template <
typename FN,
typename FD>
445 template <
typename FN,
typename FD,
typename FC>
448 llvm::StringRef errMsg,
449 mlir::Type resultType,
452 template <
typename FN,
typename FD>
454 mlir::Type resultType,
462 mlir::Value
genQNan(mlir::Type resultType);
467 using ExtendedGenerator =
decltype(&IntrinsicLibrary::genLenTrim);
468 using SubroutineGenerator =
decltype(&IntrinsicLibrary::genDateAndTime);
480 template <
typename GeneratorType>
482 mlir::Type resultType,
484 template <
typename GeneratorType>
486 outlineInExtendedWrapper(GeneratorType, llvm::StringRef name,
487 std::optional<mlir::Type> resultType,
490 template <
typename GeneratorType>
491 mlir::func::FuncOp getWrapper(GeneratorType, llvm::StringRef name,
493 bool loadRefArguments =
false);
496 template <
typename GeneratorType>
498 genElementalCall(GeneratorType, llvm::StringRef name, mlir::Type resultType,
503 mlir::Type resultType,
506 mlir::Type resultType,
509 mlir::Type resultType,
522 mlir::FunctionType signature);
526 mlir::Type resultType,
527 llvm::StringRef errMsg);
529 void setResultMustBeFreed() { resultMustBeFreed =
true; }
533 bool resultMustBeFreed =
false;
538 const char *name =
nullptr;
540 bool handleDynamicOptional =
false;
547 constexpr bool hasDefaultRules()
const {
return args[0].name ==
nullptr; }
554 IntrinsicLibrary::Generator generator;
557 bool isElemental =
true;
565 using Key = std::string_view;
567 constexpr operator Key()
const {
return key; }
571 llvm::StringRef symbol;
572 fir::runtime::FuncTypeBuilderFunc typeGenerator;
583 using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *,
587 using Key = std::string_view;
589 constexpr operator Key()
const {
return key; }
594 llvm::StringRef runtimeFunc;
595 FuncTypeBuilderFunc typeGenerator;
601 MathGeneratorTy funcGenerator;
605enum class ParamTypeId {
617static int getVecLen(mlir::Type eleTy) {
618 assert((mlir::isa<mlir::IntegerType>(eleTy) ||
619 mlir::isa<mlir::FloatType>(eleTy)) &&
620 "unsupported vector element type");
621 return 16 / (eleTy.getIntOrFloatBitWidth() / 8);
624template <ParamTypeId t,
int k>
627 static_assert(t != ParamTypeId::Integer || k == 1 || k == 2 || k == 4 ||
629 "Unsupported integer kind");
630 static_assert(t != ParamTypeId::Real || k == 4 || k == 8 || k == 10 ||
632 "Unsupported real kind");
633 static_assert(t != ParamTypeId::Complex || k == 2 || k == 3 || k == 4 ||
634 k == 8 || k == 10 || k == 16,
635 "Unsupported complex kind");
637 static const ParamTypeId ty = t;
638 static const int kind = k;
663static inline mlir::Type getTypeHelper(mlir::MLIRContext *context,
665 ParamTypeId typeId,
int kind) {
669 case ParamTypeId::Void:
670 llvm::report_fatal_error(
"can not get type of void");
672 case ParamTypeId::Address:
674 assert(bits != 0 &&
"failed to convert address kind to integer bitsize");
675 r = fir::ReferenceType::get(mlir::IntegerType::get(context, bits));
677 case ParamTypeId::Integer:
678 case ParamTypeId::IntegerVector:
680 assert(bits != 0 &&
"failed to convert kind to integer bitsize");
681 r = mlir::IntegerType::get(context, bits);
683 case ParamTypeId::UnsignedVector:
685 assert(bits != 0 &&
"failed to convert kind to unsigned bitsize");
686 r = mlir::IntegerType::get(context, bits, mlir::IntegerType::Unsigned);
688 case ParamTypeId::Real:
689 case ParamTypeId::RealVector:
692 case ParamTypeId::Complex:
693 r = mlir::ComplexType::get(builder.
getRealType(kind));
698 case ParamTypeId::Void:
699 case ParamTypeId::Address:
700 case ParamTypeId::Integer:
701 case ParamTypeId::Real:
702 case ParamTypeId::Complex:
704 case ParamTypeId::IntegerVector:
705 case ParamTypeId::UnsignedVector:
706 case ParamTypeId::RealVector:
708 r = fir::VectorType::get(getVecLen(r), r);
715template <
typename TyR,
typename... ArgTys>
716static inline mlir::FunctionType genFuncType(mlir::MLIRContext *context,
717 fir::FirOpBuilder &builder) {
718 llvm::SmallVector<ParamTypeId> argTys = {ArgTys::ty...};
719 llvm::SmallVector<int> argKinds = {ArgTys::kind...};
720 llvm::SmallVector<mlir::Type> argTypes;
722 for (
size_t i = 0; i < argTys.size(); ++i) {
723 argTypes.push_back(getTypeHelper(context, builder, argTys[i], argKinds[i]));
726 if (TyR::ty == ParamTypeId::Void)
727 return mlir::FunctionType::get(context, argTypes, {});
729 auto resType = getTypeHelper(context, builder, TyR::ty, TyR::kind);
730 return mlir::FunctionType::get(context, argTypes, {resType});
734struct IntrinsicHandlerEntry {
735 using RuntimeGeneratorRange =
736 std::pair<const MathOperation *, const MathOperation *>;
738 assert(handler &&
"handler must not be nullptr");
740 IntrinsicHandlerEntry(RuntimeGeneratorRange rt) : entry{rt} {};
742 std::variant<const IntrinsicHandler *, RuntimeGeneratorRange> entry;
748static inline mlir::Type getConvertedElementType(mlir::MLIRContext *context,
750 if (mlir::isa<mlir::IntegerType>(eleTy) && !eleTy.isSignlessInteger()) {
751 const auto intTy{mlir::dyn_cast<mlir::IntegerType>(eleTy)};
752 auto newEleTy{mlir::IntegerType::get(context, intTy.getWidth())};
758static inline llvm::SmallVector<mlir::Value, 4>
759getBasesForArgs(llvm::ArrayRef<fir::ExtendedValue> args) {
760 llvm::SmallVector<mlir::Value, 4> baseVec;
761 for (
auto arg : args)
762 baseVec.push_back(
getBase(arg));
766static inline llvm::SmallVector<mlir::Type, 4>
767getTypesForArgs(llvm::ArrayRef<mlir::Value> args) {
768 llvm::SmallVector<mlir::Type, 4> typeVec;
769 for (
auto arg : args)
770 typeVec.push_back(arg.getType());
774mlir::Value genLibCall(fir::FirOpBuilder &builder, mlir::Location loc,
776 mlir::FunctionType libFuncType,
777 llvm::ArrayRef<mlir::Value> args);
780mlir::Value genMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
782 mlir::FunctionType mathLibFuncType,
783 llvm::ArrayRef<mlir::Value> args);
786mlir::Value genComplexMathOp(fir::FirOpBuilder &builder, mlir::Location loc,
788 mlir::FunctionType mathLibFuncType,
789 llvm::ArrayRef<mlir::Value> args);
791mlir::Value genLibSplitComplexArgsCall(fir::FirOpBuilder &builder,
794 mlir::FunctionType libFuncType,
795 llvm::ArrayRef<mlir::Value> args);
799std::optional<IntrinsicHandlerEntry>
801 std::optional<mlir::Type> resultType);
824 llvm::StringRef name,
825 mlir::FunctionType signature);
834mlir::Value
genMax(fir::FirOpBuilder &, mlir::Location,
835 llvm::ArrayRef<mlir::Value> args);
838mlir::Value
genMin(fir::FirOpBuilder &, mlir::Location,
839 llvm::ArrayRef<mlir::Value> args);
843mlir::Value
genDivC(fir::FirOpBuilder &builder, mlir::Location loc,
844 mlir::Type resultType, mlir::Value x, mlir::Value y);
848mlir::Value
genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType,
849 mlir::Value x, mlir::Value y);
Definition BoxValue.h:480
Definition FIRBuilder.h:59
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition FIRBuilder.h:126
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:362
Definition AbstractConverter.h:37
std::optional< IntrinsicHandlerEntry > lookupIntrinsicHandler(fir::FirOpBuilder &, llvm::StringRef intrinsicName, std::optional< mlir::Type > resultType)
Definition IntrinsicCall.cpp:1932
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(fir::FirOpBuilder &, mlir::Location, llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > args, IntrinsicLoweringOptions options={})
Definition IntrinsicCall.cpp:9414
const IntrinsicArgumentLoweringRules * getIntrinsicArgumentLowering(llvm::StringRef intrinsicName)
Definition IntrinsicCall.cpp:9373
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:9429
mlir::Value genPow(fir::FirOpBuilder &, mlir::Location, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9441
mlir::Value genDivC(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type resultType, mlir::Value x, mlir::Value y)
Definition IntrinsicCall.cpp:9436
fir::ExtendedValue getAbsentIntrinsicArgument()
Return place-holder for absent intrinsic arguments.
Definition IntrinsicCall.cpp:89
void crashOnMissingIntrinsic(mlir::Location loc, llvm::StringRef name)
Generate a TODO error message for an as yet unimplemented intrinsic.
Definition IntrinsicCall.cpp:1957
ArgLoweringRule lowerIntrinsicArgumentAs(const IntrinsicArgumentLoweringRules &, unsigned position)
Definition IntrinsicCall.cpp:9401
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:9462
LowerIntrinsicArgAs
Definition IntrinsicCall.h:60
@ Addr
Definition IntrinsicCall.h:65
@ Inquired
Definition IntrinsicCall.h:71
@ Box
Lower argument to a box.
Definition IntrinsicCall.h:67
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
Definition IntrinsicCall.h:62
mlir::Value genMax(fir::FirOpBuilder &, mlir::Location, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:9422
Define how a given intrinsic argument must be lowered.
Definition IntrinsicCall.h:75
bool handleDynamicOptional
Value:
Definition IntrinsicCall.h:87
This is shared by intrinsics and intrinsic module procedures.
Definition IntrinsicCall.h:544
IntrinsicDummyArgument args[7]
There is no more than 7 non repeated arguments in Fortran intrinsics.
Definition IntrinsicCall.h:546
Definition IntrinsicCall.h:537
Entry into the tables describing how an intrinsic must be lowered.
Definition IntrinsicCall.h:734
Definition IntrinsicCall.h:552
bool outline
Definition IntrinsicCall.h:560
mlir::SymbolRefAttr getUnrestrictedIntrinsicSymbolRefAttr(llvm::StringRef name, mlir::FunctionType signature)
Definition IntrinsicCall.cpp:2464
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:1969
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:4556
std::pair< fir::ExtendedValue, bool > genIntrinsicCall(llvm::StringRef name, std::optional< mlir::Type > resultType, llvm::ArrayRef< fir::ExtendedValue > arg)
Definition IntrinsicCall.cpp:2154
decltype(&IntrinsicLibrary::genEtime) DualGenerator
The generator for intrinsic that has both function and subroutine form.
Definition IntrinsicCall.h:470
mlir::Value outlineInWrapper(GeneratorType, llvm::StringRef name, mlir::Type resultType, llvm::ArrayRef< mlir::Value > args)
Definition IntrinsicCall.cpp:2363
mlir::Value genRuntimeCall(llvm::StringRef name, mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2535
mlir::Value genQNan(mlir::Type resultType)
Generate a quiet NaN of a given floating point type.
Definition IntrinsicCall.cpp:4778
mlir::Value genConversion(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2543
mlir::Value genAbs(mlir::Type, llvm::ArrayRef< mlir::Value >)
Definition IntrinsicCall.cpp:2557
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:9285
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:2498
decltype(&IntrinsicLibrary::genAbs) ElementalGenerator
Definition IntrinsicCall.h:466
fir::ExtendedValue genCAssociatedCPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_PTR [, C_PTR])
Definition IntrinsicCall.cpp:3202
fir::ExtendedValue genCAssociatedCFunPtr(mlir::Type, llvm::ArrayRef< fir::ExtendedValue >)
C_ASSOCIATED (C_FUNPTR [, C_FUNPTR])
Definition IntrinsicCall.cpp:3195
void genRaiseExcept(int excepts, mlir::Value cond={})
Definition IntrinsicCall.cpp:4785
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:2169
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:9204
Options controlling intrinsic lowering that depend on front-end state.
Definition IntrinsicCall.h:30
bool noPPCNativeVecElemOrder
Whether to avoid using the native vector element order on PPC targets.
Definition IntrinsicCall.h:34
bool coarrayEnabled
Whether multi-image (coarray) features are enabled (e.g. via -fcoarray).
Definition IntrinsicCall.h:32
Definition IntrinsicCall.h:575
Definition IntrinsicCall.h:625
Definition IntrinsicCall.h:563