blob: bf6a15b9df69e13482046f47e8a6724c041755ae [file] [log] [blame]
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +01001/*
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
29namespace ckw
30{
31
32TileOperand::TileOperand(const std::string &name, const TileInfo &info)
Jakub Sujake1c96e72023-07-31 13:36:58 +010033 : OperandBase(name),
34 _info(info),
35 _value{ std::vector<std::string>{ "0" } },
36 _constant(false)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010037{
38}
39
40TileOperand::TileOperand(const std::string &name, DataType data_type)
Jakub Sujake1c96e72023-07-31 13:36:58 +010041 : OperandBase(name),
42 _info(TileInfo{ data_type }),
43 _value{ std::vector<std::string>{ "0" } },
44 _constant(false)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010045{
46}
47
48TileOperand::TileOperand(const std::string &name, int32_t value)
Jakub Sujake1c96e72023-07-31 13:36:58 +010049 : OperandBase(name),
50 _info(TileInfo{ DataType::Int32 }),
51 _value{ std::vector<std::string>{ std::to_string(value) } },
52 _constant(true)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010053{
54}
55
56TileOperand::TileOperand(const std::string &name, float value)
Jakub Sujake1c96e72023-07-31 13:36:58 +010057 : OperandBase(name),
58 _info(TileInfo{ DataType::Fp32 }),
59 _value{ std::vector<std::string>{ std::to_string(value) } },
60 _constant(true)
61{
62}
63
64TileOperand::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 Dobd4f6b92023-05-30 09:34:32 +010069{
70}
71
72prototype::Operand TileOperand::create_impl_operand(prototype::IGpuKernelWriter *writer) const
73{
74 CKW_UNUSED(writer);
75
76 if(_constant)
77 {
Jakub Sujake1c96e72023-07-31 13:36:58 +010078 if(is_scalar())
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010079 {
Jakub Sujake1c96e72023-07-31 13:36:58 +010080 switch(_info.data_type())
81 {
82 case DataType::Int32:
83 return prototype::Operand(_value[0][0], prototype::OperandType::ScalarInt32);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010084
Jakub Sujake1c96e72023-07-31 13:36:58 +010085 case DataType::Fp32:
86 return prototype::Operand(_value[0][0], prototype::OperandType::ScalarFp32);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010087
Jakub Sujake1c96e72023-07-31 13:36:58 +010088 default:
89 CKW_ASSERT(false);
90 }
91 }
92 else
93 {
94 return prototype::Operand(name());
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010095 }
96 }
97 else
98 {
99 return prototype::Operand(name(), prototype::OperandType::Tile);
100 }
101}
102
103const TileInfo &TileOperand::tile_info() const
104{
105 return _info;
106}
107
108DataType TileOperand::data_type() const
109{
110 return _info.data_type();
111}
112
113bool TileOperand::is_constant() const
114{
115 return _constant;
116}
117
118bool TileOperand::is_scalar() const
119{
120 return _info.width() == 1 && _info.height() == 1;
121}
122
Jakub Sujake1c96e72023-07-31 13:36:58 +0100123std::string TileOperand::scalar_value() const
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100124{
125 CKW_ASSERT(is_scalar());
126 CKW_ASSERT(is_constant());
127
Jakub Sujake1c96e72023-07-31 13:36:58 +0100128 return _value[0][0];
129}
130
131const TileContainer &TileOperand::value() const
132{
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100133 return _value;
134}
135
136} // namespace ckw