blob: 99244fb1a970abc2bbd652bc966ace66994c7089 [file] [log] [blame]
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +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
28#include "ckw/Kernel.h"
29#include "ckw/TensorInfo.h"
30#include "ckw/TensorOperand.h"
31#include "ckw/TileInfo.h"
32#include "ckw/TileOperand.h"
33
34#include <memory>
35
36namespace ckw
37{
38
39namespace prototype
40{
41class GpuKernelWriterAttribute;
42class IGpuKernelWriter;
43} // namespace prototype
44
45/** Kernel writer. */
46class KernelWriter
47{
48public:
49 // =============================================================================================
50 // Constructors and destructor
51 // =============================================================================================
52
53 /** Initialize a new instance of kernel writer.
54 *
55 * @param[in] kernel The kernel to be written to.
56 */
57 explicit KernelWriter(Kernel &kernel);
58
59 /** Destructor */
60 ~KernelWriter();
61
62 /** No copy constructor. */
63 KernelWriter(const KernelWriter &) = delete;
64
65 /** No copy assignment. */
66 KernelWriter &operator=(const KernelWriter &) = delete;
67
68 // =============================================================================================
69 // Scope management
70 // =============================================================================================
71
72 /** Get the current ID space. */
73 int32_t id_space() const;
74
75 /** Set the current ID space. */
76 KernelWriter &id_space(int32_t id_space);
77
78 /** Switch to and return a new ID space. */
79 int32_t next_id_space();
80
81 // =============================================================================================
82 // Tensor and tile declaration
83 // =============================================================================================
84
85 /** Define a tensor argument.
86 *
87 * @param[in] name The name of the tensor.
88 * @param[in] info The tensor info.
89 *
90 * @return The @ref TensorOperand object.
91 */
92 TensorOperand &create_tensor_argument(const char *name, const TensorInfo &info);
93
94 /** Define a compile-time constant scalar argument.
95 *
96 * @param[in] name The name of the tile.
97 * @param[in] value The value of the tile.
98 *
99 * @return The @ref TileOperand object.
100 */
101 TileOperand &create_tile_argument(const char *name, int32_t value);
102
103 /** Declare a new tile.
104 *
105 * The name of the tile must be unique in the current ID space.
106 *
107 * @param[in] name The name of the tile.
108 * @param[in] ... The necessary arguments to create a new @ref TileOperand.
109 *
110 * @return The @ref TileOperand object.
111 */
112 template <typename... TArgs>
113 TileOperand &declare_tile(const char *name, TArgs &&...args)
114 {
115 const auto var_name = generate_variable_name(name);
116 auto operand = new TileOperand(var_name, ::std::forward<TArgs>(args)...);
117 register_operand(operand, true);
118
119 return *operand;
120 }
121
122 // =============================================================================================
123 // Load and store
124 // =============================================================================================
125
126 /** Load the data from the tensor memory to the tile using the sampling information.
127 *
128 * @param[out] tile The tile to be loaded.
129 * @param[in] tensor The tensor to be read.
130 * @param[in] sampler The tensor sampling information.
131 */
132 void op_load(TileOperand &tile, TensorOperand &tensor, const TensorTileSampler &sampler);
133
134 /** Store the tile to the tensor using the specified sampling information.
135 *
136 * @param[out] dst The tensor that the tile is written to.
137 * @param[in] src The tile to be stored.
138 * @param[in] sampler The tensor sampling information.
139 */
140 void op_store(TensorOperand &tensor, const TileOperand &tile, const TensorTileSampler &sampler);
141
142 // =============================================================================================
143 // Data processing
144 // =============================================================================================
145
146 /** Write assignment: `<dst> = <src>`.
147 *
148 * @param[in] dst The destination tile.
149 * @param[in] src The source tile.
150 */
151 void op_assign(TileOperand &dst, const TileOperand &src);
152
153 /** Write binary expression: `<dst> = <lhs> <op> <rhs>`.
154 *
155 * @param[in] dst The destination tile.
156 * @param[in] lhs The LHS operand.
157 * @param[in] rhs The RHS operand.
158 * @param[in] op The binary operator.
159 */
160 void op_binary_expression(TileOperand &dst, const TileOperand &lhs, const TileOperand &rhs, BinaryOp op);
161
162 /** Write function applied to scalar value: `<dst> = <func>(<src>)`.
163 *
164 * @param[in] dst The destination tile.
165 * @param[in] src The source tile.
166 * @param[in] func The function to be applied to the source tile.
167 */
168 void op_scalar_function(TileOperand &dst, const TileOperand &src, ScalarUnaryFunction func);
169
170 // =============================================================================================
171 // Misc
172 // =============================================================================================
173
174 /** Set `dst` the global ID of dimension `dim`.
175 *
176 * @param[in] dst The tile to be written to.
177 * @param[in] dim The global ID dimension.
178 */
179 void op_get_global_id(TileOperand &dst, int32_t dim);
180
181 // =============================================================================================
182 // Code generation
183 // =============================================================================================
184
185 /** Generate the source code of the kernel. */
186 ::std::string generate_code();
187
188private:
189 /** Generate the full variable name based on the original name and the ID space.
190 *
191 * @param[in] name The name of the variable.
192 *
193 * @return The full variable name.
194 */
195 ::std::string generate_variable_name(const char *name) const;
196
197 /** Register the operand to the kernel.
198 *
199 * The operand is uniquely owned by the kernel afterward.
200 *
201 * @param[in] operand The operand to be registered.
202 * @param[in] declaring Whether the tile declaration is generated.
203 */
204 void register_operand(OperandBase *operand, bool declaring);
205
206private:
207 Kernel *_kernel;
208 ::std::unique_ptr<prototype::GpuKernelWriterAttribute> _impl_attr;
209 ::std::unique_ptr<prototype::IGpuKernelWriter> _impl;
210
211 int32_t _id_space{ 0 };
212 int32_t _max_id_space{ 0 };
213};
214
215} // namespace ckw
216
217#endif // CKW_INCLUDE_CKW_KERNELWRITER_H