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/TileOperand.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 26 | |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 27 | #include "ckw/Error.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 29 | #include "src/Prototype.h" |
| 30 | |
| 31 | namespace ckw |
| 32 | { |
| 33 | |
| 34 | TileOperand::TileOperand(const std::string &name, const TileInfo &info) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 35 | : OperandBase(name), _info(info), _value{std::vector<std::string>{"0"}}, _constant(false) |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | TileOperand::TileOperand(const std::string &name, DataType data_type) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | : OperandBase(name), _info(TileInfo{data_type}), _value{std::vector<std::string>{"0"}}, _constant(false) |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | TileOperand::TileOperand(const std::string &name, int32_t value) |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 45 | : OperandBase(name), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 46 | _info(TileInfo{DataType::Int32}), |
| 47 | _value{std::vector<std::string>{std::to_string(value)}}, |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 48 | _constant(true) |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | TileOperand::TileOperand(const std::string &name, float value) |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 53 | : OperandBase(name), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | _info(TileInfo{DataType::Fp32}), |
| 55 | _value{std::vector<std::string>{std::to_string(value)}}, |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 56 | _constant(true) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | TileOperand::TileOperand(const std::string &name, const TileContainer &vals, DataType dt) |
| 61 | : OperandBase(name), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | _info(TileInfo{dt, static_cast<int32_t>(vals.size()), static_cast<int32_t>(vals[0].size())}), |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 63 | _value(vals), |
| 64 | _constant(true) |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
| 68 | prototype::Operand TileOperand::create_impl_operand(prototype::IGpuKernelWriter *writer) const |
| 69 | { |
| 70 | CKW_UNUSED(writer); |
| 71 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | if (_constant) |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 73 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | if (is_scalar()) |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 75 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | switch (_info.data_type()) |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 77 | { |
| 78 | case DataType::Int32: |
| 79 | return prototype::Operand(_value[0][0], prototype::OperandType::ScalarInt32); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 80 | |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 81 | case DataType::Fp32: |
| 82 | return prototype::Operand(_value[0][0], prototype::OperandType::ScalarFp32); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 83 | |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 84 | case DataType::Fp16: |
| 85 | return prototype::Operand(_value[0][0], prototype::OperandType::ScalarFp16); |
| 86 | |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 87 | default: |
| 88 | CKW_ASSERT(false); |
| 89 | } |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | return prototype::Operand(name()); |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | return prototype::Operand(name(), prototype::OperandType::Tile); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | const TileInfo &TileOperand::tile_info() const |
| 103 | { |
| 104 | return _info; |
| 105 | } |
| 106 | |
| 107 | DataType TileOperand::data_type() const |
| 108 | { |
| 109 | return _info.data_type(); |
| 110 | } |
| 111 | |
| 112 | bool TileOperand::is_constant() const |
| 113 | { |
| 114 | return _constant; |
| 115 | } |
| 116 | |
| 117 | bool TileOperand::is_scalar() const |
| 118 | { |
| 119 | return _info.width() == 1 && _info.height() == 1; |
| 120 | } |
| 121 | |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 122 | std::string TileOperand::scalar_value() const |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 123 | { |
| 124 | CKW_ASSERT(is_scalar()); |
| 125 | CKW_ASSERT(is_constant()); |
| 126 | |
Jakub Sujak | e1c96e7 | 2023-07-31 13:36:58 +0100 | [diff] [blame] | 127 | return _value[0][0]; |
| 128 | } |
| 129 | |
| 130 | const TileContainer &TileOperand::value() const |
| 131 | { |
Viet-Hoa Do | bd4f6b9 | 2023-05-30 09:34:32 +0100 | [diff] [blame] | 132 | return _value; |
| 133 | } |
| 134 | |
| 135 | } // namespace ckw |