FLANG

intrinsics-library.h
1 //===-- include/flang/Evaluate/intrinsics-library.h -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef FORTRAN_EVALUATE_INTRINSICS_LIBRARY_H_
10 #define FORTRAN_EVALUATE_INTRINSICS_LIBRARY_H_
11
12 // Defines structures to be used in F18 for folding intrinsic function with host
13 // runtime libraries. To avoid unnecessary header circular dependencies, the
14 // actual implementation of the templatized member function are defined in
15 // intrinsics-library-templates.h The header at hand is meant to be included by
16 // files that need to define intrinsic runtime data structure but that do not
17 // use them directly. To actually use the runtime data structures,
18 // intrinsics-library-templates.h must be included.
19
20 #include <functional>
21 #include <map>
22 #include <optional>
23 #include <string>
24 #include <vector>
25
26 namespace Fortran::evaluate {
27 class FoldingContext;
28
29 using TypeCode = unsigned char;
30
31 template <typename TR, typename... TA> using FuncPointer = TR (*)(TA...);
32 // This specific type signature prevents GCC complaining about function casts.
33 using GenericFunctionPointer = void (*)(void);
34
35 enum class PassBy { Ref, Val };
36 template <typename TA, PassBy Pass = PassBy::Ref> struct ArgumentInfo {
37 using Type = TA;
38 static constexpr PassBy pass{Pass};
39 };
40
41 template <typename TR, typename... ArgInfo> struct Signature {
42 // Note valid template argument are of form
43 //<TR, ArgumentInfo<TA, PassBy>...> where TA and TR belong to RuntimeTypes.
44 // RuntimeTypes is a type union defined in intrinsics-library-templates.h to
45 // avoid circular dependencies. Argument of type void cannot be passed by
46 // value. So far TR cannot be a pointer.
47 const std::string name;
48 };
49
50 struct IntrinsicProcedureRuntimeDescription {
51 const std::string name;
52 const TypeCode returnType;
53 const std::vector<TypeCode> argumentsType;
54 const std::vector<PassBy> argumentsPassedBy;
55 const bool isElemental;
56 const GenericFunctionPointer callable;
57 // Construct from description using host independent types (RuntimeTypes)
58 template <typename TR, typename... ArgInfo>
59 IntrinsicProcedureRuntimeDescription(
60 const Signature<TR, ArgInfo...> &signature, bool isElemental = false);
61 };
62
63 // HostRuntimeIntrinsicProcedure allows host runtime function to be called for
64 // constant folding.
65 struct HostRuntimeIntrinsicProcedure : IntrinsicProcedureRuntimeDescription {
66 // Construct from runtime pointer with host types (float, double....)
67 template <typename HostTR, typename... HostTA>
68 HostRuntimeIntrinsicProcedure(const std::string &name,
69 FuncPointer<HostTR, HostTA...> func, bool isElemental = false);
70 HostRuntimeIntrinsicProcedure(
71 const IntrinsicProcedureRuntimeDescription &rteProc,
72 GenericFunctionPointer handle)
73 : IntrinsicProcedureRuntimeDescription{rteProc}, handle{handle} {}
74 GenericFunctionPointer handle;
75 };
76
77 // Defines a wrapper type that indirects calls to host runtime functions.
78 // Valid ConstantContainer are Scalar (only for elementals) and Constant.
79 template <template <typename> typename ConstantContainer, typename TR,
80 typename... TA>
81 using HostProcedureWrapper = std::function<ConstantContainer<TR>(
82 FoldingContext &, ConstantContainer<TA>...)>;
83
84 // HostIntrinsicProceduresLibrary is a data structure that holds
85 // HostRuntimeIntrinsicProcedure elements. It is meant for constant folding.
86 // When queried for an intrinsic procedure, it can return a callable object that
87 // implements this intrinsic if a host runtime function pointer for this
88 // intrinsic was added to its data structure.
89 class HostIntrinsicProceduresLibrary {
90 public:
91 HostIntrinsicProceduresLibrary();
92 void AddProcedure(HostRuntimeIntrinsicProcedure &&sym) {
93 const std::string name{sym.name};
94 procedures_.insert(std::make_pair(name, std::move(sym)));
95 }
96 bool HasEquivalentProcedure(
97 const IntrinsicProcedureRuntimeDescription &sym) const;
98 template <template <typename> typename ConstantContainer, typename TR,
99 typename... TA>
100 std::optional<HostProcedureWrapper<ConstantContainer, TR, TA...>>
101 GetHostProcedureWrapper(const std::string &name) const;
102
103 private:
104 std::multimap<std::string, const HostRuntimeIntrinsicProcedure> procedures_;
105 };
106
107 } // namespace Fortran::evaluate
108 #endif // FORTRAN_EVALUATE_INTRINSICS_LIBRARY_H_
Fortran::evaluate::HostIntrinsicProceduresLibrary
Definition: intrinsics-library.h:89
Fortran::evaluate::IntrinsicProcedureRuntimeDescription
Definition: intrinsics-library.h:50
Fortran::evaluate::FoldingContext
Definition: common.h:209
Fortran::evaluate::HostRuntimeIntrinsicProcedure
Definition: intrinsics-library.h:65
Fortran::evaluate::ArgumentInfo
Definition: intrinsics-library.h:36
Fortran::evaluate::Signature
Definition: intrinsics-library.h:41