blob: e09c833d960466964a8b1ab4eee7aec7164ab81f [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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010027#include "ckw/Error.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010029#include "src/Prototype.h"
30
31namespace ckw
32{
33
34TileOperand::TileOperand(const std::string &name, const TileInfo &info)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035 : OperandBase(name), _info(info), _value{std::vector<std::string>{"0"}}, _constant(false)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010036{
37}
38
39TileOperand::TileOperand(const std::string &name, DataType data_type)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010040 : OperandBase(name), _info(TileInfo{data_type}), _value{std::vector<std::string>{"0"}}, _constant(false)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010041{
42}
43
44TileOperand::TileOperand(const std::string &name, int32_t value)
Jakub Sujake1c96e72023-07-31 13:36:58 +010045 : OperandBase(name),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 _info(TileInfo{DataType::Int32}),
47 _value{std::vector<std::string>{std::to_string(value)}},
Jakub Sujake1c96e72023-07-31 13:36:58 +010048 _constant(true)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010049{
50}
51
52TileOperand::TileOperand(const std::string &name, float value)
Jakub Sujake1c96e72023-07-31 13:36:58 +010053 : OperandBase(name),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 _info(TileInfo{DataType::Fp32}),
55 _value{std::vector<std::string>{std::to_string(value)}},
Jakub Sujake1c96e72023-07-31 13:36:58 +010056 _constant(true)
57{
58}
59
60TileOperand::TileOperand(const std::string &name, const TileContainer &vals, DataType dt)
61 : OperandBase(name),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 _info(TileInfo{dt, static_cast<int32_t>(vals.size()), static_cast<int32_t>(vals[0].size())}),
Jakub Sujake1c96e72023-07-31 13:36:58 +010063 _value(vals),
64 _constant(true)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010065{
66}
67
68prototype::Operand TileOperand::create_impl_operand(prototype::IGpuKernelWriter *writer) const
69{
70 CKW_UNUSED(writer);
71
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 if (_constant)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010073 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 if (is_scalar())
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010075 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 switch (_info.data_type())
Jakub Sujake1c96e72023-07-31 13:36:58 +010077 {
78 case DataType::Int32:
79 return prototype::Operand(_value[0][0], prototype::OperandType::ScalarInt32);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010080
Jakub Sujake1c96e72023-07-31 13:36:58 +010081 case DataType::Fp32:
82 return prototype::Operand(_value[0][0], prototype::OperandType::ScalarFp32);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010083
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010084 case DataType::Fp16:
85 return prototype::Operand(_value[0][0], prototype::OperandType::ScalarFp16);
86
Jakub Sujake1c96e72023-07-31 13:36:58 +010087 default:
88 CKW_ASSERT(false);
89 }
90 }
91 else
92 {
93 return prototype::Operand(name());
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010094 }
95 }
96 else
97 {
98 return prototype::Operand(name(), prototype::OperandType::Tile);
99 }
100}
101
102const TileInfo &TileOperand::tile_info() const
103{
104 return _info;
105}
106
107DataType TileOperand::data_type() const
108{
109 return _info.data_type();
110}
111
112bool TileOperand::is_constant() const
113{
114 return _constant;
115}
116
117bool TileOperand::is_scalar() const
118{
119 return _info.width() == 1 && _info.height() == 1;
120}
121
Jakub Sujake1c96e72023-07-31 13:36:58 +0100122std::string TileOperand::scalar_value() const
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100123{
124 CKW_ASSERT(is_scalar());
125 CKW_ASSERT(is_constant());
126
Jakub Sujake1c96e72023-07-31 13:36:58 +0100127 return _value[0][0];
128}
129
130const TileContainer &TileOperand::value() const
131{
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100132 return _value;
133}
134
135} // namespace ckw