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