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 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 25 | #include "ckw/KernelWriter.h" |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 26 | #include "ckw/Error.h" |
Gunes Bayir | 3c77606 | 2023-07-12 14:50:56 +0100 | [diff] [blame] | 27 | #include "ckw/TileOperand.h" |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 28 | #include "ckw/types/TargetArchitecture.h" |
| 29 | #include "ckw/types/TargetLanguage.h" |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 30 | #include "src/TileView.h" |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 31 | #include "src/cl/CLKernelWriter.h" |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 32 | #include "src/cl/CLTensorArgument.h" |
| 33 | #include "src/cl/CLTile.h" |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 34 | |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 35 | #include <tuple> |
| 36 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 37 | namespace ckw |
| 38 | { |
| 39 | |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 40 | KernelWriter::~KernelWriter() = default; |
| 41 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 42 | std::unique_ptr<KernelWriter> KernelWriter::create_instance(TargetArchitecture architecture, TargetLanguage language) |
| 43 | { |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 44 | CKW_UNUSED(architecture); |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 45 | switch(language) |
| 46 | { |
| 47 | case TargetLanguage::OpenCL: |
| 48 | // Currently this is the oldest and the only supported GPU architecture. |
| 49 | CKW_ASSERT(architecture == TargetArchitecture::GpuArmMaliValhall); |
| 50 | return std::make_unique<CLKernelWriter>(); |
| 51 | |
| 52 | default: |
| 53 | CKW_THROW_MSG("Language not supported!"); |
| 54 | } |
| 55 | } |
| 56 | |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 57 | int32_t KernelWriter::new_id_space() |
| 58 | { |
| 59 | _id_space = ++_last_created_id_space; |
| 60 | |
| 61 | return _id_space; |
| 62 | } |
| 63 | |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 64 | int32_t KernelWriter::id_space() const |
| 65 | { |
| 66 | return _id_space; |
| 67 | } |
| 68 | |
Viet-Hoa Do | 2d0c2f5 | 2023-08-24 11:48:19 +0100 | [diff] [blame] | 69 | KernelWriter &KernelWriter::id_space(int32_t value) |
| 70 | { |
| 71 | CKW_ASSERT(value <= _last_created_id_space); |
| 72 | |
| 73 | _id_space = value; |
| 74 | |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | void KernelWriter::write_body(const std::function<void()> &body) |
| 79 | { |
| 80 | const auto curr_id_space = id_space(); |
| 81 | new_id_space(); |
| 82 | body(); |
| 83 | id_space(curr_id_space); |
| 84 | } |
| 85 | |
Gunes Bayir | ab0b750 | 2023-07-11 14:57:36 +0100 | [diff] [blame] | 86 | std::string KernelWriter::generate_full_name(const std::string &name) const |
| 87 | { |
| 88 | return "G" + std::to_string(id_space()) + "__" + name; |
| 89 | } |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 90 | |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 91 | TileOperand KernelWriter::create_tile_operand(ITile &tile) |
| 92 | { |
| 93 | return TileOperand(tile); |
| 94 | } |
| 95 | |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 96 | std::tuple<ITile &, TileArea> KernelWriter::get_tile(const TileOperand &operand) |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 97 | { |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 98 | return { *operand._tile, { operand._row_start, operand._row_end, operand._col_start, operand._col_end } }; |
Viet-Hoa Do | 25d26f4 | 2023-07-20 17:31:47 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Viet-Hoa Do | 0b23e0e | 2023-07-25 14:00:46 +0100 | [diff] [blame] | 101 | TensorOperand KernelWriter::create_tensor_operand(ITensor &tensor) |
| 102 | { |
| 103 | return TensorOperand(tensor); |
| 104 | } |
| 105 | |
| 106 | ITensor &KernelWriter::get_tensor(const TensorOperand &operand) |
| 107 | { |
| 108 | return operand._tensor; |
| 109 | } |
| 110 | |
Gunes Bayir | 806b8e8 | 2023-08-23 23:28:31 +0100 | [diff] [blame] | 111 | const std::vector<std::vector<std::string>> &KernelWriter::get_values(const ConstantData &data) |
| 112 | { |
| 113 | return data.values(); |
| 114 | } |
| 115 | |
| 116 | DataType KernelWriter::get_data_type(const ConstantData &data) |
| 117 | { |
| 118 | return data.data_type(); |
| 119 | } |
| 120 | |
Viet-Hoa Do | 3389f53 | 2023-07-05 17:36:40 +0100 | [diff] [blame] | 121 | } // namespace ckw |