Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +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 | #include "ckw/KernelWriter.h" |
| 26 | #include "ckw/Error.h" |
| 27 | #include "ckw/TensorOperand.h" |
| 28 | #include "src/Prototype.h" |
| 29 | |
| 30 | #include <sstream> |
| 31 | |
| 32 | namespace ckw |
| 33 | { |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | |
| 38 | inline prototype::TensorInfo create_impl_tensor_info(const TensorInfo &info) |
| 39 | { |
| 40 | return prototype::TensorInfo{ info.shape(), info.data_type(), info.data_layout(), info.id() }; |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | // ================================================================================================= |
| 46 | // Constructors and destructor |
| 47 | // ================================================================================================= |
| 48 | |
| 49 | KernelWriter::KernelWriter(Kernel &kernel) |
| 50 | : _kernel(&kernel), |
| 51 | _impl_attr(std::make_unique<prototype::GpuKernelWriterAttribute>()), |
| 52 | _impl(prototype::GpuKernelWriterFactory::create(_impl_attr.get(), kernel.impl())) |
| 53 | { |
| 54 | _impl->set_IdSpace(1); |
| 55 | } |
| 56 | |
| 57 | KernelWriter::~KernelWriter() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | // ================================================================================================= |
| 62 | // Scope management |
| 63 | // ================================================================================================= |
| 64 | |
| 65 | int32_t KernelWriter::id_space() const |
| 66 | { |
| 67 | return _id_space; |
| 68 | } |
| 69 | |
| 70 | KernelWriter &KernelWriter::id_space(int32_t id_space) |
| 71 | { |
| 72 | CKW_ASSERT(id_space <= _max_id_space); |
| 73 | |
| 74 | _id_space = id_space; |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | int32_t KernelWriter::next_id_space() |
| 79 | { |
| 80 | id_space(++_max_id_space); |
| 81 | return _id_space; |
| 82 | } |
| 83 | |
| 84 | // ================================================================================================= |
| 85 | // Tensor and tile declaration |
| 86 | // ================================================================================================= |
| 87 | |
| 88 | TensorOperand &KernelWriter::create_tensor_argument(const char *name, const TensorInfo &info) |
| 89 | { |
| 90 | const auto var_name = generate_variable_name(name); |
| 91 | |
| 92 | _impl->declare_argument(var_name, create_impl_tensor_info(info)); |
| 93 | |
| 94 | auto operand = new TensorOperand(var_name, info); |
| 95 | register_operand(operand, false); |
| 96 | |
| 97 | return *operand; |
| 98 | } |
| 99 | |
| 100 | TileOperand &KernelWriter::create_tile_argument(const char *name, int32_t value) |
| 101 | { |
| 102 | const auto var_name = generate_variable_name(name); |
| 103 | |
| 104 | auto operand = new TileOperand(var_name, value); |
| 105 | register_operand(operand, false); |
| 106 | |
| 107 | return *operand; |
| 108 | } |
| 109 | |
| 110 | std::string KernelWriter::generate_variable_name(const char *name) const |
| 111 | { |
| 112 | std::stringstream var_name; |
| 113 | |
| 114 | var_name << "_" << _id_space << "_" << name; |
| 115 | |
| 116 | return var_name.str(); |
| 117 | } |
| 118 | |
| 119 | void KernelWriter::register_operand(OperandBase *operand, bool declaring) |
| 120 | { |
| 121 | const auto &name = operand->name(); |
| 122 | auto &operands = _kernel->operands(); |
| 123 | |
| 124 | CKW_ASSERT(operands.find(name) == operands.end()); |
| 125 | operands[name] = std::unique_ptr<OperandBase>(operand); |
| 126 | |
| 127 | if(declaring && !operand->is_constant()) |
| 128 | { |
| 129 | const auto tile = reinterpret_cast<TileOperand *>(operand); |
| 130 | |
| 131 | const auto &info = tile->tile_info(); |
| 132 | _impl->declare_tile(tile->name(), prototype::TileInfo(info.data_type(), info.width(), info.height())); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // ================================================================================================= |
| 137 | // Load and store |
| 138 | // ================================================================================================= |
| 139 | |
| 140 | void KernelWriter::op_load(TileOperand &tile, TensorOperand &tensor, const TensorTileSampler &sampler) |
| 141 | { |
| 142 | auto impl_tensor = prototype::TensorOperand( |
| 143 | tensor.name(), |
| 144 | prototype::GpuSampler{ |
| 145 | sampler.format(), |
| 146 | prototype::GpuSamplerTensorStorage::BufferUint8Ptr, |
| 147 | sampler.address_mode_x(), |
| 148 | sampler.address_mode_y(), |
| 149 | sampler.address_mode_z() }); |
| 150 | |
| 151 | auto impl_x = sampler.x().create_impl_operand(_impl.get()); |
| 152 | auto impl_y = sampler.y().create_impl_operand(_impl.get()); |
| 153 | auto impl_z = sampler.z().create_impl_operand(_impl.get()); |
| 154 | auto impl_b = sampler.b().create_impl_operand(_impl.get()); |
| 155 | |
| 156 | auto impl_dst = tile.create_impl_operand(_impl.get()); |
| 157 | |
| 158 | _impl->op_load_immediate(impl_tensor, impl_dst, impl_x, impl_y, impl_z, impl_b); |
| 159 | } |
| 160 | |
| 161 | void KernelWriter::op_store(TensorOperand &tensor, const TileOperand &tile, const TensorTileSampler &sampler) |
| 162 | { |
| 163 | auto impl_tensor = prototype::TensorOperand( |
| 164 | tensor.name(), |
| 165 | prototype::GpuSampler{ |
| 166 | sampler.format(), |
| 167 | prototype::GpuSamplerTensorStorage::BufferUint8Ptr, |
| 168 | sampler.address_mode_x(), |
| 169 | sampler.address_mode_y(), |
| 170 | sampler.address_mode_z() }); |
| 171 | auto impl_src = tile.create_impl_operand(_impl.get()); |
| 172 | auto impl_x = sampler.x().create_impl_operand(_impl.get()); |
| 173 | auto impl_y = sampler.y().create_impl_operand(_impl.get()); |
| 174 | auto impl_z = sampler.z().create_impl_operand(_impl.get()); |
| 175 | auto impl_b = sampler.b().create_impl_operand(_impl.get()); |
| 176 | |
| 177 | _impl->op_store_immediate(impl_tensor, impl_src, impl_x, impl_y, impl_z, impl_b); |
| 178 | } |
| 179 | |
| 180 | // ================================================================================================= |
| 181 | // Data processing |
| 182 | // ================================================================================================= |
| 183 | |
| 184 | void KernelWriter::op_assign(TileOperand &dst, const TileOperand &src) |
| 185 | { |
| 186 | auto impl_dst = dst.create_impl_operand(_impl.get()); |
| 187 | auto impl_src = src.create_impl_operand(_impl.get()); |
| 188 | |
| 189 | _impl->op_assign(impl_dst, impl_src); |
| 190 | } |
| 191 | |
| 192 | void KernelWriter::op_binary_expression(TileOperand &dst, const TileOperand &lhs, const TileOperand &rhs, BinaryOp op) |
| 193 | { |
| 194 | auto impl_lhs = lhs.create_impl_operand(_impl.get()); |
| 195 | auto impl_rhs = rhs.create_impl_operand(_impl.get()); |
| 196 | auto impl_dst = dst.create_impl_operand(_impl.get()); |
| 197 | |
| 198 | _impl->op_binary_expression(impl_dst, impl_lhs, op, impl_rhs); |
| 199 | } |
| 200 | |
| 201 | void KernelWriter::op_scalar_function(TileOperand &dst, const TileOperand &src, ScalarUnaryFunction opcode) |
| 202 | { |
| 203 | auto impl_dst = dst.create_impl_operand(_impl.get()); |
| 204 | auto impl_src = src.create_impl_operand(_impl.get()); |
| 205 | |
| 206 | _impl->op_scalar_function(impl_dst, impl_src, opcode); |
| 207 | } |
| 208 | |
| 209 | // ================================================================================================= |
| 210 | // Misc |
| 211 | // ================================================================================================= |
| 212 | |
| 213 | void KernelWriter::op_get_global_id(TileOperand &dst, int32_t dim) |
| 214 | { |
| 215 | _impl->op_get_global_id(prototype::Operand(dst.name()), dim); |
| 216 | } |
| 217 | |
| 218 | // ================================================================================================= |
| 219 | // Code generation |
| 220 | // ================================================================================================= |
| 221 | |
| 222 | std::string KernelWriter::generate_code() |
| 223 | { |
| 224 | return prototype::generate_code(*_kernel->impl(), _kernel->name()); |
| 225 | } |
| 226 | |
| 227 | } // namespace ckw |