blob: 0eb2ca6a643a5ce4697f8d3862f930fa3cbe0237 [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
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010088 case DataType::Fp16:
89 return prototype::Operand(_value[0][0], prototype::OperandType::ScalarFp16);
90
Jakub Sujake1c96e72023-07-31 13:36:58 +010091 default:
92 CKW_ASSERT(false);
93 }
94 }
95 else
96 {
97 return prototype::Operand(name());
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010098 }
99 }
100 else
101 {
102 return prototype::Operand(name(), prototype::OperandType::Tile);
103 }
104}
105
106const TileInfo &TileOperand::tile_info() const
107{
108 return _info;
109}
110
111DataType TileOperand::data_type() const
112{
113 return _info.data_type();
114}
115
116bool TileOperand::is_constant() const
117{
118 return _constant;
119}
120
121bool TileOperand::is_scalar() const
122{
123 return _info.width() == 1 && _info.height() == 1;
124}
125
Jakub Sujake1c96e72023-07-31 13:36:58 +0100126std::string TileOperand::scalar_value() const
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100127{
128 CKW_ASSERT(is_scalar());
129 CKW_ASSERT(is_constant());
130
Jakub Sujake1c96e72023-07-31 13:36:58 +0100131 return _value[0][0];
132}
133
134const TileContainer &TileOperand::value() const
135{
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100136 return _value;
137}
138
139} // namespace ckw