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