1 /*
2  * Copyright (c) 2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_GPUKERNELVARIABLETABLE
25 #define SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_GPUKERNELVARIABLETABLE
26 
27 #include "arm_compute/core/ITensorInfo.h"
28 #include "src/dynamic_fusion/sketch/gpu/GpuKernelArgument.h"
29 #include "support/Requires.h"
30 #include "support/StringSupport.h"
31 
32 #include <set>
33 #include <string>
34 #include <type_traits>
35 
36 namespace arm_compute
37 {
38 namespace experimental
39 {
40 namespace dynamic_fusion
41 {
42 class GpuKernelComponentGroup;
43 
44 /** A table of all the variables used in the kernel.
45  * Each kernel has exactly one variable table.
46  */
47 class GpuKernelVariableTable
48 {
49 public:
50     /** A tensor variable whose main purposes are:
51      *  - Hold the newly assigned @ref GpuKernelArgumentInfo for the associated tensor info
52      *  - Hold the generated variable name for the associated tensor info
53      */
54     struct TensorVariable
55     {
56     public:
57         TensorVariable()                       = default;
58         TensorVariable(const TensorVariable &) = default;
59         TensorVariable       &operator=(const TensorVariable &) = default;
60         ITensorInfo::Id       id{ ITensorInfo::invalid_tensor_id };
61         std::string           uniq_name{ "empty" }; // Unique name, also the final variable name used in the built code
62         GpuKernelArgumentInfo kernel_argument_info{};
has_valid_idTensorVariable63         bool                  has_valid_id() const
64         {
65             return id != ITensorInfo::invalid_tensor_id;
66         }
67     };
68     using VariableList = std::vector<TensorVariable>;
69 
70 public:
71     /** Declare a @ref TensorVariable for a corresponding tensor info.
72      *
73      * @param[in] comp_group    Component group the tensor belongs to
74      * @param[in] tensor        Tensor info with which the new variable is associated
75      * @param[in] argument_info Kernel argument information
76      * @param[in] alias         Alias for the variable. Will be used as part of the variable name
77      */
78     void declare_variable(const GpuKernelComponentGroup &comp_group, const ITensorInfo *tensor, GpuKernelArgumentInfo argument_info, const std::string &alias = "unnamed");
79     /** Get the @ref TensorVariable associated with @p tensor
80      *
81      * @param[in] tensor Tensor info to be queried
82      *
83      * @return TensorVariable
84      */
85     TensorVariable get_variable(const ITensorInfo *tensor) const;
86     /** Get the @ref TensorVariable list associated with @p tensors
87      * @note Empty tensors are skipped
88      *
89      * @param[in] tensors List of tensor infos to be queried
90      *
91      * @return VariableList
92      */
93     VariableList get_variable_list(const std::vector<const ITensorInfo *> &tensors) const;
94 
95 private:
96     std::map<ITensorInfo::Id, TensorVariable> _vars{};
97 };
98 
99 /** A tag value will substitute a tag in a string template during its instantiation */
100 struct TagVal
101 {
102     /** Default constructor */
103     TagVal() = default;
104     /** Construct a @ref TagVal from a @ref GpuKernelVariableTable::TensorVariable */
105     TagVal(const GpuKernelVariableTable::TensorVariable &var);
106     /** Construct a @ref TagVal from an integral type */
107     template <typename T, ARM_COMPUTE_REQUIRES_TA(std::is_integral<T>::value)>
TagValTagVal108     TagVal(T val)
109         : value{ support::cpp11::to_string(val) }
110     {
111     }
112     /** Construct a @ref TagVal from a string */
113     TagVal(const std::string &val);
114     /** Construct a @ref TagVal from a c-style string */
115     TagVal(const char *val);
116     /** Construct a @ref TagVal from a @ref DataType */
117     TagVal(const DataType &data_type);
118     /** Get the value of the TagVal as a converted string */
119     std::string value{};
120 };
121 
122 /** A tag used in a string template is a placeholder string to be substituted by real values during template instantiation */
123 using Tag = std::string;
124 
125 /** Tag lookup table. It is used to instantiate a string template */
126 using TagLUT = std::unordered_map<Tag, TagVal>;
127 
128 } // namespace dynamic_fusion
129 } // namespace experimental
130 } // namespace arm_compute
131 #endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_GPUKERNELVARIABLETABLE */
132