blob: 0972b4e8e2b381f836c8a8449945b9af672eaf67 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Gunes Bayir3a1e1252023-01-03 21:26:09 +00002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +01003 *
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#include "GpuKernelVariableTable.h"
25#include "arm_compute/core/CL/CLHelpers.h"
26#include "arm_compute/core/ITensorInfo.h"
Viet-Hoa Do3558c582022-12-16 14:45:57 +000027#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010028
29namespace arm_compute
30{
31namespace experimental
32{
33namespace dynamic_fusion
34{
Viet-Hoa Do3558c582022-12-16 14:45:57 +000035void GpuKernelVariableTable::declare_variable(const GpuKernelComponentGroup &comp_group, const ITensorInfo *tensor, GpuKernelArgumentInfo argument_info, const std::string &alias)
SiCong Lif44bbc52022-08-29 18:25:51 +010036{
37 ARM_COMPUTE_ERROR_ON_MSG(!tensor->has_valid_id(), "Tensor info with valid id expected");
Viet-Hoa Do3558c582022-12-16 14:45:57 +000038
SiCong Lif44bbc52022-08-29 18:25:51 +010039 // Do not re-declare if the variable associated with the tensor has already been declared
Viet-Hoa Do3558c582022-12-16 14:45:57 +000040 auto it = _vars.find(tensor->id());
41
42 if(it != _vars.end())
SiCong Lif44bbc52022-08-29 18:25:51 +010043 {
Viet-Hoa Do3558c582022-12-16 14:45:57 +000044 ARM_COMPUTE_ERROR_ON(!(it->second.kernel_argument_info == argument_info));
SiCong Lif44bbc52022-08-29 18:25:51 +010045 return;
46 }
SiCong Lif44bbc52022-08-29 18:25:51 +010047
Viet-Hoa Do3558c582022-12-16 14:45:57 +000048 const auto target = comp_group.get_tile_for_tensor(tensor);
49
50 if(target != tensor)
SiCong Lif44bbc52022-08-29 18:25:51 +010051 {
Viet-Hoa Do3558c582022-12-16 14:45:57 +000052 // If the tensor uses a shared tile, don't declare another variable.
53 it = _vars.find(target->id());
54
55 ARM_COMPUTE_ERROR_ON_MSG(
56 it == _vars.end(),
57 "The variable used for this tensor must have been declared.");
58
59 _vars[tensor->id()] = it->second;
SiCong Lif44bbc52022-08-29 18:25:51 +010060 }
61 else
62 {
Viet-Hoa Do3558c582022-12-16 14:45:57 +000063 // Declare variable associated with the tensor
64 std::stringstream ss;
Gunes Bayir3a1e1252023-01-03 21:26:09 +000065 ss << alias << "_t" << abs(tensor->id());
Viet-Hoa Do3558c582022-12-16 14:45:57 +000066 const auto uniq_name = ss.str();
67 TensorVariable var{ tensor->id(), uniq_name, argument_info };
68
SiCong Lif44bbc52022-08-29 18:25:51 +010069 _vars.emplace(tensor->id(), var);
70 }
71}
72
73GpuKernelVariableTable::TensorVariable GpuKernelVariableTable::get_variable(const ITensorInfo *tensor) const
74{
Viet-Hoa Do3558c582022-12-16 14:45:57 +000075 const auto var = _vars.at(tensor->id());
76 return var;
SiCong Lif44bbc52022-08-29 18:25:51 +010077}
78
79GpuKernelVariableTable::VariableList GpuKernelVariableTable::get_variable_list(const std::vector<const ITensorInfo *> &tensors) const
80{
81 VariableList vars{};
82 for(const auto &tensor : tensors)
83 {
84 if(!tensor->has_valid_id())
85 {
86 continue;
87 }
88 vars.push_back(get_variable(tensor));
89 }
90 return vars;
91}
92
93TagVal::TagVal(const GpuKernelVariableTable::TensorVariable &var)
94 : value{ var.uniq_name }
95{
96}
97
98TagVal::TagVal(const std::string &val)
99 : value{ val }
100{
101}
102
103TagVal::TagVal(const char *val)
104 : value{ std::string(val) }
105{
106}
107
108TagVal::TagVal(const DataType &data_type)
109 : value{ get_cl_type_from_data_type(data_type) }
110{
111}
112} // namespace dynamic_fusion
113} // namespace experimental
114} // namespace arm_compute