blob: fb0e62c8cec2f3ded37b481b1f9b4d9bcfbab663 [file] [log] [blame]
Viet-Hoa Do3389f532023-07-05 17:36:40 +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
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010025#include "ckw/KernelWriter.h"
Viet-Hoa Do3389f532023-07-05 17:36:40 +010026#include "ckw/Error.h"
Gunes Bayir3c776062023-07-12 14:50:56 +010027#include "ckw/TileOperand.h"
Gunes Bayirab0b7502023-07-11 14:57:36 +010028#include "ckw/types/TargetArchitecture.h"
29#include "ckw/types/TargetLanguage.h"
Viet-Hoa Do3389f532023-07-05 17:36:40 +010030#include "src/cl/CLKernelWriter.h"
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010031#include "src/cl/CLTensorArgument.h"
32#include "src/cl/CLTile.h"
Viet-Hoa Do3389f532023-07-05 17:36:40 +010033
34namespace ckw
35{
36
Gunes Bayirab0b7502023-07-11 14:57:36 +010037KernelWriter::~KernelWriter() = default;
38
Viet-Hoa Do3389f532023-07-05 17:36:40 +010039std::unique_ptr<KernelWriter> KernelWriter::create_instance(TargetArchitecture architecture, TargetLanguage language)
40{
Gunes Bayirab0b7502023-07-11 14:57:36 +010041 CKW_UNUSED(architecture);
Viet-Hoa Do3389f532023-07-05 17:36:40 +010042 switch(language)
43 {
44 case TargetLanguage::OpenCL:
45 // Currently this is the oldest and the only supported GPU architecture.
46 CKW_ASSERT(architecture == TargetArchitecture::GpuArmMaliValhall);
47 return std::make_unique<CLKernelWriter>();
48
49 default:
50 CKW_THROW_MSG("Language not supported!");
51 }
52}
53
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010054int32_t KernelWriter::new_id_space()
55{
56 _id_space = ++_last_created_id_space;
57
58 return _id_space;
59}
60
Gunes Bayirab0b7502023-07-11 14:57:36 +010061int32_t KernelWriter::id_space() const
62{
63 return _id_space;
64}
65
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010066KernelWriter &KernelWriter::id_space(int32_t value)
67{
68 CKW_ASSERT(value <= _last_created_id_space);
69
70 _id_space = value;
71
72 return *this;
73}
74
75void KernelWriter::write_body(const std::function<void()> &body)
76{
77 const auto curr_id_space = id_space();
78 new_id_space();
79 body();
80 id_space(curr_id_space);
81}
82
Gunes Bayirab0b7502023-07-11 14:57:36 +010083std::string KernelWriter::generate_full_name(const std::string &name) const
84{
85 return "G" + std::to_string(id_space()) + "__" + name;
86}
Viet-Hoa Do3389f532023-07-05 17:36:40 +010087
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010088TileOperand KernelWriter::create_tile_operand(ITile &tile)
89{
90 return TileOperand(tile);
91}
92
93ITile &KernelWriter::get_tile(const TileOperand &operand)
94{
95 return operand._tile;
96}
97
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010098TensorOperand KernelWriter::create_tensor_operand(ITensor &tensor)
99{
100 return TensorOperand(tensor);
101}
102
103ITensor &KernelWriter::get_tensor(const TensorOperand &operand)
104{
105 return operand._tensor;
106}
107
Gunes Bayir806b8e82023-08-23 23:28:31 +0100108const std::vector<std::vector<std::string>> &KernelWriter::get_values(const ConstantData &data)
109{
110 return data.values();
111}
112
113DataType KernelWriter::get_data_type(const ConstantData &data)
114{
115 return data.data_type();
116}
117
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100118} // namespace ckw