blob: d59867fa6fe3125d82060753f4f56fded0b8609a [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.
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100103 * @param[in] op The unary operator.
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100104 * @param[in] src The source tile.
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100105 */
Viet-Hoa Do34b6c3a2023-08-22 11:11:23 +0100106 virtual void op_unary(const TileOperand &dst, UnaryOp op, const TileOperand &src) = 0;
107
108 /** Write the binary expression statement: `<dst> = <op>(<first>, <second>);`.
109 *
110 * @param[in] dst The destination tile.
111 * @param[in] op The binary operator.
112 * @param[in] first The first source tile.
113 * @param[in] second The second source tile.
114 */
115 virtual void op_binary(const TileOperand &dst, BinaryOp op, const TileOperand &first, const TileOperand &second) = 0;
116
117 /** Write ternary expression statement: `<dst> = <op>(<first>, <second>, <third>);`.
118 *
119 * @param[in] dst The destination tile.
120 * @param[in] op The ternary operator.
121 * @param[in] first The first source tile.
122 * @param[in] second The second source tile.
123 * @param[in] third The third source tile.
124 */
125 virtual void op_ternary(const TileOperand &dst, TernaryOp op, const TileOperand &first, const TileOperand &second, const TileOperand &third) = 0;
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100126
127 // =============================================================================================
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100128 // Misc
129 // =============================================================================================
130
131 /** Write the line comment in debug build.
Gunes Bayirab0b7502023-07-11 14:57:36 +0100132 *
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100133 * This function does not take effect on release build.
134 *
135 * The comment must only contain one line (i.e. no newline character is allowed).
136 *
137 * @param[in] text The comment to be written.
138 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100139 virtual void op_comment(const std::string &text) = 0;
140
141 /** Write the given raw code to kernel source code
142 * It's used to address the cases where the user needs to
143 * explicitly add a code where it's not (yet) supported by
144 * the kernel writer utility calls.
145 *
146 * @param[in] raw_code raw code to write as string
147 */
148 virtual void op_write_raw_code(const std::string &raw_code) = 0;
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100149
150 // =============================================================================================
151 // Code generation
152 // =============================================================================================
153
154 /** Emit the kernel object.
155 *
156 * @param[in] name The name of the kernel object to be generated.
157 */
158 virtual std::unique_ptr<Kernel> emit_kernel(const std::string &name) = 0;
Gunes Bayirab0b7502023-07-11 14:57:36 +0100159
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100160 // =============================================================================================
161 // Tensor and tile declaration
162 // =============================================================================================
163
164 /** Declare a tensor argument.
165 *
166 * @param[in] name The name of the tensor.
167 * @param[in] info The tensor info.
168 *
169 * @return The @ref TensorOperand object.
170 */
171 virtual TensorOperand declare_tensor_argument(const std::string &name, const TensorInfo &info) = 0;
172
Gunes Bayirab0b7502023-07-11 14:57:36 +0100173 /** Declare a tile given its name and tile info
174 *
175 * @param[in] name Name of the tile
176 * @param[in] tile_info Shape and data type of the tile
177 *
178 * @returns The created tile operand
179 */
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100180 virtual TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) = 0;
Gunes Bayirab0b7502023-07-11 14:57:36 +0100181
Gunes Bayir47a396e2023-08-17 11:04:02 +0100182 /** Load the data from the tensor memory to the tile using the sampling information.
183 *
184 * @param[in] tile_op The tile to be loaded.
185 * @param[in] tensor_op The tensor to be read.
186 * @param[in] sampler The tensor sampling information.
187 * @param[in] x x-coordinate
188 * @param[in] y y-coordinate
189 * @param[in] z z-coordinate
190 * @param[in] batch batch offset
191 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100192 virtual void op_load(
193 const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100194 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch) = 0;
195
196 /** Load the data from the tensor memory to the tile in a dilated way using the sampling information.
197 *
198 * Similar to @ref KernelWriter::op_load() and
199 *
200 * @param[in] dilation_x Dilation while reading in x-dimension
201 * @param[in] dilation_y Dilation while reading in y-dimension
202 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100203 virtual void op_load_dilated(
204 const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100205 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch,
206 const TileOperand &dilation_x, const TileOperand &dilation_y) = 0;
207
208 /** Store the data to the tensor memory from the tile using the sampling information.
209 *
210 * Similar to @ref KernelWriter::op_load()
211 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100212 virtual void op_store(
213 const TensorOperand &tensor_op, const TileOperand &tile_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100214 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch) = 0;
215
216 /** Store the data to the tensor memory from the tile in a dilated way using the sampling information.
217 *
218 * Similar to @ref KernelWriter::op_load_dilated()
219 */
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +0100220 virtual void op_store_dilated(
221 const TensorOperand &tensor_op, const TileOperand &tile_op, TensorSampler &sampler,
Gunes Bayir47a396e2023-08-17 11:04:02 +0100222 const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch,
223 const TileOperand &dilation_x, const TileOperand &dilation_y) = 0;
224
Gunes Bayirab0b7502023-07-11 14:57:36 +0100225protected:
226 int32_t id_space() const;
227
Gunes Bayirab0b7502023-07-11 14:57:36 +0100228 /** Generate full variable name by prefixing it with id space */
229 std::string generate_full_name(const std::string &name) const;
230
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100231 /** Create a new tile operand referring to the specified tile object. */
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100232 static TileOperand create_tile_operand(ITile &tile);
233
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100234 /** Get the reference to tile object from the tile operand. */
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100235 static ITile &get_tile(const TileOperand &operand);
236
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100237 /** Create a new tensor operand from a tensor object. */
238 static TensorOperand create_tensor_operand(ITensor &tensor);
239
240 /** Get the reference to tensor object from the tensor operand. */
241 static ITensor &get_tensor(const TensorOperand &operand);
242
Gunes Bayirab0b7502023-07-11 14:57:36 +0100243private:
244 int32_t _id_space{ 0 };
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100245};
246
247} // namespace ckw
248
249#endif // CKW_INCLUDE_CKW_KERNELWRITER_H