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 | #include "src/cl/CLKernelWriter.h" |
| 26 | #include "ckw/Error.h" |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 27 | #include "ckw/Kernel.h" |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 28 | #include "ckw/TileOperand.h" |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 29 | #include "src/cl/CLHelpers.h" |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 30 | #include "src/cl/CLTensorArgument.h" |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 31 | #include "src/cl/CLTile.h" |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 32 | #include <cstdint> |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 33 | |
| 34 | namespace ckw |
| 35 | { |
| 36 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 37 | CLKernelWriter::CLKernelWriter() = default; |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 38 | CLKernelWriter::~CLKernelWriter() = default; |
| 39 | |
| 40 | std::unique_ptr<Kernel> CLKernelWriter::emit_kernel(const std::string &name) |
| 41 | { |
| 42 | CKW_UNUSED(name); |
| 43 | CKW_THROW_MSG("Not implemented!"); |
| 44 | } |
| 45 | |
| 46 | void CLKernelWriter::comment(const std::string &text) |
| 47 | { |
| 48 | #ifdef COMPUTE_KERNEL_WRITER_DEBUG_ENABLED |
| 49 | |
| 50 | CKW_ASSERT(text.find("\n") == text.npos); |
| 51 | CKW_ASSERT(text.find("\r") == text.npos); |
| 52 | |
| 53 | append_code("// ", text, "\n"); |
| 54 | |
| 55 | #else // COMPUTE_KERNEL_WRITER_DEBUG_ENABLED |
| 56 | |
| 57 | CKW_UNUSED(text); |
| 58 | |
| 59 | #endif // COMPUTE_KERNEL_WRITER_DEBUG_ENABLED |
| 60 | } |
| 61 | |
| 62 | const std::string &CLKernelWriter::body_source_code() const |
| 63 | { |
| 64 | return _body_source_code; |
| 65 | } |
| 66 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 67 | TensorOperand CLKernelWriter::declare_tensor_argument(const std::string &name, const TensorInfo &info) |
| 68 | { |
| 69 | const auto fullname = generate_full_name(name); |
| 70 | |
| 71 | auto tensor = std::make_unique<CLTensorArgument>(fullname, info, false /* return_dims_by_value */); |
| 72 | const auto operand = create_tensor_operand(*tensor); |
| 73 | |
| 74 | _tensors.insert(std::move(tensor)); |
| 75 | |
| 76 | return operand; |
| 77 | } |
| 78 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 79 | TileOperand CLKernelWriter::declare_tile(const std::string &name, const TileInfo &tile_info) |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 80 | { |
| 81 | const std::string fullname = generate_full_name(name); |
| 82 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 83 | const int32_t height = tile_info.height(); |
| 84 | const int32_t width = tile_info.width(); |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 85 | const DataType data_type = tile_info.data_type(); |
| 86 | |
| 87 | for(int32_t row = 0; row < height; ++row) |
| 88 | { |
| 89 | const std::string cl_type = cl_get_variable_datatype_as_string(data_type, width); |
| 90 | append_code(cl_type, " ", fullname, std::to_string(row), ";\n"); |
| 91 | } |
| 92 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 93 | auto tile = std::make_unique<CLTile>(name, tile_info); |
| 94 | const auto operand = create_tile_operand(*tile); |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 95 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 96 | _tiles.insert(std::move(tile)); |
| 97 | |
| 98 | return operand; |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Gunes Bayir | 366514d | 2023-07-27 22:52:32 +0100 | [diff] [blame^] | 101 | void CLKernelWriter::op_write_raw_code(const std::string &raw_code) |
| 102 | { |
| 103 | append_code(raw_code); |
| 104 | } |
| 105 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 106 | } // namespace ckw |