Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 1 | /* |
| 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_SRC_CL_CLKERNELWRITER_H |
| 26 | #define CKW_SRC_CL_CLKERNELWRITER_H |
| 27 | |
| 28 | #include "ckw/KernelWriter.h" |
Gunes Bayir | 3c77606 | 2023-07-12 14:50:56 +0100 | [diff] [blame] | 29 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 30 | #include <memory> |
| 31 | #include <set> |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 32 | #include <string> |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 33 | #include <utility> |
| 34 | |
| 35 | namespace ckw |
| 36 | { |
| 37 | |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 38 | // Forward Declarations |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 39 | class CLTile; |
| 40 | class CLTensorArgument; |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 41 | class ConstantData; |
| 42 | class TensorOperand; |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 43 | class TensorSampler; |
| 44 | class TileOperand; |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 45 | |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 46 | enum class DataType; |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 47 | enum class MemoryOperation; |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 48 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 49 | /** OpenCL kernel writer. */ |
| 50 | class CLKernelWriter : public KernelWriter |
| 51 | { |
| 52 | public: |
| 53 | // ============================================================================================= |
| 54 | // Construtors and destructor |
| 55 | // ============================================================================================= |
| 56 | |
| 57 | /** Initialize a new instance of @ref CLKernelWriter class. */ |
| 58 | CLKernelWriter(); |
| 59 | |
| 60 | /** Destructor */ |
| 61 | ~CLKernelWriter(); |
| 62 | |
| 63 | // ============================================================================================= |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 64 | // Data processing |
| 65 | // ============================================================================================= |
| 66 | |
| 67 | void op_assign(const TileOperand &dst, const TileOperand &src) override; |
| 68 | |
| 69 | void op_cast(const TileOperand &dst, const TileOperand &src, ConvertPolicy policy) override; |
| 70 | |
Viet-Hoa Do | 34b6c3a | 2023-08-22 11:11:23 +0100 | [diff] [blame] | 71 | void op_unary(const TileOperand &dst, UnaryOp op, const TileOperand &src) override; |
| 72 | |
| 73 | void op_binary(const TileOperand &dst, BinaryOp op, const TileOperand &first, const TileOperand &second) override; |
| 74 | |
| 75 | void op_ternary(const TileOperand &dst, TernaryOp op, const TileOperand &first, const TileOperand &second, const TileOperand &third) override; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 76 | |
| 77 | // ============================================================================================= |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 78 | // Flow control |
| 79 | // ============================================================================================= |
| 80 | |
| 81 | void op_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) override; |
| 82 | |
| 83 | void op_else_if(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body) override; |
| 84 | |
| 85 | void op_else(const std::function<void()> &body) override; |
| 86 | |
| 87 | void op_for_loop( |
| 88 | const TileOperand &var, BinaryOp cond_op, const TileOperand &cond_value, |
| 89 | const TileOperand &update_var, AssignmentOp update_op, const TileOperand &update_value, |
| 90 | const std::function<void()> &body) override; |
| 91 | |
| 92 | void op_return() override; |
| 93 | |
| 94 | // ============================================================================================= |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 95 | // Misc |
| 96 | // ============================================================================================= |
| 97 | |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 98 | void op_comment(const std::string &text) override; |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 99 | |
Gunes Bayir | 366514d | 2023-07-27 22:52:32 +0100 | [diff] [blame] | 100 | void op_write_raw_code(const std::string &raw_code) override; |
| 101 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 102 | // ============================================================================================= |
| 103 | // Code generation |
| 104 | // ============================================================================================= |
| 105 | |
| 106 | std::unique_ptr<Kernel> emit_kernel(const std::string &name) override; |
| 107 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 108 | // ============================================================================================= |
| 109 | // Tensor and tile declaration |
| 110 | // ============================================================================================= |
| 111 | |
| 112 | TensorOperand declare_tensor_argument(const std::string &name, const TensorInfo &info) override; |
| 113 | |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 114 | /** Declare a tile given name and tile information |
| 115 | * |
| 116 | * Similar to @ref KernelWriter::declare_tile() |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 117 | */ |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 118 | TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) override; |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 119 | |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 120 | /** Declare a constant tile given a @ref:ConstantData object |
| 121 | * |
| 122 | * Similar to @ref KernelWriter::declare_constant_tile() |
| 123 | */ |
| 124 | TileOperand declare_constant_tile(const ConstantData &data) override; |
| 125 | |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 126 | // ============================================================================================= |
| 127 | // Memory Operations |
| 128 | // ============================================================================================= |
| 129 | |
| 130 | /** Load the data from the tensor memory to the tile using the sampling information. |
| 131 | * |
| 132 | * Similar to @ref KernelWriter::op_load() |
| 133 | */ |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 134 | void op_load( |
| 135 | const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler, |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 136 | const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch) override; |
| 137 | |
| 138 | /** Load the data from the tensor memory to the tile in a dilated way using the sampling information. |
| 139 | * |
| 140 | * Similar to @ref KernelWriter::op_load_dilated() |
| 141 | */ |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 142 | void op_load_dilated( |
| 143 | const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler, |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 144 | const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch, |
| 145 | const TileOperand &dilation_x, const TileOperand &dilation_y) override; |
| 146 | |
| 147 | /** Store the data to the tensor memory from the tile using the sampling information. |
| 148 | * |
| 149 | * Similar to @ref KernelWriter::op_store() |
| 150 | */ |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 151 | void op_store( |
| 152 | const TensorOperand &tensor_op, const TileOperand &tile_op, TensorSampler &sampler, |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 153 | const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch) override; |
| 154 | |
| 155 | /** Store the data to the tensor memory from the tile in a dilated way using the sampling information. |
| 156 | * |
| 157 | * Similar to @ref KernelWriter::op_store_dilated() |
| 158 | */ |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 159 | void op_store_dilated( |
| 160 | const TensorOperand &tensor_op, const TileOperand &tile_op, TensorSampler &sampler, |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 161 | const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch, |
| 162 | const TileOperand &dilation_x, const TileOperand &dilation_y) override; |
| 163 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 164 | protected: |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 165 | /** Return @ref CLTile object from the @ref TileOperand object. |
| 166 | * |
| 167 | * This function performs appropriate check before doing type casting. |
| 168 | */ |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 169 | const CLTile &to_cl_tile(const TileOperand &operand) const; |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 170 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 171 | /** Append the specified code to the kernel body source code. */ |
| 172 | template <typename T, typename... TArgs> |
| 173 | void append_code(T &&code, TArgs &&...args) |
| 174 | { |
| 175 | append_code(std::forward<T>(code)); |
| 176 | append_code(std::forward<TArgs>(args)...); |
| 177 | } |
| 178 | |
| 179 | /** Append the specified code to the kernel body source code. */ |
| 180 | template <typename T> |
| 181 | void append_code(T &&code) |
| 182 | { |
| 183 | _body_source_code += std::forward<T>(code); |
| 184 | } |
| 185 | |
| 186 | /** Get the current kernel body source code. */ |
| 187 | const std::string &body_source_code() const; |
| 188 | |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 189 | // For helper functions |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 190 | private: |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 191 | /** Helper function to consolidate all load/store logic in this class */ |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 192 | void op_load_store( |
| 193 | MemoryOperation op, const TileOperand &tile_op, const TensorOperand &tensor_op, TensorSampler &sampler, |
Gunes Bayir | 47a396e | 2023-08-17 11:04:02 +0100 | [diff] [blame] | 194 | const TileOperand &x, const TileOperand &y, const TileOperand &z, const TileOperand &batch, |
| 195 | const CLTile &dilation_x, const CLTile &dilation_y); |
| 196 | |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 197 | /** This function is the generic function to write both `if` and `else if` blocks. |
| 198 | * |
| 199 | * It is used for both @ref CLKernelWriter::op_if and @ref CLKernelWriter::op_else_if. |
| 200 | * |
Viet-Hoa Do | 98901e4 | 2023-08-30 10:12:22 +0100 | [diff] [blame] | 201 | * @param[in] lhs The LHS tile of the condition. |
| 202 | * @param[in] op The relational binary operator. |
| 203 | * @param[in] rhs The RHS tile of the condition. |
| 204 | * @param[in] body The function that writes the body of the else-if block. |
| 205 | * @param[in] is_else_if True if this is an `else if` block, otherwise this is an `if` block. |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 206 | */ |
Viet-Hoa Do | 98901e4 | 2023-08-30 10:12:22 +0100 | [diff] [blame] | 207 | void op_if_generic(const TileOperand &lhs, BinaryOp op, const TileOperand &rhs, const std::function<void()> &body, bool is_else_if); |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 208 | |
Viet-Hoa Do | e1c3b46 | 2023-07-31 17:13:34 +0100 | [diff] [blame] | 209 | // For attributes |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 210 | private: |
| 211 | /** This string contains the kernel body source code, not the full CL source code. |
| 212 | * The full source code will only be generated when the user calls @ref KernelWriter::emit_kernel. |
| 213 | * |
| 214 | * In order to add code to this, use @ref CLKernelWriter::append_code. |
| 215 | * Do not attempt to concatenate and alter this string directly. |
| 216 | */ |
| 217 | std::string _body_source_code{}; |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 218 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 219 | std::set<std::unique_ptr<CLTensorArgument>> _tensors{}; |
| 220 | std::set<std::unique_ptr<CLTile>> _tiles{}; |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 221 | std::set<std::unique_ptr<CLTile>> _constant_tiles{}; |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace ckw |
| 225 | |
| 226 | #endif // CKW_SRC_CL_CLKERNELWRITER_H |