blob: 0bea1200b70b1e6230b87b66f98d438752ed1b9d [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 Docd1f03e2023-09-19 16:41:34 +010030#include "src/TileView.h"
Viet-Hoa Do3389f532023-07-05 17:36:40 +010031#include "src/cl/CLKernelWriter.h"
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010032#include "src/cl/CLTensorArgument.h"
33#include "src/cl/CLTile.h"
Viet-Hoa Do3389f532023-07-05 17:36:40 +010034
Viet-Hoa Docd1f03e2023-09-19 16:41:34 +010035#include <tuple>
36
Viet-Hoa Do3389f532023-07-05 17:36:40 +010037namespace ckw
38{
39
Gunes Bayirab0b7502023-07-11 14:57:36 +010040KernelWriter::~KernelWriter() = default;
41
Viet-Hoa Do3389f532023-07-05 17:36:40 +010042std::unique_ptr<KernelWriter> KernelWriter::create_instance(TargetArchitecture architecture, TargetLanguage language)
43{
Gunes Bayirab0b7502023-07-11 14:57:36 +010044 CKW_UNUSED(architecture);
Viet-Hoa Do3389f532023-07-05 17:36:40 +010045 switch(language)
46 {
47 case TargetLanguage::OpenCL:
48 // Currently this is the oldest and the only supported GPU architecture.
49 CKW_ASSERT(architecture == TargetArchitecture::GpuArmMaliValhall);
50 return std::make_unique<CLKernelWriter>();
51
52 default:
53 CKW_THROW_MSG("Language not supported!");
54 }
55}
56
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010057int32_t KernelWriter::new_id_space()
58{
59 _id_space = ++_last_created_id_space;
60
61 return _id_space;
62}
63
Gunes Bayirab0b7502023-07-11 14:57:36 +010064int32_t KernelWriter::id_space() const
65{
66 return _id_space;
67}
68
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010069KernelWriter &KernelWriter::id_space(int32_t value)
70{
71 CKW_ASSERT(value <= _last_created_id_space);
72
73 _id_space = value;
74
75 return *this;
76}
77
78void KernelWriter::write_body(const std::function<void()> &body)
79{
80 const auto curr_id_space = id_space();
81 new_id_space();
82 body();
83 id_space(curr_id_space);
84}
85
Gunes Bayirab0b7502023-07-11 14:57:36 +010086std::string KernelWriter::generate_full_name(const std::string &name) const
87{
88 return "G" + std::to_string(id_space()) + "__" + name;
89}
Viet-Hoa Do3389f532023-07-05 17:36:40 +010090
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010091TileOperand KernelWriter::create_tile_operand(ITile &tile)
92{
93 return TileOperand(tile);
94}
95
Viet-Hoa Docd1f03e2023-09-19 16:41:34 +010096std::tuple<ITile &, TileArea> KernelWriter::get_tile(const TileOperand &operand)
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010097{
Viet-Hoa Docd1f03e2023-09-19 16:41:34 +010098 return { *operand._tile, { operand._row_start, operand._row_end, operand._col_start, operand._col_end } };
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010099}
100
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100101TensorOperand KernelWriter::create_tensor_operand(ITensor &tensor)
102{
103 return TensorOperand(tensor);
104}
105
106ITensor &KernelWriter::get_tensor(const TensorOperand &operand)
107{
108 return operand._tensor;
109}
110
Gunes Bayir806b8e82023-08-23 23:28:31 +0100111const std::vector<std::vector<std::string>> &KernelWriter::get_values(const ConstantData &data)
112{
113 return data.values();
114}
115
116DataType KernelWriter::get_data_type(const ConstantData &data)
117{
118 return data.data_type();
119}
120
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100121} // namespace ckw