xref: /aosp_15_r20/external/ComputeLibrary/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateWriter.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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_CL_CLTEMPLATEWRITER
25 #define SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATEWRITER
26 
27 #include "src/dynamic_fusion/sketch/gpu/GpuKernelArgument.h"
28 #include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
29 #include "src/dynamic_fusion/sketch/gpu/IGpuKernelWriter.h"
30 #include "src/dynamic_fusion/sketch/gpu/template_writer/GpuKernelVariableTable.h"
31 
32 #include <map>
33 
34 namespace arm_compute
35 {
36 namespace experimental
37 {
38 namespace dynamic_fusion
39 {
40 /** Use a templated-string-based method to write kernel code
41  *  It stitches the component code templates together based on the valid fusion configuration.
42  *  It then instantiates the actual kernel code from the template and the generated tag lookup table.
43  */
44 class ClTemplateWriter : public IGpuKernelWriter
45 {
46 public:
47     /** Instantiates a kernel code string from the kernel code template
48      * @note: some tags can be unused since they could be used only for the macros, or only for the component code
49      *
50      * @param[in] code_template Kernel code template
51      * @param[in] tags          Tag lookup table
52      *
53      * @return std::string  Instantiated kernel string
54      */
55     static std::string replace_tags(const std::string &code_template, const TagLUT &tags);
56     /** Default constructor */
57     ClTemplateWriter() = default;
58     /** Constructor
59      *
60      * @param[in] components Kernel component group from which the kernel will be generated
61      */
62     ClTemplateWriter(const GpuKernelComponentGroup &components);
63     /** Destructor */
64     ~ClTemplateWriter() override;
65     /** Generate kernel name */
66     std::string get_name() override;
67     /** Generate kernel code */
68     std::string get_code() override;
69     /** Generate build options */
70     CLBuildOptions get_build_options() override;
71     /** Generate config id string of the entire kernel. This is used for tuning */
72     std::string get_config_id() override;
73     /** Generate execution window */
74     Window get_window() const override;
75     /** Get the kernel argument lists of the kernel*/
76     std::map<ITensorInfo::Id, GpuKernelArgument> get_tensors() override;
77 
78 private:
79     std::string write_kernel_name() const;
80     std::string write_code();
81     std::string write_global_section() const;
82     std::string write_argument_declaration(const GpuKernelVariableTable::TensorVariable &var) const;
83     std::string write_kernel_signature(const GpuKernelVariableTable::VariableList &argument_list) const;
84 
85 private:
86     GpuKernelComponentGroup _components{};
87     GpuKernelVariableTable  _vtable{};
88 };
89 } // namespace dynamic_fusion
90 } // namespace experimental
91 } // namespace arm_compute
92 #endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATEWRITER */
93