blob: 7eb6d2894a5c04a97a7c5cfa0335954ccbb64b4c [file] [log] [blame]
Viet-Hoa Do3389f532023-07-05 17:36:40 +01001/*
2 * Copyright (c) 2023 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
25#ifndef CKW_INCLUDE_CKW_KERNELWRITER_H
26#define CKW_INCLUDE_CKW_KERNELWRITER_H
27
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010028#include "ckw/TensorOperand.h"
Gunes Bayir3c776062023-07-12 14:50:56 +010029#include "ckw/TileOperand.h"
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +010030#include "ckw/types/ConvertPolicy.h"
31#include "ckw/types/Operators.h"
Gunes Bayirab0b7502023-07-11 14:57:36 +010032
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +010033#include <functional>
Viet-Hoa Do3389f532023-07-05 17:36:40 +010034#include <memory>
35#include <string>
36
37namespace ckw
38{
39
40class Kernel;
41
Gunes Bayirab0b7502023-07-11 14:57:36 +010042/** Forward Declerations */
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010043class TensorInfo;
Gunes Bayir47a396e2023-08-17 11:04:02 +010044class TensorSampler;
Gunes Bayirab0b7502023-07-11 14:57:36 +010045class TileInfo;
Gunes Bayir47a396e2023-08-17 11:04:02 +010046
Gunes Bayirab0b7502023-07-11 14:57:36 +010047enum class TargetArchitecture;
48enum class TargetLanguage;
49
Viet-Hoa Do3389f532023-07-05 17:36:40 +010050/** A kernel writer.
51 *
52 * This class is used to construct a new kernel by defining arguments, declaring variable and writing code.
53 *
54 * Use @ref KernelWriter::create_instance method to create the kernel writer for the specific target architecture and language.
55 *
56 * After having finished constructing the kernel, call @ref KernelWriter::emit_kernel to get the kernel object.
57 */
58class KernelWriter
59{
60public:
61 // =============================================================================================
62 // Construtors and destructor
63 // =============================================================================================
64
65 /** Initialize a new instance of @ref KernelWriter class for the specific architecture and language.
66 *
67 * Supported target architectures and languages:
68 *
69 * Architecture | Languages |
70 * ------------------------------|------------------------------|
71 * GpuArmMaliValhall | OpenCL |
72 *
73 * @param[in] architecture The architecture on which the kernel is executed.
74 * @param[in] language The language to write the kernel.
75 */
76 static std::unique_ptr<KernelWriter> create_instance(TargetArchitecture architecture, TargetLanguage language);
77
78 /** Destructor */
79 virtual ~KernelWriter();
80
81 // =============================================================================================
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +010082 // Data processing
83 // =============================================================================================
84
85 /** Write assignment statement: `<dst> = <src>;`.
86 *
87 * @param[in] dst The destination tile.
88 * @param[in] src The source tile.
89 */
90 virtual void op_assign(const TileOperand &dst, const TileOperand &src) = 0;
91
92 /** Write the cast statement: `<dst> = convert_<dst.type><policy>(<src>);`.
93 *
94 * @param[in] dst The destination tile.
95 * @param[in] src The source tile.
96 * @param[in] policy The policy governing the behavior of the cast.
97 */
98 virtual void op_cast(const TileOperand &dst, const TileOperand &src, ConvertPolicy policy) = 0;
99
100 /** Write the unary expression statement: `<dst> = <op> <src>;`.
101 *
102 * @param[in] dst The destination tile.
103 * @param[in] src The source tile.
104 * @param[in] op The unary operator.
105 */
106 virtual void op_unary(const TileOperand &dst, const TileOperand &src, UnaryOp op) = 0;
107
108 // =============================================================================================
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100109 // Misc
110 // =============================================================================================
111
112 /** Write the line comment in debug build.
Gunes Bayirab0b7502023-07-11 14:57:36 +0100113 *
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100114 * This function does not take effect on release build.
115 *
116 * The comment must only contain one line (i.e. no newline character is allowed).
117 *
118 * @param[in] text The comment to be written.
119 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100120 virtual void op_comment(const std::string &text) = 0;
121
122 /** Write the given raw code to kernel source code
123 * It's used to address the cases where the user needs to
124 * explicitly add a code where it's not (yet) supported by
125 * the kernel writer utility calls.
126 *
127 * @param[in] raw_code raw code to write as string
128 */
129 virtual void op_write_raw_code(const std::string &raw_code) = 0;
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100130
131 // =============================================================================================
132 // Code generation
133 // =============================================================================================
134
135 /** Emit the kernel object.
136 *
137 * @param[in] name The name of the kernel object to be generated.
138 */
139 virtual std::unique_ptr<Kernel> emit_kernel(const std::string &name) = 0;
Gunes Bayirab0b7502023-07-11 14:57:36 +0100140
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100141 // =============================================================================================
142 // Tensor and tile declaration
143 // =============================================================================================
144
145 /** Declare a tensor argument.
146 *
147 * @param[in] name The name of the tensor.
148 * @param[in] info The tensor info.
149 *
150 * @return The @ref TensorOperand object.
151 */
152 virtual TensorOperand declare_tensor_argument(const std::string &name, const TensorInfo &info) = 0;
153
Gunes Bayirab0b7502023-07-11 14:57:36 +0100154 /** Declare a tile given its name and tile info
155 *
156 * @param[in] name Name of the tile
157 * @param[in] tile_info Shape and data type of the tile
158 *
159 * @returns The created tile operand
160 */
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100161 virtual TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) = 0;
Gunes Bayirab0b7502023-07-11 14:57:36 +0100162
Gunes Bayir47a396e2023-08-17 11:04:02 +0100163 /** Load the data from the tensor memory to the tile using the sampling information.
164 *
165 * @param[in] tile_op The tile to be loaded.
166 * @param[in] tensor_op The tensor to be read.
167 * @param[in] sampler The tensor sampling information.
168 * @param[in] x x-coordinate
169 * @param[in] y y-coordinate
170 * @param[in] z z-coordinate
171 * @param[in] batch batch offset
172 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100173 virtual void op_load(
174 const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100175 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch) = 0;
176
177 /** Load the data from the tensor memory to the tile in a dilated way using the sampling information.
178 *
179 * Similar to @ref KernelWriter::op_load() and
180 *
181 * @param[in] dilation_x Dilation while reading in x-dimension
182 * @param[in] dilation_y Dilation while reading in y-dimension
183 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100184 virtual void op_load_dilated(
185 const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100186 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch,
187 const TileOperand &dilation_x, const TileOperand &dilation_y) = 0;
188
189 /** Store the data to the tensor memory from the tile using the sampling information.
190 *
191 * Similar to @ref KernelWriter::op_load()
192 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100193 virtual void op_store(
194 const TensorOperand &tensor_op, const TileOperand &tile_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100195 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch) = 0;
196
197 /** Store the data to the tensor memory from the tile in a dilated way using the sampling information.
198 *
199 * Similar to @ref KernelWriter::op_load_dilated()
200 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100201 virtual void op_store_dilated(
202 const TensorOperand &tensor_op, const TileOperand &tile_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100203 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch,
204 const TileOperand &dilation_x, const TileOperand &dilation_y) = 0;
205
Gunes Bayirab0b7502023-07-11 14:57:36 +0100206protected:
207 int32_t id_space() const;
208
Gunes Bayirab0b7502023-07-11 14:57:36 +0100209 /** Generate full variable name by prefixing it with id space */
210 std::string generate_full_name(const std::string &name) const;
211
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100212 /** Create a new tile operand referring to the specified tile object. */
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100213 static TileOperand create_tile_operand(ITile &tile);
214
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100215 /** Get the reference to tile object from the tile operand. */
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100216 static ITile &get_tile(const TileOperand &operand);
217
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100218 /** Create a new tensor operand from a tensor object. */
219 static TensorOperand create_tensor_operand(ITensor &tensor);
220
221 /** Get the reference to tensor object from the tensor operand. */
222 static ITensor &get_tensor(const TensorOperand &operand);
223
Gunes Bayirab0b7502023-07-11 14:57:36 +0100224private:
225 int32_t _id_space{ 0 };
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100226};
227
228} // namespace ckw
229
230#endif // CKW_INCLUDE_CKW_KERNELWRITER_H