blob: a478231c09119fffc43bc6ffc8d9e3792bc57cc7 [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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026
Viet-Hoa Do3389f532023-07-05 17:36:40 +010027#include "ckw/Error.h"
Gunes Bayir3c776062023-07-12 14:50:56 +010028#include "ckw/TileOperand.h"
Gunes Bayirab0b7502023-07-11 14:57:36 +010029#include "ckw/types/TargetArchitecture.h"
30#include "ckw/types/TargetLanguage.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Viet-Hoa Do3389f532023-07-05 17:36:40 +010032#include "src/cl/CLKernelWriter.h"
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010033#include "src/cl/CLTensorArgument.h"
34#include "src/cl/CLTile.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035#include "src/TileView.h"
Viet-Hoa Do3389f532023-07-05 17:36:40 +010036
Viet-Hoa Docd1f03e2023-09-19 16:41:34 +010037#include <tuple>
38
Viet-Hoa Do3389f532023-07-05 17:36:40 +010039namespace ckw
40{
41
Gunes Bayirab0b7502023-07-11 14:57:36 +010042KernelWriter::~KernelWriter() = default;
43
Viet-Hoa Do3389f532023-07-05 17:36:40 +010044std::unique_ptr<KernelWriter> KernelWriter::create_instance(TargetArchitecture architecture, TargetLanguage language)
45{
Gunes Bayirab0b7502023-07-11 14:57:36 +010046 CKW_UNUSED(architecture);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 switch (language)
Viet-Hoa Do3389f532023-07-05 17:36:40 +010048 {
49 case TargetLanguage::OpenCL:
50 // Currently this is the oldest and the only supported GPU architecture.
51 CKW_ASSERT(architecture == TargetArchitecture::GpuArmMaliValhall);
52 return std::make_unique<CLKernelWriter>();
53
54 default:
55 CKW_THROW_MSG("Language not supported!");
56 }
57}
58
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010059int32_t KernelWriter::new_id_space()
60{
61 _id_space = ++_last_created_id_space;
62
63 return _id_space;
64}
65
Gunes Bayirab0b7502023-07-11 14:57:36 +010066int32_t KernelWriter::id_space() const
67{
68 return _id_space;
69}
70
Viet-Hoa Do2d0c2f52023-08-24 11:48:19 +010071KernelWriter &KernelWriter::id_space(int32_t value)
72{
73 CKW_ASSERT(value <= _last_created_id_space);
74
75 _id_space = value;
76
77 return *this;
78}
79
80void KernelWriter::write_body(const std::function<void()> &body)
81{
82 const auto curr_id_space = id_space();
83 new_id_space();
84 body();
85 id_space(curr_id_space);
86}
87
Gunes Bayirab0b7502023-07-11 14:57:36 +010088std::string KernelWriter::generate_full_name(const std::string &name) const
89{
90 return "G" + std::to_string(id_space()) + "__" + name;
91}
Viet-Hoa Do3389f532023-07-05 17:36:40 +010092
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010093TileOperand KernelWriter::create_tile_operand(ITile &tile)
94{
95 return TileOperand(tile);
96}
97
Viet-Hoa Docd1f03e2023-09-19 16:41:34 +010098std::tuple<ITile &, TileArea> KernelWriter::get_tile(const TileOperand &operand)
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010099{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 return {*operand._tile, {operand._row_start, operand._row_end, operand._col_start, operand._col_end}};
Viet-Hoa Do25d26f42023-07-20 17:31:47 +0100101}
102
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +0100103TensorOperand KernelWriter::create_tensor_operand(ITensor &tensor)
104{
105 return TensorOperand(tensor);
106}
107
108ITensor &KernelWriter::get_tensor(const TensorOperand &operand)
109{
110 return operand._tensor;
111}
112
Gunes Bayir806b8e82023-08-23 23:28:31 +0100113const std::vector<std::vector<std::string>> &KernelWriter::get_values(const ConstantData &data)
114{
115 return data.values();
116}
117
118DataType KernelWriter::get_data_type(const ConstantData &data)
119{
120 return data.data_type();
121}
122
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100123} // namespace ckw