|
10 | 10 | #define FORTRAN_EVALUATE_INTRINSICS_LIBRARY_H_
|
11 | 11 |
|
12 | 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. |
| 13 | +// runtime libraries. |
19 | 14 |
|
20 | 15 | #include <functional>
|
21 |
| -#include <map> |
22 | 16 | #include <optional>
|
23 | 17 | #include <string>
|
24 | 18 | #include <vector>
|
25 | 19 |
|
26 | 20 | namespace Fortran::evaluate {
|
27 | 21 | 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 |
| - |
| 22 | +class DynamicType; |
| 23 | +struct SomeType; |
| 24 | +template <typename> class Expr; |
| 25 | + |
| 26 | +// Define a callable type that is used to fold scalar intrinsic function using |
| 27 | +// host runtime. These callables are responsible for the conversions between |
| 28 | +// host types and Fortran abstract types (Scalar<T>). They also deal with |
| 29 | +// floating point environment (To set it up to match the Fortran compiling |
| 30 | +// options and to clean it up after the call). Floating point errors are |
| 31 | +// reported to the FoldingContext. For 16bits float types, 32bits float host |
| 32 | +// runtime plus conversions may be used to build the host wrappers if no 16bits |
| 33 | +// runtime is available. IEEE 128bits float may also be used for x87 float. |
| 34 | +// Potential conversion overflows are reported by the HostRuntimeWrapper in the |
| 35 | +// FoldingContext. |
| 36 | +using HostRuntimeWrapper = std::function<Expr<SomeType>( |
| 37 | + FoldingContext &, std::vector<Expr<SomeType>> &&)>; |
| 38 | + |
| 39 | +// Returns the folder using host runtime given the intrinsic function name, |
| 40 | +// result and argument types. Nullopt if no host runtime is available for such |
| 41 | +// intrinsic function. |
| 42 | +std::optional<HostRuntimeWrapper> GetHostRuntimeWrapper(const std::string &name, |
| 43 | + DynamicType resultType, const std::vector<DynamicType> &argTypes); |
107 | 44 | } // namespace Fortran::evaluate
|
108 | 45 | #endif // FORTRAN_EVALUATE_INTRINSICS_LIBRARY_H_
|
0 commit comments